Skip to content

Commit 2dd4a65

Browse files
Release version 5.0.4-beta.1
1 parent cbae1f2 commit 2dd4a65

File tree

47 files changed

+926
-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

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

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: 38 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 \Magento\Framework\UrlInterface $url;
1010
private \Magento\Catalog\Api\ProductRepositoryInterface $productRepository;
1111
private \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder;
12+
protected \Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $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,9 +97,12 @@ 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();
104+
$amount = $isCBTCurrencyAvailable ? $item->getPriceInclTax() : $item->getBasePriceInclTax();
105+
$currencyCode = $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode();
91106

92107
$formattedItem = [
93108
'name' => $item->getName(),
@@ -96,8 +111,8 @@ protected function getItems(\Magento\Quote\Model\Quote $quote): array
96111
'pageUrl' => $item->getProduct()->getProductUrl(),
97112
'categories' => [array_values($this->getQuoteItemCategoriesNames($item))],
98113
'price' => [
99-
'amount' => $this->formatPrice($item->getBasePriceInclTax()),
100-
'currency' => $quote->getBaseCurrencyCode()
114+
'amount' => $this->formatPrice($amount),
115+
'currency' => $currencyCode
101116
]
102117
];
103118

@@ -162,9 +177,15 @@ protected function getShippingAmount(\Magento\Quote\Model\Quote $quote): ?array
162177
if ($quote->isVirtual()) {
163178
return null;
164179
}
180+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
181+
$amount = $isCBTCurrencyAvailable
182+
? $quote->getShippingAddress()->getShippingAmount()
183+
: $quote->getShippingAddress()->getBaseShippingAmount();
184+
$currencyCode = $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode();
185+
165186
return [
166-
'amount' => $this->formatPrice($quote->getShippingAddress()->getBaseShippingAmount()),
167-
'currency' => $quote->getBaseCurrencyCode()
187+
'amount' => $this->formatPrice($amount),
188+
'currency' => $currencyCode
168189
];
169190
}
170191

@@ -184,11 +205,17 @@ protected function getDiscounts(\Magento\Quote\Model\Quote $quote): ?array
184205
if (!$quote->getBaseDiscountAmount()) {
185206
return null;
186207
}
208+
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
209+
$amount = $isCBTCurrencyAvailable
210+
? $quote->getDiscountAmount()
211+
: $quote->getBaseDiscountAmount();
212+
$currencyCode = $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode();
213+
187214
return [
188215
'displayName' => __('Discount'),
189216
'amount' => [
190-
'amount' => $this->formatPrice($quote->getBaseDiscountAmount()),
191-
'currency' => $quote->getBaseCurrencyCode()
217+
'amount' => $this->formatPrice($amount),
218+
'currency' => $currencyCode
192219
]
193220
];
194221
}

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ 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+
$cbtCurrency = $payment->getAdditionalInformation(
22+
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
23+
);
24+
$currencyCode = ($isCBTCurrency && $cbtCurrency) ? $cbtCurrency : $paymentDO->getOrder()->getCurrencyCode();
25+
1826
$afterpayOrderId = $payment->getAdditionalInformation(
1927
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ORDER_ID
2028
);
@@ -24,7 +32,7 @@ public function build(array $buildSubject): array
2432
'orderId' => $afterpayOrderId,
2533
'amount' => [
2634
'amount' => $this->formatPrice(SubjectReader::readAmount($buildSubject)),
27-
'currency' => $payment->getOrder()->getBaseCurrencyCode()
35+
'currency' => $currencyCode
2836
]
2937
];
3038
}

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 \Afterpay\Afterpay\Model\Config $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 \Afterpay\Afterpay\Model\Config $config;
8+
private \Magento\Checkout\Model\Session $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)