give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
Actions
/
UpdateDonationStatus.php
GetEventHandlerClassByDonationStatus.php
1 year ago
GetEventHandlerClassBySubscriptionStatus.php
1 year ago
UpdateDonationStatus.php
2 years ago
UpdateSubscriptionStatus.php
2 weeks ago
UpdateDonationStatus.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions; |
| 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\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 10 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 11 | |
| 12 | /** |
| 13 | * @since 3.6.0 |
| 14 | */ |
| 15 | class UpdateDonationStatus |
| 16 | { |
| 17 | /** |
| 18 | * @since 3.6.0 |
| 19 | * |
| 20 | * @throws Exception |
| 21 | */ |
| 22 | public function __invoke( |
| 23 | Donation $donation, |
| 24 | DonationStatus $status, |
| 25 | string $message = '' |
| 26 | ) { |
| 27 | $donation->status = $status; |
| 28 | $donation->save(); |
| 29 | |
| 30 | if (empty($message)) { |
| 31 | $message = $this->getMessageFromStatus($status); |
| 32 | } |
| 33 | |
| 34 | DonationNote::create([ |
| 35 | 'donationId' => $donation->id, |
| 36 | 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'), |
| 37 | $donation->gateway()->getName(), |
| 38 | $donation->gatewayTransactionId |
| 39 | ), |
| 40 | ]); |
| 41 | |
| 42 | PaymentGatewayLog::info( |
| 43 | $message . ' ' . sprintf('Donation ID: %s.', $donation->id), |
| 44 | [ |
| 45 | 'Payment Gateway' => $donation->gatewayId, |
| 46 | 'Gateway Transaction Id' => $donation->gatewayTransactionId, |
| 47 | 'Donation' => $donation->id, |
| 48 | 'Subscription' => $donation->subscriptionId, |
| 49 | 'Gateway Subscription Id' => ! $donation->type->isSingle() ? $donation->subscription->gatewaySubscriptionId : null, |
| 50 | ] |
| 51 | ); |
| 52 | |
| 53 | if ($this->isInvalidSubscriptionFirstPayment($donation) && |
| 54 | ! $donation->subscription->status->isCancelled()) { |
| 55 | $donation->subscription->status = SubscriptionStatus::CANCELLED(); |
| 56 | $donation->subscription->save(); |
| 57 | |
| 58 | PaymentGatewayLog::info( |
| 59 | sprintf("The subscription was canceled because the first payment wasn't finished. Subscription ID: %s.", |
| 60 | $donation->subscription->id), |
| 61 | [ |
| 62 | 'Payment Gateway' => $donation->gatewayId, |
| 63 | 'First Payment Status' => $donation->status->getValue(), |
| 64 | 'First Payment ID' => $donation->id, |
| 65 | 'First Payment Gateway Transaction ID' => $donation->gatewayTransactionId, |
| 66 | 'Gateway Subscription Id' => $donation->subscription->gatewaySubscriptionId, |
| 67 | 'Subscription ID' => $donation->subscription->id, |
| 68 | ] |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @since 3.6.0 |
| 75 | */ |
| 76 | private function isInvalidSubscriptionFirstPayment(Donation $donation): bool |
| 77 | { |
| 78 | return $donation->type->isSubscription() && |
| 79 | ($donation->status->isAbandoned() || $donation->status->isCancelled()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @since 3.6.0 |
| 84 | */ |
| 85 | protected function getMessageFromStatus(DonationStatus $status): string |
| 86 | { |
| 87 | $message = ''; |
| 88 | |
| 89 | switch (true): |
| 90 | case ($status->isAbandoned()): |
| 91 | $message = __('Donation Abandoned.', 'give'); |
| 92 | break; |
| 93 | case ($status->isCancelled()): |
| 94 | $message = __('Donation Cancelled.', 'give'); |
| 95 | break; |
| 96 | case ($status->isComplete()): |
| 97 | $message = __('Donation Completed.', 'give'); |
| 98 | break; |
| 99 | case ($status->isFailed()): |
| 100 | $message = __('Donation Failed.', 'give'); |
| 101 | break; |
| 102 | case ($status->isPending()): |
| 103 | $message = __('Donation Pending.', 'give'); |
| 104 | break; |
| 105 | case ($status->isPreapproval()): |
| 106 | $message = __('Donation Pre Approval.', 'give'); |
| 107 | break; |
| 108 | case ($status->isProcessing()): |
| 109 | $message = __('Donation Processing.', 'give'); |
| 110 | break; |
| 111 | case ($status->isRefunded()): |
| 112 | $message = __('Donation Refunded.', 'give'); |
| 113 | break; |
| 114 | case ($status->isRevoked()): |
| 115 | $message = __('Donation Revoked.', 'give'); |
| 116 | break; |
| 117 | endswitch; |
| 118 | |
| 119 | return $message; |
| 120 | } |
| 121 | } |
| 122 |