| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Events\Subscription; |
| 4 |
|
| 5 |
use FluentCart\App\Events\EventDispatcher; |
| 6 |
use FluentCart\App\Models\Order; |
| 7 |
use FluentCart\App\Models\Subscription; |
| 8 |
|
| 9 |
class SubscriptionEOT extends EventDispatcher |
| 10 |
{ |
| 11 |
|
| 12 |
public string $hook = 'fluent_cart/subscription_eot'; |
| 13 |
protected array $listeners = []; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var Subscription |
| 17 |
*/ |
| 18 |
public Subscription $subscription; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var Order |
| 22 |
*/ |
| 23 |
public Order $order; |
| 24 |
|
| 25 |
public function __construct(Subscription $subscription, Order $order) |
| 26 |
{ |
| 27 |
$this->subscription = $subscription; |
| 28 |
$this->order = $order; |
| 29 |
$this->order->load('customer'); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
public function toArray(): array |
| 34 |
{ |
| 35 |
return [ |
| 36 |
'subscription' => $this->subscription, |
| 37 |
'order' => $this->order, |
| 38 |
'customer' => $this->order->customer ?? [], |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public function beforeDispatch() |
| 43 |
{ |
| 44 |
$result = $this->subscription->cancelRemoteSubscription([ |
| 45 |
'reason' => 'end_of_term', |
| 46 |
'fire_hooks' => false, |
| 47 |
'effective_from' => 'immediately', |
| 48 |
]); |
| 49 |
|
| 50 |
if (is_wp_error($result)) { |
| 51 |
fluent_cart_add_log( |
| 52 |
'Remote subscription cancellation failed on EOT', |
| 53 |
'Subscription #' . $this->subscription->id . ': ' . $result->get_error_message(), |
| 54 |
'error', |
| 55 |
[ |
| 56 |
'module' => 'subscription', |
| 57 |
'module_id' => $this->subscription->id |
| 58 |
] |
| 59 |
); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function getActivityEventModel() |
| 64 |
{ |
| 65 |
return $this->subscription; |
| 66 |
} |
| 67 |
|
| 68 |
public function shouldCreateActivity(): bool |
| 69 |
{ |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
|