3 final class FundInitiativeBackController
4 extends FundController
{
6 public function handleRequest(AphrontRequest
$request) {
7 $viewer = $request->getViewer();
8 $id = $request->getURIData('id');
10 $initiative = id(new FundInitiativeQuery())
15 return new Aphront404Response();
18 $merchant = id(new PhortuneMerchantQuery())
20 ->withPHIDs(array($initiative->getMerchantPHID()))
23 return new Aphront404Response();
26 $initiative_uri = '/'.$initiative->getMonogram();
28 if ($initiative->isClosed()) {
29 return $this->newDialog()
30 ->setTitle(pht('Initiative Closed'))
32 pht('You can not back a closed initiative.'))
33 ->addCancelButton($initiative_uri);
36 $accounts = PhortuneAccountQuery
::loadAccountsForUser(
38 PhabricatorContentSource
::newFromRequest($request));
43 $v_account = head($accounts)->getPHID();
46 if ($request->isFormPost()) {
47 $v_amount = $request->getStr('amount');
48 $v_account = $request->getStr('accountPHID');
50 if (empty($accounts[$v_account])) {
51 $errors[] = pht('You must specify an account.');
53 $account = $accounts[$v_account];
56 if (!strlen($v_amount)) {
58 'You must specify how much money you want to contribute to the '.
60 $e_amount = pht('Required');
63 $currency = PhortuneCurrency
::newFromUserInput(
66 $currency->assertInRange('1.00 USD', null);
67 } catch (Exception
$ex) {
68 $errors[] = $ex->getMessage();
69 $e_amount = pht('Invalid');
74 $backer = FundBacker
::initializeNewBacker($viewer)
75 ->setInitiativePHID($initiative->getPHID())
76 ->attachInitiative($initiative)
77 ->setAmountAsCurrency($currency)
80 $product = id(new PhortuneProductQuery())
82 ->withClassAndRef('FundBackerProduct', $initiative->getPHID())
85 $cart_implementation = id(new FundBackerCart())
86 ->setInitiative($initiative);
88 $cart = $account->newCart($viewer, $cart_implementation, $merchant);
90 $purchase = $cart->newPurchase($viewer, $product);
92 ->setBasePriceAsCurrency($currency)
93 ->setMetadataValue('backerPHID', $backer->getPHID())
98 $xactions[] = id(new FundBackerTransaction())
99 ->setTransactionType(FundBackerStatusTransaction
::TRANSACTIONTYPE
)
100 ->setNewValue(FundBacker
::STATUS_IN_CART
);
102 $editor = id(new FundBackerEditor())
104 ->setContentSourceFromRequest($request);
106 $editor->applyTransactions($backer, $xactions);
108 $cart->activateCart();
110 return id(new AphrontRedirectResponse())
111 ->setURI($cart->getCheckoutURI());
115 $form = id(new AphrontFormView())
118 id(new AphrontFormSelectControl())
119 ->setName('accountPHID')
120 ->setLabel(pht('Account'))
121 ->setValue($v_account)
122 ->setOptions(mpull($accounts, 'getName', 'getPHID')))
124 id(new AphrontFormTextControl())
126 ->setLabel(pht('Amount'))
127 ->setValue($v_amount)
128 ->setError($e_amount));
130 return $this->newDialog()
132 pht('Back %s %s', $initiative->getMonogram(), $initiative->getName()))
134 ->appendChild($form->buildLayoutView())
135 ->addCancelButton($initiative_uri)
136 ->addSubmitButton(pht('Continue'));