PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.20.0
GiveWP – Donation Plugin and Fundraising Platform v2.20.0
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / Donations / Models / Donation.php
give / src / Donations / Models Last commit date
Donation.php 4 years ago
Donation.php
293 lines
1 <?php
2
3 namespace Give\Donations\Models;
4
5 use DateTime;
6 use Exception;
7 use Give\Donations\DataTransferObjects\DonationQueryData;
8 use Give\Donations\Factories\DonationFactory;
9 use Give\Donations\Properties\BillingAddress;
10 use Give\Donations\ValueObjects\DonationMode;
11 use Give\Donations\ValueObjects\DonationStatus;
12 use Give\Donors\Models\Donor;
13 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
14 use Give\Framework\Models\Contracts\ModelCrud;
15 use Give\Framework\Models\Contracts\ModelHasFactory;
16 use Give\Framework\Models\Model;
17 use Give\Framework\Models\ModelQueryBuilder;
18 use Give\Framework\Models\ValueObjects\Relationship;
19 use Give\Framework\PaymentGateways\PaymentGateway;
20 use Give\Framework\Support\ValueObjects\Money;
21 use Give\Subscriptions\Models\Subscription;
22
23 /**
24 * Class Donation
25 *
26 * @since 2.20.0 update amount type, fee recovered, and exchange rate
27 * @since 2.19.6
28 *
29 * @property int $id
30 * @property int $formId
31 * @property string $formTitle
32 * @property DateTime $createdAt
33 * @property DateTime $updatedAt
34 * @property DonationStatus $status
35 * @property DonationMode $mode
36 * @property Money $amount amount charged to the gateway
37 * @property Money $feeAmountRecovered
38 * @property string $exchangeRate
39 * @property string $gatewayId
40 * @property int $donorId
41 * @property string $firstName
42 * @property string $lastName
43 * @property string $email
44 * @property int $parentId
45 * @property int $subscriptionId
46 * @property BillingAddress $billingAddress
47 * @property string $purchaseKey
48 * @property string $donorIp
49 * @property bool $anonymous
50 * @property int $levelId
51 * @property string $gatewayTransactionId
52 * @property Donor $donor
53 * @property Subscription $subscription
54 */
55 class Donation extends Model implements ModelCrud, ModelHasFactory
56 {
57 /**
58 * @inheritdoc
59 */
60 protected $properties = [
61 'id' => 'int',
62 'formId' => 'int',
63 'formTitle' => 'string',
64 'purchaseKey' => 'string',
65 'donorIp' => 'string',
66 'createdAt' => DateTime::class,
67 'updatedAt' => DateTime::class,
68 'status' => DonationStatus::class,
69 'mode' => DonationMode::class,
70 'amount' => Money::class,
71 'feeAmountRecovered' => Money::class,
72 'exchangeRate' => ['string', '1'],
73 'gatewayId' => 'string',
74 'donorId' => 'int',
75 'firstName' => 'string',
76 'lastName' => 'string',
77 'email' => 'string',
78 'parentId' => ['int', 0],
79 'subscriptionId' => ['int', 0],
80 'billingAddress' => BillingAddress::class,
81 'anonymous' => ['bool', false],
82 'levelId' => ['int', 0],
83 'gatewayTransactionId' => 'string',
84 ];
85
86 /**
87 * @inheritdoc
88 */
89 protected $relationships = [
90 'donor' => Relationship::BELONGS_TO,
91 'subscription' => Relationship::BELONGS_TO,
92 ];
93
94 /**
95 * Find donation by ID
96 *
97 * @since 2.19.6
98 *
99 * @param int $id
100 *
101 * @return Donation
102 */
103 public static function find($id)
104 {
105 return give()->donations->getById($id);
106 }
107
108 /**
109 * @since 2.20.0 return mutated model instance
110 * @since 2.19.6
111 *
112 * @param array $attributes
113 *
114 * @return Donation
115 *
116 * @throws Exception|InvalidArgumentException
117 */
118 public static function create(array $attributes)
119 {
120 $donation = new static($attributes);
121
122 give()->donations->insert($donation);
123
124 return $donation;
125 }
126
127 /**
128 * @since 2.20.0 mutate model in repository and return void
129 * @since 2.19.6
130 *
131 * @return void
132 *
133 * @throws Exception|InvalidArgumentException
134 */
135 public function save()
136 {
137 if (!$this->id) {
138 give()->donations->insert($this);
139 } else {
140 give()->donations->update($this);
141 }
142 }
143
144 /**
145 * @since 2.19.6
146 *
147 * @return bool
148 *
149 * @throws Exception|InvalidArgumentException
150 */
151 public function delete()
152 {
153 return give()->donations->delete($this);
154 }
155
156 /**
157 * @since 2.19.6
158 *
159 * @return ModelQueryBuilder<Donor>
160 */
161 public function donor()
162 {
163 return give()->donors->queryById($this->donorId);
164 }
165
166 /**
167 * @since 2.19.6
168 *
169 * @return ModelQueryBuilder<Subscription>
170 */
171 public function subscription()
172 {
173 if ($this->subscriptionId) {
174 return give()->subscriptions->queryById($this->subscriptionId);
175 }
176
177 return give()->subscriptions->queryByDonationId($this->id);
178 }
179
180 /**
181 * @since 2.19.6
182 *
183 * @return int|null
184 */
185 public function getSequentialId()
186 {
187 return give()->donations->getSequentialId($this->id);
188 }
189
190 /**
191 * @since 2.19.6
192 *
193 * @return object[]
194 */
195 public function getNotes()
196 {
197 return give()->donations->getNotesByDonationId($this->id);
198 }
199
200 /**
201 * Returns the amount charged in the currency the GiveWP site is set to
202 *
203 * @since 2.20.0
204 *
205 * @return Money
206 */
207 public function amountInBaseCurrency()
208 {
209 return $this->amount->inBaseCurrency($this->exchangeRate);
210 }
211
212 /**
213 * Returns the donation amount the donor "intended", which means it is the amount without recovered fees. So if the
214 * donor paid $100, but the donation was charged $105 with a $5 fee, this method will return $100.
215 *
216 * @since 2.20.0
217 *
218 * @return Money
219 */
220 public function intendedAmount()
221 {
222 return $this->feeAmountRecovered === null
223 ? $this->amount
224 : $this->amount->subtract($this->feeAmountRecovered);
225 }
226
227 /**
228 * Returns the amount intended in the currency the GiveWP site is set to
229 *
230 * @since 2.20.0
231 *
232 * @return Money
233 */
234 public function intendedAmountInBaseCurrency()
235 {
236 return $this->intendedAmount()->inBaseCurrency($this->exchangeRate);
237 }
238
239 /**
240 * Returns the gateway instance for this donation
241 *
242 * @since 2.20.0
243 */
244 public function gateway(): PaymentGateway
245 {
246 return give()->gateways->getPaymentGateway($this->gatewayId);
247 }
248
249 /**
250 * @since 2.20.0
251 *
252 * @inheritDoc
253 */
254 protected function getPropertyDefaults()
255 {
256 return array_merge(parent::getPropertyDefaults(), [
257 'mode' => give_is_test_mode() ? DonationMode::TEST() : DonationMode::LIVE(),
258 'donorIp' => give_get_ip(),
259 'billingAddress' => new BillingAddress(),
260 ]);
261 }
262
263 /**
264 * @since 2.19.6
265 *
266 * @return ModelQueryBuilder<Donation>
267 */
268 public static function query()
269 {
270 return give()->donations->prepareQuery();
271 }
272
273 /**
274 * @since 2.19.6
275 *
276 * @param object $object
277 *
278 * @return Donation
279 */
280 public static function fromQueryBuilderObject($object)
281 {
282 return DonationQueryData::fromObject($object)->toDonation();
283 }
284
285 /**
286 * @return DonationFactory<Donation>
287 */
288 public static function factory()
289 {
290 return new DonationFactory(static::class);
291 }
292 }
293