Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / fund / controller / FundInitiativeBackController.php
blob17ead82ebff4c619b25177a9f4e7e3d3fdadc8c1
1 <?php
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())
11 ->setViewer($viewer)
12 ->withIDs(array($id))
13 ->executeOne();
14 if (!$initiative) {
15 return new Aphront404Response();
18 $merchant = id(new PhortuneMerchantQuery())
19 ->setViewer($viewer)
20 ->withPHIDs(array($initiative->getMerchantPHID()))
21 ->executeOne();
22 if (!$merchant) {
23 return new Aphront404Response();
26 $initiative_uri = '/'.$initiative->getMonogram();
28 if ($initiative->isClosed()) {
29 return $this->newDialog()
30 ->setTitle(pht('Initiative Closed'))
31 ->appendParagraph(
32 pht('You can not back a closed initiative.'))
33 ->addCancelButton($initiative_uri);
36 $accounts = PhortuneAccountQuery::loadAccountsForUser(
37 $viewer,
38 PhabricatorContentSource::newFromRequest($request));
40 $v_amount = null;
41 $e_amount = true;
43 $v_account = head($accounts)->getPHID();
45 $errors = array();
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.');
52 } else {
53 $account = $accounts[$v_account];
56 if (!strlen($v_amount)) {
57 $errors[] = pht(
58 'You must specify how much money you want to contribute to the '.
59 'initiative.');
60 $e_amount = pht('Required');
61 } else {
62 try {
63 $currency = PhortuneCurrency::newFromUserInput(
64 $viewer,
65 $v_amount);
66 $currency->assertInRange('1.00 USD', null);
67 } catch (Exception $ex) {
68 $errors[] = $ex->getMessage();
69 $e_amount = pht('Invalid');
73 if (!$errors) {
74 $backer = FundBacker::initializeNewBacker($viewer)
75 ->setInitiativePHID($initiative->getPHID())
76 ->attachInitiative($initiative)
77 ->setAmountAsCurrency($currency)
78 ->save();
80 $product = id(new PhortuneProductQuery())
81 ->setViewer($viewer)
82 ->withClassAndRef('FundBackerProduct', $initiative->getPHID())
83 ->executeOne();
85 $cart_implementation = id(new FundBackerCart())
86 ->setInitiative($initiative);
88 $cart = $account->newCart($viewer, $cart_implementation, $merchant);
90 $purchase = $cart->newPurchase($viewer, $product);
91 $purchase
92 ->setBasePriceAsCurrency($currency)
93 ->setMetadataValue('backerPHID', $backer->getPHID())
94 ->save();
96 $xactions = array();
98 $xactions[] = id(new FundBackerTransaction())
99 ->setTransactionType(FundBackerStatusTransaction::TRANSACTIONTYPE)
100 ->setNewValue(FundBacker::STATUS_IN_CART);
102 $editor = id(new FundBackerEditor())
103 ->setActor($viewer)
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())
116 ->setUser($viewer)
117 ->appendChild(
118 id(new AphrontFormSelectControl())
119 ->setName('accountPHID')
120 ->setLabel(pht('Account'))
121 ->setValue($v_account)
122 ->setOptions(mpull($accounts, 'getName', 'getPHID')))
123 ->appendChild(
124 id(new AphrontFormTextControl())
125 ->setName('amount')
126 ->setLabel(pht('Amount'))
127 ->setValue($v_amount)
128 ->setError($e_amount));
130 return $this->newDialog()
131 ->setTitle(
132 pht('Back %s %s', $initiative->getMonogram(), $initiative->getName()))
133 ->setErrors($errors)
134 ->appendChild($form->buildLayoutView())
135 ->addCancelButton($initiative_uri)
136 ->addSubmitButton(pht('Continue'));