From 8afc427650a622548aedae67ba07e5a003669076 Mon Sep 17 00:00:00 2001 From: Sameer Ababneh Date: Mon, 13 Sep 2021 18:53:26 +0400 Subject: [PATCH] Updating to PointCheckout API version 2.0 --- .../includes/lib/class-pc-payment-service.php | 85 ++++++++++--------- .../includes/lib/class-pc-utils.php | 6 +- .../woocommerce-pointcheckout-card.php | 2 +- ...lass-woocommerce-pointcheckout-rewards.php | 14 ++- .../includes/lib/class-pc-payment-service.php | 85 ++++++++++--------- .../includes/lib/class-pc-utils.php | 6 +- .../woocommerce-pointcheckout-rewards.php | 2 +- 7 files changed, 108 insertions(+), 92 deletions(-) diff --git a/pointcheckout_payment_card/includes/lib/class-pc-payment-service.php b/pointcheckout_payment_card/includes/lib/class-pc-payment-service.php index 1887d3c..596317f 100644 --- a/pointcheckout_payment_card/includes/lib/class-pc-payment-service.php +++ b/pointcheckout_payment_card/includes/lib/class-pc-payment-service.php @@ -1,5 +1,5 @@ pcOrder->loadOrder($orderId); $order->update_status($this->pcConfig->getNewOrderStatus()); + // ORDER INFO $params = array( - 'transactionId' => $orderId, + 'requestId' => $orderId, + 'orderId' => $orderId, + 'extVersion' => PC_CARD_EXT_VERSION ); - $params["extVersion"] = PC_CARD_EXT_VERSION; try{ - $params["ecommerce"]= 'WordPress ' . $this->get_wp_version() . ', WooCommerce ' . $this->wpbo_get_woo_version_number(); + $params['ecommerce']= 'WordPress ' . $this->get_wp_version() . ', WooCommerce ' . $this->wpbo_get_woo_version_number(); } catch (\Throwable $e) { // NOTHING TO DO } + $params['allowedPaymentMethods'] = ['CARD']; + $params['returnUrl'] = get_site_url() . '?wc-api=wc_gateway_pointcheckout_card_process_response'; + + + // CURRENCY AND AMOUNT + $params['currency'] = $this->pcOrder->getCurrencyCode(); + $params['amount'] = $this->pcOrder->getTotal(); + + // TOTALS + $totals = array(); + $totals['subtotal'] = $this->pcOrder->getSubtotal(); + $totals['tax'] = $this->pcOrder->getTaxAmount(); + $totals['shipping'] = $this->pcOrder->getShippingAmount(); + $totals['discount'] = $this->pcOrder->getDiscountAmount(); + if(!$totals['subtotal'] || $totals['subtotal'] <= 0) { + $totals['subtotal'] = $this->pcOrder->getTotal(); + } + $params['totals'] = $totals; + $cartItems = $order->get_items(); $items = array(); $i = 0; @@ -52,7 +73,7 @@ public function getPaymentRequestParams() 'sku' => $product->get_sku(), 'quantity' => $item_data->get_quantity(), 'type' => $product->get_type(), - 'total' => $item_data->get_total() + 'linetotal' => $item_data->get_total() ); //in case of bundles the bundle group item total is set to zero here to prevent conflict in totals if ($product->get_type() == 'bundle') { @@ -61,18 +82,6 @@ public function getPaymentRequestParams() $items[$i++] = $item; } $params['items'] = array_values($items); - $params['amount'] = $this->pcOrder->getTotal(); - $params['tax'] = $this->pcOrder->getTaxAmount(); - $params['shipping'] = $this->pcOrder->getShippingAmount(); - $params['subtotal'] = $this->pcOrder->getSubtotal(); - if(!$params['subtotal'] || $params['subtotal'] <= 0) { - $params['subtotal'] = $this->pcOrder->getTotal(); - } - - $params['discount'] = $this->pcOrder->getDiscountAmount(); - $params['currency'] = $this->pcOrder->getCurrencyCode(); - $params['paymentMethods'] = ["CARD"]; - $params['resultUrl'] = get_site_url() . "?wc-api=wc_gateway_pointcheckout_card_process_response"; // CUSTOMER $customer = array(); @@ -83,6 +92,7 @@ public function getPaymentRequestParams() $customer['lastName'] = $order->get_billing_last_name(); $customer['email'] = $order->get_billing_email(); $customer['phone'] = $order->get_billing_phone(); + $params['customer'] = $customer; // BILLING ADDRESS $billingAddress = array(); @@ -92,19 +102,19 @@ public function getPaymentRequestParams() $billingAddress['city'] = $order->get_billing_city(); $billingAddress['state'] = $order->get_billing_state(); $billingAddress['country'] = $order->get_billing_country(); - $customer['billingAddress'] = $billingAddress; - - // SHIPPING ADDRESS - $shippingAddress = array(); - $shippingAddress['name'] = $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(); - $shippingAddress['address1'] = $order->get_shipping_address_1(); - $shippingAddress['address2'] = $order->get_shipping_address_2(); - $shippingAddress['city'] = $order->get_shipping_city(); - $shippingAddress['country'] = $order->get_shipping_country(); - $shippingAddress['state'] = $order->get_shipping_state(); - $customer['shippingAddress'] = $shippingAddress; - - $params['customer'] = $customer; + $params['billingAddress'] = $billingAddress; + + // DELIVERY ADDRESS + if(!empty($order->get_shipping_country()) && !empty($order->get_shipping_first_name())) { + $shippingAddress = array(); + $shippingAddress['name'] = $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(); + $shippingAddress['address1'] = $order->get_shipping_address_1(); + $shippingAddress['address2'] = $order->get_shipping_address_2(); + $shippingAddress['city'] = $order->get_shipping_city(); + $shippingAddress['country'] = $order->get_shipping_country(); + $shippingAddress['state'] = $order->get_shipping_state(); + $params['deliveryAddress'] = $shippingAddress; + } return $params; } @@ -126,14 +136,14 @@ public function postOrderToPoitCheckout() public function postCheckout($paymentRequestParams) { - return $this->pcUtils->apiCall("", $paymentRequestParams); + return $this->pcUtils->apiCall('checkout/web', $paymentRequestParams); } public function getCheckout() { WC()->session->set('pointCheckoutCurrentOrderId', $_REQUEST['reference']); - return $this->pcUtils->apiCall($_REQUEST['checkout'], null); + return $this->pcUtils->apiCall('checkout/' . $_REQUEST['checkout'], null); } @@ -146,9 +156,9 @@ public function checkPaymentStatus() if (!$response->success) { - $order->update_status('canceled'); + $order->cancel_order(); $errorMsg = isset($response->error) ? $response->error : 'connecting to pointcheckout failed'; - $note = __("[ERROR] order canceled :" . $errorMsg); + $note = __('[ERROR] order canceled :' . $errorMsg); // Add the note $order->add_order_note($note); // Save the data @@ -164,8 +174,7 @@ public function checkPaymentStatus() if ($response->success && $result->status != 'PAID') { - - $order->update_status('canceled'); + $order->cancel_order(); $note = __($this->getOrderHistoryMessage($result->id, 0, $result->status, $result->currency)); // Add the note $order->add_order_note($note); @@ -202,9 +211,9 @@ public function getOrderHistoryMessage($checkout, $codAmount, $orderStatus, $cur default: $color = 'style="color:red;"'; } - $message = 'PointCheckout Status: ' . $orderStatus . '
PointCheckout Transaction ID: ' . $checkout . '' . "\n"; + $message = 'PointCheckout Status: ' . $orderStatus . '
PointCheckout Transaction ID: ' . $checkout . '' . '\n'; if ($codAmount > 0) { - $message .= '[NOTICE] COD Amount: ' . $codAmount . ' ' . $this->session->data['currency'] . '' . "\n"; + $message .= '[NOTICE] COD Amount: ' . $codAmount . ' ' . $this->session->data['currency'] . '' . '\n'; } return $message; } diff --git a/pointcheckout_payment_card/includes/lib/class-pc-utils.php b/pointcheckout_payment_card/includes/lib/class-pc-utils.php index c2231a8..7a4bfab 100755 --- a/pointcheckout_payment_card/includes/lib/class-pc-utils.php +++ b/pointcheckout_payment_card/includes/lib/class-pc-utils.php @@ -49,11 +49,11 @@ public function log($messages) public function getApiBaseUrl() { if ($this->pcConfig->isLiveMode()) { - return 'https://api.pointcheckout.com/mer/v1.2/checkouts/'; + return 'https://api.pointcheckout.com/mer/v2.0/'; } elseif ($this->pcConfig->isStagingMode()) { - return 'https://api.staging.pointcheckout.com/mer/v1.2/checkouts/'; + return 'https://api.staging.pointcheckout.com/mer/v2.0/'; } else { - return 'https://api.test.pointcheckout.com/mer/v1.2/checkouts/'; + return 'https://api.test.pointcheckout.com/mer/v2.0/'; } } diff --git a/pointcheckout_payment_card/woocommerce-pointcheckout-card.php b/pointcheckout_payment_card/woocommerce-pointcheckout-card.php index f47c0ea..4f5a26d 100755 --- a/pointcheckout_payment_card/woocommerce-pointcheckout-card.php +++ b/pointcheckout_payment_card/woocommerce-pointcheckout-card.php @@ -2,7 +2,7 @@ /* Plugin Name: PointCheckout Card Payment * Description: Have your customers pay with credit or debit cards via PointCheckout - * Version: 2.0.6 + * Version: 2.1.0 * Author: PointCheckout * Author URI: https://docs.pointcheckout.com/guides/woocommerce */ diff --git a/pointcheckout_payment_rewards/includes/class-woocommerce-pointcheckout-rewards.php b/pointcheckout_payment_rewards/includes/class-woocommerce-pointcheckout-rewards.php index 8fddb1a..d31c91e 100755 --- a/pointcheckout_payment_rewards/includes/class-woocommerce-pointcheckout-rewards.php +++ b/pointcheckout_payment_rewards/includes/class-woocommerce-pointcheckout-rewards.php @@ -1,5 +1,5 @@ id, array($this, 'receipt_page')); - add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); // Save options add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); @@ -53,7 +52,6 @@ function process_admin_options() return $result; } - public function is_available() { if (!$this->config->isEnabled()) { @@ -82,9 +80,9 @@ public function is_available() $valid = false; if(WC()->customer != null) { $billingCountry = WC()->customer->get_billing_country(); - - if (!$billingCountry == null) { - foreach ($this->config->getSpecificCountries() as $country) { + $specfiedCountries = $this->config->getSpecificCountries(); + if (!$billingCountry == null && !empty($specfiedCountries) && is_array($specfiedCountries)) { + foreach ($specfiedCountries as $country) { if ($country == $billingCountry) { $valid = true; } @@ -274,7 +272,7 @@ function process_payment($order_id) $this->pcUtils->log('Failed to initiate rewards payment using PointCheckout, message : ' . $response->error); } - $note = $this->paymentService->getOrderHistoryMessage($form['response']->result->id, 0, $form['response']->result->status, ''); + $note = $this->paymentService->getOrderHistoryMessage($response->result->id, 0, $response->result->status, ''); $order->add_order_note($note); if ($response->success == 'true') { @@ -302,7 +300,7 @@ public function process_response() WC()->session->set('refresh_totals', true); $redirectUrl = $this->get_return_url($order); } else { - $redirectUrl = esc_url($woocommerce->cart->get_checkout_url()); + $redirectUrl = esc_url(wc_get_checkout_url()); $order->cancel_order(); } echo ''; diff --git a/pointcheckout_payment_rewards/includes/lib/class-pc-payment-service.php b/pointcheckout_payment_rewards/includes/lib/class-pc-payment-service.php index 4a4c6bd..662e8e2 100755 --- a/pointcheckout_payment_rewards/includes/lib/class-pc-payment-service.php +++ b/pointcheckout_payment_rewards/includes/lib/class-pc-payment-service.php @@ -1,5 +1,5 @@ pcOrder->loadOrder($orderId); $order->update_status($this->pcConfig->getNewOrderStatus()); + // ORDER INFO $params = array( - 'transactionId' => $orderId, + 'requestId' => $orderId, + 'orderId' => $orderId, + 'extVersion' => PC_REWARD_EXT_VERSION ); - $params["extVersion"] = PC_REWARD_EXT_VERSION; try{ - $params["ecommerce"]= 'WordPress ' . $this->get_wp_version() . ', WooCommerce ' . $this->wpbo_get_woo_version_number(); + $params['ecommerce']= 'WordPress ' . $this->get_wp_version() . ', WooCommerce ' . $this->wpbo_get_woo_version_number(); } catch (\Throwable $e) { // NOTHING TO DO } + $params['allowedPaymentMethods'] = ['POINTCHECKOUT']; + $params['returnUrl'] = get_site_url() . '?wc-api=wc_gateway_pointcheckout_rewards_process_response'; + + + // CURRENCY AND AMOUNT + $params['currency'] = $this->pcOrder->getCurrencyCode(); + $params['amount'] = $this->pcOrder->getTotal(); + + // TOTALS + $totals = array(); + $totals['subtotal'] = $this->pcOrder->getSubtotal(); + $totals['tax'] = $this->pcOrder->getTaxAmount(); + $totals['shipping'] = $this->pcOrder->getShippingAmount(); + $totals['discount'] = $this->pcOrder->getDiscountAmount(); + if(!$totals['subtotal'] || $totals['subtotal'] <= 0) { + $totals['subtotal'] = $this->pcOrder->getTotal(); + } + $params['totals'] = $totals; + $cartItems = $order->get_items(); $items = array(); $i = 0; @@ -53,7 +74,7 @@ public function getPaymentRequestParams() 'sku' => $product->get_sku(), 'quantity' => $item_data->get_quantity(), 'type' => $product->get_type(), - 'total' => $item_data->get_total() + 'linetotal' => $item_data->get_total() ); //in case of bundles the bundle group item total is set to zero here to prevent conflict in totals if ($product->get_type() == 'bundle') { @@ -62,18 +83,6 @@ public function getPaymentRequestParams() $items[$i++] = $item; } $params['items'] = array_values($items); - $params['amount'] = $this->pcOrder->getTotal(); - $params['tax'] = $this->pcOrder->getTaxAmount(); - $params['shipping'] = $this->pcOrder->getShippingAmount(); - $params['subtotal'] = $this->pcOrder->getSubtotal(); - if(!$params['subtotal'] || $params['subtotal'] <= 0) { - $params['subtotal'] = $this->pcOrder->getTotal(); - } - - $params['discount'] = $this->pcOrder->getDiscountAmount(); - $params['currency'] = $this->pcOrder->getCurrencyCode(); - $params['paymentMethods'] = ["POINTCHECKOUT"]; - $params['resultUrl'] = get_site_url() . "?wc-api=wc_gateway_pointcheckout_rewards_process_response"; // CUSTOMER $customer = array(); @@ -84,6 +93,7 @@ public function getPaymentRequestParams() $customer['lastName'] = $order->get_billing_last_name(); $customer['email'] = $order->get_billing_email(); $customer['phone'] = $order->get_billing_phone(); + $params['customer'] = $customer; // BILLING ADDRESS $billingAddress = array(); @@ -93,19 +103,19 @@ public function getPaymentRequestParams() $billingAddress['city'] = $order->get_billing_city(); $billingAddress['state'] = $order->get_billing_state(); $billingAddress['country'] = $order->get_billing_country(); - $customer['billingAddress'] = $billingAddress; - - // SHIPPING ADDRESS - $shippingAddress = array(); - $shippingAddress['name'] = $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(); - $shippingAddress['address1'] = $order->get_shipping_address_1(); - $shippingAddress['address2'] = $order->get_shipping_address_2(); - $shippingAddress['city'] = $order->get_shipping_city(); - $shippingAddress['country'] = $order->get_shipping_country(); - $shippingAddress['state'] = $order->get_shipping_state(); - $customer['shippingAddress'] = $shippingAddress; - - $params['customer'] = $customer; + $params['billingAddress'] = $billingAddress; + + // DELIVERY ADDRESS + if(!empty($order->get_shipping_country()) && !empty($order->get_shipping_first_name())) { + $shippingAddress = array(); + $shippingAddress['name'] = $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name(); + $shippingAddress['address1'] = $order->get_shipping_address_1(); + $shippingAddress['address2'] = $order->get_shipping_address_2(); + $shippingAddress['city'] = $order->get_shipping_city(); + $shippingAddress['country'] = $order->get_shipping_country(); + $shippingAddress['state'] = $order->get_shipping_state(); + $params['deliveryAddress'] = $shippingAddress; + } return $params; } @@ -127,14 +137,14 @@ public function postOrderToPoitCheckout() public function postCheckout($paymentRequestParams) { - return $this->pcUtils->apiCall("", $paymentRequestParams); + return $this->pcUtils->apiCall('checkout/web', $paymentRequestParams); } public function getCheckout() { WC()->session->set('pointCheckoutCurrentOrderId', $_REQUEST['reference']); - return $this->pcUtils->apiCall($_REQUEST['checkout'], null); + return $this->pcUtils->apiCall('checkout/' . $_REQUEST['checkout'], null); } @@ -147,9 +157,9 @@ public function checkPaymentStatus() if (!$response->success) { - $order->update_status('canceled'); + $order->cancel_order(); $errorMsg = isset($response->error) ? $response->error : 'connecting to pointcheckout failed'; - $note = __("[ERROR] order canceled :" . $errorMsg); + $note = __('[ERROR] order canceled :' . $errorMsg); // Add the note $order->add_order_note($note); // Save the data @@ -165,8 +175,7 @@ public function checkPaymentStatus() if ($response->success && $result->status != 'PAID') { - - $order->update_status('canceled'); + $order->cancel_order(); $note = __($this->getOrderHistoryMessage($result->id, 0, $result->status, $result->currency)); // Add the note $order->add_order_note($note); @@ -203,9 +212,9 @@ public function getOrderHistoryMessage($checkout, $codAmount, $orderStatus, $cur default: $color = 'style="color:red;"'; } - $message = 'PointCheckout Status: ' . $orderStatus . '
PointCheckout Transaction ID: ' . $checkout . '' . "\n"; + $message = 'PointCheckout Status: ' . $orderStatus . '
PointCheckout Transaction ID: ' . $checkout . '' . '\n'; if ($codAmount > 0) { - $message .= '[NOTICE] COD Amount: ' . $codAmount . ' ' . $this->session->data['currency'] . '' . "\n"; + $message .= '[NOTICE] COD Amount: ' . $codAmount . ' ' . $this->session->data['currency'] . '' . '\n'; } return $message; } diff --git a/pointcheckout_payment_rewards/includes/lib/class-pc-utils.php b/pointcheckout_payment_rewards/includes/lib/class-pc-utils.php index 21d2a19..951d78a 100755 --- a/pointcheckout_payment_rewards/includes/lib/class-pc-utils.php +++ b/pointcheckout_payment_rewards/includes/lib/class-pc-utils.php @@ -49,11 +49,11 @@ public function log($messages) public function getApiBaseUrl() { if ($this->pcConfig->isLiveMode()) { - return 'https://api.pointcheckout.com/mer/v1.2/checkouts/'; + return 'https://api.pointcheckout.com/mer/v2.0/'; } elseif ($this->pcConfig->isStagingMode()) { - return 'https://api.staging.pointcheckout.com/mer/v1.2/checkouts/'; + return 'https://api.staging.pointcheckout.com/mer/v2.0/'; } else { - return 'https://api.test.pointcheckout.com/mer/v1.2/checkouts/'; + return 'https://api.test.pointcheckout.com/mer/v2.0/'; } } diff --git a/pointcheckout_payment_rewards/woocommerce-pointcheckout-rewards.php b/pointcheckout_payment_rewards/woocommerce-pointcheckout-rewards.php index bcda073..4c99b18 100755 --- a/pointcheckout_payment_rewards/woocommerce-pointcheckout-rewards.php +++ b/pointcheckout_payment_rewards/woocommerce-pointcheckout-rewards.php @@ -2,7 +2,7 @@ /* Plugin Name: PointCheckout Rewards * Description: PointCheckout payment allows your customer to pay for there cart using reward programs - * Version: 2.0.6 + * Version: 2.1.0 * Author: PointCheckout * Author URI: https://docs.pointcheckout.com/guides/woocommerce */