give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
SubscriptionRenewalDonationCreated.php
Actions
2 years 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
2 years ago
SubscriptionRenewalDonationCreated.php
2 years ago
SubscriptionSuspended.php
2 years ago
SubscriptionRenewalDonationCreated.php
85 lines
| 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\Donations\ValueObjects\DonationType; |
| 10 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 11 | |
| 12 | /** |
| 13 | * @since 3.6.0 |
| 14 | */ |
| 15 | class SubscriptionRenewalDonationCreated |
| 16 | { |
| 17 | /** |
| 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 | return; |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | $donation = Donation::create([ |
| 33 | 'subscriptionId' => $subscription->id, |
| 34 | 'amount' => $subscription->amount, |
| 35 | 'status' => DonationStatus::COMPLETE(), |
| 36 | 'type' => DonationType::RENEWAL(), |
| 37 | 'donorId' => $subscription->donor->id, |
| 38 | 'firstName' => $subscription->donor->firstName, |
| 39 | 'lastName' => $subscription->donor->lastName, |
| 40 | 'email' => $subscription->donor->email, |
| 41 | 'gatewayId' => $subscription->gatewayId, |
| 42 | 'formId' => $subscription->donationFormId, |
| 43 | 'levelId' => $subscription->initialDonation()->levelId, |
| 44 | 'anonymous' => $subscription->initialDonation()->anonymous, |
| 45 | 'company' => $subscription->initialDonation()->company, |
| 46 | 'gatewayTransactionId' => $gatewayTransactionId, |
| 47 | ]); |
| 48 | |
| 49 | if (empty($message)) { |
| 50 | $message = __('Subscription Renewal Donation Created.', 'give'); |
| 51 | } |
| 52 | |
| 53 | DonationNote::create([ |
| 54 | 'donationId' => $donation->id, |
| 55 | 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'), |
| 56 | $donation->gateway()->getName(), |
| 57 | $donation->gatewayTransactionId |
| 58 | ), |
| 59 | ]); |
| 60 | |
| 61 | PaymentGatewayLog::info( |
| 62 | $message . ' ' . sprintf('Donation ID: %s', $donation->id), |
| 63 | [ |
| 64 | 'Payment Gateway' => $donation->gateway()->getId(), |
| 65 | 'Gateway Transaction Id' => $donation->gatewayTransactionId, |
| 66 | 'Gateway Subscription Id' => $donation->subscription->gatewaySubscriptionId, |
| 67 | 'Donation' => $donation->id, |
| 68 | 'Subscription' => $donation->subscriptionId, |
| 69 | ] |
| 70 | ); |
| 71 | } catch (Exception $e) { |
| 72 | PaymentGatewayLog::error( |
| 73 | sprintf('Subscription Renewal Donation Failed! Error: %s', |
| 74 | $e->getCode() . ' - ' . $e->getMessage()), |
| 75 | [ |
| 76 | 'Subscription Id' => $subscription->id, |
| 77 | 'Gateway Subscription Id' => $gatewaySubscriptionId, |
| 78 | 'Gateway Transaction Id' => $gatewayTransactionId, |
| 79 | 'message' => $message, |
| 80 | ] |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 |