-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDonationNotifier.php
More file actions
141 lines (119 loc) · 5.69 KB
/
Copy pathDonationNotifier.php
File metadata and controls
141 lines (119 loc) · 5.69 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
<?php
namespace MatchBot\Domain;
use MatchBot\Application\Assertion;
use MatchBot\Application\Email\EmailMessage;
use MatchBot\Client\Mailer;
use Psr\Clock\ClockInterface;
class DonationNotifier
{
public function __construct(
private Mailer $mailer,
private EmailVerificationTokenRepository $emailVerificationTokenRepository,
private ClockInterface $clock,
private string $donateBaseUri,
) {
}
public static function emailMessageForCollectedDonation(
Donation $donation,
string $donateBaseUri,
bool $accountAlreadyExistsForEmail,
?EmailVerificationToken $emailVerificationToken = null
): EmailMessage {
if (! $donation->getDonationStatus()->isSuccessful()) {
throw new \RuntimeException("{$donation} is not successful - cannot send success email");
}
$paymentMethodType = $donation->getPaymentMethodType();
$emailAddress = $donation->getDonorEmailAddress();
$collectedAt = $donation->getCollectedAt();
Assertion::notNull(
$paymentMethodType,
"payment method should not be null for successful donation: {$donation}"
);
Assertion::notNull(
$emailAddress,
"email address should not be null for successful donation: {$donation}"
);
Assertion::notNull(
$collectedAt,
"collectedAt should not be null for successful donation: {$donation}"
);
$campaign = $donation->getCampaign();
$charity = $campaign->getCharity();
$createAccountUri = null;
if ($emailVerificationToken) {
$personId = $donation->getDonorId();
if ($personId) {
$createAccountUri = sprintf(
'%s/register?c=%s&u=%s',
$donateBaseUri,
$emailVerificationToken->randomCode,
$personId->id,
);
}
}
return EmailMessage::donorDonationSuccess($emailAddress, [
// see required params in mailer:
// https://github.com/thebiggive/mailer/blob/ca2c70f10720a66ff8fb041d3af430a07f49d625/app/settings.php#L27
'campaignName' => $campaign->getCampaignName(),
'campaignThankYouMessage' => $campaign->getThankYouMessage(),
'charityName' => $charity->getName(),
'charityRegistrationAuthority' => $charity->getRegulatorName(),
'charityNumber' => $charity->getRegulatorNumber(),
// charityIsExempt is not yet used by mailer as it has its own logic
// to work out if a charity is exempt. I'm hoping we can remove that soon.
'charityIsExempt' => $charity->isExempt(),
'createAccountUri' => $createAccountUri,
'accountAlreadyExistsForEmail' => $accountAlreadyExistsForEmail,
'currencyCode' => $donation->currency()->isoCode(),
'donationAmount' => (float)$donation->getAmount(),
'donationDatetime' => $collectedAt->format('c'),
'donorFirstName' => $donation->getDonorFirstName(),
'donorLastName' => $donation->getDonorLastName(),
'donorGreetingName' => $donation->getDonorFirstName() === '' ? $donation->getDonorLastName() : $donation->getDonorFirstName(), // org name is fallback
'giftAidAmountClaimed' => (float) $donation->getGiftAidValue(),
'matchedAmount' => $donation->matchedAmount()->toMajorUnitFloat(),
'paymentMethodType' => $paymentMethodType->value,
'statementReference' => $charity->getStatementDescriptor(),
'tipAmount' => (float) $donation->getTipAmount(),
'totalChargedAmount' => (float) $donation->getTotalPaidByDonor(),
'totalCharityValueAmount' => (float) $donation->totalCharityValueAmount(),
'transactionId' => $donation->getReferenceCode(), // @todo switch mailer to use referenceCode and delete this after line below is in prod
'referenceCode' => $donation->getReferenceCode(),
'charityLogoUri' => $charity->getLogoUri()?->__toString(),
'charityWebsite' => $charity->getWebsiteUri()?->__toString(),
'charityPhoneNumber' => $charity->getPhoneNumber(),
'charityEmailAddress' => $charity->getEmailAddress()?->email,
]);
}
/**
* Sends (Or resends) a donation thanks message to the donor of a donation. By default, uses the email
* address and all other details as recorded on the donation, but if $to is passed the email is sent
* to that address instead.
*
* @param Donation $donation
* @param EmailAddress|null $to
* @param bool $showAccountExistsForEmail - whether to tell the donor that although they were not logged in a donor account exists for their email address.
* @return void
*/
public function notifyDonorOfDonationSuccess(
Donation $donation,
bool $sendRegisterUri,
bool $showAccountExistsForEmail,
?EmailAddress $to = null,
): void {
$emailAddress = $donation->getDonorEmailAddress();
Assertion::notNull($emailAddress);
$emailVerificationToken = null;
if ($sendRegisterUri) {
$emailVerificationToken = $this->emailVerificationTokenRepository->findRecentTokenForEmailAddress(
$emailAddress,
$this->clock->now(),
);
}
$emailMessage = self::emailMessageForCollectedDonation($donation, $this->donateBaseUri, $showAccountExistsForEmail, $emailVerificationToken);
if ($to !== null) {
$emailMessage = $emailMessage->withToAddress($to);
}
$this->mailer->send($emailMessage);
}
}