Skip to content

Commit

Permalink
Merge pull request #83 from pagantis/int-1058-a
Browse files Browse the repository at this point in the history
INT-1058
  • Loading branch information
tmeliotpg authored Sep 30, 2020
2 parents 72ec0a7 + 37267b1 commit f6d0fe9
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 35 deletions.
17 changes: 11 additions & 6 deletions WC_Pagantis.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Pagantis
* Plugin URI: http://www.pagantis.com/
* Description: Financiar con Pagantis
* Version: 8.6.12
* Version: 8.6.13
* Author: Pagantis
*
* Text Domain: pagantis
Expand Down Expand Up @@ -259,7 +259,13 @@ public function pagantisActivation()
if (!isPgTableCreated(PG_LOGS_TABLE_NAME)){
createLogsTable();
}
checkCartProcessTable();
if (isPgTableCreated(PG_CART_PROCESS_TABLE)){
alterCartProcessTable();
}

if (!isPgTableCreated(PG_CART_PROCESS_TABLE)) {
checkCartProcessTable();
}

//Adapting selector to array < v8.2.2
$tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME;
Expand Down Expand Up @@ -378,11 +384,10 @@ public function pagantisActivation()
*/
public function checkWcPriceSettings()
{
if (!is_product() || !is_shop()) {
return;
if (class_exists( 'WooCommerce' ) ){
$this->checkWcDecimalSeparatorSettings();
$this->checkWcThousandsSeparatorSettings();
}
$this->checkWcDecimalSeparatorSettings();
$this->checkWcThousandsSeparatorSettings();
}

/**
Expand Down
62 changes: 55 additions & 7 deletions controllers/notifyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class WcPagantisNotify extends WcPagantisGateway
/** @var $string */
protected $product;

/** @var $string */
protected $urlTokenVerification = null;

/**
* Validation vs PagantisClient
*
Expand All @@ -68,6 +71,8 @@ public function processInformation()
if ($_SERVER['REQUEST_METHOD'] == 'GET' && $_GET['origin'] == 'notification') {
return $this->buildResponse();
}


$this->checkConcurrency();
$this->getProductType();
$this->getMerchantOrder();
Expand Down Expand Up @@ -148,15 +153,19 @@ private function getMerchantOrder()
}

/**
*
* @throws NoIdentificationException
*/
private function getPagantisOrderId()
{
global $wpdb;

$this->setUrlToken();

$this->checkDbTable();
$tableName = $wpdb->prefix.PG_CART_PROCESS_TABLE;
$queryResult = $wpdb->get_row("select order_id from $tableName where id='".$this->woocommerceOrderId."'");
$this->pagantisOrderId = $queryResult->order_id;
$order_id = $wpdb->get_var("SELECT order_id FROM $tableName WHERE token='{$this->getUrlToken()}' ");
$this->pagantisOrderId = $order_id;

if ($this->pagantisOrderId == '') {
throw new NoIdentificationException();
Expand Down Expand Up @@ -294,8 +303,13 @@ private function checkDbTable()

if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName (id int, order_id varchar(50), wc_order_id varchar(50),
UNIQUE KEY id (id)) $charset_collate";
$sql= "CREATE TABLE IF NOT EXISTS $tableName
(id INT,
order_id varchar(60),
wc_order_id varchar(60),
token varchar(32) NOT NULL,
ADD PRIMARY KEY (id,order_id)
)$charset_collate";

require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
Expand Down Expand Up @@ -400,9 +414,9 @@ private function updateBdInfo()
$wpdb->update(
$tableName,
array('wc_order_id'=>$this->woocommerceOrderId),
array('id' => $this->woocommerceOrderId),
array('%s'),
array('%d')
array('token' => $this->getUrlToken(),'order_id' => $this->pagantisOrderId),
array( '%s'),
array( '%s', '%s' )
);
}

Expand Down Expand Up @@ -560,4 +574,38 @@ public function setProduct($product)
$this->product = Ucfirst($product);
}

/**
* @return mixed
*/
public function getWoocommerceOrderId()
{
return $this->woocommerceOrderId;
}

/**
* @return mixed
*/
private function getUrlToken()
{
return $this->urlTokenVerification;
}

/**
*
*/
private function setUrlToken()
{
if (!isset($_GET['token'])){
$logEntry = '$_GET[\'token\'] is empty' .
" cart hash: ".WC()->cart->get_cart_hash().
" Merchant order id: ".$this->woocommerceOrderId.
" Pagantis order id: ".$this->pagantisOrderId.
" Pagantis urlToken: ".$this->getUrlToken().
" Function: " . __FUNCTION__;
insertLogEntry(null, $logEntry);
}

$this->urlTokenVerification = $_GET['token'];
}

}
11 changes: 9 additions & 2 deletions controllers/paymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class WcPagantisGateway extends WC_Payment_Gateway
*/
private $allowed_currencies;

/**
* @var string
*/
private $urlToken;

/**
* WcPagantisGateway constructor.
*/
Expand All @@ -55,6 +60,7 @@ public function __construct()
$this->id = WcPagantisGateway::METHOD_ID;
$this->has_fields = true;
$this->method_title = ucfirst($this->id);
$this->urlToken = strtoupper(md5(uniqid(rand(), true)));

//Useful vars
$this->template_path = plugin_dir_path(__FILE__) . '../templates/';
Expand Down Expand Up @@ -306,7 +312,8 @@ public function pagantisReceiptPage($order_id)
$callback_arg = array('wc-api'=>'wcpagantisgateway',
'key'=>$order->get_order_key(),
'order-received'=>$order->get_id(),
'origin' => ''
'origin' => '',
'token' => $this->urlToken
);

$callback_arg_user = $callback_arg;
Expand Down Expand Up @@ -360,7 +367,7 @@ public function pagantisReceiptPage($order_id)
$pagantisOrder = $orderClient->createOrder($orderApiClient);
if ($pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) {
$url = $pagantisOrder->getActionUrls()->getForm();
addOrderToCartProcessingQueue($order->get_id(), $pagantisOrder->getId());
addOrderToProcessingQueue($pagantisOrder->getId(), $order->get_id(), $this->urlToken, self::METHOD_ID);
} else {
throw new OrderNotFoundException();
}
Expand Down
12 changes: 10 additions & 2 deletions controllers/paymentController4x.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class WcPagantis4xGateway extends WC_Payment_Gateway
*/
private $allowed_currencies;

/**
* @var string
*/
private $urlToken4x;


/**
* WcPagantis4xGateway constructor.
Expand All @@ -58,6 +63,7 @@ public function __construct()
$this->id = WcPagantis4xGateway::METHOD_ID;
$this->has_fields = true;
$this->method_title = ucfirst($this->id);
$this->urlToken4x = strtoupper(md5(uniqid(rand(), true)));

//Useful vars
$this->template_path = plugin_dir_path(__FILE__) . '../templates/';
Expand Down Expand Up @@ -271,7 +277,9 @@ public function pagantisReceiptPage($order_id)
$callback_arg = array('wc-api'=>'wcpagantisgateway',
'key'=>$order->get_order_key(),
'order-received'=>$order->get_id(),
'origin' => ''
'origin' => '',
'token' => $this->urlToken4x

);

$callback_arg_user = $callback_arg;
Expand Down Expand Up @@ -326,7 +334,7 @@ public function pagantisReceiptPage($order_id)
$pagantisOrder = $orderClient->createOrder($orderApiClient);
if ($pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) {
$url = $pagantisOrder->getActionUrls()->getForm();
addOrderToCartProcessingQueue($order->get_id(), $pagantisOrder->getId());
addOrderToProcessingQueue($pagantisOrder->getId(), $order->get_id(), $this->urlToken4x,self::METHOD_ID);
} else {
throw new OrderNotFoundException();
}
Expand Down
62 changes: 47 additions & 15 deletions includes/pg-functions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

function requireWPPluginFunctions()
{
if (! function_exists('is_plugin_active')) {
Expand Down Expand Up @@ -405,39 +406,75 @@ function getPromotedAmount()
}

/**
* @param $orderId
* @param $pagantisOrderId
*
* @throws Exception
* @param $wcOrderID
* @param $urlToken
* @param $methodID
*/
function addOrderToCartProcessingQueue($orderId, $pagantisOrderId)
function addOrderToProcessingQueue($pagantisOrderId, $wcOrderID, $urlToken ,$methodID)
{
global $wpdb;
checkCartProcessTable();
$tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;

//Check if id exists
$resultsSelect = $wpdb->get_results("SELECT * FROM $tableName WHERE id='$orderId'");
$resultsSelect = $wpdb->get_results("SELECT * FROM $tableName WHERE id='$pagantisOrderId'");
$countResults = count($resultsSelect);

if ($countResults == 0) {
$wpdb->insert($tableName, array('id' => $orderId, 'order_id' => $pagantisOrderId), array('%d', '%s'));
$wpdb->insert($tableName, array(
'order_id' => $pagantisOrderId,
'wc_order_id' => $wcOrderID,
'token' => $urlToken
), array('%s', '%s', '%s'));

$logEntry = "Cart Added to Processing Queue" .
" cart hash: ".WC()->cart->get_cart_hash().
" Merchant order id: ".$wcOrderID.
" Pagantis order id: ".$pagantisOrderId.
" Pagantis urlToken: ".$urlToken.
" Pagantis Product: ".$methodID;
insertLogEntry(null, $logEntry);
} else {
$wpdb->update($tableName, array('order_id' => $pagantisOrderId), array('id' => $orderId), array('%s'), array('%d'));
$wpdb->update($tableName,
array('order_id' => $pagantisOrderId,'token' => $urlToken),
array('wc_order_id' => $wcOrderID),
array('%s,%s'),
array('%s'));
}

}

function alterCartProcessTable()
{
global $wpdb;
$tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
if (! $wpdb->get_var( "SHOW COLUMNS FROM `{$tableName}` LIKE 'token';" ) ) {
$wpdb->query("ALTER TABLE $tableName ADD COLUMN `token` VARCHAR(32) NOT NULL AFTER `order_id`");
$wpdb->query("ALTER TABLE $tableName DROP PRIMARY KEY, ADD PRIMARY KEY(order_id)");
// OLDER VERSIONS OF MODULE USE UNIQUE KEY ON `id` MEANING THIS VALUE WAS NULLABLE
$wpdb->query("ALTER TABLE $tableName MODIFY `id` INT AUTO_INCREMENT");
}
}

/**
* Check if orders table exists
* Check if cart processing table exists
*/
function checkCartProcessTable()
{
global $wpdb;
$tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;

if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) {

$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName ( id int, order_id varchar(50), wc_order_id varchar(50),
UNIQUE KEY id (id)) $charset_collate";
$sql = "CREATE TABLE IF NOT EXISTS $tableName(
`id` INT AUTO_INCREMENT,
`order_id` varchar(60),
`wc_order_id` varchar(60),
`token` varchar(32) NOT NULL,
PRIMARY KEY (`id`, `order_id`)
)$charset_collate";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
Expand Down Expand Up @@ -490,8 +527,3 @@ function getOrders($current_user, $billingEmail)

return $customer_orders;
}

function isPagePaymentPage()
{
return (is_checkout() && ! is_order_received_page()) || is_checkout_pay_page();
}
7 changes: 4 additions & 3 deletions test/selenium/buy/AbstractBuy.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function makeValidation()
{
$this->verifyOrderInformation();
$this->orderUrl = $this->webDriver->getCurrentURL();
$this->checkNotificationException();
// $this->checkNotificationException();
}

/**
Expand Down Expand Up @@ -261,7 +261,8 @@ public function checkCheckoutPage()
*/
public function goToPagantis()
{
$this->findByName('checkout')->submit();
// $this->findByName('checkout')->submit();
$this->moveToElementAndClick($this->findById('place_order'));
}

/**
Expand Down Expand Up @@ -470,7 +471,7 @@ protected function checkLogs()
$dateTo = date("Ymd", strtotime("+1 day"));
$logUrl = $this->woocommerceUrl.self::LOG_FOLDER.$this->configuration['secretKey']."/$dateFrom/$dateTo";
$response = Request::get($logUrl)->expects('json')->send();
$this->assertEquals(2, count($response->body), "PR60=>".$logUrl." = ".count($response->body));
$this->assertEquals(1, count($response->body), "PR60=>".$logUrl." = ".count($response->body));
}

}

0 comments on commit f6d0fe9

Please sign in to comment.