From 3f3a4ba1ee31bd279458708445c029f51de039c2 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Thu, 16 Dec 2021 18:49:54 +0700 Subject: [PATCH 1/3] [PLAT-124] Paymentwall support shipment tracking plugin --- src/includes/class-paymentwall-api.php | 4 +- src/paymentwall-for-woocommerce.php | 52 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/includes/class-paymentwall-api.php b/src/includes/class-paymentwall-api.php index 639bac7..0975e05 100644 --- a/src/includes/class-paymentwall-api.php +++ b/src/includes/class-paymentwall-api.php @@ -67,8 +67,8 @@ function prepare_delivery_confirmation_data($orderId, $deliveryStatus = '', $tra 'shipping_address[lastname]' => !empty($shippingAddress['last_name']) ? $shippingAddress['last_name'] : $billingAddress['last_name'], 'shipping_address[country]' => !empty($shippingAddress['country']) ? $shippingAddress['country'] : $billingAddress['country'], 'shipping_address[street]' => !empty($shippingAddress['address_1']) ? $shippingAddress['address_1'] : $billingAddress['address_1'], - 'shipping_address[state]' => !empty($shippingAddress['state']) ? $shippingAddress['state'] : !empty($billingAddress['state'])? $billingAddress['state'] : 'N/A', - 'shipping_address[zip]' => !empty($shippingAddress['postcode']) ? $shippingAddress['postcode'] : !empty($billingAddress['postcode']) ? $billingAddress['postcode'] : 'N/A' , + 'shipping_address[state]' => !empty($shippingAddress['state']) ? $shippingAddress['state'] : (!empty($billingAddress['state']) ? $billingAddress['state'] : 'N/A'), + 'shipping_address[zip]' => !empty($shippingAddress['postcode']) ? $shippingAddress['postcode'] : (!empty($billingAddress['postcode']) ? $billingAddress['postcode'] : 'N/A'), 'shipping_address[city]' => !empty($shippingAddress['city']) ? $shippingAddress['city'] : $billingAddress['city'], 'shipping_address[phone]' => !empty($shippingAddress['phone']) ? $shippingAddress['phone'] : $billingAddress['phone'], 'reason' => 'none', diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 9c0b4de..7b23170 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -150,6 +150,58 @@ function start_session() { } } +add_action('updated_postmeta', 'pw_on_order_tracking_change', 10, 4); + +add_action('added_post_meta', 'pw_on_order_tracking_change', 10, 4); + +function pw_on_order_tracking_change($meta_id, $post_id, $meta_key, $meta_value) { + if ($meta_key != '_wc_shipment_tracking_items' || empty($meta_value)) { + return; + } + + $tracking_data = $meta_value; + + if (!is_array($meta_value)) { + $tracking_data = unserialize($meta_value); + } + + if (empty($tracking_data) || empty($tracking_data[count($tracking_data) - 1])) { + return; + } + + $tracking_data = $tracking_data[0]; + + $order = wc_get_order($post_id); + if (!$order) { + return; + } + + $gateway = pw_get_order_paymentwall_gateway($order); + if (!$gateway || $gateway->id != Paymentwall_Gateway::PAYMENTWALL_METHOD) { + return; + } + + pw_update_delivery_status($order, Paymentwall_Api::DELIVERY_STATUS_ORDER_SHIPPED, $tracking_data); +} + +function pw_get_order_paymentwall_gateway(WC_Order $order) { + $paymentGateway = wc_get_payment_gateway_by_order($order); + + if ($paymentGateway->id == Paymentwall_Gateway::PAYMENTWALL_METHOD) { + return $paymentGateway; + } + return false; +} + +function pw_update_delivery_status(WC_Order $order, $status, $tracking_data = null) { + try { + $paymentwallApi = new Paymentwall_Api(); + $paymentwallApi->sendDeliveryApi($order->get_id(), $status, $tracking_data); + } catch (\Exception $e) { + return $e->getMessage(); + } +} + add_filter('woocommerce_available_payment_gateways', 'addPaymentwallGateway', 99, 2); function addPaymentwallGateway($availableGateways){ From 9d890575e299f0c4a399b5c31c05aae7b2032ffc Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Mon, 20 Dec 2021 18:10:58 +0700 Subject: [PATCH 2/3] update --- src/paymentwall-for-woocommerce.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 7b23170..ea86c76 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -165,7 +165,7 @@ function pw_on_order_tracking_change($meta_id, $post_id, $meta_key, $meta_value) $tracking_data = unserialize($meta_value); } - if (empty($tracking_data) || empty($tracking_data[count($tracking_data) - 1])) { + if (empty($tracking_data) || empty($tracking_data[0])) { return; } @@ -176,19 +176,18 @@ function pw_on_order_tracking_change($meta_id, $post_id, $meta_key, $meta_value) return; } - $gateway = pw_get_order_paymentwall_gateway($order); - if (!$gateway || $gateway->id != Paymentwall_Gateway::PAYMENTWALL_METHOD) { + $gateway = wc_get_payment_gateway_by_order($order); + if (!$gateway || !is_paymentwall_gateway($gateway)) { return; } pw_update_delivery_status($order, Paymentwall_Api::DELIVERY_STATUS_ORDER_SHIPPED, $tracking_data); } -function pw_get_order_paymentwall_gateway(WC_Order $order) { - $paymentGateway = wc_get_payment_gateway_by_order($order); - +function is_paymentwall_gateway($paymentGateway) : bool +{ if ($paymentGateway->id == Paymentwall_Gateway::PAYMENTWALL_METHOD) { - return $paymentGateway; + return true; } return false; } From 0b432963456f53601618770253f754c5ff66ba3e Mon Sep 17 00:00:00 2001 From: mason Date: Tue, 12 Apr 2022 18:27:09 +0700 Subject: [PATCH 3/3] Refactor code --- readme.txt | 3 +++ src/lib/paymentwall-php | 2 +- src/paymentwall-for-woocommerce.php | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/readme.txt b/readme.txt index a9dfc99..fe2f489 100644 --- a/readme.txt +++ b/readme.txt @@ -37,6 +37,9 @@ View our full installation guide: id == Paymentwall_Gateway::PAYMENTWALL_METHOD) { - return true; +function is_gateway_valid($gateway = null) { + if (empty($gateway) || empty($gateway->id) || $gateway->id != Paymentwall_Gateway::PAYMENTWALL_METHOD) { + return false; } - return false; + + return true; } function pw_update_delivery_status(WC_Order $order, $status, $tracking_data = null) {