Skip to content

Commit 4cc3f8d

Browse files
Release version 4.0.5-beta.1
1 parent 4fbfd36 commit 4cc3f8d

File tree

47 files changed

+961
-80
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+961
-80
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
.DS_Store

Api/Data/CheckoutInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface CheckoutInterface
1414
const AFTERPAY_TOKEN = 'afterpay_token';
1515
const AFTERPAY_AUTH_TOKEN_EXPIRES = 'afterpay_expires';
1616
const AFTERPAY_REDIRECT_CHECKOUT_URL = 'afterpay_redirectCheckoutUrl';
17+
const AFTERPAY_IS_CBT_CURRENCY = 'afterpay_is_cbt_currency';
18+
const AFTERPAY_CBT_CURRENCY = 'afterpay_cbt_currency';
1719
/**#@-*/
1820

1921
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Afterpay\Afterpay\Block\Adminhtml\System\Config\Form\Field;
6+
7+
class CBTAvailableCurrencies extends \Magento\Config\Block\System\Config\Form\Field
8+
{
9+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
10+
{
11+
/** @phpstan-ignore-next-line */
12+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
13+
return parent::render($element);
14+
}
15+
16+
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
17+
{
18+
/** @phpstan-ignore-next-line */
19+
$element->setDisabled('disabled');
20+
return $element->getElementHtml();
21+
}
22+
}

Gateway/Command/GetMerchantConfigurationCommandWrapper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private function eraseMerchantConfiguration(int $websiteId, bool $websiteHasOwnC
6767
{
6868
$this->afterpayConfig
6969
->deleteMaxOrderTotal($websiteId, $websiteHasOwnConfig)
70-
->deleteMinOrderTotal($websiteId, $websiteHasOwnConfig);
70+
->deleteMinOrderTotal($websiteId, $websiteHasOwnConfig)
71+
->deleteCbtCurrencyLimits($websiteId, $websiteHasOwnConfig);
7172
$this->afterpayConfig->deleteSpecificCountries($websiteId, $websiteHasOwnConfig);
7273
}
7374

Gateway/Request/Checkout/CheckoutDataBuilder.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ class CheckoutDataBuilder implements \Magento\Payment\Gateway\Request\BuilderInt
99
private $url;
1010
private $productRepository;
1111
private $searchCriteriaBuilder;
12+
protected $checkCBTCurrencyAvailability;
1213

1314
public function __construct(
1415
\Magento\Framework\UrlInterface $url,
1516
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
16-
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
17+
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
18+
\Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability
1719
) {
1820
$this->url = $url;
1921
$this->productRepository = $productRepository;
2022
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
23+
$this->checkCBTCurrencyAvailability = $checkCBTCurrencyAvailability;
2124
}
2225

2326
public function build(array $buildSubject): array
@@ -27,14 +30,23 @@ public function build(array $buildSubject): array
2730
/** @var \Afterpay\Afterpay\Api\Data\RedirectPathInterface $redirectPath */
2831
$redirectPath = $buildSubject['redirect_path'];
2932

33+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
3034
$shippingAddress = $quote->getShippingAddress();
3135
$billingAddress = $quote->getBillingAddress();
36+
$billingTaxAmount = $isCBTCurrencyAvailable
37+
? $billingAddress->getTaxAmount()
38+
: $billingAddress->getBaseTaxAmount();
39+
$shippingTaxAmount = $isCBTCurrencyAvailable
40+
? $shippingAddress->getTaxAmount()
41+
: $shippingAddress->getBaseTaxAmount();
3242

3343
$data = [
3444
'storeId' => $quote->getStoreId(),
3545
'amount' => [
36-
'amount' => $this->formatPrice($quote->getBaseGrandTotal()),
37-
'currency' => $quote->getBaseCurrencyCode()
46+
'amount' => $this->formatPrice(
47+
$isCBTCurrencyAvailable ? $quote->getGrandTotal() : $quote->getBaseGrandTotal()
48+
),
49+
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
3850
],
3951
'consumer' => [
4052
'givenNames' => $quote->getCustomerFirstname() ?: $billingAddress->getFirstname(),
@@ -60,9 +72,9 @@ public function build(array $buildSubject): array
6072
'merchantReference' => $quote->getReservedOrderId(),
6173
'taxAmount' => [
6274
'amount' => $this->formatPrice(
63-
$billingAddress->getBaseTaxAmount() ?: $shippingAddress->getBaseTaxAmount()
75+
$billingTaxAmount ?: $shippingTaxAmount
6476
),
65-
'currency' => $quote->getBaseCurrencyCode()
77+
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
6678
],
6779
'purchaseCountry' => $billingAddress->getCountryId()
6880
];
@@ -85,6 +97,7 @@ protected function getItems(\Magento\Quote\Model\Quote $quote): array
8597
$formattedItems = [];
8698
$quoteItems = $quote->getAllVisibleItems();
8799
$itemsImages = $this->getItemsImages($quoteItems);
100+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
88101

89102
foreach ($quoteItems as $item) {
90103
$productId = $item->getProduct()->getId();
@@ -96,8 +109,10 @@ protected function getItems(\Magento\Quote\Model\Quote $quote): array
96109
'pageUrl' => $item->getProduct()->getProductUrl(),
97110
'categories' => [array_values($this->getQuoteItemCategoriesNames($item))],
98111
'price' => [
99-
'amount' => $this->formatPrice($item->getBasePriceInclTax()),
100-
'currency' => $quote->getBaseCurrencyCode()
112+
'amount' => $this->formatPrice(
113+
$isCBTCurrencyAvailable ? $item->getPriceInclTax() : $item->getBasePriceInclTax()
114+
),
115+
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
101116
]
102117
];
103118

@@ -168,9 +183,14 @@ protected function getShippingAmount(\Magento\Quote\Model\Quote $quote): ?array
168183
if ($quote->isVirtual()) {
169184
return null;
170185
}
186+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
187+
171188
return [
172-
'amount' => $this->formatPrice($quote->getShippingAddress()->getBaseShippingAmount()),
173-
'currency' => $quote->getBaseCurrencyCode()
189+
'amount' => $this->formatPrice($isCBTCurrencyAvailable
190+
? $quote->getShippingAddress()->getShippingAmount()
191+
: $quote->getShippingAddress()->getBaseShippingAmount()
192+
),
193+
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
174194
];
175195
}
176196

@@ -190,11 +210,14 @@ protected function getDiscounts(\Magento\Quote\Model\Quote $quote): ?array
190210
if (!$quote->getBaseDiscountAmount()) {
191211
return null;
192212
}
213+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
193214
return [
194215
'displayName' => __('Discount'),
195216
'amount' => [
196-
'amount' => $this->formatPrice($quote->getBaseDiscountAmount()),
197-
'currency' => $quote->getBaseCurrencyCode()
217+
'amount' => $this->formatPrice($isCBTCurrencyAvailable
218+
? $quote->getDiscountAmount()
219+
: $quote->getBaseDiscountAmount()),
220+
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
198221
]
199222
];
200223
}

Gateway/Request/ExpressCheckoutDataBuilder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ public function build(array $buildSubject): array
88
{
99
/** @var \Magento\Quote\Model\Quote $quote */
1010
$quote = $buildSubject['quote'];
11-
11+
$currentCurrencyCode = $quote->getQuoteCurrencyCode();
12+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
13+
$amount = $isCBTCurrencyAvailable ? $quote->getGrandTotal() : $quote->getBaseGrandTotal();
14+
$currencyCode = $isCBTCurrencyAvailable ? $currentCurrencyCode : $quote->getBaseCurrencyCode();
1215
$popupOriginUrl = $buildSubject['popup_origin_url'];
1316

1417
$data = [
1518
'mode' => 'express',
1619
'storeId' => $quote->getStoreId(),
1720
'amount' => [
18-
'amount' => $this->formatPrice($quote->getBaseGrandTotal()),
19-
'currency' => $quote->getBaseCurrencyCode()
21+
'amount' => $this->formatPrice($amount),
22+
'currency' => $currencyCode
2023
],
2124
'merchant' => [
2225
'popupOriginUrl' => $popupOriginUrl

Gateway/Request/PaymentAction/AuthCaptureDataBuilder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@ public function build(array $buildSubject): array
1515
/** @var \Magento\Sales\Model\Order\Payment $payment */
1616
$payment = $paymentDO->getPayment();
1717

18+
$isCBTCurrency = (bool) $payment->getAdditionalInformation(
19+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
20+
);
21+
22+
$CBTCurrency = $payment->getAdditionalInformation(
23+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
24+
);
25+
1826
$afterpayOrderId = $payment->getAdditionalInformation(
1927
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ORDER_ID
2028
);
2129

30+
$currencyCode = ($isCBTCurrency && $CBTCurrency)
31+
? $CBTCurrency
32+
: $payment->getOrder()->getBaseCurrencyCode();
33+
2234
return [
2335
'storeId' => $paymentDO->getOrder()->getStoreId(),
2436
'orderId' => $afterpayOrderId,
2537
'amount' => [
2638
'amount' => $this->formatPrice(SubjectReader::readAmount($buildSubject)),
27-
'currency' => $payment->getOrder()->getBaseCurrencyCode()
39+
'currency' => $currencyCode
2840
]
2941
];
3042
}

Gateway/Request/PaymentAction/CaptureDataBuilder.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@ public function build(array $buildSubject): array
88
{
99
$paymentDO = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
1010
$payment = $paymentDO->getPayment();
11+
/** @var \Magento\Sales\Api\Data\OrderInterface $order */
12+
$order = $payment->getOrder();
13+
14+
$isCBTCurrency = (bool) $payment->getAdditionalInformation(
15+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
16+
);
17+
$cbtCurrency = $payment->getAdditionalInformation(
18+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
19+
);
20+
$currencyCode = ($isCBTCurrency && $cbtCurrency) ? $cbtCurrency : $order->getBaseCurrencyCode();
1121

1222
$token = $payment->getAdditionalInformation(\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_TOKEN);
1323
$data = [
14-
'storeId' => $paymentDO->getOrder()->getStoreId(),
24+
'storeId' => $order->getStoreId(),
1525
'token' => $token
1626
];
1727

1828
if ($payment->getAdditionalInformation('afterpay_express')) {
1929
$data['amount'] = [
2030
'amount' => \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject),
21-
'currency' => $payment->getOrder()->getBaseCurrencyCode()
31+
'currency' => $currencyCode
2232
];
2333
}
2434

Gateway/Request/PaymentAction/RefundAndVoidDataBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public function build(array $buildSubject): array
1616
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ORDER_ID
1717
);
1818

19+
$isCBTCurrency = (bool) $paymentDO->getPayment()->getAdditionalInformation(
20+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
21+
);
22+
$CBTCurrency = $paymentDO->getPayment()->getAdditionalInformation(
23+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
24+
);
25+
$currencyCode = ($isCBTCurrency && $CBTCurrency) ? $CBTCurrency : $paymentDO->getOrder()->getCurrencyCode();
26+
1927
$data = [
2028
'storeId' => $paymentDO->getOrder()->getStoreId(),
2129
'orderId' => $afterpayOrderId
@@ -24,7 +32,7 @@ public function build(array $buildSubject): array
2432
return array_merge($data, [
2533
'amount' => [
2634
'amount' => $this->formatPrice(SubjectReader::readAmount($buildSubject)),
27-
'currency' => $paymentDO->getOrder()->getCurrencyCode()
35+
'currency' => $currencyCode
2836
]
2937
]);
3038
} catch (\InvalidArgumentException $e) {

Gateway/Response/Checkout/CheckoutItemsAmountValidationHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ class CheckoutItemsAmountValidationHandler implements \Magento\Payment\Gateway\R
77
public function handle(array $handlingSubject, array $response)
88
{
99
$paymentDO = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($handlingSubject);
10-
10+
$payment = $paymentDO->getPayment();
1111
/** @var \Magento\Quote\Model\Quote $quote */
12-
$quote = $paymentDO->getPayment()->getQuote();
12+
$quote = $payment->getQuote();
13+
$isCBTCurrency = $payment->getAdditionalInformation(\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY);
14+
$grandTotal = $isCBTCurrency ? $quote->getGrandTotal() : $quote->getBaseGrandTotal();
1315

14-
if (round(1 * $quote->getBaseGrandTotal(), 2) != round(1 * $response['amount']['amount'], 2)) {
16+
if (round(1 * $grandTotal, 2) != round(1 * $response['amount']['amount'], 2)) {
1517
throw new \Magento\Framework\Exception\LocalizedException(
1618
__('There are issues when processing your payment. Invalid Amount')
1719
);

Gateway/Response/DiscountHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function handle(array $handlingSubject, array $response)
3939

4040
protected function getOrderDiscountAmount(\Magento\Sales\Model\Order $order): float
4141
{
42-
return (float)($order->getBaseGiftCardsAmount() + $order->getBaseCustomerBalanceAmount());
42+
$isCBTCurrency = (bool) $order->getPayment()->getAdditionalInformation(
43+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
44+
);
45+
46+
if ($isCBTCurrency) {
47+
return (float)($order->getGiftCardsAmount() + $order->getCustomerBalanceAmount());
48+
} else {
49+
return (float)($order->getBaseGiftCardsAmount() + $order->getBaseCustomerBalanceAmount());
50+
}
4351
}
4452
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Afterpay\Afterpay\Gateway\Response\MerchantConfiguration;
4+
5+
class CBTAvailableCurrenciesConfigurationHandler implements \Magento\Payment\Gateway\Response\HandlerInterface
6+
{
7+
private $config;
8+
9+
public function __construct(
10+
\Afterpay\Afterpay\Model\Config $config
11+
) {
12+
$this->config = $config;
13+
}
14+
15+
public function handle(array $handlingSubject, array $response): void
16+
{
17+
$websiteId = (int)$handlingSubject['websiteId'];
18+
$cbtAvailableCurrencies = [];
19+
if (isset($response['CBT']['enabled']) &&
20+
isset($response['CBT']['limits']) &&
21+
is_array($response['CBT']['limits'])
22+
) {
23+
foreach ($response['CBT']['limits'] as $limit) {
24+
if (isset($limit['maximumAmount']['currency']) && isset($limit['maximumAmount']['amount'])) {
25+
$cbtAvailableCurrencies[] = $limit['maximumAmount']['currency'] . ':' . $limit['maximumAmount']['amount'];
26+
}
27+
}
28+
}
29+
$this->config->setCbtCurrencyLimits(implode(",", $cbtAvailableCurrencies), $websiteId);
30+
}
31+
}

Gateway/Validator/CaptureResponseValidator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public function validate(array $validationSubject): \Magento\Payment\Gateway\Val
1414
if (isset($response['status']) && $response['status'] == self::STATUS_DECLINED) {
1515
return $this->createResult(
1616
false,
17-
[__('Afterpay payment declined. Please select an alternative payment method.')]
17+
[
18+
__('%1 payment declined. Please select an alternative payment method.',
19+
($validationSubject['payment'])->getPayment()->getMethodInstance()->getTitle())
20+
]
1821
);
1922
}
2023

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Afterpay\Afterpay\Gateway\Validator\Method;
4+
5+
class CurrencyValidator extends \Magento\Payment\Gateway\Validator\AbstractValidator
6+
{
7+
private $config;
8+
private $checkoutSession;
9+
10+
public function __construct(
11+
\Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory,
12+
\Magento\Checkout\Model\Session $checkoutSession,
13+
\Afterpay\Afterpay\Model\Config $config
14+
) {
15+
$this->config = $config;
16+
$this->checkoutSession = $checkoutSession;
17+
parent::__construct($resultFactory);
18+
}
19+
20+
public function validate(array $validationSubject): \Magento\Payment\Gateway\Validator\ResultInterface
21+
{
22+
$quote = $this->checkoutSession->getQuote();
23+
$currentCurrency = $quote->getQuoteCurrencyCode();
24+
$allowedCurrencies = $this->config->getAllowedCurrencies();
25+
$cbtCurrencies = array_keys($this->config->getCbtCurrencyLimits());
26+
27+
if (in_array($currentCurrency, array_merge($allowedCurrencies, $cbtCurrencies))) {
28+
return $this->createResult(true);
29+
}
30+
31+
return $this->createResult(false);
32+
}
33+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Afterpay\Afterpay\Model\CBT;
4+
5+
interface CheckCBTCurrencyAvailabilityInterface
6+
{
7+
public function check(string $currencyCode, float $amount = null): bool;
8+
9+
public function checkByQuote(\Magento\Quote\Model\Quote $quote): bool;
10+
}

0 commit comments

Comments
 (0)