give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
SubscriptionRenewalDonationCreated.php
SubscriptionRenewalDonationCreated.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/PaymentGateways/Webhooks/EventHandlers/SubscriptionRenewalDonationCreated.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Donations\Models\DonationNote; |
| 7 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 8 | |
| 9 | /** |
| 10 | * @since 3.6.0 |
| 11 | */ |
| 12 | class SubscriptionRenewalDonationCreated |
| 13 | { |
| 14 | /** |
| 15 | * @since 4.18.0 Bail when the subscription has no initial donation instead of fataling on it. |
| 16 | * @since 4.0.0 updated to create the renewal from subscription model |
| 17 | * @since 3.16.0 Add log messages and a defensive approach to prevent duplicated renewals |
| 18 | * @since 3.6.0 |
| 19 | */ |
| 20 | public function __invoke( |
| 21 | string $gatewaySubscriptionId, |
| 22 | string $gatewayTransactionId, |
| 23 | string $message = '' |
| 24 | ) { |
| 25 | $subscription = give()->subscriptions->getByGatewaySubscriptionId($gatewaySubscriptionId); |
| 26 | |
| 27 | if ( ! $subscription) { |
| 28 | PaymentGatewayLog::error( |
| 29 | sprintf('The renewal was not created for the gateway transaction ID %s because no subscription with the gateway subscription %s was found.', |
| 30 | $gatewayTransactionId, $gatewaySubscriptionId), |
| 31 | [ |
| 32 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 33 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 34 | 'Message' => $message, |
| 35 | ] |
| 36 | ); |
| 37 | |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $initialDonation = $subscription->initialDonation(); |
| 42 | |
| 43 | if ( ! $initialDonation) { |
| 44 | PaymentGatewayLog::error( |
| 45 | sprintf('The renewal was not created for the gateway transaction ID %s because the subscription %s has no initial donation to copy the donor details from.', |
| 46 | $gatewayTransactionId, $subscription->id), |
| 47 | [ |
| 48 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 49 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 50 | 'Message' => $message, |
| 51 | 'Subscription' => $subscription->toArray(), |
| 52 | ] |
| 53 | ); |
| 54 | |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if ($initialDonation->gatewayTransactionId === $gatewayTransactionId) { |
| 59 | PaymentGatewayLog::error( |
| 60 | sprintf('The renewal was not created for the gateway transaction ID %s because the initial donation of the subscription %s is already using the informed gateway transaction ID %s.', |
| 61 | $gatewayTransactionId, $subscription->id, $gatewaySubscriptionId), |
| 62 | [ |
| 63 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 64 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 65 | 'Message' => $message, |
| 66 | 'Subscription' => $subscription->toArray(), |
| 67 | ] |
| 68 | ); |
| 69 | |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId); |
| 74 | |
| 75 | if ($donation) { |
| 76 | PaymentGatewayLog::error( |
| 77 | sprintf('The renewal was not created for the gateway transaction ID %s because the donation %s is already using the informed gateway transaction ID %s.', |
| 78 | $gatewayTransactionId, $donation->id, $gatewaySubscriptionId), |
| 79 | [ |
| 80 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 81 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 82 | 'Message' => $message, |
| 83 | 'Donation' => $donation->toArray(), |
| 84 | ] |
| 85 | ); |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | try { |
| 91 | $donation = $subscription->createRenewal(['gatewayTransactionId' => $gatewayTransactionId]); |
| 92 | |
| 93 | if (empty($message)) { |
| 94 | $message = __('Subscription Renewal Donation Created.', 'give'); |
| 95 | } |
| 96 | |
| 97 | DonationNote::create([ |
| 98 | 'donationId' => $donation->id, |
| 99 | 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'), |
| 100 | $donation->gateway()->getName(), |
| 101 | $donation->gatewayTransactionId |
| 102 | ), |
| 103 | ]); |
| 104 | |
| 105 | PaymentGatewayLog::info( |
| 106 | $message . ' ' . sprintf('Donation ID: %s', $donation->id), |
| 107 | [ |
| 108 | 'Payment Gateway' => $donation->gateway()->getId(), |
| 109 | 'Gateway Transaction Id' => $donation->gatewayTransactionId, |
| 110 | 'Gateway Subscription Id' => $donation->subscription->gatewaySubscriptionId, |
| 111 | 'Donation' => $donation->id, |
| 112 | 'Subscription' => $donation->subscriptionId, |
| 113 | ] |
| 114 | ); |
| 115 | } catch (Exception $e) { |
| 116 | PaymentGatewayLog::error( |
| 117 | sprintf('Subscription Renewal Donation Failed! Error: %s', |
| 118 | $e->getCode() . ' - ' . $e->getMessage()), |
| 119 | [ |
| 120 | 'Subscription Id' => $subscription->id, |
| 121 | 'Gateway Subscription Id' => $gatewaySubscriptionId, |
| 122 | 'Gateway Transaction Id' => $gatewayTransactionId, |
| 123 | 'message' => $message, |
| 124 | ] |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 |