Skip to content

Commit

Permalink
Release version 4.0.5-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
afterpayplugins committed Nov 10, 2022
1 parent 4fbfd36 commit 4cc3f8d
Show file tree
Hide file tree
Showing 47 changed files with 961 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.DS_Store
2 changes: 2 additions & 0 deletions Api/Data/CheckoutInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface CheckoutInterface
const AFTERPAY_TOKEN = 'afterpay_token';
const AFTERPAY_AUTH_TOKEN_EXPIRES = 'afterpay_expires';
const AFTERPAY_REDIRECT_CHECKOUT_URL = 'afterpay_redirectCheckoutUrl';
const AFTERPAY_IS_CBT_CURRENCY = 'afterpay_is_cbt_currency';
const AFTERPAY_CBT_CURRENCY = 'afterpay_cbt_currency';
/**#@-*/

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Afterpay\Afterpay\Block\Adminhtml\System\Config\Form\Field;

class CBTAvailableCurrencies extends \Magento\Config\Block\System\Config\Form\Field
{
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
/** @phpstan-ignore-next-line */
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}

protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
/** @phpstan-ignore-next-line */
$element->setDisabled('disabled');
return $element->getElementHtml();
}
}
3 changes: 2 additions & 1 deletion Gateway/Command/GetMerchantConfigurationCommandWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private function eraseMerchantConfiguration(int $websiteId, bool $websiteHasOwnC
{
$this->afterpayConfig
->deleteMaxOrderTotal($websiteId, $websiteHasOwnConfig)
->deleteMinOrderTotal($websiteId, $websiteHasOwnConfig);
->deleteMinOrderTotal($websiteId, $websiteHasOwnConfig)
->deleteCbtCurrencyLimits($websiteId, $websiteHasOwnConfig);
$this->afterpayConfig->deleteSpecificCountries($websiteId, $websiteHasOwnConfig);
}

Expand Down
45 changes: 34 additions & 11 deletions Gateway/Request/Checkout/CheckoutDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ class CheckoutDataBuilder implements \Magento\Payment\Gateway\Request\BuilderInt
private $url;
private $productRepository;
private $searchCriteriaBuilder;
protected $checkCBTCurrencyAvailability;

public function __construct(
\Magento\Framework\UrlInterface $url,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Afterpay\Afterpay\Model\CBT\CheckCBTCurrencyAvailabilityInterface $checkCBTCurrencyAvailability
) {
$this->url = $url;
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->checkCBTCurrencyAvailability = $checkCBTCurrencyAvailability;
}

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

$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
$shippingAddress = $quote->getShippingAddress();
$billingAddress = $quote->getBillingAddress();
$billingTaxAmount = $isCBTCurrencyAvailable
? $billingAddress->getTaxAmount()
: $billingAddress->getBaseTaxAmount();
$shippingTaxAmount = $isCBTCurrencyAvailable
? $shippingAddress->getTaxAmount()
: $shippingAddress->getBaseTaxAmount();

$data = [
'storeId' => $quote->getStoreId(),
'amount' => [
'amount' => $this->formatPrice($quote->getBaseGrandTotal()),
'currency' => $quote->getBaseCurrencyCode()
'amount' => $this->formatPrice(
$isCBTCurrencyAvailable ? $quote->getGrandTotal() : $quote->getBaseGrandTotal()
),
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
],
'consumer' => [
'givenNames' => $quote->getCustomerFirstname() ?: $billingAddress->getFirstname(),
Expand All @@ -60,9 +72,9 @@ public function build(array $buildSubject): array
'merchantReference' => $quote->getReservedOrderId(),
'taxAmount' => [
'amount' => $this->formatPrice(
$billingAddress->getBaseTaxAmount() ?: $shippingAddress->getBaseTaxAmount()
$billingTaxAmount ?: $shippingTaxAmount
),
'currency' => $quote->getBaseCurrencyCode()
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
],
'purchaseCountry' => $billingAddress->getCountryId()
];
Expand All @@ -85,6 +97,7 @@ protected function getItems(\Magento\Quote\Model\Quote $quote): array
$formattedItems = [];
$quoteItems = $quote->getAllVisibleItems();
$itemsImages = $this->getItemsImages($quoteItems);
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);

foreach ($quoteItems as $item) {
$productId = $item->getProduct()->getId();
Expand All @@ -96,8 +109,10 @@ protected function getItems(\Magento\Quote\Model\Quote $quote): array
'pageUrl' => $item->getProduct()->getProductUrl(),
'categories' => [array_values($this->getQuoteItemCategoriesNames($item))],
'price' => [
'amount' => $this->formatPrice($item->getBasePriceInclTax()),
'currency' => $quote->getBaseCurrencyCode()
'amount' => $this->formatPrice(
$isCBTCurrencyAvailable ? $item->getPriceInclTax() : $item->getBasePriceInclTax()
),
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
]
];

Expand Down Expand Up @@ -168,9 +183,14 @@ protected function getShippingAmount(\Magento\Quote\Model\Quote $quote): ?array
if ($quote->isVirtual()) {
return null;
}
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);

return [
'amount' => $this->formatPrice($quote->getShippingAddress()->getBaseShippingAmount()),
'currency' => $quote->getBaseCurrencyCode()
'amount' => $this->formatPrice($isCBTCurrencyAvailable
? $quote->getShippingAddress()->getShippingAmount()
: $quote->getShippingAddress()->getBaseShippingAmount()
),
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
];
}

Expand All @@ -190,11 +210,14 @@ protected function getDiscounts(\Magento\Quote\Model\Quote $quote): ?array
if (!$quote->getBaseDiscountAmount()) {
return null;
}
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
return [
'displayName' => __('Discount'),
'amount' => [
'amount' => $this->formatPrice($quote->getBaseDiscountAmount()),
'currency' => $quote->getBaseCurrencyCode()
'amount' => $this->formatPrice($isCBTCurrencyAvailable
? $quote->getDiscountAmount()
: $quote->getBaseDiscountAmount()),
'currency' => $isCBTCurrencyAvailable ? $quote->getQuoteCurrencyCode() : $quote->getBaseCurrencyCode()
]
];
}
Expand Down
9 changes: 6 additions & 3 deletions Gateway/Request/ExpressCheckoutDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ public function build(array $buildSubject): array
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $buildSubject['quote'];

$currentCurrencyCode = $quote->getQuoteCurrencyCode();
$isCBTCurrencyAvailable = $this->checkCBTCurrencyAvailability->checkByQuote($quote);
$amount = $isCBTCurrencyAvailable ? $quote->getGrandTotal() : $quote->getBaseGrandTotal();
$currencyCode = $isCBTCurrencyAvailable ? $currentCurrencyCode : $quote->getBaseCurrencyCode();
$popupOriginUrl = $buildSubject['popup_origin_url'];

$data = [
'mode' => 'express',
'storeId' => $quote->getStoreId(),
'amount' => [
'amount' => $this->formatPrice($quote->getBaseGrandTotal()),
'currency' => $quote->getBaseCurrencyCode()
'amount' => $this->formatPrice($amount),
'currency' => $currencyCode
],
'merchant' => [
'popupOriginUrl' => $popupOriginUrl
Expand Down
14 changes: 13 additions & 1 deletion Gateway/Request/PaymentAction/AuthCaptureDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,28 @@ public function build(array $buildSubject): array
/** @var \Magento\Sales\Model\Order\Payment $payment */
$payment = $paymentDO->getPayment();

$isCBTCurrency = (bool) $payment->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
);

$CBTCurrency = $payment->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
);

$afterpayOrderId = $payment->getAdditionalInformation(
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ORDER_ID
);

$currencyCode = ($isCBTCurrency && $CBTCurrency)
? $CBTCurrency
: $payment->getOrder()->getBaseCurrencyCode();

return [
'storeId' => $paymentDO->getOrder()->getStoreId(),
'orderId' => $afterpayOrderId,
'amount' => [
'amount' => $this->formatPrice(SubjectReader::readAmount($buildSubject)),
'currency' => $payment->getOrder()->getBaseCurrencyCode()
'currency' => $currencyCode
]
];
}
Expand Down
14 changes: 12 additions & 2 deletions Gateway/Request/PaymentAction/CaptureDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ public function build(array $buildSubject): array
{
$paymentDO = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
$payment = $paymentDO->getPayment();
/** @var \Magento\Sales\Api\Data\OrderInterface $order */
$order = $payment->getOrder();

$isCBTCurrency = (bool) $payment->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
);
$cbtCurrency = $payment->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
);
$currencyCode = ($isCBTCurrency && $cbtCurrency) ? $cbtCurrency : $order->getBaseCurrencyCode();

$token = $payment->getAdditionalInformation(\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_TOKEN);
$data = [
'storeId' => $paymentDO->getOrder()->getStoreId(),
'storeId' => $order->getStoreId(),
'token' => $token
];

if ($payment->getAdditionalInformation('afterpay_express')) {
$data['amount'] = [
'amount' => \Magento\Payment\Gateway\Helper\SubjectReader::readAmount($buildSubject),
'currency' => $payment->getOrder()->getBaseCurrencyCode()
'currency' => $currencyCode
];
}

Expand Down
10 changes: 9 additions & 1 deletion Gateway/Request/PaymentAction/RefundAndVoidDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public function build(array $buildSubject): array
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ORDER_ID
);

$isCBTCurrency = (bool) $paymentDO->getPayment()->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
);
$CBTCurrency = $paymentDO->getPayment()->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_CBT_CURRENCY
);
$currencyCode = ($isCBTCurrency && $CBTCurrency) ? $CBTCurrency : $paymentDO->getOrder()->getCurrencyCode();

$data = [
'storeId' => $paymentDO->getOrder()->getStoreId(),
'orderId' => $afterpayOrderId
Expand All @@ -24,7 +32,7 @@ public function build(array $buildSubject): array
return array_merge($data, [
'amount' => [
'amount' => $this->formatPrice(SubjectReader::readAmount($buildSubject)),
'currency' => $paymentDO->getOrder()->getCurrencyCode()
'currency' => $currencyCode
]
]);
} catch (\InvalidArgumentException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ class CheckoutItemsAmountValidationHandler implements \Magento\Payment\Gateway\R
public function handle(array $handlingSubject, array $response)
{
$paymentDO = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($handlingSubject);

$payment = $paymentDO->getPayment();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $paymentDO->getPayment()->getQuote();
$quote = $payment->getQuote();
$isCBTCurrency = $payment->getAdditionalInformation(\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY);
$grandTotal = $isCBTCurrency ? $quote->getGrandTotal() : $quote->getBaseGrandTotal();

if (round(1 * $quote->getBaseGrandTotal(), 2) != round(1 * $response['amount']['amount'], 2)) {
if (round(1 * $grandTotal, 2) != round(1 * $response['amount']['amount'], 2)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('There are issues when processing your payment. Invalid Amount')
);
Expand Down
10 changes: 9 additions & 1 deletion Gateway/Response/DiscountHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public function handle(array $handlingSubject, array $response)

protected function getOrderDiscountAmount(\Magento\Sales\Model\Order $order): float
{
return (float)($order->getBaseGiftCardsAmount() + $order->getBaseCustomerBalanceAmount());
$isCBTCurrency = (bool) $order->getPayment()->getAdditionalInformation(
\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY
);

if ($isCBTCurrency) {
return (float)($order->getGiftCardsAmount() + $order->getCustomerBalanceAmount());
} else {
return (float)($order->getBaseGiftCardsAmount() + $order->getBaseCustomerBalanceAmount());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Afterpay\Afterpay\Gateway\Response\MerchantConfiguration;

class CBTAvailableCurrenciesConfigurationHandler implements \Magento\Payment\Gateway\Response\HandlerInterface
{
private $config;

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

public function handle(array $handlingSubject, array $response): void
{
$websiteId = (int)$handlingSubject['websiteId'];
$cbtAvailableCurrencies = [];
if (isset($response['CBT']['enabled']) &&
isset($response['CBT']['limits']) &&
is_array($response['CBT']['limits'])
) {
foreach ($response['CBT']['limits'] as $limit) {
if (isset($limit['maximumAmount']['currency']) && isset($limit['maximumAmount']['amount'])) {
$cbtAvailableCurrencies[] = $limit['maximumAmount']['currency'] . ':' . $limit['maximumAmount']['amount'];
}
}
}
$this->config->setCbtCurrencyLimits(implode(",", $cbtAvailableCurrencies), $websiteId);
}
}
5 changes: 4 additions & 1 deletion Gateway/Validator/CaptureResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public function validate(array $validationSubject): \Magento\Payment\Gateway\Val
if (isset($response['status']) && $response['status'] == self::STATUS_DECLINED) {
return $this->createResult(
false,
[__('Afterpay payment declined. Please select an alternative payment method.')]
[
__('%1 payment declined. Please select an alternative payment method.',
($validationSubject['payment'])->getPayment()->getMethodInstance()->getTitle())
]
);
}

Expand Down
33 changes: 33 additions & 0 deletions Gateway/Validator/Method/CurrencyValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Afterpay\Afterpay\Gateway\Validator\Method;

class CurrencyValidator extends \Magento\Payment\Gateway\Validator\AbstractValidator
{
private $config;
private $checkoutSession;

public function __construct(
\Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Afterpay\Afterpay\Model\Config $config
) {
$this->config = $config;
$this->checkoutSession = $checkoutSession;
parent::__construct($resultFactory);
}

public function validate(array $validationSubject): \Magento\Payment\Gateway\Validator\ResultInterface
{
$quote = $this->checkoutSession->getQuote();
$currentCurrency = $quote->getQuoteCurrencyCode();
$allowedCurrencies = $this->config->getAllowedCurrencies();
$cbtCurrencies = array_keys($this->config->getCbtCurrencyLimits());

if (in_array($currentCurrency, array_merge($allowedCurrencies, $cbtCurrencies))) {
return $this->createResult(true);
}

return $this->createResult(false);
}
}
10 changes: 10 additions & 0 deletions Model/CBT/CheckCBTCurrencyAvailabilityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Afterpay\Afterpay\Model\CBT;

interface CheckCBTCurrencyAvailabilityInterface
{
public function check(string $currencyCode, float $amount = null): bool;

public function checkByQuote(\Magento\Quote\Model\Quote $quote): bool;
}
Loading

0 comments on commit 4cc3f8d

Please sign in to comment.