Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/fund/phortune/FundBackerProduct.php
12256 views
1
<?php
2
3
final class FundBackerProduct extends PhortuneProductImplementation {
4
5
private $initiativePHID;
6
private $initiative;
7
private $viewer;
8
9
public function setViewer(PhabricatorUser $viewer) {
10
$this->viewer = $viewer;
11
return $this;
12
}
13
14
public function getViewer() {
15
return $this->viewer;
16
}
17
18
public function getRef() {
19
return $this->getInitiativePHID();
20
}
21
22
public function getName(PhortuneProduct $product) {
23
$initiative = $this->getInitiative();
24
25
if (!$initiative) {
26
return pht('Fund <Unknown Initiative>');
27
} else {
28
return pht(
29
'Fund %s %s',
30
$initiative->getMonogram(),
31
$initiative->getName());
32
}
33
}
34
35
public function getPriceAsCurrency(PhortuneProduct $product) {
36
return PhortuneCurrency::newEmptyCurrency();
37
}
38
39
public function setInitiativePHID($initiative_phid) {
40
$this->initiativePHID = $initiative_phid;
41
return $this;
42
}
43
44
public function getInitiativePHID() {
45
return $this->initiativePHID;
46
}
47
48
public function setInitiative(FundInitiative $initiative) {
49
$this->initiative = $initiative;
50
return $this;
51
}
52
53
public function getInitiative() {
54
return $this->initiative;
55
}
56
57
public function loadImplementationsForRefs(
58
PhabricatorUser $viewer,
59
array $refs) {
60
61
$initiatives = id(new FundInitiativeQuery())
62
->setViewer($viewer)
63
->withPHIDs($refs)
64
->execute();
65
$initiatives = mpull($initiatives, null, 'getPHID');
66
67
$objects = array();
68
foreach ($refs as $ref) {
69
$object = id(new FundBackerProduct())
70
->setViewer($viewer)
71
->setInitiativePHID($ref);
72
73
$initiative = idx($initiatives, $ref);
74
if ($initiative) {
75
$object->setInitiative($initiative);
76
}
77
78
$objects[] = $object;
79
}
80
81
return $objects;
82
}
83
84
public function didPurchaseProduct(
85
PhortuneProduct $product,
86
PhortunePurchase $purchase) {
87
$viewer = $this->getViewer();
88
89
$backer = id(new FundBackerQuery())
90
->setViewer($viewer)
91
->withPHIDs(array($purchase->getMetadataValue('backerPHID')))
92
->executeOne();
93
if (!$backer) {
94
throw new Exception(pht('Unable to load %s!', 'FundBacker'));
95
}
96
97
// Load the actual backing user -- they may not be the curent viewer if this
98
// product purchase is completing from a background worker or a merchant
99
// action.
100
101
$actor = id(new PhabricatorPeopleQuery())
102
->setViewer($viewer)
103
->withPHIDs(array($backer->getBackerPHID()))
104
->executeOne();
105
106
$xactions = array();
107
$xactions[] = id(new FundInitiativeTransaction())
108
->setTransactionType(FundInitiativeBackerTransaction::TRANSACTIONTYPE)
109
->setMetadataValue(
110
FundInitiativeTransaction::PROPERTY_AMOUNT,
111
$backer->getAmountAsCurrency()->serializeForStorage())
112
->setNewValue($backer->getPHID());
113
114
$editor = id(new FundInitiativeEditor())
115
->setActor($actor)
116
->setContentSource($this->getContentSource());
117
118
$editor->applyTransactions($this->getInitiative(), $xactions);
119
}
120
121
public function didRefundProduct(
122
PhortuneProduct $product,
123
PhortunePurchase $purchase,
124
PhortuneCurrency $amount) {
125
$viewer = $this->getViewer();
126
127
$backer = id(new FundBackerQuery())
128
->setViewer($viewer)
129
->withPHIDs(array($purchase->getMetadataValue('backerPHID')))
130
->executeOne();
131
if (!$backer) {
132
throw new Exception(pht('Unable to load %s!', 'FundBacker'));
133
}
134
135
$xactions = array();
136
$xactions[] = id(new FundInitiativeTransaction())
137
->setTransactionType(FundInitiativeRefundTransaction::TRANSACTIONTYPE)
138
->setMetadataValue(
139
FundInitiativeTransaction::PROPERTY_AMOUNT,
140
$amount->serializeForStorage())
141
->setMetadataValue(
142
FundInitiativeTransaction::PROPERTY_BACKER,
143
$backer->getBackerPHID())
144
->setNewValue($backer->getPHID());
145
146
$editor = id(new FundInitiativeEditor())
147
->setActor($viewer)
148
->setContentSource($this->getContentSource());
149
150
$editor->applyTransactions($this->getInitiative(), $xactions);
151
}
152
153
}
154
155