Path: blob/master/src/applications/fund/phortune/FundBackerProduct.php
12256 views
<?php12final class FundBackerProduct extends PhortuneProductImplementation {34private $initiativePHID;5private $initiative;6private $viewer;78public function setViewer(PhabricatorUser $viewer) {9$this->viewer = $viewer;10return $this;11}1213public function getViewer() {14return $this->viewer;15}1617public function getRef() {18return $this->getInitiativePHID();19}2021public function getName(PhortuneProduct $product) {22$initiative = $this->getInitiative();2324if (!$initiative) {25return pht('Fund <Unknown Initiative>');26} else {27return pht(28'Fund %s %s',29$initiative->getMonogram(),30$initiative->getName());31}32}3334public function getPriceAsCurrency(PhortuneProduct $product) {35return PhortuneCurrency::newEmptyCurrency();36}3738public function setInitiativePHID($initiative_phid) {39$this->initiativePHID = $initiative_phid;40return $this;41}4243public function getInitiativePHID() {44return $this->initiativePHID;45}4647public function setInitiative(FundInitiative $initiative) {48$this->initiative = $initiative;49return $this;50}5152public function getInitiative() {53return $this->initiative;54}5556public function loadImplementationsForRefs(57PhabricatorUser $viewer,58array $refs) {5960$initiatives = id(new FundInitiativeQuery())61->setViewer($viewer)62->withPHIDs($refs)63->execute();64$initiatives = mpull($initiatives, null, 'getPHID');6566$objects = array();67foreach ($refs as $ref) {68$object = id(new FundBackerProduct())69->setViewer($viewer)70->setInitiativePHID($ref);7172$initiative = idx($initiatives, $ref);73if ($initiative) {74$object->setInitiative($initiative);75}7677$objects[] = $object;78}7980return $objects;81}8283public function didPurchaseProduct(84PhortuneProduct $product,85PhortunePurchase $purchase) {86$viewer = $this->getViewer();8788$backer = id(new FundBackerQuery())89->setViewer($viewer)90->withPHIDs(array($purchase->getMetadataValue('backerPHID')))91->executeOne();92if (!$backer) {93throw new Exception(pht('Unable to load %s!', 'FundBacker'));94}9596// Load the actual backing user -- they may not be the curent viewer if this97// product purchase is completing from a background worker or a merchant98// action.99100$actor = id(new PhabricatorPeopleQuery())101->setViewer($viewer)102->withPHIDs(array($backer->getBackerPHID()))103->executeOne();104105$xactions = array();106$xactions[] = id(new FundInitiativeTransaction())107->setTransactionType(FundInitiativeBackerTransaction::TRANSACTIONTYPE)108->setMetadataValue(109FundInitiativeTransaction::PROPERTY_AMOUNT,110$backer->getAmountAsCurrency()->serializeForStorage())111->setNewValue($backer->getPHID());112113$editor = id(new FundInitiativeEditor())114->setActor($actor)115->setContentSource($this->getContentSource());116117$editor->applyTransactions($this->getInitiative(), $xactions);118}119120public function didRefundProduct(121PhortuneProduct $product,122PhortunePurchase $purchase,123PhortuneCurrency $amount) {124$viewer = $this->getViewer();125126$backer = id(new FundBackerQuery())127->setViewer($viewer)128->withPHIDs(array($purchase->getMetadataValue('backerPHID')))129->executeOne();130if (!$backer) {131throw new Exception(pht('Unable to load %s!', 'FundBacker'));132}133134$xactions = array();135$xactions[] = id(new FundInitiativeTransaction())136->setTransactionType(FundInitiativeRefundTransaction::TRANSACTIONTYPE)137->setMetadataValue(138FundInitiativeTransaction::PROPERTY_AMOUNT,139$amount->serializeForStorage())140->setMetadataValue(141FundInitiativeTransaction::PROPERTY_BACKER,142$backer->getBackerPHID())143->setNewValue($backer->getPHID());144145$editor = id(new FundInitiativeEditor())146->setActor($viewer)147->setContentSource($this->getContentSource());148149$editor->applyTransactions($this->getInitiative(), $xactions);150}151152}153154155