give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
SubscriptionFirstDonationCompleted.php
Actions
11 months 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
11 months ago
SubscriptionPaused.php
11 months ago
SubscriptionPending.php
11 months ago
SubscriptionRenewalDonationCreated.php
1 year ago
SubscriptionSuspended.php
2 years ago
SubscriptionFirstDonationCompleted.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Donations\Models\DonationNote; |
| 7 | use Give\Donations\ValueObjects\DonationStatus; |
| 8 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 9 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateDonationStatus; |
| 10 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateSubscriptionStatus; |
| 11 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 12 | |
| 13 | /** |
| 14 | * @since 3.6.0 |
| 15 | */ |
| 16 | class SubscriptionFirstDonationCompleted |
| 17 | { |
| 18 | /** |
| 19 | * @since 4.5.0 Add $setDonationComplete and $gatewaySubscriptionId parameters |
| 20 | * @since 3.6.0 |
| 21 | */ |
| 22 | public function __invoke( |
| 23 | string $gatewayTransactionId, |
| 24 | string $message = '', |
| 25 | bool $setSubscriptionActive = true, |
| 26 | bool $setDonationComplete = true, |
| 27 | string $gatewaySubscriptionId = '' |
| 28 | ) |
| 29 | { |
| 30 | $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId); |
| 31 | |
| 32 | if (! $donation && ! empty($gatewaySubscriptionId) && $subscription = give()->subscriptions->getByGatewaySubscriptionId($gatewaySubscriptionId)) { |
| 33 | $donation = $subscription->initialDonation(); |
| 34 | $donation->gatewayTransactionId = $gatewayTransactionId; |
| 35 | $donation->save(); |
| 36 | } |
| 37 | |
| 38 | if ( ! $donation || ! $donation->type->isSubscription() || $donation->id !== $donation->subscription->initialDonation()->id) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | try { |
| 43 | if ($setDonationComplete && ! $donation->status->isComplete()) { |
| 44 | if (empty($message)) { |
| 45 | $message = __('Subscription First Donation Completed.', 'give'); |
| 46 | } |
| 47 | |
| 48 | (new UpdateDonationStatus())($donation, DonationStatus::COMPLETE(), $message); |
| 49 | } else { |
| 50 | if (empty($message)) { |
| 51 | $message = __('Subscription First Donation Updated.', 'give'); |
| 52 | } |
| 53 | |
| 54 | DonationNote::create([ |
| 55 | 'donationId' => $donation->id, |
| 56 | 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'), |
| 57 | $donation->gateway()->getName(), |
| 58 | $donation->gatewayTransactionId |
| 59 | ), |
| 60 | ]); |
| 61 | } |
| 62 | |
| 63 | if ($setSubscriptionActive && ! $donation->subscription->status->isActive()) { |
| 64 | (new UpdateSubscriptionStatus())($donation->subscription, SubscriptionStatus::ACTIVE(), |
| 65 | __('Subscription Active After The First Donation Got Completed.', 'give')); |
| 66 | } |
| 67 | } catch (Exception $e) { |
| 68 | PaymentGatewayLog::error( |
| 69 | sprintf('Subscription First Donation Failed! Error: %s', |
| 70 | $e->getCode() . ' - ' . $e->getMessage()), |
| 71 | [ |
| 72 | 'Donation Id' => $donation->id, |
| 73 | 'Gateway Transaction Id' => $gatewayTransactionId, |
| 74 | 'Gateway Subscription Id' => $donation->subscriptionId, |
| 75 | 'message' => $message, |
| 76 | ] |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |