OrderPurchased.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Api\Webhooks\Handlers; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\IntegrationsApiHandler; |
| 6 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 7 | |
| 8 | use Hostinger\Reach\Dto\CartItem; |
| 9 | use Hostinger\Reach\Dto\Totals; |
| 10 | use Hostinger\Reach\Repositories\FormRepository; |
| 11 | use WC_Order; |
| 12 | |
| 13 | class OrderPurchased extends WebhookHandler { |
| 14 | const WEBHOOK_NAME = 'order.purchased'; |
| 15 | |
| 16 | private IntegrationsApiHandler $integrations_api_handler; |
| 17 | |
| 18 | public function __construct( ReachApiHandler $reach_api_handler, IntegrationsApiHandler $integrations_api_handler, FormRepository $form_repository ) { |
| 19 | parent::__construct( $reach_api_handler, $form_repository ); |
| 20 | $this->integrations_api_handler = $integrations_api_handler; |
| 21 | } |
| 22 | |
| 23 | public function get_name(): string { |
| 24 | return self::WEBHOOK_NAME; |
| 25 | } |
| 26 | |
| 27 | public function get_metadata( mixed $data ): array { |
| 28 | if ( ! $data instanceof WC_Order ) { |
| 29 | return array(); |
| 30 | } |
| 31 | |
| 32 | return array( |
| 33 | 'order_id' => $data->get_id(), |
| 34 | 'order_number' => $data->get_order_number(), |
| 35 | 'totals' => $this->get_order_totals( $data ), |
| 36 | 'items' => $this->get_order_items( $data ), |
| 37 | 'purchase_date' => $this->get_purchase_date( $data ), |
| 38 | 'payment_method' => $data->get_payment_method(), |
| 39 | 'shipping_method' => $this->get_shipping_method_code( $data ), |
| 40 | 'customer' => $this->get_customer_data( $data ), |
| 41 | 'currency' => $data->get_currency(), |
| 42 | 'type' => $data->get_type(), |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | public function init_hooks(): void { |
| 47 | add_action( |
| 48 | 'woocommerce_order_status_processing', |
| 49 | array( |
| 50 | $this, |
| 51 | 'handle_woocommerce_order_status_processing', |
| 52 | ), |
| 53 | 10, |
| 54 | 2 |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | public function handle_woocommerce_order_status_processing( int $order_id ): void { |
| 59 | if ( ! $this->is_enabled() ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $order = wc_get_order( $order_id ); |
| 64 | |
| 65 | if ( ! $order ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $email = $order->get_billing_email(); |
| 70 | |
| 71 | if ( ! $email ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $this->handle( $email, $order ); |
| 76 | } |
| 77 | |
| 78 | public function is_enabled(): bool { |
| 79 | return parent::is_enabled() && $this->integrations_api_handler->is_active( 'woocommerce' ); |
| 80 | } |
| 81 | |
| 82 | private function get_purchase_date( WC_Order $order ): ?string { |
| 83 | $date_created = $order->get_date_created(); |
| 84 | |
| 85 | return $date_created?->format( 'Y-m-d\TH:i:s\Z' ); |
| 86 | } |
| 87 | |
| 88 | private function get_order_totals( WC_Order $order ): array { |
| 89 | $totals = Totals::from_order( $order ); |
| 90 | |
| 91 | return $totals->to_array(); |
| 92 | } |
| 93 | |
| 94 | private function get_shipping_method_code( WC_Order $order ): ?string { |
| 95 | $shipping_methods = $order->get_shipping_methods(); |
| 96 | |
| 97 | if ( empty( $shipping_methods ) ) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | $shipping_method = reset( $shipping_methods ); |
| 102 | |
| 103 | return $shipping_method->get_method_id(); |
| 104 | } |
| 105 | |
| 106 | private function get_order_items( WC_Order $order ): array { |
| 107 | $items = array(); |
| 108 | |
| 109 | foreach ( $order->get_items() as $item ) { |
| 110 | $cart_item = new CartItem( $item->get_product_id(), $item->get_quantity() ); |
| 111 | $items[] = $cart_item->to_array(); |
| 112 | } |
| 113 | |
| 114 | return $items; |
| 115 | } |
| 116 | |
| 117 | private function get_customer_data( WC_Order $order ): array { |
| 118 | return array( |
| 119 | 'email' => $order->get_billing_email(), |
| 120 | 'name' => $order->get_billing_first_name(), |
| 121 | 'surname' => $order->get_billing_last_name(), |
| 122 | 'phone' => $order->get_billing_phone(), |
| 123 | 'company' => $order->get_billing_company(), |
| 124 | 'address' => array( |
| 125 | 'address_1' => $order->get_billing_address_1(), |
| 126 | 'address_2' => $order->get_billing_address_2(), |
| 127 | 'city' => $order->get_billing_city(), |
| 128 | 'state' => $order->get_billing_state(), |
| 129 | 'postcode' => $order->get_billing_postcode(), |
| 130 | 'country' => $order->get_billing_country(), |
| 131 | ), |
| 132 | ); |
| 133 | } |
| 134 | } |
| 135 |