Skip to content

Commit

Permalink
Merge pull request #14 from pointcheckout/develop
Browse files Browse the repository at this point in the history
Updating to PointCheckout API version 2.0
  • Loading branch information
max2u committed Sep 13, 2021
2 parents de578ed + 8afc427 commit 9e819b1
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 92 deletions.
@@ -1,5 +1,5 @@
<?php
define("PC_CARD_EXT_VERSION", "WooCommerce-Card-2.0.6");
define('PC_CARD_EXT_VERSION', 'WooCommerce-Card-2.1.0');

class PointCheckout_Card_Payment extends PointCheckout_Card_Parent
{
Expand Down Expand Up @@ -32,16 +32,37 @@ public function getPaymentRequestParams()
$this->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;
Expand All @@ -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') {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -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);
}


Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -202,9 +211,9 @@ public function getOrderHistoryMessage($checkout, $codAmount, $orderStatus, $cur
default:
$color = 'style="color:red;"';
}
$message = 'PointCheckout Status: <b ' . $color . '>' . $orderStatus . '</b><br/>PointCheckout Transaction ID: <a href="' . $this->pcUtils->getAdminUrl() . '/merchant/transactions/' . $checkout . '/read " target="_blank"><b>' . $checkout . '</b></a>' . "\n";
$message = 'PointCheckout Status: <b ' . $color . '>' . $orderStatus . '</b><br/>PointCheckout Transaction ID: <a href="' . $this->pcUtils->getAdminUrl() . '/merchant/transactions/' . $checkout . '/read " target="_blank"><b>' . $checkout . '</b></a>' . '\n';
if ($codAmount > 0) {
$message .= '<b style="color:red;">[NOTICE] </b><i>COD Amount: <b>' . $codAmount . ' ' . $this->session->data['currency'] . '</b></i>' . "\n";
$message .= '<b style="color:red;">[NOTICE] </b><i>COD Amount: <b>' . $codAmount . ' ' . $this->session->data['currency'] . '</b></i>' . '\n';
}
return $message;
}
Expand Down
6 changes: 3 additions & 3 deletions pointcheckout_payment_card/includes/lib/class-pc-utils.php
Expand Up @@ -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/';
}
}

Expand Down
Expand Up @@ -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
*/
Expand Down
@@ -1,5 +1,5 @@
<?php
require_once dirname( __FILE__ ) . '/lib/index.php';
require_once dirname(__FILE__) . '/lib/index.php';


class WC_Gateway_PointCheckout_Rewards extends PointCheckout_Rewards_Parent
Expand Down Expand Up @@ -36,7 +36,6 @@ public function __construct()

// Actions
add_action('woocommerce_receipt_' . $this->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'));
Expand All @@ -53,7 +52,6 @@ function process_admin_options()
return $result;
}


public function is_available()
{
if (!$this->config->isEnabled()) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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 '<script>window.top.location.href = "' . $redirectUrl . '"</script>';
Expand Down

0 comments on commit 9e819b1

Please sign in to comment.