Skip to content

Commit

Permalink
Merge pull request #30 from paynl/feature/PLUG-1140
Browse files Browse the repository at this point in the history
PLUG-1140 - Prevent Double Processing
  • Loading branch information
woutse authored Oct 11, 2022
2 parents 1170b71 + 9d71550 commit 995933d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
25 changes: 20 additions & 5 deletions paynlpaymentmethods/controllers/front/exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
/**
* @since 1.5.0
*/

use PaynlPaymentMethods\PrestaShop\Transaction;

class PaynlPaymentMethodsExchangeModuleFrontController extends ModuleFrontController
{
/**
Expand Down Expand Up @@ -59,8 +62,8 @@ public function postProcess()
}

/**
* @var $module PaynlPaymentMethods
*/
* @var $module PaynlPaymentMethods
*/
$module = $this->module;

$module->payLog('Exchange', 'Action: ' . $action, $cartid, $transactionId);
Expand All @@ -75,13 +78,25 @@ public function postProcess()
die('TRUE| Empty transactionId in call');
}

if ($action == 'new_ppt') {
$processing = Transaction::checkProcessing($transactionId);
if (!empty($processing)) {
die('FALSE| Already Processing payment');
}
}

try {
$message = '';
$module->processPayment($transactionId, $message);
$response = 'TRUE| ' . $message;
} catch (Exception $e) {
$response = 'FALSE| ' . $e->getMessage();
}

die('TRUE| ' . $message);
} catch(Exception $e){
die('FALSE| '. $e->getMessage());
if ($action == 'new_ppt') {
Transaction::removeProcessing($transactionId);
}

exit($response);
}
}
10 changes: 9 additions & 1 deletion paynlpaymentmethods/paynlpaymentmethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct()
{
$this->name = 'paynlpaymentmethods';
$this->tab = 'payments_gateways';
$this->version = '4.7.0';
$this->version = '4.8.0';

$this->payLogEnabled = null;
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
Expand Down Expand Up @@ -143,6 +143,14 @@ public function createDatabaseTable()
INDEX (`transaction_id`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');

Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pay_processing` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`payOrderId` varchar(255) DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `payOrderId` (`payOrderId`) USING BTREE
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8 ;');

return true;
}

Expand Down
34 changes: 34 additions & 0 deletions paynlpaymentmethods/src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PaynlPaymentMethods\PrestaShop;

use Db;
use DbQuery;

class Transaction
{
Expand Down Expand Up @@ -94,4 +95,37 @@ public static function doCapture($transactionId, $amount = null)

return array('result' => $result, 'data' => $captureResult);
}

/**
* @param $payOrderId
* @return array
*/
public static function checkProcessing($payOrderId)
{
try {
$db = Db::getInstance();
$payOrderId = $db->escape($payOrderId);
$sql = new DbQuery();
$sql->select('*');
$sql->from('pay_processing');
$sql->where("payOrderId = '" . $payOrderId . "'");
$sql->where("created_at > date_sub('" . date('Y-m-d H:i:s') . "', interval 1 minute)");
$result = $db->executeS($sql);
if (empty($result)) {
$db->insert('pay_processing', ['payOrderId' => $payOrderId, 'created_at' => date('Y-m-d H:i:s')], false, false, Db::ON_DUPLICATE_KEY, true);
}
} catch (\Exception $e) {
$result = array();
}

return is_array($result) ? $result : array();
}

/**
* @param $payOrderId
*/
public static function removeProcessing($payOrderId)
{
Db::getInstance()->delete('pay_processing', 'payOrderId = "' . $payOrderId . '"');
}
}
15 changes: 15 additions & 0 deletions paynlpaymentmethods/upgrade/Upgrade-4.8.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* @param $module PaynlPaymentMethods
*/
function upgrade_module_4_8_0($module)
{
$results = array();
$results[] = $module->createDatabaseTable();

if (in_array(false, $results)) {
return false;
}
return true;
}

0 comments on commit 995933d

Please sign in to comment.