give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
SubscriptionFirstDonationCompleted.php
SubscriptionFirstDonationCompleted.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/PaymentGateways/Webhooks/EventHandlers/SubscriptionFirstDonationCompleted.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Donations\Models\Donation; |
| 7 | use Give\Donations\Models\DonationNote; |
| 8 | use Give\Donations\ValueObjects\DonationStatus; |
| 9 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 10 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateDonationStatus; |
| 11 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateSubscriptionStatus; |
| 12 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 13 | |
| 14 | /** |
| 15 | * @since 3.6.0 |
| 16 | */ |
| 17 | class SubscriptionFirstDonationCompleted |
| 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). |
| 22 | * @since 4.5.0 Add $setDonationComplete and $gatewaySubscriptionId parameters |
| 23 | * @since 3.6.0 |
| 24 | */ |
| 25 | public function __invoke( |
| 26 | string $gatewayTransactionId, |
| 27 | string $message = '', |
| 28 | bool $setSubscriptionActive = true, |
| 29 | bool $setDonationComplete = true, |
| 30 | string $gatewaySubscriptionId = '', |
| 31 | int $donationId = 0 |
| 32 | ) |
| 33 | { |
| 34 | if ($donationId > 0) { |
| 35 | $donation = Donation::find($donationId); |
| 36 | |
| 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 | } |
| 57 | } |
| 58 | |
| 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 | |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | try { |
| 75 | if ($setDonationComplete && ! $donation->status->isComplete()) { |
| 76 | if (empty($message)) { |
| 77 | $message = __('Subscription First Donation Completed.', 'give'); |
| 78 | } |
| 79 | |
| 80 | (new UpdateDonationStatus())($donation, DonationStatus::COMPLETE(), $message); |
| 81 | } else { |
| 82 | if (empty($message)) { |
| 83 | $message = __('Subscription First Donation Updated.', 'give'); |
| 84 | } |
| 85 | |
| 86 | DonationNote::create([ |
| 87 | 'donationId' => $donation->id, |
| 88 | 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'), |
| 89 | $donation->gateway()->getName(), |
| 90 | $donation->gatewayTransactionId |
| 91 | ), |
| 92 | ]); |
| 93 | } |
| 94 | |
| 95 | if ($setSubscriptionActive && ! $donation->subscription->status->isActive()) { |
| 96 | (new UpdateSubscriptionStatus())($donation->subscription, SubscriptionStatus::ACTIVE(), |
| 97 | __('Subscription Active After The First Donation Got Completed.', 'give')); |
| 98 | } |
| 99 | } catch (Exception $e) { |
| 100 | PaymentGatewayLog::error( |
| 101 | sprintf('Subscription First Donation Failed! Error: %s', |
| 102 | $e->getCode() . ' - ' . $e->getMessage()), |
| 103 | [ |
| 104 | 'Donation Id' => $donation->id, |
| 105 | 'Gateway Transaction Id' => $gatewayTransactionId, |
| 106 | 'Gateway Subscription Id' => $donation->subscriptionId, |
| 107 | 'message' => $message, |
| 108 | ] |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 |