Skip to content

Commit 22b7e14

Browse files
Afterpay Magento Release v5.2.0
- Upgraded the JS Library to v2 to support new Dynamic Messaging variants including “Pay Monthly”.
1 parent 020b472 commit 22b7e14

File tree

28 files changed

+198
-47
lines changed

28 files changed

+198
-47
lines changed

Controller/Express/PlaceOrder.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
use Magento\Framework\App\RequestInterface;
1111
use Magento\Framework\Controller\Result\JsonFactory;
1212
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Framework\Message\ManagerInterface;
1413
use Magento\Payment\Gateway\CommandInterface;
1514
use Magento\Store\Model\StoreManagerInterface;
1615

1716
class PlaceOrder implements HttpPostActionInterface
1817
{
1918
private RequestInterface $request;
20-
private ManagerInterface $messageManager;
2119
private Session $checkoutSession;
2220
private JsonFactory $jsonFactory;
2321
private PlaceOrderProcessor $placeOrderProcessor;
@@ -26,15 +24,13 @@ class PlaceOrder implements HttpPostActionInterface
2624

2725
public function __construct(
2826
RequestInterface $request,
29-
ManagerInterface $messageManager,
3027
Session $checkoutSession,
3128
JsonFactory $jsonFactory,
3229
PlaceOrderProcessor $placeOrderProcessor,
3330
CommandInterface $syncCheckoutDataCommand,
3431
StoreManagerInterface $storeManager
3532
) {
3633
$this->request = $request;
37-
$this->messageManager = $messageManager;
3834
$this->checkoutSession = $checkoutSession;
3935
$this->jsonFactory = $jsonFactory;
4036
$this->placeOrderProcessor = $placeOrderProcessor;
@@ -55,9 +51,11 @@ public function execute()
5551

5652
if ($status !== Capture::CHECKOUT_STATUS_SUCCESS) {
5753
$errorMessage = (string)__('Afterpay payment is declined. Please select an alternative payment method.');
58-
$this->messageManager->addErrorMessage($errorMessage);
5954

60-
return $jsonResult->setData(['redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')]);
55+
return $jsonResult->setData([
56+
'error' => $errorMessage,
57+
'redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')
58+
]);
6159
}
6260

6361
try {
@@ -69,9 +67,11 @@ public function execute()
6967
$errorMessage = $e instanceof LocalizedException
7068
? $e->getMessage()
7169
: (string)__('Afterpay payment is declined. Please select an alternative payment method.');
72-
$this->messageManager->addErrorMessage($errorMessage);
7370

74-
return $jsonResult->setData(['redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')]);
71+
return $jsonResult->setData([
72+
'error' => $errorMessage,
73+
'redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')
74+
]);
7575
}
7676

7777
return $jsonResult->setData(['redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/onepage/success')]);

Gateway/ErrorMessageMapper/CaptureErrorMessageMapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
class CaptureErrorMessageMapper implements ErrorMessageMapperInterface
99
{
10+
public const STATUS_DECLINED_ERROR_MESSAGE = 'Aftepay payment declined. Please select an alternative payment method.';
11+
1012
public function getMessage(string $code)
1113
{
1214
switch ($code) {
1315
case CaptureResponseValidator::STATUS_DECLINED:
14-
return __('Aftepay payment declined. Please select an alternative payment method.');
16+
return __(self::STATUS_DECLINED_ERROR_MESSAGE);
1517
default:
1618
return null;
1719
}

Gateway/Http/TransferFactory/UserAgentProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function provide(?int $websiteId = null): string
3333
$magentoVersion = $this->productMetadata->getVersion();
3434
$phpVersion = $this->util->getTrimmedPhpVersion();
3535
$afterpayMerchantId = $this->config->getMerchantId($websiteId);
36-
$afterpayMPId = $this->config->getPublicId($websiteId);
36+
$publicId = $this->config->getPublicId($websiteId);
37+
$afterpayMPId=$publicId??"null";
3738
$websiteDomain = $this->store->getBaseUrl();
3839
$CashAppPayAvailable=(int)$this->config->getCashAppPayAvailable($websiteId);
3940
$CashAppPayEnabled=(int)$this->config->getCashAppPayEnabled($websiteId);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Afterpay\Afterpay\Gateway\Response\MerchantConfiguration;
4+
5+
class ConsumerLendingConfigurationHandler 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+
$consumerLending = $response['consumerLending']['enabled'] ?? false;
19+
$this->config->setConsumerLendingEnabled((int)$consumerLending, $websiteId);
20+
if ($consumerLending) {
21+
$minAmount = $response['consumerLending']['minimumAmount']['amount'] ?? null;
22+
$this->config->setConsumerLendingMinAmount($minAmount, $websiteId);
23+
}
24+
}
25+
}

Gateway/Response/MerchantConfiguration/MpidConfigurationHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class MpidConfigurationHandler implements \Magento\Payment\Gateway\Response\HandlerInterface
66
{
7-
private \Afterpay\Afterpay\Model\Config $config;
7+
private \Afterpay\Afterpay\Model\Config $config;
88

99
public function __construct(
1010
\Afterpay\Afterpay\Model\Config $config
@@ -15,7 +15,7 @@ public function __construct(
1515
public function handle(array $handlingSubject, array $response): void
1616
{
1717
$websiteId = (int)$handlingSubject['websiteId'];
18-
$mpid =$response['publicId']??"null";
18+
$mpid = $response['publicId'] ?? '';
1919
$this->config->setPublicId($mpid, $websiteId);
2020
}
2121
}

Model/CheckoutConfigProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ class CheckoutConfigProvider implements \Magento\Checkout\Model\ConfigProviderIn
99
private \Magento\Checkout\Model\Session $checkoutSession;
1010

1111
private \Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability;
12+
private Config $config;
1213

1314
public function __construct(
1415
\Magento\Framework\Locale\Resolver $localeResolver,
1516
\Magento\Checkout\Model\Session $checkoutSession,
16-
\Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability
17+
\Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability,
18+
\Afterpay\Afterpay\Model\Config $config
1719
) {
1820
$this->localeResolver = $localeResolver;
1921
$this->checkoutSession = $checkoutSession;
2022
$this->checkCBTCurrencyAvailability = $checkCBTCurrencyAvailability;
23+
$this->config = $config;
2124
}
2225

2326
public function getConfig(): array
@@ -28,7 +31,9 @@ public function getConfig(): array
2831
'payment' => [
2932
'afterpay' => [
3033
'locale' => $this->localeResolver->getLocale(),
31-
'isCBTCurrency' => $this->checkCBTCurrencyAvailability->checkByQuote($quote)
34+
'isCBTCurrency' => $this->checkCBTCurrencyAvailability->checkByQuote($quote),
35+
'consumerLendingEnabled' => $this->config->getConsumerLendingEnabled(),
36+
'consumerLendingMinimumAmount' => $this->config->getConsumerLendingMinAmount(),
3237
]
3338
]
3439
];

Model/Config.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ class Config
3333
const XML_PATH_PAYPAL_MERCHANT_COUNTRY = 'paypal/general/merchant_country';
3434
const XML_PATH_ENABLE_REVERSAL = 'payment/afterpay/enable_reversal';
3535
const XML_PATH_MPID = 'payment/afterpay/public_id';
36-
const XML_PATH_CASHAPP_PAY_AVAILABLE='payment/afterpay/cash_app_pay_available';
37-
const XML_PATH_CASHAPP_PAY_ENABLE='payment/cashapp/active';
38-
36+
const XML_PATH_CASHAPP_PAY_AVAILABLE = 'payment/afterpay/cash_app_pay_available';
37+
const XML_PATH_CASHAPP_PAY_ENABLE = 'payment/cashapp/active';
38+
const XML_PATH_CONSUMER_LENDING_ENABLED = 'payment/afterpay/consumer_lending_enabled';
39+
const XML_PATH_CONSUMER_LENDING_MIN_AMOUNT = 'payment/afterpay/consumer_lending_min_amount';
3940
private ScopeConfigInterface $scopeConfig;
4041
private WriterInterface $writer;
4142
private ResourceConnection $resourceConnection;
@@ -495,6 +496,7 @@ public function getCashAppPayAvailable(?int $scopeCode = null): bool
495496
$scopeCode
496497
);
497498
}
499+
498500
public function getCashAppPayEnabled(?int $scopeCode = null): bool
499501
{
500502
return (bool)$this->scopeConfig->getValue(
@@ -503,4 +505,62 @@ public function getCashAppPayEnabled(?int $scopeCode = null): bool
503505
$scopeCode
504506
);
505507
}
508+
509+
public function setConsumerLendingEnabled(int $value, int $scopeId = 0): self
510+
{
511+
if ($scopeId) {
512+
$this->writer->save(
513+
self::XML_PATH_CONSUMER_LENDING_ENABLED,
514+
$value,
515+
ScopeInterface::SCOPE_WEBSITES,
516+
$scopeId
517+
);
518+
519+
return $this;
520+
}
521+
$this->writer->save(
522+
self::XML_PATH_CONSUMER_LENDING_ENABLED,
523+
$value
524+
);
525+
526+
return $this;
527+
}
528+
529+
public function getConsumerLendingEnabled(?int $scopeCode = null): bool
530+
{
531+
return (bool)$this->scopeConfig->getValue(
532+
self::XML_PATH_CONSUMER_LENDING_ENABLED,
533+
ScopeInterface::SCOPE_WEBSITE,
534+
$scopeCode
535+
);
536+
}
537+
538+
public function setConsumerLendingMinAmount(string $value, int $scopeId = 0): self
539+
{
540+
if ($scopeId) {
541+
$this->writer->save(
542+
self::XML_PATH_CONSUMER_LENDING_MIN_AMOUNT,
543+
$value,
544+
ScopeInterface::SCOPE_WEBSITES,
545+
$scopeId
546+
);
547+
548+
return $this;
549+
}
550+
$this->writer->save(
551+
self::XML_PATH_CONSUMER_LENDING_MIN_AMOUNT,
552+
$value
553+
);
554+
555+
return $this;
556+
}
557+
558+
public function getConsumerLendingMinAmount(?int $scopeCode = null): bool
559+
{
560+
return (bool)$this->scopeConfig->getValue(
561+
self::XML_PATH_CONSUMER_LENDING_MIN_AMOUNT,
562+
ScopeInterface::SCOPE_WEBSITE,
563+
$scopeCode
564+
);
565+
}
506566
}

Model/GraphQl/Resolver/AfterpayConfig.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function resolve(
3838
$isEnabledCtaProductPage = $this->config->getIsEnableCtaProductPage((int)$websiteId);
3939
$isEnabledCtaMinicart = $this->config->getIsEnableCtaMiniCart((int)$websiteId);
4040
$isEnabledCtaCheckout = $this->config->getIsEnableCtaCartPage((int)$websiteId);
41+
$publicId = $this->config->getPublicId((int)$websiteId);
4142

4243
return [
4344
'max_amount' => $maxAmount,
@@ -47,6 +48,7 @@ public function resolve(
4748
'is_enabled_cta_pdp' => $isEnabledCtaProductPage,
4849
'is_enabled_cta_minicart' => $isEnabledCtaMinicart,
4950
'is_enabled_cta_checkout' => $isEnabledCtaCheckout,
51+
'mpid' => $publicId,
5052
];
5153
}
5254
}

Model/Payment/PaymentErrorProcessor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Afterpay\Afterpay\Model\Payment;
44

5+
use Afterpay\Afterpay\Gateway\ErrorMessageMapper\CaptureErrorMessageMapper;
56
use Afterpay\Afterpay\Model\Payment\Capture\CancelOrderProcessor;
67
use Magento\Checkout\Model\Session;
78
use Magento\Framework\Exception\LocalizedException;
89
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Payment\Gateway\Command\CommandException;
911
use Magento\Quote\Model\Quote;
1012
use Magento\Quote\Model\Quote\Payment;
1113
use Magento\Sales\Api\OrderRepositoryInterface;
@@ -49,6 +51,10 @@ public function execute(Quote $quote, \Throwable $e, Payment $payment): int
4951
}
5052
}
5153

54+
if ($e instanceof CommandException && $e->getMessage() === CaptureErrorMessageMapper::STATUS_DECLINED_ERROR_MESSAGE) {
55+
throw $e;
56+
}
57+
5258
$this->cancelOrderProcessor->execute($payment, (int)$quote->getId());
5359

5460
if ($e instanceof LocalizedException) {

Model/Url/Lib/CtaLibUrlProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ protected function buildUrl(): string
88
{
99
return $this->urlBuilder->build(
1010
\Afterpay\Afterpay\Model\Url\UrlBuilder::TYPE_JS_LIB,
11-
'afterpay-1.x.js'
11+
'square-marketplace.js'
1212
);
1313
}
1414
}

0 commit comments

Comments
 (0)