Skip to content

Commit

Permalink
Refund feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
XYG6 authored and XYG6 committed Feb 26, 2021
1 parent 54efa02 commit dd834cf
Show file tree
Hide file tree
Showing 31 changed files with 875 additions and 420 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# EasyTransac-Prestashop
EasyTransac update payment gateway module for Prestashop v1.7
EasyTransac payment gateway module for Prestashop v1.7
19 changes: 12 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions config_fr.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>easytransac</name>
<displayName><![CDATA[EasyTransac]]></displayName>
<version><![CDATA[3.0.2]]></version>
<description><![CDATA[Service de paiement en ligne]]></description>
<displayName><![CDATA[Paiement CB EasyTransac]]></displayName>
<version><![CDATA[3.1.0]]></version>
<description><![CDATA[Service de paiement en ligne par carte bancaire]]></description>
<author><![CDATA[EasyTransac]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall><![CDATA[Êtes-vous sûr de vouloir désinstaller?]]></confirmUninstall>
Expand Down
6 changes: 3 additions & 3 deletions controllers/front/listcards.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function postProcess()
die(json_encode(array('status' => 0)));

$this->module->loginit();
EasyTransac\Core\Logger::getInstance()->write($this->context->customer->getClient_id());
$this->module->debugLog($this->context->customer->getClient_id());
EasyTransac\Core\Services::getInstance()->provideAPIKey(Configuration::get('EASYTRANSAC_API_KEY'));
$clientId = $this->context->customer->getClient_id();
if (empty($clientId))
Expand All @@ -41,13 +41,13 @@ public function postProcess()
$buffer[] = array('Alias' => $cc->getAlias(), 'CardNumber' => $cc->getNumber(), "CardYear" => $year, "CardMonth" => $cc->getMonth());
}
$output = array('status' => !empty($buffer), 'packet' => $buffer);
EasyTransac\Core\Logger::getInstance()->write($output);
$this->module->debugLog($output);
echo json_encode($output);
die;
}
else
{
EasyTransac\Core\Logger::getInstance()->write('List Cards Error: ' . $response->getErrorCode() . ' - ' . $response->getErrorMessage());
$this->module->debugLog('List Cards Error: ' . $response->getErrorCode() . ' - ' . $response->getErrorMessage());
}
die(json_encode(array('status' => 0)));
}
Expand Down
68 changes: 23 additions & 45 deletions controllers/front/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class EasyTransacNotificationModuleFrontController extends ModuleFrontController
public function postProcess()
{
$this->module->loginit();
EasyTransac\Core\Logger::getInstance()->write('Start Notification');
EasyTransac\Core\Logger::getInstance()->write("\n\n" . var_export($_POST, true), FILE_APPEND);
$this->module->debugLog('Start Notification');
$this->module->debugLog('Data', var_export($_POST, true));
try
{
$response = \EasyTransac\Core\PaymentNotification::getContent($_POST, Configuration::get('EASYTRANSAC_API_KEY'));
Expand All @@ -23,54 +23,33 @@ public function postProcess()
}
catch (Exception $exc)
{
EasyTransac\Core\Logger::getInstance()->write('Notification error : ' . $exc->getMessage());
$this->module->debugLog('Notification error', $exc->getMessage());
error_log('EasyTransac error: ' . $exc->getMessage());
die;
}

// Lock so that validation don't interfere
if ($this->module->isLocked($response->getOrderId()))
{
EasyTransac\Core\Logger::getInstance()->write('Notification lock loop : ' . $response->getOrderId());

// Wait loop (max 10s) to let the validation finish
$max = 10;
$i = 0;
while ($i < $max)
{
sleep(1);
if (!$this->module->isLocked($response->getOrderId()))
break;
$i++;
}
}
// Lock
$this->module->lock($response->getOrderId());

EasyTransac\Core\Logger::getInstance()->write('Notification locked : ' . $response->getOrderId());

$this->module->debugLog('Notification for Tid', $response->getTid());
$this->module->create_easytransac_order_state();

$cart = new Cart($response->getOrderId());

if (empty($cart->id))
{
Logger::AddLog('EasyTransac: Unknown Cart id');
$this->module->unlock($response->getOrderId());
die;
}
$customer = new Customer($cart->id_customer);

$existing_order_id = OrderCore::getOrderByCartId($response->getOrderId());
$existing_order = new Order($existing_order_id);

EasyTransac\Core\Logger::getInstance()->write('Notification cart id : ' . $response->getOrderId());
EasyTransac\Core\Logger::getInstance()->write('Notification order id from cart : ' . $existing_order_id);
EasyTransac\Core\Logger::getInstance()->write('Notification customer : ' . $existing_order->id_customer);
EasyTransac\Core\Logger::getInstance()->write('Notification client ID : ' . $response->getClient()->getId());

$this->module->debugLog('Notification cart id', $response->getOrderId());
$this->module->debugLog('Notification order id from cart', $existing_order_id);
$this->module->debugLog('Notification customer', $existing_order->id_customer);
$this->module->debugLog('Notification client ID', $response->getClient()->getId());
$this->module->debugLog('save tid - orderId', $response->getOrderId().' - '.$response->getTid());

$this->module->setTransactionId($response->getOrderId(), $response->getTid());
$payment_status = null;

$payment_message = $response->getMessage();

// 2: payment accepted, 6: canceled, 7: refunded, 8: payment error
Expand All @@ -95,32 +74,32 @@ public function postProcess()
break;
}

EasyTransac\Core\Logger::getInstance()->write('Notification for OrderId : ' . $response->getOrderId() . ', Status: ' . $response->getStatus() . ', Prestashop Status: ' . $payment_status);
$this->module->debugLog('Notification for OrderId : ' . $response->getOrderId() . ', Status: ' . $response->getStatus() . ', Prestashop Status: ' . $payment_status);

// Checks that paid amount matches cart total.
$paid_total = number_format($response->getAmount(), 2, '.', '');
$cart_total = number_format($cart->getOrderTotal(true, Cart::BOTH), 2, '.', '');
$amount_match = $paid_total === $cart_total;

EasyTransac\Core\Logger::getInstance()->write('Notification Paid total: ' . $paid_total . ' prestashop price: ' . $cart_total);
$this->module->debugLog('Notification Paid total: ' . $paid_total . ' prestashop price: ' . $cart_total);

// Useful if amount doesn't match and it's an update
$original_new_state = $payment_status;

if (!$amount_match && 2 == $payment_status)
{
$payment_message = $this->module->l('Price paid on EasyTransac is not the same that on Prestashop - Transaction : ') . $response->getTid();
$payment_message = $this->l('Price paid on EasyTransac is not the same that on Prestashop - Transaction : ') . $response->getTid();
$payment_status = 8;
EasyTransac\Core\Logger::getInstance()->write('Notification Amount mismatch');
$this->module->debugLog('Notification Amount mismatch');
}

if (empty($existing_order->id) || empty($existing_order->current_state))
{
$total_paid_float = (float) $paid_total;
EasyTransac\Core\Logger::getInstance()->write('Notification Total paid float: ' . $total_paid_float);
$this->module->validateOrder($cart->id, $payment_status, $total_paid_float, $this->module->displayName, $payment_message, $mailVars = array(), null, false, $customer->secure_key);
EasyTransac\Core\Logger::getInstance()->write('Notification Order validated');
$this->module->unlock($response->getOrderId());
$this->module->debugLog('Notification Total paid float: ' . $total_paid_float);
$extra_vars = ['transaction_id' => $response->getTid()];
$this->module->validateOrder($cart->id, $payment_status, $total_paid_float, $this->module->displayName, $payment_message, $extra_vars, null, false, $customer->secure_key);
$this->module->debugLog('Notification Order validated');
die('Presta '._PS_VERSION_.' Module ' . $this->module->version . '-OK');
}

Expand All @@ -139,16 +118,15 @@ public function postProcess()
$msg->id_order = intval($existing_order->id);
$msg->private = 1;
$msg->add();
$this->module->debugLog('message added', $message);
}
EasyTransac\Core\Logger::getInstance()->write('Notification : order state changed to : ' . $payment_status);
$this->module->debugLog('Notification : order state changed to', $payment_status);
}
else
{
EasyTransac\Core\Logger::getInstance()->write('Notification : invalid target state or same state as: ' . $payment_status);
$this->module->debugLog('Notification : invalid target state or same state as', $payment_status);
}
EasyTransac\Core\Logger::getInstance()->write('Notification End of Script');
$this->module->unlock($response->getOrderId());
EasyTransac\Core\Logger::getInstance()->write('Notification unlocked : ' . $response->getOrderId());
$this->module->debugLog('Notification End of Script');
die('Presta '._PS_VERSION_.' Module ' . $this->module->version . '-OK');
}

Expand Down
18 changes: 9 additions & 9 deletions controllers/front/oneclick.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class EasyTransacOneClickModuleFrontController extends ModuleFrontController
public function postProcess()
{
$this->module->loginit();
EasyTransac\Core\Logger::getInstance()->write('Start Oneclick');
$this->module->debugLog('Start Oneclick');

EasyTransac\Core\Logger::getInstance()->write($this->context->cart->getOrderTotal(true, Cart::BOTH));
$this->module->debugLog($this->context->cart->getOrderTotal(true, Cart::BOTH));
if (!$this->context->customer->id || empty($_POST['Alias']) || !$this->context->cart->id)
die;

Expand Down Expand Up @@ -60,7 +60,7 @@ public function postProcess()
$existing_order_id = OrderCore::getOrderByCartId($doneTransaction->getOrderId());
$existing_order = new Order($existing_order_id);

EasyTransac\Core\Logger::getInstance()->write('OneClick customer : ' . $existing_order->id_customer);
$this->module->debugLog('OneClick customer : ' . $existing_order->id_customer);

$payment_status = null;

Expand Down Expand Up @@ -98,7 +98,7 @@ public function postProcess()
break;
}

EasyTransac\Core\Logger::getInstance()->write('OneClick for OrderId : ' . $doneTransaction->getOrderId() . ', Status: ' . $doneTransaction->getStatus() . ', Prestashop Status: ' . $payment_status);
$this->module->debugLog('OneClick for OrderId : ' . $doneTransaction->getOrderId() . ', Status: ' . $doneTransaction->getStatus() . ', Prestashop Status: ' . $payment_status);

// Check that paid amount matches cart total (v1.7)
# String string format compare
Expand All @@ -107,24 +107,24 @@ public function postProcess()
$amount_match = $paid_total === $cart_total;


EasyTransac\Core\Logger::getInstance()->write('OneClick Paid total: ' . $paid_total . ' prestashop price: ' . $cart_total);
$this->module->debugLog('OneClick Paid total: ' . $paid_total . ' prestashop price: ' . $cart_total);

if (!$amount_match && 2 == $payment_status)
{
$payment_message = $this->module->l('Price paid on EasyTransac is not the same that on Prestashop - Transaction : ') . $doneTransaction->getTid();
$payment_status = 8;
EasyTransac\Core\Logger::getInstance()->write('OneClick Amount mismatch');
$this->module->debugLog('OneClick Amount mismatch');
}

// Creating Order
$total_paid_float = (float) $paid_total;
EasyTransac\Core\Logger::getInstance()->write('OneClick Total paid float: ' . $total_paid_float);
$this->module->debugLog('OneClick Total paid float: ' . $total_paid_float);

if ('failed' != $doneTransaction->getStatus())
{
$this->module->validateOrder($cart->id, $payment_status, $total_paid_float, $this->module->displayName, $payment_message, $mailVars = array(), null, false, $customer->secure_key);
}
EasyTransac\Core\Logger::getInstance()->write('OneClick Order validated');
$this->module->debugLog('OneClick Order validated');

// AJAX Output
$json_status_output = '';
Expand All @@ -142,7 +142,7 @@ public function postProcess()

if ($doneTransaction->getError())
{
EasyTransac\Core\Logger::getInstance()->write('OneClick error:' . $doneTransaction->getError());
$this->module->debugLog('OneClick error:' . $doneTransaction->getError());
echo json_encode(array(
'error' => 'yes',
'message' => $doneTransaction->getError()
Expand Down
30 changes: 11 additions & 19 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function initContent()
$total = 100 * $cart->getOrderTotal(true, Cart::BOTH);
$langcode = $this->context->language->iso_code == 'fr' ? 'FRE' : 'ENG';
$this->module->loginit();
EasyTransac\Core\Logger::getInstance()->write('Start Payment Page Request');
$this->module->debugLog('Start Payment Page Request');
EasyTransac\Core\Services::getInstance()->provideAPIKey($api_key);

// Replaces "+" with "00" since there is no calling code field
Expand Down Expand Up @@ -82,29 +82,21 @@ public function initContent()
}
catch (Exception $exc)
{
EasyTransac\Core\Logger::getInstance()->write('Payment Exception: ' . $exc->getMessage());
}


if (!$response->isSuccess())
{
EasyTransac\Core\Logger::getInstance()->write('Payment Page Request error: ' . $response->getErrorCode() . ' - ' . $response->getErrorMessage());
$this->module->debugLog('Payment Exception: ' . $exc->getMessage());
}

// Store cart_id in session
$this->module->create_easytransac_order_state();
$this->context->cookie->cart_id = $this->context->cart->id;

$this->context->cookie->order_total = $cart->getOrderTotal(true, Cart::BOTH);

$this->context->smarty->assign(array(
'nbProducts' => $cart->nbProducts(),
'total' => $cart->getOrderTotal(true, Cart::BOTH),
'payment_page_url' => $response->isSuccess() ? $response->getContent()->getPageUrl() : false,
));

$this->module->create_easytransac_order_state();

$this->setTemplate('module:easytransac/views/templates/front/payment_execution.tpl');
if (!$response->isSuccess())
{
$this->module->debugLog('Payment Page Request error: ' . $response->getErrorCode() . ' - ' . $response->getErrorMessage());
throw new Exception('Please check your EasyTransac configuration.');
} else {
header('Location: '.$response->getContent()->getPageUrl());
exit;
}
}

}
6 changes: 3 additions & 3 deletions controllers/front/spinner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class EasyTransacSpinnerModuleFrontController extends ModuleFrontController
public function initContent()
{
$this->module->loginit();
EasyTransac\Core\Logger::getInstance()->write('Start Spinner');
$this->module->debugLog('Start Spinner');

parent::initContent();
EasyTransac\Core\Logger::getInstance()->write('Spinner context cookie cart: ' . $this->context->cookie->cart_id);
$this->module->debugLog('Spinner context cookie cart: ' . $this->context->cookie->cart_id);
$existing_order_id = OrderCore::getOrderByCartId($this->context->cookie->cart_id);
EasyTransac\Core\Logger::getInstance()->write('Spinner context order id: ' . $existing_order_id);
$this->module->debugLog('Spinner context order id: ' . $existing_order_id);

$existing_order = new Order($existing_order_id);

Expand Down
12 changes: 6 additions & 6 deletions controllers/front/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class EasyTransacValidationModuleFrontController extends ModuleFrontController
public function postProcess()
{
$this->module->loginit();
EasyTransac\Core\Logger::getInstance()->write('Start Validation');
EasyTransac\Core\Logger::getInstance()->write("\n\n" . var_export($_POST, true), FILE_APPEND);
$this->module->debugLog('Start Validation');
$this->module->debugLog("\n\n" . var_export($_POST, true), FILE_APPEND);

EasyTransac\Core\Logger::getInstance()->write('Validation order cart id : ' . $this->context->cookie->order_id);
$this->module->debugLog('Validation order cart id : ' . $this->context->cookie->order_id);
$cart = new Cart($this->context->cookie->cart_id);

if (empty($cart->id))
Expand All @@ -25,16 +25,16 @@ public function postProcess()

$existing_order = new Order($existing_order_id);

EasyTransac\Core\Logger::getInstance()->write('Validation order id from cart : ' . $existing_order_id);
EasyTransac\Core\Logger::getInstance()->write('Validation customer : ' . $existing_order->id_customer);
$this->module->debugLog('Validation order id from cart : ' . $existing_order_id);
$this->module->debugLog('Validation customer : ' . $existing_order->id_customer);

$this->module->create_easytransac_order_state();

/**
* Version 2.1 : spinner forced, update via notification only.
*/
// HTTP Only
EasyTransac\Core\Logger::getInstance()->write('Validation redirect');
$this->module->debugLog('Validation redirect');
// Redirect to validation page.
if (empty($existing_order->id) || empty($existing_order->current_state))
{
Expand Down
Loading

0 comments on commit dd834cf

Please sign in to comment.