@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 139 lines 4.1 kB view raw
1<?php 2 3final class FundInitiativeBackController 4 extends FundController { 5 6 public function handleRequest(AphrontRequest $request) { 7 $viewer = $request->getViewer(); 8 $id = $request->getURIData('id'); 9 10 $initiative = id(new FundInitiativeQuery()) 11 ->setViewer($viewer) 12 ->withIDs(array($id)) 13 ->executeOne(); 14 if (!$initiative) { 15 return new Aphront404Response(); 16 } 17 18 $merchant = id(new PhortuneMerchantQuery()) 19 ->setViewer($viewer) 20 ->withPHIDs(array($initiative->getMerchantPHID())) 21 ->executeOne(); 22 if (!$merchant) { 23 return new Aphront404Response(); 24 } 25 26 $initiative_uri = '/'.$initiative->getMonogram(); 27 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); 34 } 35 36 $accounts = PhortuneAccountQuery::loadAccountsForUser( 37 $viewer, 38 PhabricatorContentSource::newFromRequest($request)); 39 40 $v_amount = null; 41 $e_amount = true; 42 43 $v_account = head($accounts)->getPHID(); 44 45 $errors = array(); 46 if ($request->isFormPost()) { 47 $v_amount = $request->getStr('amount'); 48 $v_account = $request->getStr('accountPHID'); 49 50 if (empty($accounts[$v_account])) { 51 $errors[] = pht('You must specify an account.'); 52 } else { 53 $account = $accounts[$v_account]; 54 } 55 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'); 70 } 71 } 72 73 if (!$errors) { 74 $backer = FundBacker::initializeNewBacker($viewer) 75 ->setInitiativePHID($initiative->getPHID()) 76 ->attachInitiative($initiative) 77 ->setAmountAsCurrency($currency) 78 ->save(); 79 80 $product = id(new PhortuneProductQuery()) 81 ->setViewer($viewer) 82 ->withClassAndRef('FundBackerProduct', $initiative->getPHID()) 83 ->executeOne(); 84 85 $cart_implementation = id(new FundBackerCart()) 86 ->setInitiative($initiative); 87 88 $cart = $account->newCart($viewer, $cart_implementation, $merchant); 89 90 $purchase = $cart->newPurchase($viewer, $product); 91 $purchase 92 ->setBasePriceAsCurrency($currency) 93 ->setMetadataValue('backerPHID', $backer->getPHID()) 94 ->save(); 95 96 $xactions = array(); 97 98 $xactions[] = id(new FundBackerTransaction()) 99 ->setTransactionType(FundBackerStatusTransaction::TRANSACTIONTYPE) 100 ->setNewValue(FundBacker::STATUS_IN_CART); 101 102 $editor = id(new FundBackerEditor()) 103 ->setActor($viewer) 104 ->setContentSourceFromRequest($request); 105 106 $editor->applyTransactions($backer, $xactions); 107 108 $cart->activateCart(); 109 110 return id(new AphrontRedirectResponse()) 111 ->setURI($cart->getCheckoutURI()); 112 } 113 } 114 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)); 129 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')); 137 } 138 139}