We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit c74eff91 authored by Ángel Campos's avatar Ángel Campos Committed by GitHub

[PW-2029] Configuration to charge orders based on storeview or global currency (#851)

* [PW-2029] New charged currency config field

* [PW-2029] Charged currency default value

* [PW-2029] Charged currency config getter

* [PW-2029] Removing unused setOpenInvoiceData() in Redirect block class

* [PW-2029] New charged currency column for sales_order table

* [PW-2029] Event observer to save order's charged currency for future use in refunds

* [PW-2029] ChargedCurrency.php helper class for orders, quotes, stores and refunds

* [PW-2029] Using the ChargedRefund helper

* [PW-2029] Fix broken unit test because of new AdyenInitiateTerminalApi dependency

* [PW-2029] AdyenAmountCurrency model

* [PW-2029] Using AdyenAmountCurrency model

* [PW-2029] Adding discount and tax amount for items in ChargedCurrency model

* [PW-2029] Setting discount and tax amount for items

* [PW-2029] Getting tax and discount amount for items in CheckoutDataBuilder

* [PW-2029] Adjusting CaptureDataBuilder to use new AmountCurrency model

* [PW-2029] ChargedCurrency php docs

* [PW-2029] Removing use statements from AdyenSalesOrderChargedCurrencyObserver

* [PW-2029] Saving base or display in adyen_charged_currency instead of currency code on order placement

* [PW-2029] Using new AdyenAmountCurrency model

* [PW-2029] AdyenAmountCurrency for partial PBM

* [PW-2029] AdyenAmountCurrency in Capture and Refund builders

* [PW-2029] Renaming orderAmount var for code smell

* [PW-2029] Removing redundant boolean literal

* Version 7.0.0 bump

* [PW-2029] Extra usages of ChargedCurrency helper

* [PW-2029] Removed charged currency config field from admin panel

* [PW-2029] Adjusting usage of currency in Cron.php

* [PW-2029] Code styling

* Fix phpcs
Co-authored-by: default avatarattilak <attila.kiss@adyen.com>
parent 9cf251f3
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
namespace Adyen\Payment\Block\Checkout; namespace Adyen\Payment\Block\Checkout;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Model\AdyenAmountCurrency;
/** /**
* Billing agreement information on Order success page * Billing agreement information on Order success page
*/ */
...@@ -55,6 +58,11 @@ class Success extends \Magento\Framework\View\Element\Template ...@@ -55,6 +58,11 @@ class Success extends \Magento\Framework\View\Element\Template
*/ */
protected $storeManager; protected $storeManager;
/**
* @var ChargedCurrency
*/
protected $chargedCurrency;
/** /**
* Success constructor. * Success constructor.
* *
...@@ -71,6 +79,7 @@ class Success extends \Magento\Framework\View\Element\Template ...@@ -71,6 +79,7 @@ class Success extends \Magento\Framework\View\Element\Template
\Magento\Framework\Pricing\Helper\Data $priceHelper, \Magento\Framework\Pricing\Helper\Data $priceHelper,
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
ChargedCurrency $chargedCurrency,
array $data = [] array $data = []
) { ) {
$this->checkoutSession = $checkoutSession; $this->checkoutSession = $checkoutSession;
...@@ -78,6 +87,7 @@ class Success extends \Magento\Framework\View\Element\Template ...@@ -78,6 +87,7 @@ class Success extends \Magento\Framework\View\Element\Template
$this->priceHelper = $priceHelper; $this->priceHelper = $priceHelper;
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->storeManager = $storeManager; $this->storeManager = $storeManager;
$this->chargedCurrency = $chargedCurrency;
parent::__construct($context, $data); parent::__construct($context, $data);
} }
...@@ -212,4 +222,12 @@ class Success extends \Magento\Framework\View\Element\Template ...@@ -212,4 +222,12 @@ class Success extends \Magento\Framework\View\Element\Template
} }
return $this->order; return $this->order;
} }
/**
* @return AdyenAmountCurrency
*/
public function geAdyenAmountCurrency()
{
return $this->chargedCurrency->getOrderAmountCurrency($this->order, false);
}
} }
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
namespace Adyen\Payment\Block\Form; namespace Adyen\Payment\Block\Form;
use Adyen\Payment\Helper\ChargedCurrency;
class Oneclick extends \Adyen\Payment\Block\Form\Cc class Oneclick extends \Adyen\Payment\Block\Form\Cc
{ {
/** /**
...@@ -35,6 +37,11 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc ...@@ -35,6 +37,11 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
*/ */
protected $_sessionQuote; protected $_sessionQuote;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* Oneclick constructor. * Oneclick constructor.
* *
...@@ -42,6 +49,8 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc ...@@ -42,6 +49,8 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
* @param \Magento\Payment\Model\Config $paymentConfig * @param \Magento\Payment\Model\Config $paymentConfig
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Checkout\Model\Session $checkoutSession * @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
* @param ChargedCurrency $chargedCurrency
* @param array $data * @param array $data
*/ */
public function __construct( public function __construct(
...@@ -50,10 +59,12 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc ...@@ -50,10 +59,12 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Checkout\Model\Session $checkoutSession, \Magento\Checkout\Model\Session $checkoutSession,
\Magento\Backend\Model\Session\Quote $sessionQuote, \Magento\Backend\Model\Session\Quote $sessionQuote,
ChargedCurrency $chargedCurrency,
array $data = [] array $data = []
) { ) {
parent::__construct($context, $paymentConfig, $adyenHelper, $checkoutSession, $data); parent::__construct($context, $paymentConfig, $adyenHelper, $checkoutSession, $data);
$this->_sessionQuote = $sessionQuote; $this->_sessionQuote = $sessionQuote;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -63,7 +74,8 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc ...@@ -63,7 +74,8 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
{ {
$customerId = $this->_sessionQuote->getCustomerId(); $customerId = $this->_sessionQuote->getCustomerId();
$storeId = $this->_sessionQuote->getStoreId(); $storeId = $this->_sessionQuote->getStoreId();
$grandTotal = $this->_sessionQuote->getQuote()->getGrandTotal(); $grandTotal = $this->chargedCurrency->getQuoteAmountCurrency($this->_sessionQuote->getQuote())->getAmount();
// For backend only allow recurring payments // For backend only allow recurring payments
$recurringType = \Adyen\Payment\Model\RecurringType::RECURRING; $recurringType = \Adyen\Payment\Model\RecurringType::RECURRING;
......
...@@ -23,8 +23,28 @@ ...@@ -23,8 +23,28 @@
namespace Adyen\Payment\Block\Info; namespace Adyen\Payment\Block\Info;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Framework\View\Element\Template;
class Hpp extends AbstractInfo class Hpp extends AbstractInfo
{ {
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $adyenOrderPaymentCollectionFactory,
Template\Context $context,
ChargedCurrency $chargedCurrency,
array $data = []
) {
$this->chargedCurrency = $chargedCurrency;
parent::__construct($adyenHelper, $adyenOrderPaymentCollectionFactory, $context, $data);
}
/** /**
* @var string * @var string
*/ */
...@@ -75,6 +95,14 @@ class Hpp extends AbstractInfo ...@@ -75,6 +95,14 @@ class Hpp extends AbstractInfo
return $this->getInfo()->getOrder(); return $this->getInfo()->getOrder();
} }
/**
* @return \Adyen\Payment\Model\AdyenAmountCurrency
*/
public function getOrderAmountCurrency()
{
return $this->chargedCurrency->getOrderAmountCurrency($this->getInfo()->getOrder(), false);
}
/** /**
* @return null * @return null
* @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\LocalizedException
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace Adyen\Payment\Block\Info; namespace Adyen\Payment\Block\Info;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template;
class PaymentLink extends AbstractInfo class PaymentLink extends AbstractInfo
...@@ -37,16 +38,23 @@ class PaymentLink extends AbstractInfo ...@@ -37,16 +38,23 @@ class PaymentLink extends AbstractInfo
*/ */
private $payByMail; private $payByMail;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
public function __construct( public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $adyenOrderPaymentCollectionFactory, \Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $adyenOrderPaymentCollectionFactory,
Template\Context $context, Template\Context $context,
\Magento\Framework\Registry $registry, \Magento\Framework\Registry $registry,
\Adyen\Payment\Gateway\Command\PayByMailCommand $payByMailCommand, \Adyen\Payment\Gateway\Command\PayByMailCommand $payByMailCommand,
ChargedCurrency $chargedCurrency,
array $data = [] array $data = []
) { ) {
$this->registry = $registry; $this->registry = $registry;
$this->payByMail = $payByMailCommand; $this->payByMail = $payByMailCommand;
$this->chargedCurrency = $chargedCurrency;
parent::__construct($adyenHelper, $adyenOrderPaymentCollectionFactory, $context, $data); parent::__construct($adyenHelper, $adyenOrderPaymentCollectionFactory, $context, $data);
} }
...@@ -74,7 +82,8 @@ class PaymentLink extends AbstractInfo ...@@ -74,7 +82,8 @@ class PaymentLink extends AbstractInfo
*/ */
public function getPaymentLinkUrl() public function getPaymentLinkUrl()
{ {
return $this->payByMail->generatePaymentUrl($this->getPayment(), $this->getOrder()->getTotalDue()); $adyenAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($this->getOrder(), false);
return $this->payByMail->generatePaymentUrl($this->getPayment(), $adyenAmountCurrency->getAmountDue());
} }
/** /**
......
...@@ -310,80 +310,6 @@ class Redirect extends \Magento\Payment\Block\Form ...@@ -310,80 +310,6 @@ class Redirect extends \Magento\Payment\Block\Form
return $formFields; return $formFields;
} }
/**
* @param $formFields
* @return mixed
*/
protected function setOpenInvoiceData($formFields)
{
$count = 0;
$currency = $this->_order->getOrderCurrencyCode();
foreach ($this->_order->getAllVisibleItems() as $item) {
/** @var $item \Magento\Sales\Model\Order\Item */
++$count;
$numberOfItems = (int)$item->getQtyOrdered();
$formFields = $this->_adyenHelper->createOpenInvoiceLineItem(
$formFields,
$count,
$item->getName(),
$item->getPrice(),
$currency,
$item->getTaxAmount(),
$item->getPriceInclTax(),
$item->getTaxPercent(),
$numberOfItems,
$this->_order->getPayment(),
$item->getId()
);
}
// Discount cost
if ($this->_order->getDiscountAmount() > 0 || $this->_order->getDiscountAmount() < 0) {
++$count;
$description = __('Total Discount');
$itemAmount = $this->_adyenHelper->formatAmount($this->_order->getDiscountAmount(), $currency);
$itemVatAmount = "0";
$itemVatPercentage = "0";
$numberOfItems = 1;
$formFields = $this->_adyenHelper->getOpenInvoiceLineData(
$formFields,
$count,
$currency,
$description,
$itemAmount,
$itemVatAmount,
$itemVatPercentage,
$numberOfItems,
$this->_order->getPayment(),
"totalDiscount"
);
}
// Shipping cost
if ($this->_order->getShippingAmount() > 0 || $this->_order->getShippingTaxAmount() > 0) {
++$count;
$formFields = $this->_adyenHelper->createOpenInvoiceLineShipping(
$formFields,
$count,
$this->_order,
$this->_order->getShippingAmount(),
$this->_order->getShippingTaxAmount(),
$currency,
$this->_order->getPayment()
);
}
$formFields['openinvoicedata.refundDescription'] = "Refund / Correction for " .
$formFields['merchantReference'];
$formFields['openinvoicedata.numberOfLines'] = $count;
return $formFields;
}
/** /**
* @param $genderId * @param $genderId
* @return string * @return string
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
namespace Adyen\Payment\Gateway\Command; namespace Adyen\Payment\Gateway\Command;
use Magento\Payment\Gateway\Command; use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\CommandInterface; use Magento\Payment\Gateway\CommandInterface;
class PayByMailCommand implements CommandInterface class PayByMailCommand implements CommandInterface
...@@ -38,19 +38,26 @@ class PayByMailCommand implements CommandInterface ...@@ -38,19 +38,26 @@ class PayByMailCommand implements CommandInterface
*/ */
protected $_adyenLogger; protected $_adyenLogger;
/**
* @var ChargedCurrency
*/
protected $chargedCurrency;
/** /**
* PayByMailCommand constructor. * PayByMailCommand constructor.
* *
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Framework\Locale\ResolverInterface $resolver
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct( public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
ChargedCurrency $chargedCurrency
) { ) {
$this->_adyenHelper = $adyenHelper; $this->_adyenHelper = $adyenHelper;
$this->_adyenLogger = $adyenLogger; $this->_adyenLogger = $adyenLogger;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -127,7 +134,6 @@ class PayByMailCommand implements CommandInterface ...@@ -127,7 +134,6 @@ class PayByMailCommand implements CommandInterface
$order = $payment->getOrder(); $order = $payment->getOrder();
$realOrderId = $order->getRealOrderId(); $realOrderId = $order->getRealOrderId();
$orderCurrencyCode = $order->getOrderCurrencyCode();
$storeId = $order->getStore()->getId(); $storeId = $order->getStore()->getId();
// check if paybymail has it's own skin // check if paybymail has it's own skin
...@@ -143,10 +149,15 @@ class PayByMailCommand implements CommandInterface ...@@ -143,10 +149,15 @@ class PayByMailCommand implements CommandInterface
$hmacKey = $this->_adyenHelper->getHmacPayByMail(); $hmacKey = $this->_adyenHelper->getHmacPayByMail();
} }
$adyenAmount = $this->chargedCurrency->getOrderAmountCurrency($order);
$orderCurrencyCode = $adyenAmount->getCurrencyCode();
//Use the $paymentAmount arg as total if this is a partial payment, full amount if not
$amount = $this->_adyenHelper->formatAmount( $amount = $this->_adyenHelper->formatAmount(
$paymentAmount ?: $order->getGrandTotal(), $paymentAmount ?: $adyenAmount->getAmount(),
$orderCurrencyCode $orderCurrencyCode
); );
$merchantAccount = trim($this->_adyenHelper->getAdyenAbstractConfigData('merchant_account', $storeId)); $merchantAccount = trim($this->_adyenHelper->getAdyenAbstractConfigData('merchant_account', $storeId));
$shopperEmail = $order->getCustomerEmail(); $shopperEmail = $order->getCustomerEmail();
$customerId = $order->getCustomerId(); $customerId = $order->getCustomerId();
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request; namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface; use Magento\Payment\Gateway\Request\BuilderInterface;
/** /**
...@@ -35,14 +36,23 @@ class CaptureDataBuilder implements BuilderInterface ...@@ -35,14 +36,23 @@ class CaptureDataBuilder implements BuilderInterface
*/ */
private $adyenHelper; private $adyenHelper;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* CaptureDataBuilder constructor. * CaptureDataBuilder constructor.
* *
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct(\Adyen\Payment\Helper\Data $adyenHelper) public function __construct(
{ \Adyen\Payment\Helper\Data $adyenHelper,
ChargedCurrency $chargedCurrency
) {
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -55,13 +65,12 @@ class CaptureDataBuilder implements BuilderInterface ...@@ -55,13 +65,12 @@ class CaptureDataBuilder implements BuilderInterface
{ {
/** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */ /** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
$paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject); $paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
$amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject);
$payment = $paymentDataObject->getPayment(); $payment = $paymentDataObject->getPayment();
$order = $payment->getOrder();
$pspReference = $payment->getCcTransId(); $pspReference = $payment->getCcTransId();
$currency = $payment->getOrder()->getOrderCurrencyCode(); $orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order, false);
$currency = $orderAmountCurrency->getCurrencyCode();
$amount = $orderAmountCurrency->getAmount();
$amount = $this->adyenHelper->formatAmount($amount, $currency); $amount = $this->adyenHelper->formatAmount($amount, $currency);
$modificationAmount = ['currency' => $currency, 'value' => $amount]; $modificationAmount = ['currency' => $currency, 'value' => $amount];
...@@ -93,27 +102,32 @@ class CaptureDataBuilder implements BuilderInterface ...@@ -93,27 +102,32 @@ class CaptureDataBuilder implements BuilderInterface
{ {
$formFields = []; $formFields = [];
$count = 0; $count = 0;
$currency = $payment->getOrder()->getOrderCurrencyCode(); $order = $payment->getOrder();
$invoices = $order->getInvoiceCollection();
$invoices = $payment->getOrder()->getInvoiceCollection(); $currency = $this->chargedCurrency
->getOrderAmountCurrency($payment->getOrder(), false)
->getCurrencyCode();
// The latest invoice will contain only the selected items(and quantities) for the (partial) capture // The latest invoice will contain only the selected items(and quantities) for the (partial) capture
$latestInvoice = $invoices->getLastItem(); $latestInvoice = $invoices->getLastItem();
foreach ($latestInvoice->getItems() as $invoiceItem) { /* @var \Magento\Sales\Model\Order\Invoice\Item $invoiceItem */
foreach ($latestInvoice->getItems() as $invoiceItem) {
if ($invoiceItem->getOrderItem()->getParentItem()) { if ($invoiceItem->getOrderItem()->getParentItem()) {
continue; continue;
} }
++$count; ++$count;
$itemAmountCurrency = $this->chargedCurrency->getInvoiceItemAmountCurrency($invoiceItem);
$numberOfItems = (int)$invoiceItem->getQty(); $numberOfItems = (int)$invoiceItem->getQty();
$formFields = $this->adyenHelper->createOpenInvoiceLineItem( $formFields = $this->adyenHelper->createOpenInvoiceLineItem(
$formFields, $formFields,
$count, $count,
$invoiceItem->getName(), $invoiceItem->getName(),
$invoiceItem->getPrice(), $itemAmountCurrency->getAmount(),
$currency, $currency,
$invoiceItem->getTaxAmount(), $itemAmountCurrency->getTaxAmount(),
$invoiceItem->getPriceInclTax(), $itemAmountCurrency->getAmount() + $itemAmountCurrency->getTaxAmount(),
$invoiceItem->getOrderItem()->getTaxPercent(), $invoiceItem->getOrderItem()->getTaxPercent(),
$numberOfItems, $numberOfItems,
$payment, $payment,
...@@ -124,13 +138,14 @@ class CaptureDataBuilder implements BuilderInterface ...@@ -124,13 +138,14 @@ class CaptureDataBuilder implements BuilderInterface
// Shipping cost // Shipping cost
if ($latestInvoice->getShippingAmount() > 0) { if ($latestInvoice->getShippingAmount() > 0) {
++$count; ++$count;
$adyenInvoiceShippingAmount = $this->chargedCurrency->getInvoiceShippingAmountCurrency($latestInvoice);
$formFields = $this->adyenHelper->createOpenInvoiceLineShipping( $formFields = $this->adyenHelper->createOpenInvoiceLineShipping(
$formFields, $formFields,
$count, $count,
$payment->getOrder(), $order,
$latestInvoice->getShippingAmount(), $adyenInvoiceShippingAmount->getAmount(),
$latestInvoice->getShippingTaxAmount(), $adyenInvoiceShippingAmount->getTaxAmount(),
$currency, $adyenInvoiceShippingAmount->getCurrencyCode(),
$payment $payment
); );
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request; namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface; use Magento\Payment\Gateway\Request\BuilderInterface;
use Adyen\Payment\Observer\AdyenHppDataAssignObserver; use Adyen\Payment\Observer\AdyenHppDataAssignObserver;
...@@ -48,6 +49,11 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -48,6 +49,11 @@ class CheckoutDataBuilder implements BuilderInterface
*/ */
private $gender; private $gender;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Store\Model\StoreManagerInterface $storeManager
...@@ -58,12 +64,14 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -58,12 +64,14 @@ class CheckoutDataBuilder implements BuilderInterface
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Magento\Quote\Api\CartRepositoryInterface $cartRepository,
\Adyen\Payment\Model\Gender $gender \Adyen\Payment\Model\Gender $gender,
ChargedCurrency $chargedCurrency
) { ) {
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->storeManager = $storeManager; $this->storeManager = $storeManager;
$this->cartRepository = $cartRepository; $this->cartRepository = $cartRepository;
$this->gender = $gender; $this->gender = $gender;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -234,19 +242,27 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -234,19 +242,27 @@ class CheckoutDataBuilder implements BuilderInterface
/** @var \Magento\Quote\Model\Quote $cart */ /** @var \Magento\Quote\Model\Quote $cart */
$cart = $this->cartRepository->get($order->getQuoteId()); $cart = $this->cartRepository->get($order->getQuoteId());
$currency = $cart->getCurrency(); $amountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order);
$currency = $amountCurrency->getCurrencyCode();
$discountAmount = 0; $discountAmount = 0;
foreach ($cart->getAllVisibleItems() as $item) { foreach ($cart->getAllVisibleItems() as $item) {
$numberOfItems = (int)$item->getQty(); $numberOfItems = (int)$item->getQty();
$itemAmountCurrency = $this->chargedCurrency->getQuoteItemAmountCurrency($item);
// Summarize the discount amount item by item // Summarize the discount amount item by item
$discountAmount += $item->getDiscountAmount(); $discountAmount += $itemAmountCurrency->getDiscountAmount();
$formattedPriceExcludingTax = $this->adyenHelper->formatAmount($item->getPrice(), $currency); $formattedPriceExcludingTax = $this->adyenHelper->formatAmount(
$itemAmountCurrency->getAmount(),
$itemAmountCurrency->getCurrencyCode()
);
$taxAmount = $item->getPrice() * ($item->getTaxPercent() / 100); $formattedTaxAmount = $this->adyenHelper->formatAmount(
$formattedTaxAmount = $this->adyenHelper->formatAmount($taxAmount, $currency); $itemAmountCurrency->getTaxAmount(),
$itemAmountCurrency->getCurrencyCode()
);
$formattedTaxPercentage = $item->getTaxPercent() * 100; $formattedTaxPercentage = $item->getTaxPercent() * 100;
$formFields['lineItems'][] = [ $formFields['lineItems'][] = [
...@@ -264,7 +280,7 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -264,7 +280,7 @@ class CheckoutDataBuilder implements BuilderInterface
// Discount cost // Discount cost
if ($discountAmount != 0) { if ($discountAmount != 0) {
$description = __('Discount'); $description = __('Discount');
$itemAmount = -$this->adyenHelper->formatAmount($discountAmount, $currency); $itemAmount = -$this->adyenHelper->formatAmount($discountAmount, $itemAmountCurrency->getCurrencyCode());
$itemVatAmount = "0"; $itemVatAmount = "0";
$itemVatPercentage = "0"; $itemVatPercentage = "0";
$numberOfItems = 1; $numberOfItems = 1;
...@@ -281,13 +297,15 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -281,13 +297,15 @@ class CheckoutDataBuilder implements BuilderInterface
} }
// Shipping cost // Shipping cost
if ($cart->getShippingAddress()->getShippingAmount() > 0 || $cart->getShippingAddress()->getShippingTaxAmount( if ($cart->getShippingAddress()->getShippingAmount() > 0 ||
) > 0) { $cart->getShippingAddress()->getShippingTaxAmount() > 0
$priceExcludingTax = $cart->getShippingAddress()->getShippingAmount() - $cart->getShippingAddress( ) {
)->getShippingTaxAmount(); $shippingAmountCurrency = $this->chargedCurrency->getQuoteShippingAmountCurrency($cart);
$priceExcludingTax = $shippingAmountCurrency->getAmount();
$formattedTaxAmount = $this->adyenHelper->formatAmount( $formattedTaxAmount = $this->adyenHelper->formatAmount(
$cart->getShippingAddress()->getShippingTaxAmount(), $shippingAmountCurrency->getTaxAmount(),
$currency $currency
); );
...@@ -296,8 +314,7 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -296,8 +314,7 @@ class CheckoutDataBuilder implements BuilderInterface
$formattedTaxPercentage = 0; $formattedTaxPercentage = 0;
if ($priceExcludingTax !== 0) { if ($priceExcludingTax !== 0) {
$formattedTaxPercentage = $cart->getShippingAddress()->getShippingTaxAmount( $formattedTaxPercentage = $shippingAmountCurrency->getTaxAmount() / $priceExcludingTax * 100 * 100;
) / $priceExcludingTax * 100 * 100;
} }
$formFields['lineItems'][] = [ $formFields['lineItems'][] = [
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request; namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface; use Magento\Payment\Gateway\Request\BuilderInterface;
class PaymentDataBuilder implements BuilderInterface class PaymentDataBuilder implements BuilderInterface
...@@ -32,14 +33,23 @@ class PaymentDataBuilder implements BuilderInterface ...@@ -32,14 +33,23 @@ class PaymentDataBuilder implements BuilderInterface
*/ */
private $adyenRequestsHelper; private $adyenRequestsHelper;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* PaymentDataBuilder constructor. * PaymentDataBuilder constructor.
* *
* @param \Adyen\Payment\Helper\Requests $adyenRequestsHelper * @param \Adyen\Payment\Helper\Requests $adyenRequestsHelper
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct(\Adyen\Payment\Helper\Requests $adyenRequestsHelper) public function __construct(
{ \Adyen\Payment\Helper\Requests $adyenRequestsHelper,
ChargedCurrency $chargedCurrency
) {
$this->adyenRequestsHelper = $adyenRequestsHelper; $this->adyenRequestsHelper = $adyenRequestsHelper;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -55,8 +65,9 @@ class PaymentDataBuilder implements BuilderInterface ...@@ -55,8 +65,9 @@ class PaymentDataBuilder implements BuilderInterface
$payment = $paymentDataObject->getPayment(); $payment = $paymentDataObject->getPayment();
$fullOrder = $payment->getOrder(); $fullOrder = $payment->getOrder();
$currencyCode = $fullOrder->getOrderCurrencyCode(); $amountCurrency = $this->chargedCurrency->getOrderAmountCurrency($fullOrder);
$amount = $fullOrder->getGrandTotal(); $currencyCode = $amountCurrency->getCurrencyCode();
$amount = $amountCurrency->getAmount();
$reference = $order->getOrderIncrementId(); $reference = $order->getOrderIncrementId();
$paymentMethod = $payment->getMethod(); $paymentMethod = $payment->getMethod();
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request; namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface; use Magento\Payment\Gateway\Request\BuilderInterface;
/** /**
...@@ -45,20 +46,28 @@ class RefundDataBuilder implements BuilderInterface ...@@ -45,20 +46,28 @@ class RefundDataBuilder implements BuilderInterface
*/ */
protected $adyenInvoiceCollectionFactory; protected $adyenInvoiceCollectionFactory;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* RefundDataBuilder constructor. * RefundDataBuilder constructor.
* *
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory * @param \Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct( public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory, \Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory,
\Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory $adyenInvoiceCollectionFactory \Adyen\Payment\Model\ResourceModel\Invoice\CollectionFactory $adyenInvoiceCollectionFactory,
ChargedCurrency $chargedCurrency
) { ) {
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->orderPaymentCollectionFactory = $orderPaymentCollectionFactory; $this->orderPaymentCollectionFactory = $orderPaymentCollectionFactory;
$this->adyenInvoiceCollectionFactory = $adyenInvoiceCollectionFactory; $this->adyenInvoiceCollectionFactory = $adyenInvoiceCollectionFactory;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -69,16 +78,16 @@ class RefundDataBuilder implements BuilderInterface ...@@ -69,16 +78,16 @@ class RefundDataBuilder implements BuilderInterface
{ {
/** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */ /** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
$paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject); $paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
$amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject); $buildSubjectAmount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject);
$order = $paymentDataObject->getOrder(); $order = $paymentDataObject->getOrder();
$payment = $paymentDataObject->getPayment(); $payment = $paymentDataObject->getPayment();
$pspReference = $payment->getCcTransId(); $pspReference = $payment->getCcTransId();
$currency = $payment->getOrder()->getOrderCurrencyCode(); $orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($payment->getOrder(), false);
$currency = $orderAmountCurrency->getCurrencyCode();
$amount = $orderAmountCurrency->getAmount();
$storeId = $order->getStoreId(); $storeId = $order->getStoreId();
$method = $payment->getMethod(); $method = $payment->getMethod();
$merchantAccount = $this->adyenHelper->getAdyenMerchantAccount($method, $storeId); $merchantAccount = $this->adyenHelper->getAdyenMerchantAccount($method, $storeId);
$grandTotal = $payment->getOrder()->getGrandTotal();
// check if it contains a split payment // check if it contains a split payment
$orderPaymentCollection = $this->orderPaymentCollectionFactory $orderPaymentCollection = $this->orderPaymentCollectionFactory
...@@ -101,7 +110,7 @@ class RefundDataBuilder implements BuilderInterface ...@@ -101,7 +110,7 @@ class RefundDataBuilder implements BuilderInterface
$orderPaymentCollection->addPaymentFilterDescending($payment->getId()); $orderPaymentCollection->addPaymentFilterDescending($payment->getId());
} elseif ($refundStrategy == "3") { } elseif ($refundStrategy == "3") {
// refund based on ratio // refund based on ratio
$ratio = $amount / $grandTotal; $ratio = $buildSubjectAmount / $amount;
$orderPaymentCollection->addPaymentFilterAscending($payment->getId()); $orderPaymentCollection->addPaymentFilterAscending($payment->getId());
} }
...@@ -185,7 +194,9 @@ class RefundDataBuilder implements BuilderInterface ...@@ -185,7 +194,9 @@ class RefundDataBuilder implements BuilderInterface
{ {
$formFields = []; $formFields = [];
$count = 0; $count = 0;
$currency = $payment->getOrder()->getOrderCurrencyCode(); $currency = $this->chargedCurrency
->getOrderAmountCurrency($payment->getOrder(), false)
->getCurrencyCode();
/** /**
* @var \Magento\Sales\Model\Order\Creditmemo $creditMemo * @var \Magento\Sales\Model\Order\Creditmemo $creditMemo
...@@ -194,16 +205,18 @@ class RefundDataBuilder implements BuilderInterface ...@@ -194,16 +205,18 @@ class RefundDataBuilder implements BuilderInterface
foreach ($creditMemo->getItems() as $refundItem) { foreach ($creditMemo->getItems() as $refundItem) {
++$count; ++$count;
$itemAmountCurrency = $this->chargedCurrency->getCreditMemoItemAmountCurrency($refundItem);
$numberOfItems = (int)$refundItem->getQty(); $numberOfItems = (int)$refundItem->getQty();
$formFields = $this->adyenHelper->createOpenInvoiceLineItem( $formFields = $this->adyenHelper->createOpenInvoiceLineItem(
$formFields, $formFields,
$count, $count,
$refundItem->getName(), $refundItem->getName(),
$refundItem->getPrice(), $itemAmountCurrency->getAmount(),
$currency, $currency,
$refundItem->getTaxAmount(), $itemAmountCurrency->getTaxAmount(),
$refundItem->getPriceInclTax(), $itemAmountCurrency->getAmount() + $itemAmountCurrency->getTaxAmount(),
$refundItem->getOrderItem()->getTaxPercent(), $refundItem->getOrderItem()->getTaxPercent(),
$numberOfItems, $numberOfItems,
$payment, $payment,
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
namespace Adyen\Payment\Gateway\Validator; namespace Adyen\Payment\Gateway\Validator;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Validator\AbstractValidator; use Magento\Payment\Gateway\Validator\AbstractValidator;
class InstallmentValidator extends AbstractValidator class InstallmentValidator extends AbstractValidator
...@@ -49,6 +50,11 @@ class InstallmentValidator extends AbstractValidator ...@@ -49,6 +50,11 @@ class InstallmentValidator extends AbstractValidator
*/ */
private $quoteRepository; private $quoteRepository;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* InstallmentValidator constructor. * InstallmentValidator constructor.
* *
...@@ -63,12 +69,14 @@ class InstallmentValidator extends AbstractValidator ...@@ -63,12 +69,14 @@ class InstallmentValidator extends AbstractValidator
\Adyen\Payment\Logger\AdyenLogger $adyenLogger, \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Framework\Serialize\SerializerInterface $serializer, \Magento\Framework\Serialize\SerializerInterface $serializer,
\Magento\Quote\Model\QuoteRepository $quoteRepository \Magento\Quote\Model\QuoteRepository $quoteRepository,
ChargedCurrency $chargedCurrency
) { ) {
$this->adyenLogger = $adyenLogger; $this->adyenLogger = $adyenLogger;
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->serializer = $serializer; $this->serializer = $serializer;
$this->quoteRepository = $quoteRepository; $this->quoteRepository = $quoteRepository;
$this->chargedCurrency = $chargedCurrency;
parent::__construct($resultFactory); parent::__construct($resultFactory);
} }
...@@ -86,7 +94,7 @@ class InstallmentValidator extends AbstractValidator ...@@ -86,7 +94,7 @@ class InstallmentValidator extends AbstractValidator
} }
$installmentsEnabled = $this->adyenHelper->getAdyenCcConfigData('enable_installments'); $installmentsEnabled = $this->adyenHelper->getAdyenCcConfigData('enable_installments');
if ($quote && $installmentsEnabled) { if ($quote && $installmentsEnabled) {
$grandTotal = $quote->getGrandTotal(); $grandTotal = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getAmount();
$installmentsAvailable = $this->adyenHelper->getAdyenCcConfigData('installments'); $installmentsAvailable = $this->adyenHelper->getAdyenCcConfigData('installments');
$installmentSelected = $payment->getAdditionalInformation('number_of_installments'); $installmentSelected = $payment->getAdditionalInformation('number_of_installments');
$ccType = $payment->getAdditionalInformation('cc_type'); $ccType = $payment->getAdditionalInformation('cc_type');
......
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2020 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Helper;
use Adyen\Payment\Model\AdyenAmountCurrency;
use Magento\Quote\Model\Quote;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;
use Magento\Store\Model\Store;
use Magento\Sales\Api\Data\CreditmemoItemInterface;
class ChargedCurrency
{
/**
* @var string
* Charged currency value when Global/Website is selected
*/
const BASE = "base";
/**
* @var Config
*/
private $config;
public function __construct(
Config $config
) {
$this->config = $config;
}
/**
* @param Order $order
* @param bool $orderPlacement true if fetching the order's data when it is being placed,
* false to get the data according to the charged_currency already saved for the order
*
* @return AdyenAmountCurrency
*/
public function getOrderAmountCurrency(Order $order, bool $orderPlacement = true)
{
$chargedCurrency = $orderPlacement
? $this->config->getChargedCurrency($order->getStoreId())
: $order->getAdyenChargedCurrency();
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency(
$order->getBaseGrandTotal(),
$order->getGlobalCurrencyCode(),
null,
null,
$order->getBaseTotalDue()
);
}
return new AdyenAmountCurrency(
$order->getGrandTotal(),
$order->getOrderCurrencyCode(),
null,
null,
$order->getTotalDue()
);
}
/**
* @param Quote $quote
* @return AdyenAmountCurrency
*/
public function getQuoteAmountCurrency(Quote $quote)
{
$chargedCurrency = $this->config->getChargedCurrency($quote->getStoreId());
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency($quote->getBaseGrandTotal(), $quote->getBaseCurrencyCode());
}
return new AdyenAmountCurrency($quote->getGrandTotal(), $quote->getQuoteCurrencyCode());
}
/**
* @param Quote\Item $item
* @return AdyenAmountCurrency
*/
public function getQuoteItemAmountCurrency(Quote\Item $item)
{
$chargedCurrency = $this->config->getChargedCurrency($item->getStoreId());
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency(
$item->getBasePrice(),
$item->getQuote()->getBaseCurrencyCode(),
$item->getBaseDiscountAmount(),
($item->getTaxPercent() / 100) * $item->getBasePrice()
);
}
return new AdyenAmountCurrency(
$item->getRowTotal() / $item->getQty(),
$item->getQuote()->getQuoteCurrencyCode(),
$item->getDiscountAmount(),
($item->getTaxPercent() / 100) * ($item->getRowTotal() / $item->getQty())
);
}
/**
* @param Invoice\Item $item
* @return AdyenAmountCurrency
*/
public function getInvoiceItemAmountCurrency(Invoice\Item $item)
{
$chargedCurrency = $item->getInvoice()->getOrder()->getAdyenChargedCurrency();
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency(
$item->getBasePrice(),
$item->getInvoice()->getBaseCurrencyCode(),
null,
$item->getBaseTaxAmount()
);
}
return new AdyenAmountCurrency(
$item->getPrice(),
$item->getInvoice()->getBaseCurrencyCode(),
null,
$item->getTaxAmount()
);
}
/**
* @param CreditmemoItemInterface $item
* @return AdyenAmountCurrency
*/
public function getCreditMemoItemAmountCurrency(CreditmemoItemInterface $item)
{
$chargedCurrency = $item->getCreditMemo()->getInvoice()->getOrder()->getAdyenChargedCurrency();
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency(
$item->getBasePrice(),
$item->getInvoice()->getBaseCurrencyCode(),
null,
$item->getBaseTaxAmount()
);
}
return new AdyenAmountCurrency(
$item->getPrice(),
$item->getCreditMemo()->getInvoice()->getBaseCurrencyCode(),
null,
$item->getTaxAmount()
);
}
/**
* @param CreditmemoItemInterface $item
* @return AdyenAmountCurrency
*/
public function getQuoteShippingAmountCurrency(Quote $quote)
{
$chargedCurrency = $this->config->getChargedCurrency($quote->getStoreId());
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency(
$quote->getShippingAddress()->getBaseShippingAmount(),
$quote->getBaseCurrencyCode(),
$quote->getShippingAddress()->getBaseShippingDiscountAmount(),
$quote->getShippingAddress()->getBaseShippingTaxAmount()
);
}
return new AdyenAmountCurrency(
$quote->getShippingAddress()->getShippingAmount(),
$quote->getCurrencyCode(),
$quote->getShippingAddress()->getShippingDiscountAmount(),
$quote->getShippingAddress()->getShippingTaxAmount()
);
}
/**
* @param \Magento\Sales\Model\Order\Invoice $invoice
* @return AdyenAmountCurrency
*/
public function getInvoiceShippingAmountCurrency(Invoice $invoice)
{
$chargedCurrency = $invoice->getOrder()->getAdyenChargedCurrency();
if ($chargedCurrency == self::BASE) {
return new AdyenAmountCurrency(
$invoice->getBaseShippingAmount(),
$invoice->getBaseCurrencyCode(),
null,
$invoice->getBaseShippingTaxAmount()
);
}
return new AdyenAmountCurrency(
$invoice->getShippingAmount(),
$invoice->getOrderCurrencyCode(),
null,
$invoice->getShippingTaxAmount()
);
}
}
...@@ -35,6 +35,7 @@ class Config ...@@ -35,6 +35,7 @@ class Config
const XML_NOTIFICATIONS_IP_CHECK = "notifications_ip_check"; const XML_NOTIFICATIONS_IP_CHECK = "notifications_ip_check";
const XML_NOTIFICATIONS_HMAC_KEY_LIVE = "notification_hmac_key_live"; const XML_NOTIFICATIONS_HMAC_KEY_LIVE = "notification_hmac_key_live";
const XML_NOTIFICATIONS_HMAC_KEY_TEST = "notification_hmac_key_test"; const XML_NOTIFICATIONS_HMAC_KEY_TEST = "notification_hmac_key_test";
const XML_CHARGED_CURRENCY = "charged_currency";
/** /**
* @var Magento\Framework\App\Config\ScopeConfigInterface * @var Magento\Framework\App\Config\ScopeConfigInterface
...@@ -153,6 +154,17 @@ class Config ...@@ -153,6 +154,17 @@ class Config
return $this->adyenHelper->getAdyenHppVaultConfigDataFlag('active', $storeId); return $this->adyenHelper->getAdyenHppVaultConfigDataFlag('active', $storeId);
} }
/**
* Retrive charged currency selection (base or display)
*
* @param null|int|string $storeId
* @return mixed
*/
public function getChargedCurrency($storeId = null)
{
return $this->adyenHelper->getAdyenAbstractConfigData(self::XML_CHARGED_CURRENCY, $storeId);
}
/** /**
* Retrieve information from payment configuration * Retrieve information from payment configuration
* *
......
...@@ -90,6 +90,11 @@ class PaymentMethods extends AbstractHelper ...@@ -90,6 +90,11 @@ class PaymentMethods extends AbstractHelper
*/ */
protected $quote; protected $quote;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* PaymentMethods constructor. * PaymentMethods constructor.
* *
...@@ -116,7 +121,8 @@ class PaymentMethods extends AbstractHelper ...@@ -116,7 +121,8 @@ class PaymentMethods extends AbstractHelper
\Magento\Framework\App\RequestInterface $request, \Magento\Framework\App\RequestInterface $request,
\Magento\Framework\View\Asset\Source $assetSource, \Magento\Framework\View\Asset\Source $assetSource,
\Magento\Framework\View\DesignInterface $design, \Magento\Framework\View\DesignInterface $design,
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider,
ChargedCurrency $chargedCurrency
) { ) {
$this->quoteRepository = $quoteRepository; $this->quoteRepository = $quoteRepository;
$this->config = $config; $this->config = $config;
...@@ -129,6 +135,7 @@ class PaymentMethods extends AbstractHelper ...@@ -129,6 +135,7 @@ class PaymentMethods extends AbstractHelper
$this->assetSource = $assetSource; $this->assetSource = $assetSource;
$this->design = $design; $this->design = $design;
$this->themeProvider = $themeProvider; $this->themeProvider = $themeProvider;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -165,17 +172,17 @@ class PaymentMethods extends AbstractHelper ...@@ -165,17 +172,17 @@ class PaymentMethods extends AbstractHelper
return []; return [];
} }
$currencyCode = $this->getCurrentCurrencyCode($store); $amountCurrency = $this->chargedCurrency->getQuoteAmountCurrency($quote);
$adyFields = [ $adyFields = [
"channel" => "Web", "channel" => "Web",
"merchantAccount" => $merchantAccount, "merchantAccount" => $merchantAccount,
"countryCode" => $this->getCurrentCountryCode($store, $country), "countryCode" => $this->getCurrentCountryCode($store, $country),
"amount" => [ "amount" => [
"currency" => $currencyCode, "currency" => $amountCurrency->getCurrencyCode(),
"value" => $this->adyenHelper->formatAmount( "value" => $this->adyenHelper->formatAmount(
$this->getCurrentPaymentAmount(), $this->getCurrentPaymentAmount(),
$currencyCode $amountCurrency->getCurrencyCode()
), ),
], ],
"shopperReference" => $this->getCurrentShopperReference(), "shopperReference" => $this->getCurrentShopperReference(),
...@@ -257,9 +264,9 @@ class PaymentMethods extends AbstractHelper ...@@ -257,9 +264,9 @@ class PaymentMethods extends AbstractHelper
*/ */
protected function getCurrentPaymentAmount() protected function getCurrentPaymentAmount()
{ {
$grandTotal = $this->getQuote()->getGrandTotal(); $total = $this->chargedCurrency->getQuoteAmountCurrency($this->getQuote())->getAmount();
if (!is_numeric($grandTotal)) { if (!is_numeric($total)) {
throw new \Exception( throw new \Exception(
sprintf( sprintf(
'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a numeric value.', 'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a numeric value.',
...@@ -268,30 +275,21 @@ class PaymentMethods extends AbstractHelper ...@@ -268,30 +275,21 @@ class PaymentMethods extends AbstractHelper
); );
} }
$grandTotal = (float) $grandTotal; $total = (float)$total;
if ($grandTotal > 0) { if ($total > 0) {
return $grandTotal; return $total;
} }
throw new \Exception( throw new \Exception(
sprintf( sprintf(
'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a float > `0`, got `%f`.', 'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a float > `0`, got `%f`.',
$this->getQuote()->getEntityId(), $this->getQuote()->getEntityId(),
$grandTotal $total
) )
); );
} }
/**
* @param $store
* @return mixed
*/
protected function getCurrentCurrencyCode($store)
{
return $this->getQuote()->getQuoteCurrencyCode() ?: $store->getBaseCurrencyCode();
}
/** /**
* @param $store * @param $store
* @return int|mixed|string * @return int|mixed|string
......
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2020 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Model;
class AdyenAmountCurrency
{
protected $amount;
protected $discountAmount;
protected $taxAmount;
protected $currencyCode;
protected $amountDue;
public function __construct($amount, $currencyCode, $discountAmount = 0, $taxAmount = 0, $amountDue = 0)
{
$this->amount = $amount;
$this->currencyCode = $currencyCode;
$this->discountAmount = $discountAmount;
$this->taxAmount = $taxAmount;
$this->amountDue = $amountDue;
}
public function getAmount()
{
return $this->amount;
}
public function getCurrencyCode()
{
return $this->currencyCode;
}
public function getDiscountAmount()
{
return $this->discountAmount;
}
public function getTaxAmount()
{
return $this->taxAmount;
}
public function getAmountDue()
{
return $this->amountDue;
}
}
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
namespace Adyen\Payment\Model; namespace Adyen\Payment\Model;
use Adyen\Payment\Api\AdyenInitiateTerminalApiInterface; use Adyen\Payment\Api\AdyenInitiateTerminalApiInterface;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider; use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Magento\Quote\Model\Quote; use Magento\Quote\Model\Quote;
...@@ -60,6 +61,11 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -60,6 +61,11 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
*/ */
protected $productMetadata; protected $productMetadata;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* AdyenInitiateTerminalApi constructor. * AdyenInitiateTerminalApi constructor.
* *
...@@ -69,6 +75,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -69,6 +75,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
* @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
* @param array $data * @param array $data
* @param ChargedCurrency $chargedCurrency
* @throws \Adyen\AdyenException * @throws \Adyen\AdyenException
*/ */
public function __construct( public function __construct(
...@@ -77,12 +84,14 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -77,12 +84,14 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
\Magento\Checkout\Model\Session $checkoutSession, \Magento\Checkout\Model\Session $checkoutSession,
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ProductMetadataInterface $productMetadata, \Magento\Framework\App\ProductMetadataInterface $productMetadata,
ChargedCurrency $chargedCurrency,
array $data = [] array $data = []
) { ) {
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->adyenLogger = $adyenLogger; $this->adyenLogger = $adyenLogger;
$this->checkoutSession = $checkoutSession; $this->checkoutSession = $checkoutSession;
$this->productMetadata = $productMetadata; $this->productMetadata = $productMetadata;
$this->chargedCurrency = $chargedCurrency;
$this->storeId = $storeManager->getStore()->getId(); $this->storeId = $storeManager->getStore()->getId();
// initialize client // initialize client
...@@ -124,6 +133,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -124,6 +133,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
$quote = $this->checkoutSession->getQuote(); $quote = $this->checkoutSession->getQuote();
$payment = $quote->getPayment(); $payment = $quote->getPayment();
$adyenAmountCurrency = $this->chargedCurrency->getQuoteAmountCurrency($quote);
$payment->setMethod(AdyenPosCloudConfigProvider::CODE); $payment->setMethod(AdyenPosCloudConfigProvider::CODE);
$reference = $quote->reserveOrderId()->getReservedOrderId(); $reference = $quote->reserveOrderId()->getReservedOrderId();
...@@ -162,8 +172,8 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -162,8 +172,8 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
[ [
'AmountsReq' => 'AmountsReq' =>
[ [
'Currency' => $quote->getCurrency()->getQuoteCurrencyCode(), 'Currency' => $adyenAmountCurrency->getCurrencyCode(),
'RequestedAmount' => doubleval($quote->getGrandTotal()) 'RequestedAmount' => doubleval($adyenAmountCurrency->getAmount())
] ]
] ]
] ]
......
...@@ -23,12 +23,14 @@ ...@@ -23,12 +23,14 @@
namespace Adyen\Payment\Model; namespace Adyen\Payment\Model;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Vault; use Adyen\Payment\Helper\Vault;
use Adyen\Payment\Model\Ui\AdyenCcConfigProvider; use Adyen\Payment\Model\Ui\AdyenCcConfigProvider;
use Adyen\Payment\Model\Ui\AdyenHppConfigProvider; use Adyen\Payment\Model\Ui\AdyenHppConfigProvider;
use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Encryption\EncryptorInterface; use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Webapi\Exception; use Magento\Framework\Webapi\Exception;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderSender; use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender; use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Framework\App\Area; use Magento\Framework\App\Area;
...@@ -159,6 +161,21 @@ class Cron ...@@ -159,6 +161,21 @@ class Cron
*/ */
protected $_value; protected $_value;
/**
* @var
*/
protected $_currency;
/**
* @var
*/
protected $orderAmount;
/**
* @var
*/
protected $orderCurrency;
/** /**
* @var * @var
*/ */
...@@ -269,6 +286,11 @@ class Cron ...@@ -269,6 +286,11 @@ class Cron
*/ */
protected $encryptor; protected $encryptor;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* Cron constructor. * Cron constructor.
* *
...@@ -300,6 +322,7 @@ class Cron ...@@ -300,6 +322,7 @@ class Cron
* @param PaymentTokenFactoryInterface $paymentTokenFactory * @param PaymentTokenFactoryInterface $paymentTokenFactory
* @param PaymentTokenRepositoryInterface $paymentTokenRepository * @param PaymentTokenRepositoryInterface $paymentTokenRepository
* @param EncryptorInterface $encryptor * @param EncryptorInterface $encryptor
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct( public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
...@@ -329,7 +352,8 @@ class Cron ...@@ -329,7 +352,8 @@ class Cron
PaymentTokenManagement $paymentTokenManagement, PaymentTokenManagement $paymentTokenManagement,
PaymentTokenFactoryInterface $paymentTokenFactory, PaymentTokenFactoryInterface $paymentTokenFactory,
PaymentTokenRepositoryInterface $paymentTokenRepository, PaymentTokenRepositoryInterface $paymentTokenRepository,
EncryptorInterface $encryptor EncryptorInterface $encryptor,
ChargedCurrency $chargedCurrency
) { ) {
$this->_scopeConfig = $scopeConfig; $this->_scopeConfig = $scopeConfig;
$this->_adyenLogger = $adyenLogger; $this->_adyenLogger = $adyenLogger;
...@@ -359,6 +383,7 @@ class Cron ...@@ -359,6 +383,7 @@ class Cron
$this->paymentTokenFactory = $paymentTokenFactory; $this->paymentTokenFactory = $paymentTokenFactory;
$this->paymentTokenRepository = $paymentTokenRepository; $this->paymentTokenRepository = $paymentTokenRepository;
$this->encryptor = $encryptor; $this->encryptor = $encryptor;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -626,6 +651,7 @@ class Cron ...@@ -626,6 +651,7 @@ class Cron
$this->_paymentMethod = $notification->getPaymentMethod(); $this->_paymentMethod = $notification->getPaymentMethod();
$this->_reason = $notification->getReason(); $this->_reason = $notification->getReason();
$this->_value = $notification->getAmountValue(); $this->_value = $notification->getAmountValue();
$this->_currency = $notification->getAmountCurrency();
$this->_live = $notification->getLive(); $this->_live = $notification->getLive();
$additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize( $additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize(
...@@ -683,6 +709,20 @@ class Cron ...@@ -683,6 +709,20 @@ class Cron
$this->ratepayDescriptor = $ratepayDescriptor; $this->ratepayDescriptor = $ratepayDescriptor;
} }
} }
$this->declareOrderVariables();
}
/**
* Declare private variables with order charged amount and currency
*
* @return void
*/
private function declareOrderVariables()
{
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($this->_order, false);
$this->orderAmount = $orderAmountCurrency->getAmount();
$this->orderCurrency = $orderAmountCurrency->getCurrencyCode();
} }
/** /**
...@@ -712,17 +752,15 @@ class Cron ...@@ -712,17 +752,15 @@ class Cron
$success = (!empty($this->_reason)) ? "$successResult <br />reason:$this->_reason" : $successResult; $success = (!empty($this->_reason)) ? "$successResult <br />reason:$this->_reason" : $successResult;
if ($this->_eventCode == Notification::REFUND || $this->_eventCode == Notification::CAPTURE) { if ($this->_eventCode == Notification::REFUND || $this->_eventCode == Notification::CAPTURE) {
$currency = $this->_order->getOrderCurrencyCode();
// check if it is a full or partial refund // check if it is a full or partial refund
$amount = $this->_value; $amount = $this->_value;
$orderAmount = (int)$this->_adyenHelper->formatAmount($this->_order->getGrandTotal(), $currency); $formattedOrderAmount = (int)$this->_adyenHelper->formatAmount($this->orderAmount, $this->orderCurrency);
$this->_adyenLogger->addAdyenNotificationCronjob( $this->_adyenLogger->addAdyenNotificationCronjob(
'amount notification:' . $amount . ' amount order:' . $orderAmount 'amount notification:' . $amount . ' amount order:' . $formattedOrderAmount
); );
if ($amount == $orderAmount) { if ($amount == $formattedOrderAmount) {
$this->_order->setData( $this->_order->setData(
'adyen_notification_event_code', 'adyen_notification_event_code',
$this->_eventCode . " : " . strtoupper($successResult) $this->_eventCode . " : " . strtoupper($successResult)
...@@ -1371,9 +1409,8 @@ class Cron ...@@ -1371,9 +1409,8 @@ class Cron
->getFirstItem(); ->getFirstItem();
if ($orderPayment->getId() > 0) { if ($orderPayment->getId() > 0) {
$currency = $this->_order->getOrderCurrencyCode(); $amountRefunded = $orderPayment->getTotalRefunded() +
$amountRefunded = $amountRefunded = $orderPayment->getTotalRefunded() + $this->_adyenHelper->originalAmount($this->_value, $this->_currency);
$this->_adyenHelper->originalAmount($this->_value, $currency);
$orderPayment->setUpdatedAt(new \DateTime()); $orderPayment->setUpdatedAt(new \DateTime());
$orderPayment->setTotalRefunded($amountRefunded); $orderPayment->setTotalRefunded($amountRefunded);
$orderPayment->save(); $orderPayment->save();
...@@ -1394,8 +1431,7 @@ class Cron ...@@ -1394,8 +1431,7 @@ class Cron
// refund is done through adyen backoffice so create a credit memo // refund is done through adyen backoffice so create a credit memo
$order = $this->_order; $order = $this->_order;
if ($order->canCreditmemo()) { if ($order->canCreditmemo()) {
$currency = $this->_order->getOrderCurrencyCode(); $amount = $this->_adyenHelper->originalAmount($this->_value, $this->_currency);
$amount = $this->_adyenHelper->originalAmount($this->_value, $currency);
$order->getPayment()->registerRefundNotification($amount); $order->getPayment()->registerRefundNotification($amount);
$this->_adyenLogger->addAdyenNotificationCronjob('Created credit memo for order'); $this->_adyenLogger->addAdyenNotificationCronjob('Created credit memo for order');
...@@ -1569,8 +1605,7 @@ class Cron ...@@ -1569,8 +1605,7 @@ class Cron
} }
// validate if amount is total amount // validate if amount is total amount
$orderCurrencyCode = $this->_order->getOrderCurrencyCode(); $amount = $this->_adyenHelper->originalAmount($this->_value, $this->_currency);
$amount = $this->_adyenHelper->originalAmount($this->_value, $orderCurrencyCode);
try { try {
// add to order payment // add to order payment
...@@ -1594,7 +1629,7 @@ class Cron ...@@ -1594,7 +1629,7 @@ class Cron
); );
} }
if ($this->_isTotalAmount($paymentObj->getEntityId(), $orderCurrencyCode)) { if ($this->_isTotalAmount($paymentObj->getEntityId(), $this->orderCurrency)) {
$this->_createInvoice(); $this->_createInvoice();
} else { } else {
$this->_adyenLogger->addAdyenNotificationCronjob( $this->_adyenLogger->addAdyenNotificationCronjob(
...@@ -1831,7 +1866,10 @@ class Cron ...@@ -1831,7 +1866,10 @@ class Cron
); );
// get total amount of the order // get total amount of the order
$grandTotal = (int)$this->_adyenHelper->formatAmount($this->_order->getGrandTotal(), $orderCurrencyCode); $grandTotal = (int)$this->_adyenHelper->formatAmount(
$this->orderAmount,
$this->orderCurrency
);
// check if total amount of the order is authorised // check if total amount of the order is authorised
$res = $this->_adyenOrderPaymentCollectionFactory $res = $this->_adyenOrderPaymentCollectionFactory
...@@ -1840,16 +1878,16 @@ class Cron ...@@ -1840,16 +1878,16 @@ class Cron
if ($res && isset($res[0]) && is_array($res[0])) { if ($res && isset($res[0]) && is_array($res[0])) {
$amount = $res[0]['total_amount']; $amount = $res[0]['total_amount'];
$orderAmount = $this->_adyenHelper->formatAmount($amount, $orderCurrencyCode); $formattedOrderAmount = $this->_adyenHelper->formatAmount($amount, $orderCurrencyCode);
$this->_adyenLogger->addAdyenNotificationCronjob( $this->_adyenLogger->addAdyenNotificationCronjob(
sprintf( sprintf(
'The grandtotal amount is %s and the total order amount that is authorised is: %s', 'The grandtotal amount is %s and the total order amount that is authorised is: %s',
$grandTotal, $grandTotal,
$orderAmount $formattedOrderAmount
) )
); );
if ($grandTotal == $orderAmount) { if ($grandTotal == $formattedOrderAmount) {
$this->_adyenLogger->addAdyenNotificationCronjob('AUTHORISATION has the full amount'); $this->_adyenLogger->addAdyenNotificationCronjob('AUTHORISATION has the full amount');
return true; return true;
} else { } else {
...@@ -1955,14 +1993,13 @@ class Cron ...@@ -1955,14 +1993,13 @@ class Cron
$this->_adyenLogger->addAdyenNotificationCronjob('Set order to authorised'); $this->_adyenLogger->addAdyenNotificationCronjob('Set order to authorised');
// if full amount is captured create invoice // if full amount is captured create invoice
$currency = $this->_order->getOrderCurrencyCode();
$amount = $this->_value; $amount = $this->_value;
$orderAmount = (int)$this->_adyenHelper->formatAmount($this->_order->getGrandTotal(), $currency); $formattedOrderAmount = (int)$this->_adyenHelper->formatAmount($this->orderAmount, $this->orderCurrency);
// create invoice for the capture notification if you are on manual capture // create invoice for the capture notification if you are on manual capture
if ($createInvoice == true && $amount == $orderAmount) { if ($createInvoice && $amount == $formattedOrderAmount) {
$this->_adyenLogger->addAdyenNotificationCronjob( $this->_adyenLogger->addAdyenNotificationCronjob(
'amount notification:' . $amount . ' amount order:' . $orderAmount 'amount notification:' . $amount . ' amount order:' . $formattedOrderAmount
); );
$this->_createInvoice(); $this->_createInvoice();
} }
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
namespace Adyen\Payment\Model\Ui; namespace Adyen\Payment\Model\Ui;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Checkout\Model\ConfigProviderInterface; use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Payment\Helper\Data as PaymentHelper; use Magento\Payment\Helper\Data as PaymentHelper;
...@@ -60,6 +61,11 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface ...@@ -60,6 +61,11 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
*/ */
private $storeManager; private $storeManager;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* AdyenGooglePayConfigProvider constructor. * AdyenGooglePayConfigProvider constructor.
* *
...@@ -67,6 +73,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface ...@@ -67,6 +73,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Framework\App\RequestInterface $request * @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\UrlInterface $urlBuilder * @param \Magento\Framework\UrlInterface $urlBuilder
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct( public function __construct(
PaymentHelper $paymentHelper, PaymentHelper $paymentHelper,
...@@ -74,7 +81,8 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface ...@@ -74,7 +81,8 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
\Magento\Framework\App\RequestInterface $request, \Magento\Framework\App\RequestInterface $request,
\Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\UrlInterface $urlBuilder,
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Checkout\Model\Session $checkoutSession \Magento\Checkout\Model\Session $checkoutSession,
ChargedCurrency $chargedCurrency
) { ) {
$this->paymentHelper = $paymentHelper; $this->paymentHelper = $paymentHelper;
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
...@@ -82,6 +90,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface ...@@ -82,6 +90,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
$this->urlBuilder = $urlBuilder; $this->urlBuilder = $urlBuilder;
$this->storeManager = $storeManager; $this->storeManager = $storeManager;
$this->checkoutSession = $checkoutSession; $this->checkoutSession = $checkoutSession;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -119,7 +128,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface ...@@ -119,7 +128,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
); );
$quote = $this->checkoutSession->getQuote(); $quote = $this->checkoutSession->getQuote();
$currency = $quote->getCurrency(); $currency = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getCurrencyCode();
$adyenGooglePayConfig['format'] = $this->adyenHelper->decimalNumbers($currency); $adyenGooglePayConfig['format'] = $this->adyenHelper->decimalNumbers($currency);
$adyenGooglePayConfig['merchantIdentifier'] = $this->adyenHelper->getAdyenGooglePayMerchantIdentifier($this->storeManager->getStore()->getId()); $adyenGooglePayConfig['merchantIdentifier'] = $this->adyenHelper->getAdyenGooglePayMerchantIdentifier($this->storeManager->getStore()->getId());
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
namespace Adyen\Payment\Model\Ui; namespace Adyen\Payment\Model\Ui;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Checkout\Model\ConfigProviderInterface; use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Payment\Helper\Data as PaymentHelper; use Magento\Payment\Helper\Data as PaymentHelper;
...@@ -70,6 +71,11 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface ...@@ -70,6 +71,11 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
*/ */
private $ccConfig; private $ccConfig;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/** /**
* AdyenOneclickConfigProvider constructor. * AdyenOneclickConfigProvider constructor.
* *
...@@ -80,6 +86,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface ...@@ -80,6 +86,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
* @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\UrlInterface $urlBuilder * @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Payment\Model\CcConfig $ccConfig * @param \Magento\Payment\Model\CcConfig $ccConfig
* @param ChargedCurrency $chargedCurrency
*/ */
public function __construct( public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper, \Adyen\Payment\Helper\Data $adyenHelper,
...@@ -88,7 +95,8 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface ...@@ -88,7 +95,8 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
\Magento\Checkout\Model\Session $session, \Magento\Checkout\Model\Session $session,
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\UrlInterface $urlBuilder,
\Magento\Payment\Model\CcConfig $ccConfig \Magento\Payment\Model\CcConfig $ccConfig,
ChargedCurrency $chargedCurrency
) { ) {
$this->_adyenHelper = $adyenHelper; $this->_adyenHelper = $adyenHelper;
$this->_request = $request; $this->_request = $request;
...@@ -97,6 +105,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface ...@@ -97,6 +105,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
$this->_storeManager = $storeManager; $this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder; $this->_urlBuilder = $urlBuilder;
$this->ccConfig = $ccConfig; $this->ccConfig = $ccConfig;
$this->chargedCurrency = $chargedCurrency;
} }
/** /**
...@@ -178,7 +187,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface ...@@ -178,7 +187,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
if ($this->_customerSession->isLoggedIn()) { if ($this->_customerSession->isLoggedIn()) {
$customerId = $this->_customerSession->getCustomerId(); $customerId = $this->_customerSession->getCustomerId();
$storeId = $this->_storeManager->getStore()->getId(); $storeId = $this->_storeManager->getStore()->getId();
$grandTotal = $this->_getQuote()->getGrandTotal(); $grandTotal = $this->chargedCurrency->getQuoteAmountCurrency($this->_getQuote())->getAmount();
$recurringType = $this->_getRecurringContractType(); $recurringType = $this->_getRecurringContractType();
$billingAgreements = $this->_adyenHelper->getOneClickPaymentMethods( $billingAgreements = $this->_adyenHelper->getOneClickPaymentMethods(
......
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment Module
*
* Copyright (c) 2020 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Adyen\Payment\Helper\Config;
class AdyenSalesOrderChargedCurrencyObserver implements ObserverInterface
{
/**
* @var Config $config
*/
private $config;
public function __construct(
Config $config
) {
$this->config = $config;
}
public function execute(Observer $observer)
{
/** @var \Magento\Sales\Model\Order $order */
$order = $observer->getEvent()->getOrder();
$paymentMethod = $order->getPayment()->getMethod();
if (strpos($paymentMethod, 'adyen_') !== false) {
$order->setAdyenChargedCurrency($this->config->getChargedCurrency($order->getStoreId()));
}
}
}
...@@ -71,6 +71,10 @@ class UpgradeSchema implements UpgradeSchemaInterface ...@@ -71,6 +71,10 @@ class UpgradeSchema implements UpgradeSchemaInterface
$this->updateSchemaVersion540($setup); $this->updateSchemaVersion540($setup);
} }
if (version_compare($context->getVersion(), '6.7.0', '<')) {
$this->updateSchemaVersion670($setup);
}
$setup->endSetup(); $setup->endSetup();
} }
...@@ -402,4 +406,33 @@ class UpgradeSchema implements UpgradeSchemaInterface ...@@ -402,4 +406,33 @@ class UpgradeSchema implements UpgradeSchemaInterface
$adyenNotificationErrorMessageColumn $adyenNotificationErrorMessageColumn
); );
} }
/**
* Upgrade to 6.7.0
*
* New sales_order column for the currency charged based on the Adyen config option (base or display)
*
* @param SchemaSetupInterface $setup
* @return void
*/
public function updateSchemaVersion670(SchemaSetupInterface $setup)
{
$connection = $setup->getConnection();
$tableName = $setup->getTable('sales_order');
$adyenChargedCurrencyColumn = [
'type' => Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'default' => null,
'comment' => 'Charged currency depending on Adyen config option',
'after' => 'adyen_notification_event_code_success'
];
$connection->addColumn(
$tableName,
'adyen_charged_currency',
$adyenChargedCurrencyColumn
);
}
} }
...@@ -50,6 +50,7 @@ class AdyenInitiateTerminalApiTest extends \PHPUnit\Framework\TestCase ...@@ -50,6 +50,7 @@ class AdyenInitiateTerminalApiTest extends \PHPUnit\Framework\TestCase
$checkoutSession = $this->getSimpleMock(\Magento\Checkout\Model\Session::class); $checkoutSession = $this->getSimpleMock(\Magento\Checkout\Model\Session::class);
$storeManager = $this->getSimpleMock(\Magento\Store\Model\StoreManagerInterface::class); $storeManager = $this->getSimpleMock(\Magento\Store\Model\StoreManagerInterface::class);
$productMetadata = $this->getSimpleMock(\Magento\Framework\App\ProductMetadataInterface::class); $productMetadata = $this->getSimpleMock(\Magento\Framework\App\ProductMetadataInterface::class);
$chargedCurrency = $this->getSimpleMock(\Adyen\Payment\Helper\ChargedCurrency::class);
$store = $this->getSimpleMock(\Magento\Store\Api\Data\StoreInterface::class); $store = $this->getSimpleMock(\Magento\Store\Api\Data\StoreInterface::class);
$storeManager->method('getStore') $storeManager->method('getStore')
...@@ -82,7 +83,8 @@ class AdyenInitiateTerminalApiTest extends \PHPUnit\Framework\TestCase ...@@ -82,7 +83,8 @@ class AdyenInitiateTerminalApiTest extends \PHPUnit\Framework\TestCase
$adyenLogger, $adyenLogger,
$checkoutSession, $checkoutSession,
$storeManager, $storeManager,
$productMetadata $productMetadata,
$chargedCurrency
); );
} }
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
<enable_recurring>0</enable_recurring> <enable_recurring>0</enable_recurring>
<group>adyen</group> <group>adyen</group>
<notifications_can_cancel>1</notifications_can_cancel> <notifications_can_cancel>1</notifications_can_cancel>
<charged_currency>display</charged_currency>
</adyen_abstract> </adyen_abstract>
<adyen_cc> <adyen_cc>
<active>1</active> <active>1</active>
......
...@@ -44,4 +44,7 @@ ...@@ -44,4 +44,7 @@
<event name="payment_method_assign_data_adyen_google_pay"> <event name="payment_method_assign_data_adyen_google_pay">
<observer name="adyen_google_pay_gateway_data_assign" instance="Adyen\Payment\Observer\AdyenGooglePayDataAssignObserver" /> <observer name="adyen_google_pay_gateway_data_assign" instance="Adyen\Payment\Observer\AdyenGooglePayDataAssignObserver" />
</event> </event>
<event name="sales_model_service_quote_submit_before">
<observer name="adyen_sales_order_charged_currency" instance="Adyen\Payment\Observer\AdyenSalesOrderChargedCurrencyObserver" />
</event>
</config> </config>
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
--> -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Adyen_Payment" setup_version="5.4.0"> <module name="Adyen_Payment" setup_version="7.0.0">
<sequence> <sequence>
<module name="Magento_Sales"/> <module name="Magento_Sales"/>
<module name="Magento_Quote"/> <module name="Magento_Quote"/>
......
...@@ -61,15 +61,16 @@ $_isDemoMode = $block->isDemoMode(); ...@@ -61,15 +61,16 @@ $_isDemoMode = $block->isDemoMode();
<?php <?php
$banktranferData = $block->getBankTransferData(); $banktranferData = $block->getBankTransferData();
$order = $block->getOrder(); $order = $block->getOrder();
$orderAmountCurrency = $block->getOrderAmountCurrency();
?> ?>
<div> <div>
<table class="edit-order-table" style="text-align:left; margin-top:5px; margin-bottom:0;"> <table class="edit-order-table" style="text-align:left; margin-top:5px; margin-bottom:0;">
<tbody> <tbody>
<?php if (!empty($order->getGrandTotal())): ?> <?php if (!empty($orderAmountCurrency->getAmount())): ?>
<tr> <tr>
<th scope="row"><?= $block->escapeHtml(__('Amount')); ?></th> <th scope="row"><?= $block->escapeHtml(__('Amount')); ?></th>
<td><?= /* @noEscape */ $order->formatPrice($order->getGrandTotal()); ?></td> <td><?= /* @noEscape */ $order->formatPrice($orderAmountCurrency->getAmount()); ?></td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
......
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
<?php <?php
$banktranferData = $block->getBankTransferData(); $banktranferData = $block->getBankTransferData();
$order = $block->getOrder(); $order = $block->getOrder();
$orderAmountCurrency=$block->geAdyenAmountCurrency();
?> ?>
<h2><?= $block->escapeHtml(__('Pay using Bank transfer')); ?></h2> <h2><?= $block->escapeHtml(__('Pay using Bank transfer')); ?></h2>
<p><?= $block->escapeHtml( <p><?= $block->escapeHtml(
...@@ -69,11 +70,11 @@ ...@@ -69,11 +70,11 @@
); ?></p> ); ?></p>
<table> <table>
<tbody> <tbody>
<?php if (!empty($order->getGrandTotal())): ?> <?php if (!empty($orderAmountCurrency->getAmount())): ?>
<tr> <tr>
<th scope="row"><?= $block->escapeHtml(__('Amount')); ?></th> <th scope="row"><?= $block->escapeHtml(__('Amount')); ?></th>
<td><?= /* @noEscape */ <td><?= /* @noEscape */
$order->formatPrice($order->getGrandTotal()); ?></td> $order->formatPrice($orderAmountCurrency->getAmount()); ?></td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment