| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Events\Order; |
| 4 |
|
| 5 |
use FluentCart\App\Events\EventDispatcher; |
| 6 |
use FluentCart\App\Listeners; |
| 7 |
use FluentCart\App\Models\Customer; |
| 8 |
use FluentCart\App\Models\Order; |
| 9 |
use FluentCart\App\Models\OrderTransaction; |
| 10 |
|
| 11 |
class OrderPaid extends EventDispatcher |
| 12 |
{ |
| 13 |
|
| 14 |
public string $hook = 'fluent_cart/order_paid'; |
| 15 |
protected array $listeners = [ |
| 16 |
Listeners\Order\OrderPaid::class, |
| 17 |
]; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Order $order |
| 21 |
*/ |
| 22 |
public Order $order; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Customer|null $customer |
| 26 |
*/ |
| 27 |
public ?Customer $customer; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var OrderTransaction|null $transaction |
| 31 |
*/ |
| 32 |
public ?OrderTransaction $transaction; |
| 33 |
|
| 34 |
public function __construct(Order $order, $customer = null, $transaction = null) |
| 35 |
{ |
| 36 |
$this->order = $order; |
| 37 |
$this->order->load('customer', 'order_items', 'shipping_address', 'billing_address'); |
| 38 |
$this->customer = $customer; |
| 39 |
$this->transaction = $transaction; |
| 40 |
} |
| 41 |
|
| 42 |
public function toArray(): array |
| 43 |
{ |
| 44 |
return [ |
| 45 |
'order' => $this->order, |
| 46 |
'customer' => $this->customer ?? null, |
| 47 |
'transaction' => $this->transaction ?? null |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
public function getActivityEventModel() |
| 52 |
{ |
| 53 |
return $this->order; |
| 54 |
} |
| 55 |
|
| 56 |
public function beforeDispatch() |
| 57 |
{ |
| 58 |
if (!$this->order->receipt_number) { |
| 59 |
$this->order = $this->order->generateReceiptNumber(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function afterDispatch() |
| 64 |
{ |
| 65 |
/** |
| 66 |
* 3rd party devs: Please do not use this hook to add any action that |
| 67 |
* use: fluent_cart/order_paid_done |
| 68 |
*/ |
| 69 |
$id = as_enqueue_async_action('fluent_cart/order_paid_async_private_handle', [ |
| 70 |
[ |
| 71 |
'order_id' => $this->order->id |
| 72 |
] |
| 73 |
], 'fluent-cart'); |
| 74 |
|
| 75 |
if ($id) { |
| 76 |
$this->order->updateMeta('action_scheduler_id', $id); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|