Skip to content

Commit

Permalink
Afterpay Magento Release v5.2.0
Browse files Browse the repository at this point in the history
- Upgraded the JS Library to v2 to support new Dynamic Messaging variants including “Pay Monthly”.
  • Loading branch information
afterpayplugins committed Sep 4, 2023
1 parent 020b472 commit 22b7e14
Show file tree
Hide file tree
Showing 28 changed files with 198 additions and 47 deletions.
16 changes: 8 additions & 8 deletions Controller/Express/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Payment\Gateway\CommandInterface;
use Magento\Store\Model\StoreManagerInterface;

class PlaceOrder implements HttpPostActionInterface
{
private RequestInterface $request;
private ManagerInterface $messageManager;
private Session $checkoutSession;
private JsonFactory $jsonFactory;
private PlaceOrderProcessor $placeOrderProcessor;
Expand All @@ -26,15 +24,13 @@ class PlaceOrder implements HttpPostActionInterface

public function __construct(
RequestInterface $request,
ManagerInterface $messageManager,
Session $checkoutSession,
JsonFactory $jsonFactory,
PlaceOrderProcessor $placeOrderProcessor,
CommandInterface $syncCheckoutDataCommand,
StoreManagerInterface $storeManager
) {
$this->request = $request;
$this->messageManager = $messageManager;
$this->checkoutSession = $checkoutSession;
$this->jsonFactory = $jsonFactory;
$this->placeOrderProcessor = $placeOrderProcessor;
Expand All @@ -55,9 +51,11 @@ public function execute()

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

return $jsonResult->setData(['redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')]);
return $jsonResult->setData([
'error' => $errorMessage,
'redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')
]);
}

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

return $jsonResult->setData(['redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')]);
return $jsonResult->setData([
'error' => $errorMessage,
'redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/cart')
]);
}

return $jsonResult->setData(['redirectUrl' => $this->storeManager->getStore()->getUrl('checkout/onepage/success')]);
Expand Down
4 changes: 3 additions & 1 deletion Gateway/ErrorMessageMapper/CaptureErrorMessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

class CaptureErrorMessageMapper implements ErrorMessageMapperInterface
{
public const STATUS_DECLINED_ERROR_MESSAGE = 'Aftepay payment declined. Please select an alternative payment method.';

public function getMessage(string $code)
{
switch ($code) {
case CaptureResponseValidator::STATUS_DECLINED:
return __('Aftepay payment declined. Please select an alternative payment method.');
return __(self::STATUS_DECLINED_ERROR_MESSAGE);
default:
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion Gateway/Http/TransferFactory/UserAgentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function provide(?int $websiteId = null): string
$magentoVersion = $this->productMetadata->getVersion();
$phpVersion = $this->util->getTrimmedPhpVersion();
$afterpayMerchantId = $this->config->getMerchantId($websiteId);
$afterpayMPId = $this->config->getPublicId($websiteId);
$publicId = $this->config->getPublicId($websiteId);
$afterpayMPId=$publicId??"null";
$websiteDomain = $this->store->getBaseUrl();
$CashAppPayAvailable=(int)$this->config->getCashAppPayAvailable($websiteId);
$CashAppPayEnabled=(int)$this->config->getCashAppPayEnabled($websiteId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Afterpay\Afterpay\Gateway\Response\MerchantConfiguration;

class ConsumerLendingConfigurationHandler implements \Magento\Payment\Gateway\Response\HandlerInterface
{
private \Afterpay\Afterpay\Model\Config $config;

public function __construct(
\Afterpay\Afterpay\Model\Config $config
) {
$this->config = $config;
}

public function handle(array $handlingSubject, array $response): void
{
$websiteId = (int)$handlingSubject['websiteId'];
$consumerLending = $response['consumerLending']['enabled'] ?? false;
$this->config->setConsumerLendingEnabled((int)$consumerLending, $websiteId);
if ($consumerLending) {
$minAmount = $response['consumerLending']['minimumAmount']['amount'] ?? null;
$this->config->setConsumerLendingMinAmount($minAmount, $websiteId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class MpidConfigurationHandler implements \Magento\Payment\Gateway\Response\HandlerInterface
{
private \Afterpay\Afterpay\Model\Config $config;
private \Afterpay\Afterpay\Model\Config $config;

public function __construct(
\Afterpay\Afterpay\Model\Config $config
Expand All @@ -15,7 +15,7 @@ public function __construct(
public function handle(array $handlingSubject, array $response): void
{
$websiteId = (int)$handlingSubject['websiteId'];
$mpid =$response['publicId']??"null";
$mpid = $response['publicId'] ?? '';
$this->config->setPublicId($mpid, $websiteId);
}
}
9 changes: 7 additions & 2 deletions Model/CheckoutConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ class CheckoutConfigProvider implements \Magento\Checkout\Model\ConfigProviderIn
private \Magento\Checkout\Model\Session $checkoutSession;

private \Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability;
private Config $config;

public function __construct(
\Magento\Framework\Locale\Resolver $localeResolver,
\Magento\Checkout\Model\Session $checkoutSession,
\Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability
\Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability,
\Afterpay\Afterpay\Model\Config $config
) {
$this->localeResolver = $localeResolver;
$this->checkoutSession = $checkoutSession;
$this->checkCBTCurrencyAvailability = $checkCBTCurrencyAvailability;
$this->config = $config;
}

public function getConfig(): array
Expand All @@ -28,7 +31,9 @@ public function getConfig(): array
'payment' => [
'afterpay' => [
'locale' => $this->localeResolver->getLocale(),
'isCBTCurrency' => $this->checkCBTCurrencyAvailability->checkByQuote($quote)
'isCBTCurrency' => $this->checkCBTCurrencyAvailability->checkByQuote($quote),
'consumerLendingEnabled' => $this->config->getConsumerLendingEnabled(),
'consumerLendingMinimumAmount' => $this->config->getConsumerLendingMinAmount(),
]
]
];
Expand Down
66 changes: 63 additions & 3 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class Config
const XML_PATH_PAYPAL_MERCHANT_COUNTRY = 'paypal/general/merchant_country';
const XML_PATH_ENABLE_REVERSAL = 'payment/afterpay/enable_reversal';
const XML_PATH_MPID = 'payment/afterpay/public_id';
const XML_PATH_CASHAPP_PAY_AVAILABLE='payment/afterpay/cash_app_pay_available';
const XML_PATH_CASHAPP_PAY_ENABLE='payment/cashapp/active';

const XML_PATH_CASHAPP_PAY_AVAILABLE = 'payment/afterpay/cash_app_pay_available';
const XML_PATH_CASHAPP_PAY_ENABLE = 'payment/cashapp/active';
const XML_PATH_CONSUMER_LENDING_ENABLED = 'payment/afterpay/consumer_lending_enabled';
const XML_PATH_CONSUMER_LENDING_MIN_AMOUNT = 'payment/afterpay/consumer_lending_min_amount';
private ScopeConfigInterface $scopeConfig;
private WriterInterface $writer;
private ResourceConnection $resourceConnection;
Expand Down Expand Up @@ -495,6 +496,7 @@ public function getCashAppPayAvailable(?int $scopeCode = null): bool
$scopeCode
);
}

public function getCashAppPayEnabled(?int $scopeCode = null): bool
{
return (bool)$this->scopeConfig->getValue(
Expand All @@ -503,4 +505,62 @@ public function getCashAppPayEnabled(?int $scopeCode = null): bool
$scopeCode
);
}

public function setConsumerLendingEnabled(int $value, int $scopeId = 0): self
{
if ($scopeId) {
$this->writer->save(
self::XML_PATH_CONSUMER_LENDING_ENABLED,
$value,
ScopeInterface::SCOPE_WEBSITES,
$scopeId
);

return $this;
}
$this->writer->save(
self::XML_PATH_CONSUMER_LENDING_ENABLED,
$value
);

return $this;
}

public function getConsumerLendingEnabled(?int $scopeCode = null): bool
{
return (bool)$this->scopeConfig->getValue(
self::XML_PATH_CONSUMER_LENDING_ENABLED,
ScopeInterface::SCOPE_WEBSITE,
$scopeCode
);
}

public function setConsumerLendingMinAmount(string $value, int $scopeId = 0): self
{
if ($scopeId) {
$this->writer->save(
self::XML_PATH_CONSUMER_LENDING_MIN_AMOUNT,
$value,
ScopeInterface::SCOPE_WEBSITES,
$scopeId
);

return $this;
}
$this->writer->save(
self::XML_PATH_CONSUMER_LENDING_MIN_AMOUNT,
$value
);

return $this;
}

public function getConsumerLendingMinAmount(?int $scopeCode = null): bool
{
return (bool)$this->scopeConfig->getValue(
self::XML_PATH_CONSUMER_LENDING_MIN_AMOUNT,
ScopeInterface::SCOPE_WEBSITE,
$scopeCode
);
}
}
2 changes: 2 additions & 0 deletions Model/GraphQl/Resolver/AfterpayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function resolve(
$isEnabledCtaProductPage = $this->config->getIsEnableCtaProductPage((int)$websiteId);
$isEnabledCtaMinicart = $this->config->getIsEnableCtaMiniCart((int)$websiteId);
$isEnabledCtaCheckout = $this->config->getIsEnableCtaCartPage((int)$websiteId);
$publicId = $this->config->getPublicId((int)$websiteId);

return [
'max_amount' => $maxAmount,
Expand All @@ -47,6 +48,7 @@ public function resolve(
'is_enabled_cta_pdp' => $isEnabledCtaProductPage,
'is_enabled_cta_minicart' => $isEnabledCtaMinicart,
'is_enabled_cta_checkout' => $isEnabledCtaCheckout,
'mpid' => $publicId,
];
}
}
6 changes: 6 additions & 0 deletions Model/Payment/PaymentErrorProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Afterpay\Afterpay\Model\Payment;

use Afterpay\Afterpay\Gateway\ErrorMessageMapper\CaptureErrorMessageMapper;
use Afterpay\Afterpay\Model\Payment\Capture\CancelOrderProcessor;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Gateway\Command\CommandException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Payment;
use Magento\Sales\Api\OrderRepositoryInterface;
Expand Down Expand Up @@ -49,6 +51,10 @@ public function execute(Quote $quote, \Throwable $e, Payment $payment): int
}
}

if ($e instanceof CommandException && $e->getMessage() === CaptureErrorMessageMapper::STATUS_DECLINED_ERROR_MESSAGE) {
throw $e;
}

$this->cancelOrderProcessor->execute($payment, (int)$quote->getId());

if ($e instanceof LocalizedException) {
Expand Down
2 changes: 1 addition & 1 deletion Model/Url/Lib/CtaLibUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected function buildUrl(): string
{
return $this->urlBuilder->build(
\Afterpay\Afterpay\Model\Url\UrlBuilder::TYPE_JS_LIB,
'afterpay-1.x.js'
'square-marketplace.js'
);
}
}
4 changes: 2 additions & 2 deletions Model/Url/Lib/ExpressCheckoutLibUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ExpressCheckoutLibUrlProvider extends LibUrlProvider
protected function buildUrl(): string
{
return $this->urlBuilder->build(
\Afterpay\Afterpay\Model\Url\UrlBuilder::TYPE_WEB_JS_LIB,
'afterpay.js?merchant_key=magento2'
\Afterpay\Afterpay\Model\Url\UrlBuilder::TYPE_JS_LIB,
'square-marketplace.js'
);
}
}
4 changes: 2 additions & 2 deletions Model/Url/Lib/WidgetCheckoutLibUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(
protected function buildUrl(): string
{
return $this->urlBuilder->build(
\Afterpay\Afterpay\Model\Url\UrlBuilder::TYPE_WEB_JS_LIB,
'afterpay.js'
\Afterpay\Afterpay\Model\Url\UrlBuilder::TYPE_JS_LIB,
'square-marketplace.js'
);
}
}
1 change: 0 additions & 1 deletion Model/Url/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class UrlBuilder
{
const TYPE_API = 'api_url';
const TYPE_JS_LIB = 'js_lib_url';
const TYPE_WEB_JS_LIB = 'web_url';

private UrlBuilder\UrlFactory $urlFactory;

Expand Down
8 changes: 4 additions & 4 deletions Test/Unit/Model/Url/UrlBuilder/UrlFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ protected function setUp(): void
'CAD' => 'https://api.us-sandbox.afterpay.com/',
'default' => 'https://api-sandbox.afterpay.com/',
],
'js_lib_url' => 'https://js.sandbox.afterpay.com/'
'js_lib_url' => 'https://js-sandbox.squarecdn.com/'
],
'production' => [
'api_url' => [
'USD' => 'https://api.us.afterpay.com/',
'CAD' => 'https://api.us.afterpay.com/',
'default' => 'https://api.afterpay.com/',
],
'js_lib_url' => 'https://js.afterpay.com/'
'js_lib_url' => 'https://js.squarecdn.com/'
]
];

Expand Down Expand Up @@ -69,8 +69,8 @@ public function createDataProvider(): array
{
return [
['production', 'api_url', 'https://api.us.afterpay.com/', 'USD'],
['sandbox', 'js_lib_url', 'https://js.sandbox.afterpay.com/'],
['production', 'js_lib_url', 'https://js.afterpay.com/'],
['sandbox', 'js_lib_url', 'https://js-sandbox.squarecdn.com/'],
['production', 'js_lib_url', 'https://js.squarecdn.com/'],
['sandbox', 'api_url', 'https://api-sandbox.afterpay.com/', 'UAH'],
['production', 'api_url', 'https://api.afterpay.com/', 'AUD']
];
Expand Down
2 changes: 2 additions & 0 deletions ViewModel/Container/Cta/Cta.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function updateJsLayout(
$config['dataLocale'] = $this->localeResolver->getLocale();
$config['dataShowLowerLimit'] = $this->config->getMinOrderTotal() >= 1;
$config['dataCbtEnabled'] = count($this->config->getSpecificCountries()) > 1;
$config['dataMPID'] = $this->config->getPublicId();
$config['dataPlatform'] = 'Magento';
}
return parent::updateJsLayout($jsLayoutJson, $remove, $containerNodeName, $config);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "Apache-2.0",
"type": "magento2-module",
"description": "Magento 2 Afterpay Payment Module",
"version": "5.1.4",
"version": "5.2.0",
"require": {
"php": ">=7.4.0",
"magento/framework": "^103.0",
Expand Down
13 changes: 12 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@
<frontend_model>Afterpay\Afterpay\Block\Adminhtml\System\Config\Form\Field\CBTAvailableCurrencies</frontend_model>
<config_path>payment/afterpay/cbt_currency_limits</config_path>
</field>
<field id="public_id" translate="label" type="text" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="0">
<field id="public_id" translate="label" type="text" sortOrder="26" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Public Id</label>
<frontend_model>Afterpay\Afterpay\Block\Adminhtml\System\Config\Form\Field\Disable</frontend_model>
<config_path>payment/afterpay/public_id</config_path>
</field>
<field id="consumer_lending_enabled" translate="label" type="select" sortOrder="27" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Consumer Lending Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<frontend_model>Afterpay\Afterpay\Block\Adminhtml\System\Config\Form\Field\Disable</frontend_model>
<config_path>payment/afterpay/consumer_lending_enabled</config_path>
</field>
<field id="consumer_lending_min_amount" translate="label" type="text" sortOrder="28" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Consumer Lending Minimum Order Total</label>
<frontend_model>Afterpay\Afterpay\Block\Adminhtml\System\Config\Form\Field\Disable</frontend_model>
<config_path>payment/afterpay/consumer_lending_min_amount</config_path>
</field>
<field id="update_order_limits" translate="button_label comment" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label/>
<button_label>Update Merchant Configuration</button_label>
Expand Down
Loading

0 comments on commit 22b7e14

Please sign in to comment.