-
Notifications
You must be signed in to change notification settings - Fork 0
/
DonationService.php
641 lines (551 loc) · 27.8 KB
/
DonationService.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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<?php
namespace MatchBot\Domain;
use Doctrine\DBAL\Exception\ServerException as DBALServerException;
use Doctrine\ORM\Exception\ORMException;
use MatchBot\Application\Assertion;
use MatchBot\Application\Matching\Adapter as MatchingAdapter;
use MatchBot\Application\Messenger\DonationUpserted;
use MatchBot\Application\Notifier\StripeChatterInterface;
use MatchBot\Application\Persistence\RetrySafeEntityManager;
use MatchBot\Client\Stripe;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use MatchBot\Application\HttpModels\DonationCreate;
use MatchBot\Domain\DomainException\CampaignNotOpen;
use MatchBot\Domain\DomainException\CharityAccountLacksNeededCapaiblities;
use MatchBot\Domain\DomainException\CouldNotCancelStripePaymentIntent;
use MatchBot\Domain\DomainException\CouldNotMakeStripePaymentIntent;
use MatchBot\Domain\DomainException\DomainRecordNotFoundException;
use MatchBot\Domain\DomainException\DonationAlreadyFinalised;
use MatchBot\Domain\DomainException\DonationCreateModelLoadFailure;
use MatchBot\Domain\DomainException\MandateNotActive;
use MatchBot\Domain\DomainException\MissingTransactionId;
use MatchBot\Domain\DomainException\NoDonorAccountException;
use MatchBot\Domain\DomainException\RegularGivingCollectionEndPassed;
use MatchBot\Domain\DomainException\StripeAccountIdNotSetForAccount;
use MatchBot\Domain\DomainException\WrongCampaignType;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Ramsey\Uuid\UuidInterface;
use Random\Randomizer;
use Stripe\Exception\ApiErrorException;
use Stripe\Exception\InvalidRequestException;
use Stripe\StripeObject;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\RoutableMessageBus;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
class DonationService
{
private const int MAX_RETRY_COUNT = 3;
/**
* Message excerpts that we expect to see sometimes from stripe on InvalidRequestExceptions. An exception
* containing any of these strings should not generate an alarm.
*/
public const array EXPECTED_STRIPE_INVALID_REQUEST_MESSAGES = [
'The provided PaymentMethod has failed authentication',
'You must collect the security code (CVC) for this card from the cardholder before you can use it',
// When a donation is cancelled we update it to cancelled in the DB, which stops it being confirmed later. But
// we can still get this error if the cancellation is too late to stop us attempting to confirm.
// phpcs:ignore
'This PaymentIntent\'s payment_method could not be updated because it has a status of canceled. You may only update the payment_method of a PaymentIntent with one of the following statuses: requires_payment_method, requires_confirmation, requires_action.',
'The confirmation token has already been used to confirm a previous PaymentIntent',
'This PaymentIntent\'s radar_options could not be updated because it has a status of canceled.',
'This PaymentIntent\'s amount could not be updated because it has a status of canceled.',
// phpcs:ignore
'The parameter application_fee_amount cannot be updated on a PaymentIntent after a capture has already been made.',
];
public function __construct(
private DonationRepository $donationRepository,
private CampaignRepository $campaignRepository,
private LoggerInterface $logger,
private RetrySafeEntityManager $entityManager,
private Stripe $stripe,
private MatchingAdapter $matchingAdapter,
private StripeChatterInterface|ChatterInterface $chatter,
private ClockInterface $clock,
private RateLimiterFactory $rateLimiterFactory,
private DonorAccountRepository $donorAccountRepository,
private RoutableMessageBus $bus,
) {
}
/**
* Creates a new pending donation. In some edge cases (initial campaign data inserts hitting
* unique constraint violations), may reset the EntityManager; this could cause previously
* tracked entities in the Unit of Work to be lost.
*
* @param DonationCreate $donationData Details of the desired donation, as sent from the browser
* @param string $pspCustomerId The Stripe customer ID of the donor
*
* @throws CampaignNotOpen
* @throws CharityAccountLacksNeededCapaiblities
* @throws CouldNotMakeStripePaymentIntent
* @throws DBALServerException
* @throws DonationCreateModelLoadFailure
* @throws ORMException
* @throws StripeAccountIdNotSetForAccount
* @throws TransportExceptionInterface
* @throws RateLimitExceededException
* @throws WrongCampaignType
* @throws \MatchBot\Client\NotFoundException
*/
public function createDonation(DonationCreate $donationData, string $pspCustomerId): Donation
{
$this->rateLimiterFactory->create(key: $pspCustomerId)->consume()->ensureAccepted();
try {
$donation = $this->donationRepository->buildFromApiRequest($donationData);
} catch (\UnexpectedValueException $e) {
$message = 'Donation Create data initial model load';
$this->logger->warning($message . ': ' . $e->getMessage());
throw new DonationCreateModelLoadFailure(previous: $e);
} catch (UniqueConstraintViolationException) {
// If we get this, the most likely explanation is that another donation request
// created the same campaign a very short time before this request tried to. We
// saw this 3 times in the opening minutes of CC20 on 1 Dec 2020.
// If this happens, the latest campaign data should already have been pulled and
// persisted in the last second. So give the same call one more try, as
// buildFromApiRequest() should perform a fresh call to `CampaignRepository::findOneBy()`.
$this->logger->info(sprintf(
'Got campaign pull UniqueConstraintViolationException for campaign ID %s. Trying once more.',
$donationData->projectId->value,
));
$donation = $this->donationRepository->buildFromApiRequest($donationData);
}
if ($pspCustomerId !== $donation->getPspCustomerId()?->stripeCustomerId) {
throw new \UnexpectedValueException(sprintf(
'Route customer ID %s did not match %s in donation body',
$pspCustomerId,
$donation->getPspCustomerId()?->stripeCustomerId ?? 'null'
));
}
$this->enrollNewDonation($donation);
return $donation;
}
/**
* It seems like just the *first* persist of a given donation needs to be retry-safe, since there is a small
* but non-zero minority of Create attempts at the start of a big campaign which get a closed Entity Manager
* and then don't know about the connected #campaign on persist and crash when RetrySafeEntityManager tries again.
*
* The same applies to allocating match funds, which in rare cases can fail with a lock timeout exception. It could
* also fail simply because another thread keeps changing the values of funds in redis.
*
* If the EM "goes away" for any reason but only does so once, `flush()` should still replace the underlying
* EM with a new one and then the next persist should succeed.
*
* If the persist itself fails, we do not replace the underlying entity manager. This means if it's still usable
* then we still have any required related new entities in the Unit of Work.
* @param \Closure $retryable The action to be executed and then retried if necassary
* @param string $actionName The name of the action, used in logs.
* @throws ORMException|DBALServerException if they're occurring when max retry count reached.
*/
private function runWithPossibleRetry(\Closure $retryable, string $actionName): void
{
$retryCount = 0;
while ($retryCount < self::MAX_RETRY_COUNT) {
try {
$retryable();
return;
} catch (ORMException | DBALServerException $exception) {
$retryCount++;
$this->logger->info(
sprintf(
$actionName . ' error: %s. Retrying %d of %d.',
$exception->getMessage(),
$retryCount,
self::MAX_RETRY_COUNT,
)
);
$seconds = (new Randomizer())->getFloat(0.1, 1.1);
\assert(is_float($seconds)); // See https://github.com/vimeo/psalm/issues/10830
$this->clock->sleep($seconds);
if ($retryCount === self::MAX_RETRY_COUNT) {
$this->logger->error(
sprintf(
$actionName . ' error: %s. Giving up after %d retries.',
$exception->getMessage(),
self::MAX_RETRY_COUNT,
)
);
throw $exception;
}
}
}
}
/**
* Finalized a donation, instructing stripe to attempt to take payment immediately for a donor
* making an immediate, online donation.
*/
public function confirmOnSessionDonation(
Donation $donation,
StripeConfirmationTokenId $tokenId
): \Stripe\PaymentIntent {
$this->updateDonationFeesFromConfirmationToken($tokenId, $donation);
// We flush now to make sure the actual fees we're charging are recorded. If there's any DB error at this point
// we prefer to crash without collecting the donation over collecting the donation without a proper record
// or what we're charging.
$this->entityManager->flush();
return $this->stripe->confirmPaymentIntent(
$donation->getTransactionId(),
['confirmation_token' => $tokenId->stripeConfirmationTokenId]
);
}
/**
* Trigger collection of funds from a pre-authorized donation associated with a regular giving mandate
*/
public function confirmPreAuthorized(Donation $donation): void
{
$stripeAccountId = $donation->getPspCustomerId();
Assertion::notNull($stripeAccountId);
$donorAccount = $this->donorAccountRepository->findByStripeIdOrNull($stripeAccountId);
if ($donorAccount === null) {
throw new NoDonorAccountException("Donor account not found for donation $donation");
}
$mandate = $donation->getMandate();
\assert($mandate !== null);
$currentMandateStatus = $mandate->getStatus();
if ($currentMandateStatus !== MandateStatus::Active) {
throw new MandateNotActive(
"Not confirming donation as mandate is '{$currentMandateStatus->name}', not Active"
);
}
$paymentMethod = $donorAccount->getRegularGivingPaymentMethod();
if ($paymentMethod === null) {
throw new \MatchBot\Domain\NoRegularGivingPaymentMethod(
"Cannot confirm donation {$donation->getUuid()} for " .
"{$donorAccount->stripeCustomerId->stripeCustomerId}, no payment method"
);
}
$campaign = $donation->getCampaign();
if ($campaign->regularGivingCollectionIsEndedAt($this->clock->now())) {
$collectionEnd = $campaign->getRegularGivingCollectionEnd();
Assertion::notNull($collectionEnd);
$donation->cancel();
$mandate->campaignEnded();
throw new RegularGivingCollectionEndPassed(
"Cannot confirm a donation now, " .
"regular giving collections for campaign {$campaign->getSalesforceId()} ended " .
"at {$collectionEnd->format('Y-m-d')}"
);
}
$this->stripe->confirmPaymentIntent(
$donation->getTransactionId(),
['payment_method' => $paymentMethod->stripePaymentMethodId]
);
}
/**
* Does multiple things required when a new donation is added to the system including:
* - Checking that the campaign is open
* - Allocating match funds to the donation
* - Creating Stripe Payment intent
*
* @throws CampaignNotOpen
* @throws CharityAccountLacksNeededCapaiblities
* @throws CouldNotMakeStripePaymentIntent
* @throws DBALServerException
* @throws ORMException
* @throws StripeAccountIdNotSetForAccount
* @throws TransportExceptionInterface
* @throws \MatchBot\Client\NotFoundException
*/
public function enrollNewDonation(Donation $donation): void
{
$campaign = $donation->getCampaign();
if (!$campaign->isOpen(new \DateTimeImmutable())) {
throw new CampaignNotOpen("Campaign {$campaign->getSalesforceId()} is not open");
}
if ($donation->getMandate() === null && $campaign->isRegularGiving()) {
throw new WrongCampaignType(
"Campaign {$campaign->getSalesforceId()} does not accept one-off giving (regular-giving only)"
);
}
if ($donation->getMandate() !== null && $campaign->isOneOffGiving()) {
throw new WrongCampaignType(
"Campaign {$campaign->getSalesforceId()} does not accept regular giving (one-off only)"
);
}
// A closed EM can happen if the above tried to insert a campaign or fund, hit a duplicate error because
// another thread did it already, then successfully got the new copy. There's been no subsequent
// database persistence that needed an open manager, so none replaced the broken one. In that
// edge case, we need to handle that before `persistWithoutRetries()` has a chance of working.
if (!$this->entityManager->isOpen()) {
$this->entityManager->resetManager();
}
// Must persist before Stripe work to have ID available. Outer fn throws if all attempts fail.
$this->runWithPossibleRetry(function () use ($donation) {
$this->entityManager->persistWithoutRetries($donation);
$this->entityManager->flush();
}, 'Donation Create persist before stripe work');
if ($campaign->isMatched()) {
$this->runWithPossibleRetry(
function () use ($donation) {
try {
$this->donationRepository->allocateMatchFunds($donation);
} catch (\Throwable $t) {
// warning indicates that we *may* retry, as it depends on whether this is in the last retry or
// not.
$this->logger->warning(sprintf('Allocation got error, may retry: %s', $t->getMessage()));
$this->matchingAdapter->releaseNewlyAllocatedFunds();
// we have to also remove the FundingWithdrawls from MySQL - otherwise the redis amount
// would be reduced again when the donation expires.
$this->donationRepository->removeAllFundingWithdrawalsForDonation($donation);
$this->entityManager->flush();
throw $t;
}
},
'allocate match funds'
);
}
if ($donation->getPsp() === 'stripe') {
$stripeAccountId = $campaign->getCharity()->getStripeAccountId();
if ($stripeAccountId === null || $stripeAccountId === '') {
// Try re-pulling in case charity has very recently onboarded with for Stripe.
$this->campaignRepository->updateFromSf($campaign);
// If still empty, error out
$stripeAccountId = $campaign->getCharity()->getStripeAccountId();
if ($stripeAccountId === null || $stripeAccountId === '') {
$this->logger->error(sprintf(
'Stripe Payment Intent create error: Stripe Account ID not set for Account %s',
$campaign->getCharity()->getSalesforceId() ?? 'missing charity sf ID',
));
throw new StripeAccountIdNotSetForAccount();
}
// Else we found new Stripe info and can proceed
$donation->setCampaign($campaign);
}
$this->createPaymentIntent($donation);
}
}
private function doUpdateDonationFees(
CardBrand $cardBrand,
Donation $donation,
Country $cardCountry,
): void {
// at present if the following line was left out we would charge a wrong fee in some cases. I'm not happy with
// that, would like to find a way to make it so if its left out we get an error instead - either by having
// derive fees return a value, or making functions like Donation::getCharityFeeGross throw if called before it.
$donation->deriveFees($cardBrand, $cardCountry);
// we still need this
$updatedIntentData = [
// only setting things that may need to be updated at this point.
'metadata' => [
'stripeFeeRechargeGross' => $donation->getCharityFeeGross(),
'stripeFeeRechargeNet' => $donation->getCharityFee(),
'stripeFeeRechargeVat' => $donation->getCharityFeeVat(),
],
// See https://stripe.com/docs/connect/destination-charges#application-fee
// Update the fee amount in case the final charge was from
// e.g. a Non EU / Amex card where fees are varied.
'application_fee_amount' => $donation->getAmountToDeductFractional(),
// Note that `on_behalf_of` is set up on create and is *not allowed* on update.
];
$this->stripe->updatePaymentIntent($donation->getTransactionId(), $updatedIntentData);
}
private function updateDonationFeesFromConfirmationToken(
StripeConfirmationTokenId $tokenId,
Donation $donation
): void {
$token = $this->stripe->retrieveConfirmationToken($tokenId);
/** @var StripeObject $paymentMethodPreview */
$paymentMethodPreview = $token->payment_method_preview;
/** @var StripeObject $card */
$card = $paymentMethodPreview['card'];
Assertion::string($card['brand']);
$cardBrand = CardBrand::from($card['brand']);
$cardCountry = $card['country'];
Assertion::string($cardCountry);
$cardCountry = Country::fromAlpha2($cardCountry);
$this->logger->info(sprintf(
'Donation UUID %s has card brand %s and country %s',
$donation->getUuid(),
$cardBrand->value,
$cardCountry,
));
$this->doUpdateDonationFees(
cardBrand: $cardBrand,
donation: $donation,
cardCountry: $cardCountry,
);
}
/**
* Creates a payment intent at Stripe and records the PI ID against the donation.
*/
public function createPaymentIntent(Donation $donation): void
{
Assertion::same($donation->getPsp(), 'stripe');
if (!$donation->getCampaign()->isOpen(new \DateTimeImmutable())) {
throw new CampaignNotOpen("Campaign {$donation->getCampaign()->getSalesforceId()} is not open");
}
try {
$intent = $this->stripe->createPaymentIntent($donation->createStripePaymentIntentPayload());
} catch (ApiErrorException $exception) {
$message = $exception->getMessage();
$accountLacksCapabilities = str_contains(
$message,
// this message is an issue the charity needs to fix, we can't fix it for them.
// We likely want to let the team know to hide the campaign from prominents views though.
'Your destination account needs to have at least one of the following capabilities enabled'
);
$failureMessage = sprintf(
'Stripe Payment Intent create error on %s, %s [%s]: %s. Charity: %s [%s].',
$donation->getUuid(),
$exception->getStripeCode() ?? 'unknown',
get_class($exception),
$message,
$donation->getCampaign()->getCharity()->getName(),
$donation->getCampaign()->getCharity()->getStripeAccountId() ?? 'unknown',
);
$level = $accountLacksCapabilities ? LogLevel::WARNING : LogLevel::ERROR;
$this->logger->log($level, $failureMessage);
if ($accountLacksCapabilities) {
$env = getenv('APP_ENV');
\assert(is_string($env));
$failureMessageWithContext = sprintf(
'[%s] %s',
$env,
$failureMessage,
);
$this->chatter->send(new ChatMessage($failureMessageWithContext));
throw new CharityAccountLacksNeededCapaiblities();
}
throw new CouldNotMakeStripePaymentIntent();
}
$donation->setTransactionId($intent->id);
$this->runWithPossibleRetry(
function () use ($donation) {
$this->entityManager->persistWithoutRetries($donation);
$this->entityManager->flush();
},
'Donation Create persist after stripe work'
);
}
/**
* Sets donation to cancelled in matchbot db, releases match funds, cancels payment in stripe, and updates
* salesforce.
*
* Call this from inside a transaction and with a locked donation to avoid double releasing funds associated with
* the donation.
*/
public function cancel(Donation $donation): void
{
if ($donation->getDonationStatus() === DonationStatus::Cancelled) {
$this->logger->info("Donation ID {$donation->getUuid()} was already Cancelled");
return;
}
if ($donation->getDonationStatus()->isSuccessful()) {
// If a donor uses browser back before loading the thank you page, it is possible for them to get
// a Cancel dialog and send a cancellation attempt to this endpoint after finishing the donation.
throw new DonationAlreadyFinalised(
'Donation ID {$donation->getUuid()} could not be cancelled as {$donation->getDonationStatus()->value}'
);
}
$this->logger->info("Cancelled ID {$donation->getUuid()}");
$donation->cancel();
// Save & flush early to reduce chance of lock conflicts.
$this->save($donation);
if ($donation->getCampaign()->isMatched()) {
/** @psalm-suppress InternalMethod */
$this->donationRepository->releaseMatchFunds($donation);
}
if ($donation->getPsp() === 'stripe') {
try {
$this->stripe->cancelPaymentIntent($donation->getTransactionId());
} catch (ApiErrorException $exception) {
/**
* As per the notes in {@see DonationRepository::releaseMatchFunds()}, we
* occasionally see double-cancels from the frontend. If Stripe tell us the
* Cancelled Donation's PI is canceled [note US spelling doesn't match our internal
* status], in all CC21 checks this seemed to be the situation.
*
* Instead of panicking in this scenario, our best available option is to log only a
* notice – we can still easily find these in the logs on-demand if we need to
* investigate proactively – and return 200 OK to the frontend.
*/
$doubleCancelMessage = 'You cannot cancel this PaymentIntent because it has a status of canceled.';
$returnError = !str_starts_with($exception->getMessage(), $doubleCancelMessage);
$stripeErrorLogLevel = $returnError ? LogLevel::ERROR : LogLevel::NOTICE;
// We use the same log message, but reduce the severity in the case where we have detected
// that it's unlikely to be a serious issue.
$this->logger->log(
$stripeErrorLogLevel,
'Stripe Payment Intent cancel error: ' .
get_class($exception) . ': ' . $exception->getMessage()
);
if ($returnError) {
throw new CouldNotCancelStripePaymentIntent();
} // Else likely double-send -> fall through to normal return the donation as-is.
}
}
}
/**
* Save donation in all cases. Also send updated donation data to Salesforce, *if* we know
* enough to do so successfully.
*
* Assumes it will be called only after starting a transaction pre-donation-select.
*
* @param Donation $donation
*/
public function save(Donation $donation): void
{
// SF push and the corresponding DB persist only happens when names are already set.
// There could be other data we need to save before that point, e.g. comms
// preferences, so to be safe we persist here first.
$this->entityManager->persist($donation);
$this->entityManager->flush();
if (!$donation->hasEnoughDataForSalesforce()) {
return;
}
$donationUpserted = DonationUpserted::fromDonation($donation);
$envelope = new Envelope($donationUpserted);
$this->bus->dispatch($envelope);
}
/**
* InvalidRequestException can have various possible messages. If it's one we've seen before that we don't believe
* indicates a bug or failure in matchbot then we just send an error message to the client. If it's something we
* haven't seen before or didn't expect then we will also generate an alarm for Big Give devs to deal with.
* @param InvalidRequestException $exception
* @return bool
*/
public static function errorMessageFromStripeIsExpected(InvalidRequestException $exception): bool
{
$exceptionMessage = $exception->getMessage();
foreach (DonationService::EXPECTED_STRIPE_INVALID_REQUEST_MESSAGES as $expectedMessage) {
if (str_contains($exceptionMessage, $expectedMessage)) {
return true;
}
}
return false;
}
/**
* Within a transaction, loads a donation from the DB and then releases any funding matched to it.
*
* If the matching for the donation has already been released (e.g. by another process after the donationId
* was found but before we lock the donation here) then this should be a no-op because Donation::fundingWithdrawals
* are eagerly loaded with the donation so will be empty.
*/
public function releaseMatchFundsInTransaction(UuidInterface $donationId): void
{
$this->entityManager->wrapInTransaction(function () use ($donationId) {
$donationRepository = $this->donationRepository;
$donation = $donationRepository->findAndLockOneByUUID($donationId);
Assertion::notNull($donation);
$this->donationRepository->releaseMatchFunds($donation);
$this->entityManager->flush();
});
}
public function donationAsApiModel(UuidInterface $donationUUID): array
{
$donation = $this->donationRepository->findOneBy(['uuid' => $donationUUID]);
if (!$donation) {
throw new DomainRecordNotFoundException('Donation not found');
}
return $donation->toFrontEndApiModel();
}
public function findAllCompleteForCustomerAsAPIModels(StripeCustomerId $stripeCustomerId): array
{
$donations = $this->donationRepository->findAllCompleteForCustomer($stripeCustomerId);
return array_map(fn(Donation $donation) => $donation->toFrontEndApiModel(), $donations);
}
}