Skip to content

Commit 76a83b8

Browse files
Release version 4.3.5
1 parent 1f444a7 commit 76a83b8

17 files changed

Lines changed: 244 additions & 24 deletions

File tree

Gateway/Response/CaptureVirtualProductsHandler.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ class CaptureVirtualProductsHandler implements \Magento\Payment\Gateway\Response
66
{
77
private $authCaptureCommand;
88
private $paymentDataObjectFactory;
9-
private $orderAmountProcessor;
9+
private $virtualProductsAmountProcessor;
1010
private $voidCommand;
1111

1212
public function __construct(
13-
\Magento\Payment\Gateway\CommandInterface $authCaptureCommand,
14-
\Magento\Payment\Gateway\Data\PaymentDataObjectFactoryInterface $paymentDataObjectFactory,
15-
\Afterpay\Afterpay\Model\Payment\AmountProcessor\Order $orderAmountProcessor,
16-
\Magento\Payment\Gateway\CommandInterface $voidCommand
17-
)
18-
{
13+
\Magento\Payment\Gateway\CommandInterface $authCaptureCommand,
14+
\Magento\Payment\Gateway\Data\PaymentDataObjectFactoryInterface $paymentDataObjectFactory,
15+
\Afterpay\Afterpay\Model\Payment\AmountProcessor\VirtualProducts $virtualProductsAmountProcessor,
16+
\Magento\Payment\Gateway\CommandInterface $voidCommand
17+
) {
1918
$this->authCaptureCommand = $authCaptureCommand;
2019
$this->paymentDataObjectFactory = $paymentDataObjectFactory;
21-
$this->orderAmountProcessor = $orderAmountProcessor;
20+
$this->virtualProductsAmountProcessor = $virtualProductsAmountProcessor;
2221
$this->voidCommand = $voidCommand;
2322
}
2423

@@ -41,7 +40,7 @@ function ($item) {
4140
);
4241

4342
if (count($itemsToCapture)) {
44-
$amountToCapture = $this->orderAmountProcessor->process($itemsToCapture, $payment);
43+
$amountToCapture = $this->virtualProductsAmountProcessor->process($itemsToCapture, $payment);
4544
if ($amountToCapture > 0) {
4645
try {
4746
$this->authCaptureCommand->execute([

Gateway/Response/Checkout/CheckoutDataToQuoteHandler.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ class CheckoutDataToQuoteHandler implements \Magento\Payment\Gateway\Response\Ha
77
private $shippingInformationManagement;
88
private $shippingInformationFactory;
99
private $addressInterfaceFactory;
10+
private $region;
1011

1112
public function __construct(
1213
\Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement,
1314
\Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory $shippingInformationFactory,
14-
\Magento\Quote\Api\Data\AddressInterfaceFactory $addressInterfaceFactory
15+
\Magento\Quote\Api\Data\AddressInterfaceFactory $addressInterfaceFactory,
16+
\Magento\Directory\Model\Region $region
1517
) {
1618
$this->shippingInformationManagement = $shippingInformationManagement;
1719
$this->shippingInformationFactory = $shippingInformationFactory;
1820
$this->addressInterfaceFactory = $addressInterfaceFactory;
21+
$this->region = $region;
1922
}
2023

2124
public function handle(array $handlingSubject, array $response): void
@@ -60,15 +63,21 @@ public function handle(array $handlingSubject, array $response): void
6063
->setCountryId($response['shipping']['countryCode'])
6164
->setStreet([$response['shipping']['line1']])
6265
->setPostcode($response['shipping']['postcode'])
63-
->setRegion($response['shipping']['region'] ?? '');
66+
->setRegion($response['shipping']['region'] ?? '')
67+
->setRegionId(
68+
$this->region->loadByCode($address->getRegion(), $address->getCountryId())->getId()
69+
?? $this->region->loadByName($address->getRegion(), $address->getCountryId())->getId()
70+
);
6471
if (isset($response['shipping']['line2']) && $streetLine2 = $response['shipping']['line2']) {
6572
/** @var string[] $street */
6673
$street = array_merge($address->getStreet(), [$streetLine2]);
6774
$address->setStreet($street);
6875
}
6976

7077
$shippingInformation->setBillingAddress($address);
71-
if (!$quote->isVirtual()) {
78+
if ($quote->isVirtual()) {
79+
$shippingInformation->setShippingAddress($address); // to avoid an error with gift cart registry
80+
} else {
7281
$explodedShippingOption = explode('_', $response['shippingOptionIdentifier']);
7382
$carrierCode = array_shift($explodedShippingOption);
7483
$methodCode = implode('_', $explodedShippingOption);

Model/Config/Source/Category.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ private function renderSubCategory(array $categoryData, array &$optionsResult):
3737
$this->renderSubCategory($subCatData, $optionsResult);
3838
}
3939
}
40+
41+
if (!isset($categoryData['level']) || !is_numeric($categoryData['level']) || $categoryData['level'] < 2) {
42+
return;
43+
}
44+
4045
$optionsResult[] = [
4146
'label' => str_repeat('', $categoryData['level'] - 2) . $categoryData['label'],
4247
'value' => $categoryData['id']
@@ -49,15 +54,15 @@ private function getCategoriesTree(): array
4954

5055
$this->storeManager->setCurrentStore($this->getStoreIdByRequest() ?? $currentStoreId);
5156
$this->categorySourceRegistry->setShowAllCategories(true);
52-
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categories */
57+
/** @var \Magento\Framework\Data\Collection $categories */
5358
$categories = $this->categoryHelper->getStoreCategories(false, true);
5459
$this->categorySourceRegistry->setShowAllCategories(false);
5560
$this->storeManager->setCurrentStore($currentStoreId);
5661

5762
return $this->convertToTree($categories);
5863
}
5964

60-
private function convertToTree(\Magento\Catalog\Model\ResourceModel\Category\Collection $categories): array
65+
private function convertToTree(\Magento\Framework\Data\Collection $categories): array
6166
{
6267
$categoryById = [];
6368
foreach ($categories as $category) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Afterpay\Afterpay\Model\Payment\AmountProcessor;
4+
5+
class VirtualProducts extends \Afterpay\Afterpay\Model\Payment\AmountProcessor\Order
6+
{
7+
protected function processDiscount(float $amount, \Magento\Payment\Model\InfoInterface $payment): float
8+
{
9+
$capturedDiscount = $payment->getAdditionalInformation(
10+
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_CAPTURED_DISCOUNT
11+
) ?? 0;
12+
$totalDiscountAmount = $payment->getAdditionalInformation(
13+
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ROLLOVER_DISCOUNT
14+
) ?? 0;
15+
16+
$isCBTCurrency = $payment->getAdditionalInformation(\Afterpay\Afterpay\Api\Data\CheckoutInterface::AFTERPAY_IS_CBT_CURRENCY);
17+
$orderTotal = $isCBTCurrency ? $payment->getOrder()->getGrandTotal() : $payment->getOrder()->getBaseGrandTotal();
18+
$totalWithoutVirtualProducts = $orderTotal - $amount;
19+
$returnAmount = $amount;
20+
21+
if ($amount > $totalDiscountAmount) {
22+
$returnAmount -= $totalDiscountAmount;
23+
$capturedDiscount += $totalDiscountAmount;
24+
$totalDiscountAmount = '0.00';
25+
} else {
26+
if ($totalWithoutVirtualProducts < $totalDiscountAmount) {
27+
$returnAmount = $orderTotal;
28+
}
29+
30+
$openToCapture = $payment->getAdditionalInformation(
31+
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_OPEN_TO_CAPTURE_AMOUNT
32+
);
33+
if ($openToCapture && $openToCapture == $returnAmount) {
34+
$capturedDiscount += $totalDiscountAmount;
35+
$totalDiscountAmount = '0.00';
36+
}
37+
}
38+
39+
$payment->setAdditionalInformation(
40+
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_ROLLOVER_DISCOUNT,
41+
(string)$totalDiscountAmount
42+
);
43+
$payment->setAdditionalInformation(
44+
\Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface::AFTERPAY_CAPTURED_DISCOUNT,
45+
$capturedDiscount
46+
);
47+
48+
return $returnAmount;
49+
}
50+
}

Model/Shipment/Express/ShippingAddressUpdater.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function fillQuoteWithShippingAddress(
5252
$quoteShippingAddress->setCity($shippingAddress['suburb']);
5353
$quoteShippingAddress->setPostcode($shippingAddress['postcode']);
5454
$quoteShippingAddress->setRegionId(
55-
$this->region->loadByCode($shippingAddress['state'], $shippingAddress['countryCode'])
56-
->getId()
55+
$this->region->loadByCode($shippingAddress['state'], $shippingAddress['countryCode'])->getId()
56+
?? $this->region->loadByName($shippingAddress['state'], $shippingAddress['countryCode'])->getId()
5757
);
5858
$quoteShippingAddress->setRegion($shippingAddress['state']);
5959
$quoteShippingAddress->setTelephone($shippingAddress['phoneNumber']);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Afterpay\Afterpay\Observer;
4+
5+
use Afterpay\Afterpay\Gateway\Config\Config;
6+
use Afterpay\Afterpay\Model\Payment\AdditionalInformationInterface;
7+
use Magento\Framework\Event\Observer;
8+
use Magento\Framework\Event\ObserverInterface;
9+
use Magento\Framework\Exception\PaymentException;
10+
use Magento\Payment\Gateway\Command\CommandException;
11+
use Magento\Payment\Gateway\CommandInterface;
12+
use Magento\Payment\Gateway\Data\PaymentDataObjectFactoryInterface;
13+
use Magento\Sales\Model\Order;
14+
15+
class AuthCaptureBeforeCompleteOrder implements ObserverInterface
16+
{
17+
private $authCaptureCommand;
18+
19+
private $paymentDataObjectFactory;
20+
21+
public function __construct(
22+
CommandInterface $authCaptureCommand,
23+
PaymentDataObjectFactoryInterface $paymentDataObjectFactory
24+
) {
25+
$this->authCaptureCommand = $authCaptureCommand;
26+
$this->paymentDataObjectFactory = $paymentDataObjectFactory;
27+
}
28+
29+
/**
30+
* Observer that captures the remaining amount when the order status is complete.
31+
*
32+
* @throws PaymentException
33+
* @throws CommandException
34+
*/
35+
public function execute(Observer $observer): void
36+
{
37+
/** @var Order $order */
38+
$order = $observer->getEvent()->getOrder();
39+
$payment = $order->getPayment();
40+
41+
if (!$payment->getMethod() == Config::CODE || $order->getStatus() !== Order::STATE_COMPLETE) {
42+
return;
43+
}
44+
45+
$openToCapture = $payment->getAdditionalInformation(
46+
AdditionalInformationInterface::AFTERPAY_OPEN_TO_CAPTURE_AMOUNT
47+
);
48+
49+
if ($openToCapture > 0) {
50+
$this->authCaptureCommand->execute([
51+
'amount' => $openToCapture,
52+
'payment' => $this->paymentDataObjectFactory->create($payment)
53+
]);
54+
}
55+
}
56+
}

Plugin/Order/Payment/State/CaptureCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function aroundExecute(
3434
\Magento\Sales\Api\Data\OrderPaymentInterface $payment,
3535
$amount,
3636
\Magento\Sales\Api\Data\OrderInterface $order
37-
): \Magento\Framework\Phrase {
37+
) {
3838
if ($payment->getMethod() === \Afterpay\Afterpay\Gateway\Config\Config::CODE) {
3939
$state = Order::STATE_PROCESSING;
4040
$status = null;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"license": "Apache-2.0",
44
"type": "magento2-module",
55
"description": "Magento 2 Afterpay Payment Module",
6-
"version": "4.3.4",
6+
"version": "4.3.5",
77
"require": {
88
"php": "~7.1.3||~7.2.0||~7.3.0||~7.4.0",
99
"magento/framework": "^102.0",

etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,9 @@
398398
<type name="Magento\Quote\Api\CartManagementInterface">
399399
<plugin name="afterpay_check_is_redirect" type="Afterpay\Afterpay\Plugin\Quote\CheckoutManagement"/>
400400
</type>
401+
<type name="Afterpay\Afterpay\Observer\AuthCaptureBeforeCompleteOrder">
402+
<arguments>
403+
<argument name="authCaptureCommand" xsi:type="object">Afterpay\Afterpay\Gateway\Command\AuthCaptureCommand</argument>
404+
</arguments>
405+
</type>
401406
</config>

etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
<observer name="afterpay_auth_capture_after_creditmemo"
1717
instance="Afterpay\Afterpay\Observer\AuthCaptureAfterCreditMemo"/>
1818
</event>
19+
<event name="sales_order_save_before">
20+
<observer name="afterpay_auth_capture_before_complete_order" instance="Afterpay\Afterpay\Observer\AuthCaptureBeforeCompleteOrder"/>
21+
</event>
1922
</config>

0 commit comments

Comments
 (0)