give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
SubscriptionFirstDonationCompleted.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
SubscriptionFirstDonationCompleted.php
55 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Donations\ValueObjects\DonationStatus; |
| 7 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 8 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateDonationStatus; |
| 9 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateSubscriptionStatus; |
| 10 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 11 | |
| 12 | /** |
| 13 | * @since 3.6.0 |
| 14 | */ |
| 15 | class SubscriptionFirstDonationCompleted |
| 16 | { |
| 17 | /** |
| 18 | * @since 3.6.0 |
| 19 | */ |
| 20 | public function __invoke(string $gatewayTransactionId, string $message = '', bool $setSubscriptionActive = true) |
| 21 | { |
| 22 | $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId); |
| 23 | |
| 24 | if ( ! $donation || ! $donation->type->isSubscription() || $donation->id !== $donation->subscription->initialDonation()->id) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | try { |
| 29 | if ( ! $donation->status->isComplete()) { |
| 30 | if (empty($message)) { |
| 31 | $message = __('Subscription First Donation Completed.', 'give'); |
| 32 | } |
| 33 | |
| 34 | (new UpdateDonationStatus())($donation, DonationStatus::COMPLETE(), $message); |
| 35 | } |
| 36 | |
| 37 | if ($setSubscriptionActive && ! $donation->subscription->status->isActive()) { |
| 38 | (new UpdateSubscriptionStatus())($donation->subscription, SubscriptionStatus::ACTIVE(), |
| 39 | __('Subscription Active After The First Donation Got Completed.', 'give')); |
| 40 | } |
| 41 | } catch (Exception $e) { |
| 42 | PaymentGatewayLog::error( |
| 43 | sprintf('Subscription First Donation Failed! Error: %s', |
| 44 | $e->getCode() . ' - ' . $e->getMessage()), |
| 45 | [ |
| 46 | 'Donation Id' => $donation->id, |
| 47 | 'Gateway Transaction Id' => $gatewayTransactionId, |
| 48 | 'Gateway Subscription Id' => $donation->subscriptionId, |
| 49 | 'message' => $message, |
| 50 | ] |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 |