Skip to content

Commit

Permalink
Merge pull request #23 from paynl/feature/PLUG-997
Browse files Browse the repository at this point in the history
PLUG-997
  • Loading branch information
woutse authored Jun 27, 2022
2 parents a25ae42 + 2603176 commit e4c2364
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 44 deletions.
20 changes: 9 additions & 11 deletions paynlpaymentmethods/controllers/front/startPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public function postProcess()
Tools::redirect('index.php?controller=order&step=1');
}


$authorized = false;
$paymentOptionId = $_REQUEST['payment_option_id'];
$paymentOptionId = Tools::getValue('payment_option_id');
foreach (Module::getPaymentModules() as $module) {
if ($module['name'] == 'paynlpaymentmethods') {
$authorized = $this->module->isPaymentMethodAvailable($cart, $paymentOptionId);
Expand All @@ -55,17 +54,16 @@ public function postProcess()
}

$extra_data = array();
if(isset($_REQUEST['bank'])){
$extra_data['bank'] = $_REQUEST['bank'];
$bank = Tools::getValue('bank');
if (!empty($bank)) {
$extra_data['bank'] = $bank;
}
try{
try {
$redirectUrl = $this->module->startPayment($cart, $paymentOptionId, $extra_data);
Tools::redirect($redirectUrl);
} catch (Exception $e){
$this->module->payLog('postProcess', 'Error startPayment: ' . $e->getMessage(), $cart->id);
die('Error: ' . $e->getMessage());
} catch (Exception $e) {
$this->module->payLog('postProcess', 'Error startPayment: ' . $e->getMessage(), $cart->id);
die('Error: ' . $e->getMessage());
}


}
}
}
79 changes: 46 additions & 33 deletions paynlpaymentmethods/paynlpaymentmethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct()
{
$this->name = 'paynlpaymentmethods';
$this->tab = 'payments_gateways';
$this->version = '4.5.0';
$this->version = '4.5.1';

$this->payLogEnabled = null;
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
Expand Down Expand Up @@ -673,17 +673,19 @@ public function processPayment($transactionId, &$message = null)
$cart = new Cart((int)$cartId);
$cartTotalPrice = (version_compare(_PS_VERSION_, '1.7.7.0', '>=')) ? $cart->getCartTotalPrice() : $this->getCartTotalPrice($cart);
$arrPayAmounts = array($transaction->getCurrencyAmount(), $transaction->getPaidCurrencyAmount(), $transaction->getPaidAmount());
$amountPaid = in_array($cartTotalPrice, $arrPayAmounts) ? $cartTotalPrice : null;
$amountPaid = in_array(round($cartTotalPrice, 2), $arrPayAmounts) ? $cartTotalPrice : null;

if (is_null($amountPaid)) {
$amountPaid = in_array($cart->getOrderTotal(), $arrPayAmounts) ? $cart->getOrderTotal() : null;
$amountPaid = in_array(round($cart->getOrderTotal(), 2), $arrPayAmounts) ? $cart->getOrderTotal() : null;
}

$this->payLog('processPayment (order)', 'getOrderTotal: ' . $cart->getOrderTotal() . '. cartTotalPrice: ' . $cartTotalPrice . ' - ' . print_r($arrPayAmounts, true), $cartId, $transactionId);

if ($orderId) {

$order = new Order($orderId);

$this->payLog('processPayment', 'orderStateName:' . $orderStateName . '. iOrderState: ' . $iOrderState . '. ' .
$this->payLog('processPayment (order)', 'orderStateName:' . $orderStateName . '. iOrderState: ' . $iOrderState . '. ' .
'orderRef:' . $order->reference . '. orderModule:' . $order->module, $cartId, $transactionId);

# Check if the order is processed by PAY.
Expand Down Expand Up @@ -816,9 +818,10 @@ public function payLog($method, $message, $cartid = null, $transactionId = null)
* @param Cart $cart
* @param $payment_option_id
* @param array $extra_data
*
* @return string
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* @throws Exception
*/
public function startPayment(Cart $cart, $payment_option_id, $extra_data = array())
{
Expand All @@ -835,9 +838,12 @@ public function startPayment(Cart $cart, $payment_option_id, $extra_data = array
$iPaymentFee = $this->getPaymentFee($objPaymentMethod, $cartTotal);
$iPaymentFee = empty($iPaymentFee) ? 0 : $iPaymentFee;
$cartId = $cart->id;
$this->payLog('startPayment', 'Starting new payment with cart-total: ' . $cartTotal . '. Fee: ' . $iPaymentFee . ' Currency (cart): ' . $currency->iso_code, $cartId);

$this->addPaymentFee($cart, $iPaymentFee);
try {
$this->addPaymentFee($cart, $iPaymentFee);
} catch (Exception $e) {
$this->payLog('startPayment', 'Could not add payment fee: ' . $e->getMessage(), $cartId);
}

$products = $this->_getProductData($cart);

Expand All @@ -848,7 +854,7 @@ public function startPayment(Cart $cart, $payment_option_id, $extra_data = array
}

$startData = array(
'amount' => $cart->getOrderTotal(true, Cart::BOTH, null, null, false),
'amount' => $cartTotal,
'currency' => $currency->iso_code,
'returnUrl' => $this->context->link->getModuleLink($this->name, 'finish', array(), true),
'exchangeUrl' => $this->context->link->getModuleLink($this->name, 'exchange', array(), true),
Expand All @@ -871,42 +877,47 @@ public function startPayment(Cart $cart, $payment_option_id, $extra_data = array
# Retrieve language
$startData['language'] = $this->getLanguageForOrder($cart);

$payTransaction = \Paynl\Transaction::start($startData);
try {
$payTransaction = \Paynl\Transaction::start($startData);
} catch (Exception $e) {
$this->payLog('startPayment', 'Starting new payment failed: ' . $cartTotal . '. Fee: ' . $iPaymentFee . ' Currency (cart): ' . $currency->iso_code .' e:'.$e->getMessage(), $cartId);
throw new Exception($e->getMessage(), $e->getCode());
}

$payTransactionData = $payTransaction->getData();
$payTransactionId = !empty($payTransactionData['transaction']['transactionId']) ? $payTransactionData['transaction']['transactionId'] : '';


Transaction::addTransaction($payTransactionId, $cart->id, $cart->id_customer, $payment_option_id, $cart->getOrderTotal());

$this->payLog('startPayment', 'Starting new payment with cart-total: ' . $cartTotal . '. Fee: ' . $iPaymentFee . ' Currency (cart): ' . $currency->iso_code, $cartId, $payTransactionId);

if ($this->shouldValidateOnStart($payment_option_id)) {
Transaction::addTransaction($payTransactionId, $cart->id, $cart->id_customer, $payment_option_id, $cart->getOrderTotal());

$this->payLog('startPayment', 'Pre-Creating order for pp : ' . $payment_option_id, $cartId, $payTransactionId);
if ($this->shouldValidateOnStart($payment_option_id)) {

# Flush the package list, so the fee is added to it.
$this->context->cart->getPackageList(true);
$this->payLog('startPayment', 'Pre-Creating order for pp : ' . $payment_option_id, $cartId, $payTransactionId);

$paymentMethodSettings = PaymentMethod::getPaymentMethodSettings($payment_option_id);
$paymentMethodName = empty($paymentMethodSettings->name) ? 'PAY. Overboeking' : $paymentMethodSettings->name;
# Flush the package list, so the fee is added to it.
$this->context->cart->getPackageList(true);

$this->validateOrder($cart->id, $this->statusPending, 0, $paymentMethodName, null, array(), null, false, $cart->secure_key);
$paymentMethodSettings = PaymentMethod::getPaymentMethodSettings($payment_option_id);
$paymentMethodName = empty($paymentMethodSettings->name) ? 'PAY. Overboeking' : $paymentMethodSettings->name;

$orderId = Order::getIdByCartId($cartId);
$order = new Order($orderId);
$this->validateOrder($cart->id, $this->statusPending, 0, $paymentMethodName, null, array(), null, false, $cart->secure_key);

$orderPayment = new OrderPayment();
$orderPayment->order_reference = $order->reference;
$orderPayment->payment_method = $paymentMethodName;
$orderPayment->amount = $startData['amount'];
$orderPayment->transaction_id = $payTransactionData['transaction']['transactionId'];
$orderPayment->id_currency = $cart->id_currency;
$orderPayment->save();
} else
{
$this->payLog('startPayment', 'Not pre-creating the order, waiting for payment.', $cartId, $payTransactionId);
}
$orderId = Order::getIdByCartId($cartId);
$order = new Order($orderId);

$orderPayment = new OrderPayment();
$orderPayment->order_reference = $order->reference;
$orderPayment->payment_method = $paymentMethodName;
$orderPayment->amount = $startData['amount'];
$orderPayment->transaction_id = $payTransactionData['transaction']['transactionId'];
$orderPayment->id_currency = $cart->id_currency;
$orderPayment->save();
} else {
$this->payLog('startPayment', 'Not pre-creating the order, waiting for payment.', $cartId, $payTransactionId);
}

if ($payment_option_id == PaymentMethod::METHOD_INSTORE) {
if ($payment_option_id == PaymentMethod::METHOD_INSTORE) {
$this->payLog('startPayment', 'Starting Instore Payment', $cartId, $payTransactionId);
$terminalId = null;
if (isset($extra_data['bank'])) {
Expand Down Expand Up @@ -962,6 +973,8 @@ private function getPaymentMethod($payment_option_id)
/**
* @param Cart $cart
* @param $iFee_wt
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
private function addPaymentFee(Cart $cart, $iFee_wt)
{
Expand Down

0 comments on commit e4c2364

Please sign in to comment.