give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
SubscriptionRenewalDonationCreated.php
Actions
1 year ago
DonationAbandoned.php
2 years ago
DonationCancelled.php
2 years ago
DonationCompleted.php
2 years ago
DonationFailed.php
2 years ago
DonationPending.php
2 years ago
DonationPreapproval.php
2 years ago
DonationProcessing.php
2 years ago
DonationRefunded.php
2 years ago
DonationRevoked.php
2 years ago
SubscriptionActive.php
2 years ago
SubscriptionCancelled.php
2 years ago
SubscriptionCompleted.php
2 years ago
SubscriptionExpired.php
2 years ago
SubscriptionFailing.php
2 years ago
SubscriptionFirstDonationCompleted.php
1 year ago
SubscriptionPaused.php
1 year ago
SubscriptionPending.php
1 year ago
SubscriptionRenewalDonationCreated.php
1 year ago
SubscriptionSuspended.php
2 years ago
SubscriptionRenewalDonationCreated.php
111 lines
| 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.0.0 updated to create the renewal from subscription model |
| 16 | * @since 3.16.0 Add log messages and a defensive approach to prevent duplicated renewals |
| 17 | * @since 3.6.0 |
| 18 | */ |
| 19 | public function __invoke( |
| 20 | string $gatewaySubscriptionId, |
| 21 | string $gatewayTransactionId, |
| 22 | string $message = '' |
| 23 | ) { |
| 24 | $subscription = give()->subscriptions->getByGatewaySubscriptionId($gatewaySubscriptionId); |
| 25 | |
| 26 | if ( ! $subscription) { |
| 27 | PaymentGatewayLog::error( |
| 28 | sprintf('The renewal was not created for the gateway transaction ID %s because no subscription with the gateway subscription %s was found.', |
| 29 | $gatewayTransactionId, $gatewaySubscriptionId), |
| 30 | [ |
| 31 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 32 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 33 | 'Message' => $message, |
| 34 | ] |
| 35 | ); |
| 36 | |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if ($subscription->initialDonation()->gatewayTransactionId === $gatewayTransactionId) { |
| 41 | PaymentGatewayLog::error( |
| 42 | 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.', |
| 43 | $gatewayTransactionId, $subscription->id, $gatewaySubscriptionId), |
| 44 | [ |
| 45 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 46 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 47 | 'Message' => $message, |
| 48 | 'Subscription' => $subscription->toArray(), |
| 49 | ] |
| 50 | ); |
| 51 | |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId); |
| 56 | |
| 57 | if ($donation) { |
| 58 | PaymentGatewayLog::error( |
| 59 | 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.', |
| 60 | $gatewayTransactionId, $donation->id, $gatewaySubscriptionId), |
| 61 | [ |
| 62 | 'Gateway Subscription ID' => $gatewaySubscriptionId, |
| 63 | 'Gateway Transaction ID' => $gatewayTransactionId, |
| 64 | 'Message' => $message, |
| 65 | 'Donation' => $donation->toArray(), |
| 66 | ] |
| 67 | ); |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | $donation = $subscription->createRenewal(['gatewayTransactionId' => $gatewayTransactionId]); |
| 74 | |
| 75 | if (empty($message)) { |
| 76 | $message = __('Subscription Renewal Donation Created.', 'give'); |
| 77 | } |
| 78 | |
| 79 | DonationNote::create([ |
| 80 | 'donationId' => $donation->id, |
| 81 | 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'), |
| 82 | $donation->gateway()->getName(), |
| 83 | $donation->gatewayTransactionId |
| 84 | ), |
| 85 | ]); |
| 86 | |
| 87 | PaymentGatewayLog::info( |
| 88 | $message . ' ' . sprintf('Donation ID: %s', $donation->id), |
| 89 | [ |
| 90 | 'Payment Gateway' => $donation->gateway()->getId(), |
| 91 | 'Gateway Transaction Id' => $donation->gatewayTransactionId, |
| 92 | 'Gateway Subscription Id' => $donation->subscription->gatewaySubscriptionId, |
| 93 | 'Donation' => $donation->id, |
| 94 | 'Subscription' => $donation->subscriptionId, |
| 95 | ] |
| 96 | ); |
| 97 | } catch (Exception $e) { |
| 98 | PaymentGatewayLog::error( |
| 99 | sprintf('Subscription Renewal Donation Failed! Error: %s', |
| 100 | $e->getCode() . ' - ' . $e->getMessage()), |
| 101 | [ |
| 102 | 'Subscription Id' => $subscription->id, |
| 103 | 'Gateway Subscription Id' => $gatewaySubscriptionId, |
| 104 | 'Gateway Transaction Id' => $gatewayTransactionId, |
| 105 | 'message' => $message, |
| 106 | ] |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |