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 @@
namespace Adyen\Payment\Block\Checkout;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Model\AdyenAmountCurrency;
/**
* Billing agreement information on Order success page
*/
......@@ -55,6 +58,11 @@ class Success extends \Magento\Framework\View\Element\Template
*/
protected $storeManager;
/**
* @var ChargedCurrency
*/
protected $chargedCurrency;
/**
* Success constructor.
*
......@@ -71,6 +79,7 @@ class Success extends \Magento\Framework\View\Element\Template
\Magento\Framework\Pricing\Helper\Data $priceHelper,
\Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
ChargedCurrency $chargedCurrency,
array $data = []
) {
$this->checkoutSession = $checkoutSession;
......@@ -78,6 +87,7 @@ class Success extends \Magento\Framework\View\Element\Template
$this->priceHelper = $priceHelper;
$this->adyenHelper = $adyenHelper;
$this->storeManager = $storeManager;
$this->chargedCurrency = $chargedCurrency;
parent::__construct($context, $data);
}
......@@ -212,4 +222,12 @@ class Success extends \Magento\Framework\View\Element\Template
}
return $this->order;
}
/**
* @return AdyenAmountCurrency
*/
public function geAdyenAmountCurrency()
{
return $this->chargedCurrency->getOrderAmountCurrency($this->order, false);
}
}
......@@ -23,6 +23,8 @@
namespace Adyen\Payment\Block\Form;
use Adyen\Payment\Helper\ChargedCurrency;
class Oneclick extends \Adyen\Payment\Block\Form\Cc
{
/**
......@@ -35,6 +37,11 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
*/
protected $_sessionQuote;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* Oneclick constructor.
*
......@@ -42,6 +49,8 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
* @param \Magento\Payment\Model\Config $paymentConfig
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
* @param ChargedCurrency $chargedCurrency
* @param array $data
*/
public function __construct(
......@@ -50,10 +59,12 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
\Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Backend\Model\Session\Quote $sessionQuote,
ChargedCurrency $chargedCurrency,
array $data = []
) {
parent::__construct($context, $paymentConfig, $adyenHelper, $checkoutSession, $data);
$this->_sessionQuote = $sessionQuote;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -63,7 +74,8 @@ class Oneclick extends \Adyen\Payment\Block\Form\Cc
{
$customerId = $this->_sessionQuote->getCustomerId();
$storeId = $this->_sessionQuote->getStoreId();
$grandTotal = $this->_sessionQuote->getQuote()->getGrandTotal();
$grandTotal = $this->chargedCurrency->getQuoteAmountCurrency($this->_sessionQuote->getQuote())->getAmount();
// For backend only allow recurring payments
$recurringType = \Adyen\Payment\Model\RecurringType::RECURRING;
......
......@@ -23,8 +23,28 @@
namespace Adyen\Payment\Block\Info;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Framework\View\Element\Template;
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
*/
......@@ -75,6 +95,14 @@ class Hpp extends AbstractInfo
return $this->getInfo()->getOrder();
}
/**
* @return \Adyen\Payment\Model\AdyenAmountCurrency
*/
public function getOrderAmountCurrency()
{
return $this->chargedCurrency->getOrderAmountCurrency($this->getInfo()->getOrder(), false);
}
/**
* @return null
* @throws \Magento\Framework\Exception\LocalizedException
......
......@@ -23,6 +23,7 @@
namespace Adyen\Payment\Block\Info;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Framework\View\Element\Template;
class PaymentLink extends AbstractInfo
......@@ -37,16 +38,23 @@ class PaymentLink extends AbstractInfo
*/
private $payByMail;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $adyenOrderPaymentCollectionFactory,
Template\Context $context,
\Magento\Framework\Registry $registry,
\Adyen\Payment\Gateway\Command\PayByMailCommand $payByMailCommand,
ChargedCurrency $chargedCurrency,
array $data = []
) {
$this->registry = $registry;
$this->payByMail = $payByMailCommand;
$this->chargedCurrency = $chargedCurrency;
parent::__construct($adyenHelper, $adyenOrderPaymentCollectionFactory, $context, $data);
}
......@@ -74,7 +82,8 @@ class PaymentLink extends AbstractInfo
*/
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
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
* @return string
......
......@@ -23,7 +23,7 @@
namespace Adyen\Payment\Gateway\Command;
use Magento\Payment\Gateway\Command;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\CommandInterface;
class PayByMailCommand implements CommandInterface
......@@ -38,19 +38,26 @@ class PayByMailCommand implements CommandInterface
*/
protected $_adyenLogger;
/**
* @var ChargedCurrency
*/
protected $chargedCurrency;
/**
* PayByMailCommand constructor.
*
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Framework\Locale\ResolverInterface $resolver
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param ChargedCurrency $chargedCurrency
*/
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
ChargedCurrency $chargedCurrency
) {
$this->_adyenHelper = $adyenHelper;
$this->_adyenLogger = $adyenLogger;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -127,7 +134,6 @@ class PayByMailCommand implements CommandInterface
$order = $payment->getOrder();
$realOrderId = $order->getRealOrderId();
$orderCurrencyCode = $order->getOrderCurrencyCode();
$storeId = $order->getStore()->getId();
// check if paybymail has it's own skin
......@@ -143,10 +149,15 @@ class PayByMailCommand implements CommandInterface
$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(
$paymentAmount ?: $order->getGrandTotal(),
$paymentAmount ?: $adyenAmount->getAmount(),
$orderCurrencyCode
);
$merchantAccount = trim($this->_adyenHelper->getAdyenAbstractConfigData('merchant_account', $storeId));
$shopperEmail = $order->getCustomerEmail();
$customerId = $order->getCustomerId();
......
......@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface;
/**
......@@ -35,14 +36,23 @@ class CaptureDataBuilder implements BuilderInterface
*/
private $adyenHelper;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* CaptureDataBuilder constructor.
*
* @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->chargedCurrency = $chargedCurrency;
}
/**
......@@ -55,13 +65,12 @@ class CaptureDataBuilder implements BuilderInterface
{
/** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
$paymentDataObject = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
$amount = \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject);
$payment = $paymentDataObject->getPayment();
$order = $payment->getOrder();
$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);
$modificationAmount = ['currency' => $currency, 'value' => $amount];
......@@ -93,27 +102,32 @@ class CaptureDataBuilder implements BuilderInterface
{
$formFields = [];
$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
$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()) {
continue;
}
++$count;
$itemAmountCurrency = $this->chargedCurrency->getInvoiceItemAmountCurrency($invoiceItem);
$numberOfItems = (int)$invoiceItem->getQty();
$formFields = $this->adyenHelper->createOpenInvoiceLineItem(
$formFields,
$count,
$invoiceItem->getName(),
$invoiceItem->getPrice(),
$itemAmountCurrency->getAmount(),
$currency,
$invoiceItem->getTaxAmount(),
$invoiceItem->getPriceInclTax(),
$itemAmountCurrency->getTaxAmount(),
$itemAmountCurrency->getAmount() + $itemAmountCurrency->getTaxAmount(),
$invoiceItem->getOrderItem()->getTaxPercent(),
$numberOfItems,
$payment,
......@@ -124,13 +138,14 @@ class CaptureDataBuilder implements BuilderInterface
// Shipping cost
if ($latestInvoice->getShippingAmount() > 0) {
++$count;
$adyenInvoiceShippingAmount = $this->chargedCurrency->getInvoiceShippingAmountCurrency($latestInvoice);
$formFields = $this->adyenHelper->createOpenInvoiceLineShipping(
$formFields,
$count,
$payment->getOrder(),
$latestInvoice->getShippingAmount(),
$latestInvoice->getShippingTaxAmount(),
$currency,
$order,
$adyenInvoiceShippingAmount->getAmount(),
$adyenInvoiceShippingAmount->getTaxAmount(),
$adyenInvoiceShippingAmount->getCurrencyCode(),
$payment
);
}
......
......@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Adyen\Payment\Observer\AdyenHppDataAssignObserver;
......@@ -48,6 +49,11 @@ class CheckoutDataBuilder implements BuilderInterface
*/
private $gender;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
......@@ -58,12 +64,14 @@ class CheckoutDataBuilder implements BuilderInterface
\Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Api\CartRepositoryInterface $cartRepository,
\Adyen\Payment\Model\Gender $gender
\Adyen\Payment\Model\Gender $gender,
ChargedCurrency $chargedCurrency
) {
$this->adyenHelper = $adyenHelper;
$this->storeManager = $storeManager;
$this->cartRepository = $cartRepository;
$this->gender = $gender;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -234,19 +242,27 @@ class CheckoutDataBuilder implements BuilderInterface
/** @var \Magento\Quote\Model\Quote $cart */
$cart = $this->cartRepository->get($order->getQuoteId());
$currency = $cart->getCurrency();
$amountCurrency = $this->chargedCurrency->getOrderAmountCurrency($order);
$currency = $amountCurrency->getCurrencyCode();
$discountAmount = 0;
foreach ($cart->getAllVisibleItems() as $item) {
$numberOfItems = (int)$item->getQty();
$itemAmountCurrency = $this->chargedCurrency->getQuoteItemAmountCurrency($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($taxAmount, $currency);
$formattedTaxAmount = $this->adyenHelper->formatAmount(
$itemAmountCurrency->getTaxAmount(),
$itemAmountCurrency->getCurrencyCode()
);
$formattedTaxPercentage = $item->getTaxPercent() * 100;
$formFields['lineItems'][] = [
......@@ -264,7 +280,7 @@ class CheckoutDataBuilder implements BuilderInterface
// Discount cost
if ($discountAmount != 0) {
$description = __('Discount');
$itemAmount = -$this->adyenHelper->formatAmount($discountAmount, $currency);
$itemAmount = -$this->adyenHelper->formatAmount($discountAmount, $itemAmountCurrency->getCurrencyCode());
$itemVatAmount = "0";
$itemVatPercentage = "0";
$numberOfItems = 1;
......@@ -281,13 +297,15 @@ class CheckoutDataBuilder implements BuilderInterface
}
// Shipping cost
if ($cart->getShippingAddress()->getShippingAmount() > 0 || $cart->getShippingAddress()->getShippingTaxAmount(
) > 0) {
$priceExcludingTax = $cart->getShippingAddress()->getShippingAmount() - $cart->getShippingAddress(
)->getShippingTaxAmount();
if ($cart->getShippingAddress()->getShippingAmount() > 0 ||
$cart->getShippingAddress()->getShippingTaxAmount() > 0
) {
$shippingAmountCurrency = $this->chargedCurrency->getQuoteShippingAmountCurrency($cart);
$priceExcludingTax = $shippingAmountCurrency->getAmount();
$formattedTaxAmount = $this->adyenHelper->formatAmount(
$cart->getShippingAddress()->getShippingTaxAmount(),
$shippingAmountCurrency->getTaxAmount(),
$currency
);
......@@ -296,8 +314,7 @@ class CheckoutDataBuilder implements BuilderInterface
$formattedTaxPercentage = 0;
if ($priceExcludingTax !== 0) {
$formattedTaxPercentage = $cart->getShippingAddress()->getShippingTaxAmount(
) / $priceExcludingTax * 100 * 100;
$formattedTaxPercentage = $shippingAmountCurrency->getTaxAmount() / $priceExcludingTax * 100 * 100;
}
$formFields['lineItems'][] = [
......
......@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface;
class PaymentDataBuilder implements BuilderInterface
......@@ -32,14 +33,23 @@ class PaymentDataBuilder implements BuilderInterface
*/
private $adyenRequestsHelper;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* PaymentDataBuilder constructor.
*
* @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->chargedCurrency = $chargedCurrency;
}
/**
......@@ -55,8 +65,9 @@ class PaymentDataBuilder implements BuilderInterface
$payment = $paymentDataObject->getPayment();
$fullOrder = $payment->getOrder();
$currencyCode = $fullOrder->getOrderCurrencyCode();
$amount = $fullOrder->getGrandTotal();
$amountCurrency = $this->chargedCurrency->getOrderAmountCurrency($fullOrder);
$currencyCode = $amountCurrency->getCurrencyCode();
$amount = $amountCurrency->getAmount();
$reference = $order->getOrderIncrementId();
$paymentMethod = $payment->getMethod();
......
......@@ -23,6 +23,7 @@
namespace Adyen\Payment\Gateway\Request;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Request\BuilderInterface;
/**
......@@ -45,20 +46,28 @@ class RefundDataBuilder implements BuilderInterface
*/
protected $adyenInvoiceCollectionFactory;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* RefundDataBuilder constructor.
*
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory $orderPaymentCollectionFactory
* @param ChargedCurrency $chargedCurrency
*/
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
\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->orderPaymentCollectionFactory = $orderPaymentCollectionFactory;
$this->adyenInvoiceCollectionFactory = $adyenInvoiceCollectionFactory;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -69,16 +78,16 @@ class RefundDataBuilder implements BuilderInterface
{
/** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
$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();
$payment = $paymentDataObject->getPayment();
$pspReference = $payment->getCcTransId();
$currency = $payment->getOrder()->getOrderCurrencyCode();
$orderAmountCurrency = $this->chargedCurrency->getOrderAmountCurrency($payment->getOrder(), false);
$currency = $orderAmountCurrency->getCurrencyCode();
$amount = $orderAmountCurrency->getAmount();
$storeId = $order->getStoreId();
$method = $payment->getMethod();
$merchantAccount = $this->adyenHelper->getAdyenMerchantAccount($method, $storeId);
$grandTotal = $payment->getOrder()->getGrandTotal();
// check if it contains a split payment
$orderPaymentCollection = $this->orderPaymentCollectionFactory
......@@ -101,7 +110,7 @@ class RefundDataBuilder implements BuilderInterface
$orderPaymentCollection->addPaymentFilterDescending($payment->getId());
} elseif ($refundStrategy == "3") {
// refund based on ratio
$ratio = $amount / $grandTotal;
$ratio = $buildSubjectAmount / $amount;
$orderPaymentCollection->addPaymentFilterAscending($payment->getId());
}
......@@ -185,7 +194,9 @@ class RefundDataBuilder implements BuilderInterface
{
$formFields = [];
$count = 0;
$currency = $payment->getOrder()->getOrderCurrencyCode();
$currency = $this->chargedCurrency
->getOrderAmountCurrency($payment->getOrder(), false)
->getCurrencyCode();
/**
* @var \Magento\Sales\Model\Order\Creditmemo $creditMemo
......@@ -194,16 +205,18 @@ class RefundDataBuilder implements BuilderInterface
foreach ($creditMemo->getItems() as $refundItem) {
++$count;
$itemAmountCurrency = $this->chargedCurrency->getCreditMemoItemAmountCurrency($refundItem);
$numberOfItems = (int)$refundItem->getQty();
$formFields = $this->adyenHelper->createOpenInvoiceLineItem(
$formFields,
$count,
$refundItem->getName(),
$refundItem->getPrice(),
$itemAmountCurrency->getAmount(),
$currency,
$refundItem->getTaxAmount(),
$refundItem->getPriceInclTax(),
$itemAmountCurrency->getTaxAmount(),
$itemAmountCurrency->getAmount() + $itemAmountCurrency->getTaxAmount(),
$refundItem->getOrderItem()->getTaxPercent(),
$numberOfItems,
$payment,
......
......@@ -25,6 +25,7 @@
namespace Adyen\Payment\Gateway\Validator;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Payment\Gateway\Validator\AbstractValidator;
class InstallmentValidator extends AbstractValidator
......@@ -49,6 +50,11 @@ class InstallmentValidator extends AbstractValidator
*/
private $quoteRepository;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* InstallmentValidator constructor.
*
......@@ -63,12 +69,14 @@ class InstallmentValidator extends AbstractValidator
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Helper\Data $adyenHelper,
\Magento\Framework\Serialize\SerializerInterface $serializer,
\Magento\Quote\Model\QuoteRepository $quoteRepository
\Magento\Quote\Model\QuoteRepository $quoteRepository,
ChargedCurrency $chargedCurrency
) {
$this->adyenLogger = $adyenLogger;
$this->adyenHelper = $adyenHelper;
$this->serializer = $serializer;
$this->quoteRepository = $quoteRepository;
$this->chargedCurrency = $chargedCurrency;
parent::__construct($resultFactory);
}
......@@ -86,7 +94,7 @@ class InstallmentValidator extends AbstractValidator
}
$installmentsEnabled = $this->adyenHelper->getAdyenCcConfigData('enable_installments');
if ($quote && $installmentsEnabled) {
$grandTotal = $quote->getGrandTotal();
$grandTotal = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getAmount();
$installmentsAvailable = $this->adyenHelper->getAdyenCcConfigData('installments');
$installmentSelected = $payment->getAdditionalInformation('number_of_installments');
$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
const XML_NOTIFICATIONS_IP_CHECK = "notifications_ip_check";
const XML_NOTIFICATIONS_HMAC_KEY_LIVE = "notification_hmac_key_live";
const XML_NOTIFICATIONS_HMAC_KEY_TEST = "notification_hmac_key_test";
const XML_CHARGED_CURRENCY = "charged_currency";
/**
* @var Magento\Framework\App\Config\ScopeConfigInterface
......@@ -153,6 +154,17 @@ class Config
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
*
......
......@@ -90,6 +90,11 @@ class PaymentMethods extends AbstractHelper
*/
protected $quote;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* PaymentMethods constructor.
*
......@@ -116,7 +121,8 @@ class PaymentMethods extends AbstractHelper
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\View\Asset\Source $assetSource,
\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->config = $config;
......@@ -129,6 +135,7 @@ class PaymentMethods extends AbstractHelper
$this->assetSource = $assetSource;
$this->design = $design;
$this->themeProvider = $themeProvider;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -165,17 +172,17 @@ class PaymentMethods extends AbstractHelper
return [];
}
$currencyCode = $this->getCurrentCurrencyCode($store);
$amountCurrency = $this->chargedCurrency->getQuoteAmountCurrency($quote);
$adyFields = [
"channel" => "Web",
"merchantAccount" => $merchantAccount,
"countryCode" => $this->getCurrentCountryCode($store, $country),
"amount" => [
"currency" => $currencyCode,
"currency" => $amountCurrency->getCurrencyCode(),
"value" => $this->adyenHelper->formatAmount(
$this->getCurrentPaymentAmount(),
$currencyCode
$amountCurrency->getCurrencyCode()
),
],
"shopperReference" => $this->getCurrentShopperReference(),
......@@ -257,9 +264,9 @@ class PaymentMethods extends AbstractHelper
*/
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(
sprintf(
'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a numeric value.',
......@@ -268,30 +275,21 @@ class PaymentMethods extends AbstractHelper
);
}
$grandTotal = (float) $grandTotal;
$total = (float)$total;
if ($grandTotal > 0) {
return $grandTotal;
if ($total > 0) {
return $total;
}
throw new \Exception(
sprintf(
'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a float > `0`, got `%f`.',
$this->getQuote()->getEntityId(),
$grandTotal
$total
)
);
}
/**
* @param $store
* @return mixed
*/
protected function getCurrentCurrencyCode($store)
{
return $this->getQuote()->getQuoteCurrencyCode() ?: $store->getBaseCurrencyCode();
}
/**
* @param $store
* @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 @@
namespace Adyen\Payment\Model;
use Adyen\Payment\Api\AdyenInitiateTerminalApiInterface;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Magento\Quote\Model\Quote;
......@@ -60,6 +61,11 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
*/
protected $productMetadata;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* AdyenInitiateTerminalApi constructor.
*
......@@ -69,6 +75,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
* @param array $data
* @param ChargedCurrency $chargedCurrency
* @throws \Adyen\AdyenException
*/
public function __construct(
......@@ -77,12 +84,14 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
ChargedCurrency $chargedCurrency,
array $data = []
) {
$this->adyenHelper = $adyenHelper;
$this->adyenLogger = $adyenLogger;
$this->checkoutSession = $checkoutSession;
$this->productMetadata = $productMetadata;
$this->chargedCurrency = $chargedCurrency;
$this->storeId = $storeManager->getStore()->getId();
// initialize client
......@@ -124,6 +133,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
$quote = $this->checkoutSession->getQuote();
$payment = $quote->getPayment();
$adyenAmountCurrency = $this->chargedCurrency->getQuoteAmountCurrency($quote);
$payment->setMethod(AdyenPosCloudConfigProvider::CODE);
$reference = $quote->reserveOrderId()->getReservedOrderId();
......@@ -162,8 +172,8 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
[
'AmountsReq' =>
[
'Currency' => $quote->getCurrency()->getQuoteCurrencyCode(),
'RequestedAmount' => doubleval($quote->getGrandTotal())
'Currency' => $adyenAmountCurrency->getCurrencyCode(),
'RequestedAmount' => doubleval($adyenAmountCurrency->getAmount())
]
]
]
......
......@@ -23,12 +23,14 @@
namespace Adyen\Payment\Model;
use Adyen\Payment\Helper\ChargedCurrency;
use Adyen\Payment\Helper\Vault;
use Adyen\Payment\Model\Ui\AdyenCcConfigProvider;
use Adyen\Payment\Model\Ui\AdyenHppConfigProvider;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Encryption\EncryptorInterface;
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\InvoiceSender;
use Magento\Framework\App\Area;
......@@ -159,6 +161,21 @@ class Cron
*/
protected $_value;
/**
* @var
*/
protected $_currency;
/**
* @var
*/
protected $orderAmount;
/**
* @var
*/
protected $orderCurrency;
/**
* @var
*/
......@@ -269,6 +286,11 @@ class Cron
*/
protected $encryptor;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* Cron constructor.
*
......@@ -300,6 +322,7 @@ class Cron
* @param PaymentTokenFactoryInterface $paymentTokenFactory
* @param PaymentTokenRepositoryInterface $paymentTokenRepository
* @param EncryptorInterface $encryptor
* @param ChargedCurrency $chargedCurrency
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
......@@ -329,7 +352,8 @@ class Cron
PaymentTokenManagement $paymentTokenManagement,
PaymentTokenFactoryInterface $paymentTokenFactory,
PaymentTokenRepositoryInterface $paymentTokenRepository,
EncryptorInterface $encryptor
EncryptorInterface $encryptor,
ChargedCurrency $chargedCurrency
) {
$this->_scopeConfig = $scopeConfig;
$this->_adyenLogger = $adyenLogger;
......@@ -359,6 +383,7 @@ class Cron
$this->paymentTokenFactory = $paymentTokenFactory;
$this->paymentTokenRepository = $paymentTokenRepository;
$this->encryptor = $encryptor;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -626,6 +651,7 @@ class Cron
$this->_paymentMethod = $notification->getPaymentMethod();
$this->_reason = $notification->getReason();
$this->_value = $notification->getAmountValue();
$this->_currency = $notification->getAmountCurrency();
$this->_live = $notification->getLive();
$additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize(
......@@ -683,6 +709,20 @@ class Cron
$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
$success = (!empty($this->_reason)) ? "$successResult <br />reason:$this->_reason" : $successResult;
if ($this->_eventCode == Notification::REFUND || $this->_eventCode == Notification::CAPTURE) {
$currency = $this->_order->getOrderCurrencyCode();
// check if it is a full or partial refund
$amount = $this->_value;
$orderAmount = (int)$this->_adyenHelper->formatAmount($this->_order->getGrandTotal(), $currency);
$formattedOrderAmount = (int)$this->_adyenHelper->formatAmount($this->orderAmount, $this->orderCurrency);
$this->_adyenLogger->addAdyenNotificationCronjob(
'amount notification:' . $amount . ' amount order:' . $orderAmount
'amount notification:' . $amount . ' amount order:' . $formattedOrderAmount
);
if ($amount == $orderAmount) {
if ($amount == $formattedOrderAmount) {
$this->_order->setData(
'adyen_notification_event_code',
$this->_eventCode . " : " . strtoupper($successResult)
......@@ -1371,9 +1409,8 @@ class Cron
->getFirstItem();
if ($orderPayment->getId() > 0) {
$currency = $this->_order->getOrderCurrencyCode();
$amountRefunded = $amountRefunded = $orderPayment->getTotalRefunded() +
$this->_adyenHelper->originalAmount($this->_value, $currency);
$amountRefunded = $orderPayment->getTotalRefunded() +
$this->_adyenHelper->originalAmount($this->_value, $this->_currency);
$orderPayment->setUpdatedAt(new \DateTime());
$orderPayment->setTotalRefunded($amountRefunded);
$orderPayment->save();
......@@ -1394,8 +1431,7 @@ class Cron
// refund is done through adyen backoffice so create a credit memo
$order = $this->_order;
if ($order->canCreditmemo()) {
$currency = $this->_order->getOrderCurrencyCode();
$amount = $this->_adyenHelper->originalAmount($this->_value, $currency);
$amount = $this->_adyenHelper->originalAmount($this->_value, $this->_currency);
$order->getPayment()->registerRefundNotification($amount);
$this->_adyenLogger->addAdyenNotificationCronjob('Created credit memo for order');
......@@ -1569,8 +1605,7 @@ class Cron
}
// validate if amount is total amount
$orderCurrencyCode = $this->_order->getOrderCurrencyCode();
$amount = $this->_adyenHelper->originalAmount($this->_value, $orderCurrencyCode);
$amount = $this->_adyenHelper->originalAmount($this->_value, $this->_currency);
try {
// add to order payment
......@@ -1594,7 +1629,7 @@ class Cron
);
}
if ($this->_isTotalAmount($paymentObj->getEntityId(), $orderCurrencyCode)) {
if ($this->_isTotalAmount($paymentObj->getEntityId(), $this->orderCurrency)) {
$this->_createInvoice();
} else {
$this->_adyenLogger->addAdyenNotificationCronjob(
......@@ -1831,7 +1866,10 @@ class Cron
);
// 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
$res = $this->_adyenOrderPaymentCollectionFactory
......@@ -1840,16 +1878,16 @@ class Cron
if ($res && isset($res[0]) && is_array($res[0])) {
$amount = $res[0]['total_amount'];
$orderAmount = $this->_adyenHelper->formatAmount($amount, $orderCurrencyCode);
$formattedOrderAmount = $this->_adyenHelper->formatAmount($amount, $orderCurrencyCode);
$this->_adyenLogger->addAdyenNotificationCronjob(
sprintf(
'The grandtotal amount is %s and the total order amount that is authorised is: %s',
$grandTotal,
$orderAmount
$formattedOrderAmount
)
);
if ($grandTotal == $orderAmount) {
if ($grandTotal == $formattedOrderAmount) {
$this->_adyenLogger->addAdyenNotificationCronjob('AUTHORISATION has the full amount');
return true;
} else {
......@@ -1955,14 +1993,13 @@ class Cron
$this->_adyenLogger->addAdyenNotificationCronjob('Set order to authorised');
// if full amount is captured create invoice
$currency = $this->_order->getOrderCurrencyCode();
$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
if ($createInvoice == true && $amount == $orderAmount) {
if ($createInvoice && $amount == $formattedOrderAmount) {
$this->_adyenLogger->addAdyenNotificationCronjob(
'amount notification:' . $amount . ' amount order:' . $orderAmount
'amount notification:' . $amount . ' amount order:' . $formattedOrderAmount
);
$this->_createInvoice();
}
......
......@@ -24,6 +24,7 @@
namespace Adyen\Payment\Model\Ui;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Payment\Helper\Data as PaymentHelper;
......@@ -60,6 +61,11 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
*/
private $storeManager;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* AdyenGooglePayConfigProvider constructor.
*
......@@ -67,6 +73,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param ChargedCurrency $chargedCurrency
*/
public function __construct(
PaymentHelper $paymentHelper,
......@@ -74,7 +81,8 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Checkout\Model\Session $checkoutSession
\Magento\Checkout\Model\Session $checkoutSession,
ChargedCurrency $chargedCurrency
) {
$this->paymentHelper = $paymentHelper;
$this->adyenHelper = $adyenHelper;
......@@ -82,6 +90,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
$this->urlBuilder = $urlBuilder;
$this->storeManager = $storeManager;
$this->checkoutSession = $checkoutSession;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -119,7 +128,7 @@ class AdyenGooglePayConfigProvider implements ConfigProviderInterface
);
$quote = $this->checkoutSession->getQuote();
$currency = $quote->getCurrency();
$currency = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getCurrencyCode();
$adyenGooglePayConfig['format'] = $this->adyenHelper->decimalNumbers($currency);
$adyenGooglePayConfig['merchantIdentifier'] = $this->adyenHelper->getAdyenGooglePayMerchantIdentifier($this->storeManager->getStore()->getId());
......
......@@ -23,6 +23,7 @@
namespace Adyen\Payment\Model\Ui;
use Adyen\Payment\Helper\ChargedCurrency;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Payment\Helper\Data as PaymentHelper;
......@@ -70,6 +71,11 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
*/
private $ccConfig;
/**
* @var ChargedCurrency
*/
private $chargedCurrency;
/**
* AdyenOneclickConfigProvider constructor.
*
......@@ -80,6 +86,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Payment\Model\CcConfig $ccConfig
* @param ChargedCurrency $chargedCurrency
*/
public function __construct(
\Adyen\Payment\Helper\Data $adyenHelper,
......@@ -88,7 +95,8 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
\Magento\Checkout\Model\Session $session,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Payment\Model\CcConfig $ccConfig
\Magento\Payment\Model\CcConfig $ccConfig,
ChargedCurrency $chargedCurrency
) {
$this->_adyenHelper = $adyenHelper;
$this->_request = $request;
......@@ -97,6 +105,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
$this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder;
$this->ccConfig = $ccConfig;
$this->chargedCurrency = $chargedCurrency;
}
/**
......@@ -178,7 +187,7 @@ class AdyenOneclickConfigProvider implements ConfigProviderInterface
if ($this->_customerSession->isLoggedIn()) {
$customerId = $this->_customerSession->getCustomerId();
$storeId = $this->_storeManager->getStore()->getId();
$grandTotal = $this->_getQuote()->getGrandTotal();
$grandTotal = $this->chargedCurrency->getQuoteAmountCurrency($this->_getQuote())->getAmount();
$recurringType = $this->_getRecurringContractType();
$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
$this->updateSchemaVersion540($setup);
}
if (version_compare($context->getVersion(), '6.7.0', '<')) {
$this->updateSchemaVersion670($setup);
}
$setup->endSetup();
}
......@@ -402,4 +406,33 @@ class UpgradeSchema implements UpgradeSchemaInterface
$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
$checkoutSession = $this->getSimpleMock(\Magento\Checkout\Model\Session::class);
$storeManager = $this->getSimpleMock(\Magento\Store\Model\StoreManagerInterface::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);
$storeManager->method('getStore')
......@@ -82,7 +83,8 @@ class AdyenInitiateTerminalApiTest extends \PHPUnit\Framework\TestCase
$adyenLogger,
$checkoutSession,
$storeManager,
$productMetadata
$productMetadata,
$chargedCurrency
);
}
......
......@@ -39,6 +39,7 @@
<enable_recurring>0</enable_recurring>
<group>adyen</group>
<notifications_can_cancel>1</notifications_can_cancel>
<charged_currency>display</charged_currency>
</adyen_abstract>
<adyen_cc>
<active>1</active>
......
......@@ -44,4 +44,7 @@
<event name="payment_method_assign_data_adyen_google_pay">
<observer name="adyen_google_pay_gateway_data_assign" instance="Adyen\Payment\Observer\AdyenGooglePayDataAssignObserver" />
</event>
<event name="sales_model_service_quote_submit_before">
<observer name="adyen_sales_order_charged_currency" instance="Adyen\Payment\Observer\AdyenSalesOrderChargedCurrencyObserver" />
</event>
</config>
......@@ -24,7 +24,7 @@
-->
<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>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
......
......@@ -61,15 +61,16 @@ $_isDemoMode = $block->isDemoMode();
<?php
$banktranferData = $block->getBankTransferData();
$order = $block->getOrder();
$orderAmountCurrency = $block->getOrderAmountCurrency();
?>
<div>
<table class="edit-order-table" style="text-align:left; margin-top:5px; margin-bottom:0;">
<tbody>
<?php if (!empty($order->getGrandTotal())): ?>
<?php if (!empty($orderAmountCurrency->getAmount())): ?>
<tr>
<th scope="row"><?= $block->escapeHtml(__('Amount')); ?></th>
<td><?= /* @noEscape */ $order->formatPrice($order->getGrandTotal()); ?></td>
<td><?= /* @noEscape */ $order->formatPrice($orderAmountCurrency->getAmount()); ?></td>
</tr>
<?php endif; ?>
......
......@@ -62,6 +62,7 @@
<?php
$banktranferData = $block->getBankTransferData();
$order = $block->getOrder();
$orderAmountCurrency=$block->geAdyenAmountCurrency();
?>
<h2><?= $block->escapeHtml(__('Pay using Bank transfer')); ?></h2>
<p><?= $block->escapeHtml(
......@@ -69,11 +70,11 @@
); ?></p>
<table>
<tbody>
<?php if (!empty($order->getGrandTotal())): ?>
<?php if (!empty($orderAmountCurrency->getAmount())): ?>
<tr>
<th scope="row"><?= $block->escapeHtml(__('Amount')); ?></th>
<td><?= /* @noEscape */
$order->formatPrice($order->getGrandTotal()); ?></td>
$order->formatPrice($orderAmountCurrency->getAmount()); ?></td>
</tr>
<?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