-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegularGivingMandate.php
More file actions
572 lines (484 loc) · 20.4 KB
/
RegularGivingMandate.php
File metadata and controls
572 lines (484 loc) · 20.4 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
namespace MatchBot\Domain;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use MatchBot\Application\Assertion;
use MatchBot\Application\AssertionFailedException;
use MatchBot\Domain\DomainException\NonCancellableStatus;
use MatchBot\Domain\DomainException\RegularGivingCollectionEndPassed;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use UnexpectedValueException;
#[ORM\Index(name: 'uuid', columns: ['uuid'])]
#[ORM\Index(name: 'donationsCreatedUpTo', columns: ['donationsCreatedUpTo'])]
#[ORM\Entity(
repositoryClass: null // we construct our own repository
)]
#[ORM\HasLifecycleCallbacks]
class RegularGivingMandate extends SalesforceWriteProxy
{
use TimestampsTrait;
private const int MIN_AMOUNT_PENCE = 1_00;
private const int MAX_AMOUNT_PENCE = 500_00;
/**
* The first donations taken for a regular giving mandate are usually matched, later donations are not. However,
* donors can elect to make an unmatched mandate so this does not always apply. To get the number matched for
* a specific donation use getNumberofMatchedDonations, which is why this is private.
*
* @see RegularGivingMandate::getNumberofMatchedDonations()
*/
private const int NUMBER_OF_DONATIONS_TO_MATCH = 3;
#[ORM\Column(unique: true, type: 'uuid')]
private readonly UuidInterface $uuid;
#[ORM\Embedded(columnPrefix: 'person')]
private PersonId $donorId;
#[ORM\Embedded(columnPrefix: '')]
private readonly Money $donationAmount;
/**
* @var string 18 digit salesforce ID of campaign
*/
#[ORM\Column()]
private readonly string $campaignId;
/**
* @var string 18 digit salesforce ID of charity
*/
#[ORM\Column()]
private readonly string $charityId;
#[ORM\Column()]
private readonly bool $giftAid;
/**
* How many months later than normal should donations be taken. For use via db migrations in case of errors or
* unusual situations. E.g. if we need to take donations one month earlier than standard for this mandate set to -1,
* if we need to take donations one month later set to +1.
*
* Adjustments to this only affect donation records created in the future, as the pre-auth date on each
* donation is set at creation time.
*
* Currently no setter as only set via db migrations, no getter as only used within this class.
*/
#[ORM\Column(options: ['default' => 0])]
private int $paymentDateOffsetMonths = 0;
/**
* @var bool When the mandate was created, did the donor give or refuse permission for Big Give to send marketing
* emails. Similar to @see Donation::$tbgComms
*/
#[ORM\Column()]
private readonly bool $tbgComms;
/**
* @var bool When the mandate was created, did the donor give or refuse permission for the charity they're donating to
* to send marketing emails. Similar to @see Donation::$charityComms
*/
#[ORM\Column()]
private readonly bool $charityComms;
#[ORM\Embedded(columnPrefix: false)]
private DayOfMonth $dayOfMonth;
#[ORM\Column(nullable: true, type: 'datetime_immutable')]
private ?\DateTimeImmutable $activeFrom = null;
/**
* Only set by scheduled monthly jobs. Null after initial mandate creation, even when 3 matched months' donations
* are created up-front.
*
* @psalm-suppress UnusedProperty - used in DQL
*/
#[ORM\Column(nullable: true, type: 'datetime_immutable')]
private ?\DateTimeImmutable $donationsCreatedUpTo = null;
#[ORM\Column]
private MandateStatus $status = MandateStatus::Pending;
#[ORM\Column(length: 50, nullable: true)]
private ?MandateCancellationType $cancellationType = null;
/**
* Whether the first donations should be matched - if match funds are not available we will allow donors
* to create an unmatched mandate. If false then donations may still end up incidentally matched e.g. via match
* funds redistribution at campaign end.
*/
#[ORM\Column()]
private bool $isMatched;
#[ORM\Column(length: 500, nullable: true)]
private ?string $cancellationReason = null;
#[ORM\Column(nullable: true, type: 'datetime_immutable')]
private ?\DateTimeImmutable $cancelledAt = null;
/**
* @param Salesforce18Id<Campaign> $campaignId
* @param Salesforce18Id<Charity> $charityId
*
* @throws UnexpectedValueException if the amount is out of the allowed range
*/
public function __construct(
PersonId $donorId,
Money $donationAmount,
Salesforce18Id $campaignId,
Salesforce18Id $charityId,
bool $giftAid,
DayOfMonth $dayOfMonth,
bool $tbgComms = false,
bool $charityComms = false,
bool $matchDonations = true,
) {
$this->createdNow();
$minAmount = Money::fromPence(self::MIN_AMOUNT_PENCE, Currency::GBP);
$maxAmount = Money::fromPence(self::MAX_AMOUNT_PENCE, Currency::GBP);
if ($donationAmount->lessThan($minAmount) || $donationAmount->moreThan($maxAmount)) {
throw new UnexpectedValueException(
"Amount {$donationAmount} is out of allowed range {$minAmount}-{$maxAmount}"
);
}
$this->uuid = Uuid::uuid4();
$this->donationAmount = $donationAmount;
$this->campaignId = $campaignId->value;
$this->charityId = $charityId->value;
$this->giftAid = $giftAid;
$this->donorId = $donorId;
$this->dayOfMonth = $dayOfMonth;
$this->tbgComms = $tbgComms;
$this->charityComms = $charityComms;
$this->isMatched = $matchDonations;
}
/**
* Returns the average matched amount of the given donations, rounded down to the nearest major unit.
*
* This indicates the largest donation size that it would be possible to match with the same match funds, assuming
* it would be spread across the same number of donations.
*
* @param non-empty-list<Donation> $donations . Must be at least one.
* @return Money
*/
public static function averageMatched(array $donations): Money
{
$totals = array_map(fn(Donation $donation) => $donation->getFundingWithdrawalTotalAsObject(), $donations);
$grandTotal = Money::sum(...$totals);
$averagePence = intdiv($grandTotal->amountInPence(), count($donations));
// We know currency is same for all donations as otherwise `sum` would have thrown.
$currency = $donations[0]->currency();
$averageMoneyRoundedDownToMajorUnit = Money::fromPence(
intdiv($averagePence, 100) * 100,
$currency
);
return $averageMoneyRoundedDownToMajorUnit;
}
/**
* Allows us to take payments according to this agreement from now on.
*
* Precondition: Must be in Pending status
* @throws AssertionFailedException
*/
public function activate(\DateTimeImmutable $activationDate): void
{
Assertion::eq($this->status, MandateStatus::Pending);
$this->status = MandateStatus::Active;
$this->activeFrom = $activationDate;
}
/**
* @return array<string, mixed>
*/
public function toFrontEndApiModel(Charity $charity, \DateTimeImmutable $now): array
{
Assertion::same($charity->getSalesforceId(), $this->charityId);
return [
'id' => $this->uuid->toString(),
'donorId' => $this->donorId->id,
'donationAmount' => $this->donationAmount,
'matchedAmount' => $this->getMatchedAmount(),
'giftAidAmount' => $this->getGiftAidAmount(),
'totalIncGiftAid' => $this->totalIncGiftAid(),
'totalCharityReceivesPerInitial' => $this->totalCharityReceivesPerInitial(),
'campaignId' => $this->campaignId,
'charityId' => $this->charityId,
'isMatched' => $this->isMatched,
'numberOfMatchedDonations' => $this->getNumberofMatchedDonations(),
'schedule' => [
'type' => 'monthly',
'dayOfMonth' => $this->dayOfMonth->value,
'activeFrom' => $this->activeFrom?->format(\DateTimeInterface::ATOM),
'expectedNextPaymentDate' => in_array($this->status, [MandateStatus::Pending, MandateStatus::Active], true) ?
$this->firstPaymentDayAfter($now)->format(\DateTimeInterface::ATOM) :
null,
],
'charityName' => $charity->getName(),
'giftAid' => $this->giftAid,
'status' => $this->status->apiName(),
...($this->cancelledAt ?
['cancellationDate' => $this->cancelledAt->format(\DateTimeInterface::ATOM)] :
[]
)
];
}
#[\Override]
public function __toString(): string
{
return "Regular Giving Mandate # {$this->uuid->toString()}";
}
/**
* @return array<string, mixed>
*/
public function toSFApiModel(DonorAccount $donor): array
{
Assertion::eq($donor->id(), $this->donorId);
return [
'uuid' => $this->uuid->toString(),
'campaignSFId' => $this->campaignId,
'activeFrom' => $this->activeFrom?->format(DateTimeInterface::ATOM),
'dayOfMonth' => $this->dayOfMonth->value,
'donationAmount' => (float) $this->donationAmount->toNumericString(), // SF type is Decimal, so cast
'status' => ucfirst($this->status->apiName()), // Field in SF has upper case first letter and is awkard to change.
'contactUuid' => $this->donorId->id,
'giftAid' => $this->giftAid,
'donor' => $donor->toSfApiModel(),
];
}
public function firstPaymentDayAfter(\DateTimeImmutable $currentDateTime): \DateTimeImmutable
{
// Convert to Europe/London timezone for all business logic calculations
// This ensures consistent behavior regardless of input timezone
$londonTimezone = new \DateTimeZone('Europe/London');
$londonDateTime = $currentDateTime->setTimezone($londonTimezone);
// Now perform all date calculations using London time
$nextPaymentDayIsNextMonth = (int)$londonDateTime->format('d') >= $this->dayOfMonth->value;
$todayOrNextMonth = $nextPaymentDayIsNextMonth ?
$londonDateTime->add(new \DateInterval("P1M")) :
$londonDateTime;
// Create the result in London timezone, then return it
return new \DateTimeImmutable(
$todayOrNextMonth->format('Y-m-' . $this->dayOfMonth->value . 'T06:00:00'),
$londonTimezone
);
}
public function hasGiftAid(): bool
{
return $this->giftAid;
}
/**
* Records that all donations we plan to take for this donation before the given time have been created
* and saved as pre-authorized donations. This means that no more donations need to be created based on this
* mandate before that date.
*/
public function setDonationsCreatedUpTo(?\DateTimeImmutable $donationsCreatedUpTo): void
{
Assertion::same($this->status, MandateStatus::Active);
$this->donationsCreatedUpTo = $donationsCreatedUpTo;
}
public function createPreAuthorizedDonation(
DonationSequenceNumber $sequenceNumber,
DonorAccount $donor,
Campaign $campaign,
bool $requireActiveMandate = true,
?\DateTimeImmutable $expectedActivationDate = null,
): Donation {
// comms prefs below (charityComms, championComms, optInTbgEmail) are all set to null, as it's only the 1st
// donation in the mandate that carries the donor's chosen marketing comms preferences to Salesforce.
// It may be possible that gift aid was selected when this mandate was created but since then the donor
// told us to forget their home address. In that case we wouldn't be able to claim gift aid for any
// new donations.
$giftAidClaimable = $this->giftAid && $donor->hasHomeAddress();
$donation = new Donation(
amount: $this->donationAmount->toNumericString(),
currencyCode: $this->donationAmount->currency->isoCode(),
paymentMethodType: PaymentMethodType::Card,
campaign: $campaign,
charityComms: null,
championComms: null,
pspCustomerId: $donor->stripeCustomerId->stripeCustomerId,
psp: $campaign->getCharity()->psp,
optInTbgEmail: null,
donorName: $donor->donorName,
emailAddress: $donor->emailAddress,
countryCode: $donor->getBillingCountryCode(),
tipAmount: '0',
mandate: $this,
mandateSequenceNumber: $sequenceNumber,
donorId: $donor->id(),
giftAid: $giftAidClaimable,
tipGiftAid: null,
homeAddress: $donor->getHomeAddressLine1(),
homePostcode: $donor->getHomePostcode(),
billingPostcode: null,
);
Assertion::true(
($requireActiveMandate && is_null($expectedActivationDate)) ||
(!$requireActiveMandate && !is_null($expectedActivationDate)),
'When creating donations for an already active mandate require the mandate to be active, otherwise pass the activation date'
);
$donation->update(
paymentMethodType: PaymentMethodType::Card,
giftAid: $this->giftAid,
tipGiftAid: false,
donorHomeAddressLine1: $donor->getHomeAddressLine1(),
donorHomePostcode: $donor->getHomePostcode(),
donorName: $donor->donorName,
donorEmailAddress: $donor->emailAddress,
tbgComms: false,
charityComms: false,
championComms: false,
donorBillingPostcode: $donor->getBillingPostcode(),
);
if ($this->activeFrom === null && $requireActiveMandate) {
throw new \Exception('Missing activation date - is this an active mandate?');
}
$assumedActivateDate = $this->activeFrom ?? $expectedActivationDate;
\assert($assumedActivateDate !== null); // can't be null based on combination of previous assertions.
$secondDonationDate = $this->firstPaymentDayAfter($assumedActivateDate);
if ($sequenceNumber->number < 2) {
// first donation in mandate should be taken on-session, not pre-authorized.
throw new \Exception('Cannot generate pre-authorized first donation');
}
$offset = $sequenceNumber->number - 2 + $this->paymentDateOffsetMonths;
$preAuthorizationDate = $secondDonationDate->modify("+$offset months");
if ($campaign->regularGivingCollectionIsEndedAt($preAuthorizationDate)) {
$collectionEnd = $campaign->getRegularGivingCollectionEnd();
Assertion::notNull($collectionEnd);
throw new RegularGivingCollectionEndPassed(
"Cannot pre-authorize a donation for {$preAuthorizationDate->format('Y-m-d')}, " .
"regular giving collections for campaign {$campaign->getSalesforceId()} end " .
"at {$collectionEnd->format('Y-m-d')}"
);
}
$donation->preAuthorize($preAuthorizationDate);
return $donation;
}
/**
* @return Salesforce18Id<Campaign>
*/
public function getCampaignId(): Salesforce18Id
{
return Salesforce18Id::ofCampaign($this->campaignId);
}
public function getUuid(): UuidInterface
{
return $this->uuid;
}
public function getDonationAmount(): Money
{
return $this->donationAmount;
}
public function getCharityId(): string
{
return $this->charityId;
}
/**
* @throws NonCancellableStatus
*/
public function cancel(string $reason, \DateTimeImmutable $at, MandateCancellationType $type): void
{
if (!in_array($this->status, [MandateStatus::Pending, MandateStatus::Active], true)) {
throw new NonCancellableStatus('Mandate has existing non-cancelable status ' . $this->status->name);
}
$this->status = MandateStatus::Cancelled;
$this->cancellationType = $type;
$this->cancellationReason = $reason;
$this->cancelledAt = $at;
}
public function getStatus(): MandateStatus
{
return $this->status;
}
public function campaignEnded(): void
{
$this->status = MandateStatus::CampaignEnded;
}
public function getActiveFrom(): ?\DateTimeImmutable
{
return $this->activeFrom;
}
public function describeSchedule(): string
{
return "Monthly on day #{$this->dayOfMonth->value}";
}
/**
* @return Money amount we expect to be claimable in gift aid per donation.
*/
public function getGiftAidAmount(): Money
{
if (! $this->giftAid) {
return Money::fromPoundsGBP(0);
}
return $this->donationAmount->withPence(
(int) (100.0 * (float)Donation::donationAmountToGiftAidValue(amount: $this->donationAmount->toNumericString()))
);
}
public function totalIncGiftAid(): Money
{
return $this->donationAmount->plus($this->getGiftAidAmount());
}
/**
* @return Money The total amount that we expect the charity to receive per each of the donors initial, matched
* donations, from us and HMRC in total. I.e. core amount + matched amount + gift aid amount.
*
*/
private function totalCharityReceivesPerInitial(): Money
{
return Money::sum($this->donationAmount, $this->getGiftAidAmount(), $this->getMatchedAmount());
}
public function getMatchedAmount(): Money
{
return $this->isMatched ? $this->donationAmount : Money::zero($this->donationAmount->currency);
}
public function createPendingFirstDonation(Campaign $campaign, DonorAccount $donor): Donation
{
Assertion::same($campaign->getSalesforceId(), $this->campaignId);
// As this is the first donation in the mandate we give it a copy of the donor's Big Give and Charity comms
// preferences so that SF can pick them up.
return new Donation(
amount: $this->donationAmount->toNumericString(),
currencyCode: $this->donationAmount->currency->isoCode(),
paymentMethodType: PaymentMethodType::Card,
campaign: $campaign,
charityComms: $this->charityComms,
championComms: null,
pspCustomerId: $donor->stripeCustomerId->stripeCustomerId,
psp: \MatchBot\Domain\PaymentServiceProvider::Stripe,
optInTbgEmail: $this->tbgComms,
donorName: $donor->donorName,
emailAddress: $donor->emailAddress,
countryCode: $donor->getBillingCountryCode(),
tipAmount: '0',
mandate: $this,
mandateSequenceNumber: DonationSequenceNumber::of(1),
giftAid: $this->giftAid,
homeAddress: $donor->getHomeAddressLine1(),
homePostcode: $donor->isHomeOutsideUK() ? Donation::OVERSEAS : $donor->getHomePostcode(),
billingPostcode: $donor->getBillingPostcode(),
donorId: $donor->id(),
);
}
public function donorId(): PersonId
{
return $this->donorId;
}
public function isMatched(): bool
{
return $this->isMatched;
}
public function getNumberofMatchedDonations(): int
{
return $this->isMatched ? self::NUMBER_OF_DONATIONS_TO_MATCH : 0;
}
/**
* Reason for this mandate being cancelled. Should only be called for a cancelled mandate.
* @return string
* @throws \Assert\AssertionFailedException
*/
public function cancellationReason(): string
{
Assertion::same(MandateStatus::Cancelled, $this->status);
assert($this->cancellationReason !== null);
return $this->cancellationReason;
}
/**
* Type of reason for this mandate being cancelled. Should only be called for a cancelled mandate.
* @throws \Assert\AssertionFailedException
*/
public function cancellationType(): MandateCancellationType
{
Assertion::same(MandateStatus::Cancelled, $this->status);
assert($this->cancellationType !== null);
return $this->cancellationType;
}
/**
* Reason for this mandate being cancelled. Should only be called for a cancelled mandate.
* */
public function cancelledAt(): \DateTimeImmutable
{
Assertion::same(MandateStatus::Cancelled, $this->status);
\assert($this->cancelledAt !== null);
return $this->cancelledAt;
}
}