-
Notifications
You must be signed in to change notification settings - Fork 191
/
Donation.php
291 lines (268 loc) · 7.47 KB
/
Donation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace Give\Donations\Models;
use DateTime;
use Give\Donations\DataTransferObjects\DonationQueryData;
use Give\Donations\Factories\DonationFactory;
use Give\Donations\Properties\BillingAddress;
use Give\Donations\ValueObjects\DonationMode;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Donations\ValueObjects\DonationType;
use Give\Donors\Models\Donor;
use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
use Give\Framework\Models\Contracts\ModelCrud;
use Give\Framework\Models\Contracts\ModelHasFactory;
use Give\Framework\Models\Model;
use Give\Framework\Models\ModelQueryBuilder;
use Give\Framework\Models\ValueObjects\Relationship;
use Give\Framework\PaymentGateways\PaymentGateway;
use Give\Framework\Support\ValueObjects\Money;
use Give\Subscriptions\Models\Subscription;
/**
* Class Donation
*
* @since 3.9.0 Add phone property
* @since 2.23.0 add type property; remove parentId property
* @since 2.20.0 update amount type, fee recovered, and exchange rate
* @since 2.19.6
*
* @property int $id
* @property int $formId
* @property string $formTitle
* @property DateTime $createdAt
* @property DateTime $updatedAt
* @property DonationStatus $status
* @property DonationMode $mode
* @property DonationType $type
* @property Money $amount amount charged to the gateway
* @property Money $feeAmountRecovered
* @property string $exchangeRate
* @property string $gatewayId
* @property int $donorId
* @property string $honorific
* @property string $firstName
* @property string $lastName
* @property string $email
* @property string $phone
* @property int $subscriptionId
* @property BillingAddress $billingAddress
* @property string $purchaseKey
* @property string $donorIp
* @property bool $anonymous
* @property string $levelId
* @property string $gatewayTransactionId
* @property Donor $donor
* @property Subscription $subscription
* @property DonationNote[] $notes
* @property string $company
* @property string $comment
*/
class Donation extends Model implements ModelCrud, ModelHasFactory
{
/**
* @inheritdoc
*/
protected $properties = [
'id' => 'int',
'formId' => 'int',
'formTitle' => 'string',
'purchaseKey' => 'string',
'donorIp' => 'string',
'createdAt' => DateTime::class,
'updatedAt' => DateTime::class,
'status' => DonationStatus::class,
'type' => DonationType::class,
'mode' => DonationMode::class,
'amount' => Money::class,
'feeAmountRecovered' => Money::class,
'exchangeRate' => ['string', '1'],
'gatewayId' => 'string',
'donorId' => 'int',
'honorific' => 'string',
'firstName' => 'string',
'lastName' => 'string',
'email' => 'string',
'phone' => 'string',
'subscriptionId' => ['int', 0],
'billingAddress' => BillingAddress::class,
'anonymous' => ['bool', false],
'levelId' => ['string', ''],
'gatewayTransactionId' => 'string',
'company' => 'string',
'comment' => 'string',
];
/**
* @inheritdoc
*/
protected $relationships = [
'donor' => Relationship::BELONGS_TO,
'subscription' => Relationship::BELONGS_TO,
'notes' => Relationship::HAS_MANY,
];
/**
* Find donation by ID
*
* @since 2.19.6
*
* @param int $id
*
* @return Donation|null
*/
public static function find($id)
{
return give()->donations->getById($id);
}
/**
* @since 2.20.0 return mutated model instance
* @since 2.19.6
*
* @throws Exception|InvalidArgumentException
*/
public static function create(array $attributes): Donation
{
$donation = new static($attributes);
give()->donations->insert($donation);
return $donation;
}
/**
* @since 2.20.0 mutate model in repository and return void
* @since 2.19.6
*
* @return void
*
* @throws Exception|InvalidArgumentException
*/
public function save()
{
if (!$this->id) {
give()->donations->insert($this);
} else {
give()->donations->update($this);
}
}
/**
* @since 2.19.6
*
* @throws Exception|InvalidArgumentException
*/
public function delete(): bool
{
return give()->donations->delete($this);
}
/**
* @since 2.19.6
*
* @return ModelQueryBuilder<Donor>
*/
public function donor(): ModelQueryBuilder
{
return give()->donors->queryById($this->donorId);
}
/**
* @since 2.19.6
*
* @return ModelQueryBuilder<Subscription>
*/
public function subscription(): ModelQueryBuilder
{
if ($this->subscriptionId) {
return give()->subscriptions->queryById($this->subscriptionId);
}
return give()->subscriptions->queryByDonationId($this->id);
}
/**
* @since 2.19.6
*
* @return int|null
*/
public function getSequentialId()
{
return give()->donations->getSequentialId($this->id);
}
/**
* @since 2.19.6
*
* @return ModelQueryBuilder<DonationNote>
*/
public function notes(): ModelQueryBuilder
{
return give()->donations->notes->queryByDonationId($this->id);
}
/**
* Returns the amount charged in the currency the GiveWP site is set to
*
* @since 2.20.0
*/
public function amountInBaseCurrency(): Money
{
return $this->amount->inBaseCurrency($this->exchangeRate);
}
/**
* Returns the donation amount the donor "intended", which means it is the amount without recovered fees. So if the
* donor paid $100, but the donation was charged $105 with a $5 fee, this method will return $100.
*
* @since 2.20.0
*/
public function intendedAmount(): Money
{
return $this->feeAmountRecovered === null
? $this->amount
: $this->amount->subtract($this->feeAmountRecovered);
}
/**
* Returns the amount intended in the currency the GiveWP site is set to
*
* @since 2.20.0
*/
public function intendedAmountInBaseCurrency(): Money
{
return $this->intendedAmount()->inBaseCurrency($this->exchangeRate);
}
/**
* Returns the gateway instance for this donation
*
* @since 2.20.0
*/
public function gateway(): PaymentGateway
{
return give()->gateways->getPaymentGateway($this->gatewayId);
}
/**
* @since 2.20.0
*
* @inheritDoc
*/
protected function getPropertyDefaults(): array
{
return array_merge(parent::getPropertyDefaults(), [
'mode' => give_is_test_mode() ? DonationMode::TEST() : DonationMode::LIVE(),
'donorIp' => give_get_ip(),
'billingAddress' => new BillingAddress(),
]);
}
/**
* @since 2.19.6
*
* @return ModelQueryBuilder<Donation>
*/
public static function query(): ModelQueryBuilder
{
return give()->donations->prepareQuery();
}
/**
* @since 2.19.6
*
* @param object $object
*/
public static function fromQueryBuilderObject($object): Donation
{
return DonationQueryData::fromObject($object)->toDonation();
}
/**
* @since 2.19.6
*/
public static function factory(): DonationFactory
{
return new DonationFactory(static::class);
}
}