Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Broken order and payment currency comparison fix
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrjozwiak committed Jun 15, 2018
1 parent 7694f69 commit fac46f2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
50 changes: 26 additions & 24 deletions Service/TpayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Sales\Model\Service\InvoiceService;
use tpaycom\magento2cards\Api\Sales\CardsOrderRepositoryInterface;
use tpaycom\magento2cards\Api\TpayCardsInterface;
use tpaycom\magento2cards\lib\CurrencyISOCodes;
use tpaycom\magento2cards\lib\ResponseFields;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Sales\Model\Order\Payment\State\CommandInterface;
Expand Down Expand Up @@ -144,7 +145,7 @@ public function setOrderStatus($orderId, array $validParams, $tpayModel)
if (!isset($validParams[ResponseFields::STATUS]) || $validParams[ResponseFields::STATUS] !== 'correct'
|| ((double)number_format($validParams[ResponseFields::AMOUNT], 2, '.', '') !== $orderAmount)
) {
$comment = __('Payment has been declined. ') . '</br>' . $transactionDesc;
$comment = __('Payment has been declined. ').'</br>'.$transactionDesc;
$this->addCommentToHistory($orderId, $comment);
} else {
if ($order->getState() != Order::STATE_PROCESSING) {
Expand Down Expand Up @@ -180,16 +181,17 @@ protected function getTransactionDesc($validParams)
return false;
}
if (isset ($validParams['err_desc'])) {
return 'Payment error: ' . $validParams['err_desc'] . ', error code: ' . $validParams['err_code'];
return 'Payment error: '.$validParams['err_desc'].', error code: '.$validParams['err_code'];
}
$error = null;
if ($validParams['status'] === 'declined') {
$error = $validParams['reason'];
}

$transactionDesc = (is_null($error)) ? ' ' : ' Reason: <b>' . $error . '</b>';
$transactionDesc = (is_null($error)) ? ' ' : ' Reason: <b>'.$error.'</b>';
$transactionDesc .= (isset($validParams[ResponseFields::TEST_MODE])) &&
(int)$validParams[ResponseFields::TEST_MODE] === 1 ? '<b> TEST </b>' : ' ';

return $transactionDesc;
}

Expand All @@ -199,7 +201,8 @@ protected function getTransactionDesc($validParams)
* @param OrderPaymentInterface $payment
* @param string|float $amount
* @param array $validParams
* @param bool|int $skipFraudDetection
* @param bool $skipFraudDetection
* @throws \Exception
*/
private function registerCaptureNotificationTpay(
OrderPaymentInterface $payment,
Expand All @@ -221,28 +224,28 @@ private function registerCaptureNotificationTpay(
$order = $payment->getOrder();
$amount = (double)$amount;
$invoice = $this->getInvoiceForTransactionId($order, $payment->getTransactionId());

$orderCurrency = $order->getOrderCurrency()->getCode();
if (!in_array($orderCurrency, CurrencyISOCodes::ISO_CURRENCY_CODES)) {
throw new \Exception(sprintf('Order currency %s does not exist in Tpay dictionary!', $orderCurrency));
}
$orderCurrency = array_search($orderCurrency, CurrencyISOCodes::ISO_CURRENCY_CODES);
// register new capture
if (!$invoice) {
if ($payment->isSameCurrency() && $payment->isCaptureFinal($amount)) {
$invoice = $order->prepareInvoice()->register();
$invoice->setOrder($order);
$order->addRelatedObject($invoice);
$payment->setCreatedInvoice($invoice);
$payment->setShouldCloseParentTransaction(true);
} else {
$payment->setIsFraudDetected(!$skipFraudDetection);
$this->updateTotals($payment, ['base_amount_paid_online' => $amount]);
}
if (!$invoice && $payment->isCaptureFinal($amount) && $orderCurrency === (int)$validParams['currency']) {
$invoice = $order->prepareInvoice()->register();
$invoice->setOrder($order);
$order->addRelatedObject($invoice);
$payment->setCreatedInvoice($invoice);
$payment->setShouldCloseParentTransaction(true);
} else {
$payment->setIsFraudDetected(!$skipFraudDetection);
$this->updateTotals($payment, ['base_amount_paid_online' => $amount]);
}

if (!$payment->getIsTransactionPending()) {
if ($invoice && Invoice::STATE_OPEN == $invoice->getState()) {
$invoice->setOrder($order);
$invoice->pay();
$this->updateTotals($payment, ['base_amount_paid_online' => $amount]);
$order->addRelatedObject($invoice);
}
if (!$payment->getIsTransactionPending() && $invoice && Invoice::STATE_OPEN === $invoice->getState()) {
$invoice->setOrder($order);
$invoice->pay();
$this->updateTotals($payment, ['base_amount_paid_online' => $amount]);
$order->addRelatedObject($invoice);
}

$message = $this->stateCommand->execute($payment, $amount, $order);
Expand All @@ -255,7 +258,6 @@ private function registerCaptureNotificationTpay(
);
$message = $payment->prependMessage($message);
$payment->addTransactionCommentsToOrder($transaction, $message);

}

}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tpaycom/magento2cards",
"description": "tpay.com card payments module",
"type": "magento2-module",
"version": "1.3.0",
"version": "1.3.1",
"license": [
"OSL-3.0"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="tpaycom_magento2cards" setup_version="1.2.9">
<module name="tpaycom_magento2cards" setup_version="1.3.1">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down
2 changes: 1 addition & 1 deletion lib/CurrencyISOCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CurrencyISOCodes
{
public $ISO_CURRENCY_CODES = array(
const ISO_CURRENCY_CODES = array(
784 => 'AED',
41 => 'AMD',
532 => 'ANG',
Expand Down
2 changes: 1 addition & 1 deletion lib/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function validateCardCurrency($currency)
if (strlen($currency) !== 3) {
throw new \Exception('Currency is invalid.');
}
$currencies = (new CurrencyISOCodes)->ISO_CURRENCY_CODES;
$currencies = CurrencyISOCodes::ISO_CURRENCY_CODES;
switch (gettype($currency)) {
case 'string':
if (in_array($currency, $currencies)) {
Expand Down

0 comments on commit fac46f2

Please sign in to comment.