give
/
src
/
Framework
/
PaymentGateways
/
Webhooks
/
EventHandlers
/
Actions
/
UpdateSubscriptionStatus.php
GetEventHandlerClassByDonationStatus.php
11 months ago
GetEventHandlerClassBySubscriptionStatus.php
11 months ago
UpdateDonationStatus.php
2 years ago
UpdateSubscriptionStatus.php
11 months ago
UpdateSubscriptionStatus.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; |
| 7 | use Give\Subscriptions\Models\Subscription; |
| 8 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 9 | |
| 10 | /** |
| 11 | * @since 3.6.0 |
| 12 | */ |
| 13 | class UpdateSubscriptionStatus |
| 14 | { |
| 15 | /** |
| 16 | * @since 3.6.0 |
| 17 | * |
| 18 | * @throws Exception |
| 19 | */ |
| 20 | public function __invoke( |
| 21 | Subscription $subscription, |
| 22 | SubscriptionStatus $status, |
| 23 | string $message = '' |
| 24 | ) { |
| 25 | $subscription->status = $status; |
| 26 | $subscription->save(); |
| 27 | |
| 28 | if (empty($message)) { |
| 29 | $message = $this->getMessageFromStatus($status); |
| 30 | } |
| 31 | |
| 32 | PaymentGatewayLog::info( |
| 33 | $message . ' ' . sprintf('Subscription ID: %s.', $subscription->id), |
| 34 | [ |
| 35 | 'Payment Gateway' => $subscription->gatewayId, |
| 36 | 'Gateway Subscription Id' => $subscription->gatewaySubscriptionId, |
| 37 | 'Subscription ID' => $subscription->id, |
| 38 | ] |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * @since 4.5.0 Add support for PAUSED and PENDING statuses |
| 45 | * @since 3.6.0 |
| 46 | */ |
| 47 | protected function getMessageFromStatus(SubscriptionStatus $status): string |
| 48 | { |
| 49 | $message = ''; |
| 50 | |
| 51 | switch (true): |
| 52 | case ($status->isCompleted()): |
| 53 | $message = __('Subscription Completed.', 'give'); |
| 54 | break; |
| 55 | case ($status->isExpired()): |
| 56 | $message = __('Subscription Expired.', 'give'); |
| 57 | break; |
| 58 | case ($status->isActive()): |
| 59 | $message = __('Subscription Active.', 'give'); |
| 60 | break; |
| 61 | case ($status->isCancelled()): |
| 62 | $message = __('Subscription Cancelled.', 'give'); |
| 63 | break; |
| 64 | case ($status->isFailing()): |
| 65 | $message = __('Subscription Failing.', 'give'); |
| 66 | break; |
| 67 | case ($status->isPaused()): |
| 68 | $message = __('Subscription Paused.', 'give'); |
| 69 | break; |
| 70 | case ($status->isPending()): |
| 71 | $message = __('Subscription Pending.', 'give'); |
| 72 | break; |
| 73 | case ($status->isSuspended()): |
| 74 | $message = __('Subscription Suspended.', 'give'); |
| 75 | break; |
| 76 | endswitch; |
| 77 | |
| 78 | return $message; |
| 79 | } |
| 80 | } |
| 81 |