← All changes
|
src/Framework/PaymentGateways/Webhooks/EventHandlers/SubscriptionFirstDonationCompleted.php
+39
-7
4.15.2
→
4.18.0
View file →
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | +use Give\Donations\Models\Donation; | |
| 6 | 7 | use Give\Donations\Models\DonationNote; |
| 7 | 8 | use Give\Donations\ValueObjects\DonationStatus; |
| 8 | 9 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 9 | 10 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateDonationStatus; |
| @@ -15,8 +16,10 @@ | ||
| 15 | 16 | */ |
| 16 | 17 | class SubscriptionFirstDonationCompleted |
| 17 | 18 | { |
| 18 | 19 | /** |
| 20 | + * @since 4.18.0 Guard against a null initial donation and a missing subscription instead of fataling on them. | |
| 21 | + * @since 4.16.0 Add $donationId to support gateways that only receive the transaction ID via webhook (e.g. PayFast). | |
| 19 | 22 | * @since 4.5.0 Add $setDonationComplete and $gatewaySubscriptionId parameters |
| 20 | 23 | * @since 3.6.0 |
| 21 | 24 | */ |
| 22 | 25 | public function __invoke( |
| @@ -23,20 +26,49 @@ | ||
| 23 | 26 | string $gatewayTransactionId, |
| 24 | 27 | string $message = '', |
| 25 | 28 | bool $setSubscriptionActive = true, |
| 26 | 29 | bool $setDonationComplete = true, |
| 27 | - string $gatewaySubscriptionId = '' | |
| 30 | + string $gatewaySubscriptionId = '', | |
| 31 | + int $donationId = 0 | |
| 28 | 32 | ) |
| 29 | 33 | { |
| 30 | - $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId); | |
| 34 | + if ($donationId > 0) { | |
| 35 | + $donation = Donation::find($donationId); | |
| 31 | 36 | |
| 32 | - if (! $donation && ! empty($gatewaySubscriptionId) && $subscription = give()->subscriptions->getByGatewaySubscriptionId($gatewaySubscriptionId)) { | |
| 33 | - $donation = $subscription->initialDonation(); | |
| 34 | - $donation->gatewayTransactionId = $gatewayTransactionId; | |
| 35 | - $donation->save(); | |
| 37 | + if ($donation && $donation->subscription) { | |
| 38 | + $donation->gatewayTransactionId = $gatewayTransactionId; | |
| 39 | + $donation->save(); | |
| 40 | + | |
| 41 | + if ( ! empty($gatewaySubscriptionId) && empty($donation->subscription->gatewaySubscriptionId)) { | |
| 42 | + $donation->subscription->gatewaySubscriptionId = $gatewaySubscriptionId; | |
| 43 | + $donation->subscription->save(); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + } else { | |
| 47 | + $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId); | |
| 48 | + | |
| 49 | + if (! $donation && ! empty($gatewaySubscriptionId) && $subscription = give()->subscriptions->getByGatewaySubscriptionId($gatewaySubscriptionId)) { | |
| 50 | + $donation = $subscription->initialDonation(); | |
| 51 | + | |
| 52 | + if ($donation) { | |
| 53 | + $donation->gatewayTransactionId = $gatewayTransactionId; | |
| 54 | + $donation->save(); | |
| 55 | + } | |
| 56 | + } | |
| 36 | 57 | } |
| 37 | 58 | |
| 38 | - if ( ! $donation || ! $donation->type->isSubscription() || $donation->id !== $donation->subscription->initialDonation()->id) { | |
| 59 | + if ( ! $donation || ! $donation->subscription || ! $donation->type->isSubscription()) { | |
| 60 | + PaymentGatewayLog::error( | |
| 61 | + sprintf('The first donation was not updated for the gateway transaction ID %s because no valid subscription donation was found to update.', | |
| 62 | + $gatewayTransactionId), | |
| 63 | + [ | |
| 64 | + 'Gateway Subscription ID' => $gatewaySubscriptionId, | |
| 65 | + 'Gateway Transaction ID' => $gatewayTransactionId, | |
| 66 | + 'Donation ID' => $donationId, | |
| 67 | + 'Message' => $message, | |
| 68 | + ] | |
| 69 | + ); | |
| 70 | + | |
| 39 | 71 | return; |
| 40 | 72 | } |
| 41 | 73 | |
| 42 | 74 | try { |