Skip to content

Commit

Permalink
Merge branch 'feature/async-captures-for-applepay' into release-week-38
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Sep 18, 2023
2 parents 1bc286d + 672cf13 commit 48cffdf
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 38 deletions.
22 changes: 18 additions & 4 deletions Observer/SalesOrderInvoiceRegister/CaptureInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Client\Payments\CapturePayment;
use Mollie\Payment\Service\Mollie\Order\CanUseManualCapture;
use Mollie\Payment\Service\Mollie\Order\UsedMollieApi;

class CaptureInvoice implements ObserverInterface
{
Expand All @@ -25,22 +27,34 @@ class CaptureInvoice implements ObserverInterface
* @var CapturePayment
*/
private $capturePayment;
/**
* @var CanUseManualCapture
*/
private $canUseManualCapture;
/**
* @var UsedMollieApi
*/
private $usedMollieApi;

public function __construct(
Config $config,
CapturePayment $capturePayment
CapturePayment $capturePayment,
CanUseManualCapture $canUseManualCapture,
UsedMollieApi $usedMollieApi
) {
$this->capturePayment = $capturePayment;
$this->config = $config;
$this->canUseManualCapture = $canUseManualCapture;
$this->usedMollieApi = $usedMollieApi;
}

public function execute(Observer $observer)
{
/** @var OrderInterface $order */
$order = $observer->getData('order');
$transactionId = $order->getMollieTransactionId() ?? '';
$useOrdersApi = substr($transactionId, 0, 4) == 'ord_';
if ($useOrdersApi || !$this->config->useManualCapture((int)$order->getStoreId())) {
if ($this->usedMollieApi->execute($order) == UsedMollieApi::TYPE_ORDERS ||
!$this->canUseManualCapture->execute($order)
) {
return;
}

Expand Down
14 changes: 5 additions & 9 deletions Service/Mollie/Order/CanRegisterCaptureNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Api\Resources\Payment;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Methods\Creditcard;

class CanRegisterCaptureNotification
{
/**
* @var Config
* @var CanUseManualCapture
*/
private $config;
private $canUseManualCapture;

public function __construct(
Config $config
CanUseManualCapture $canUseManualCapture
) {
$this->config = $config;
$this->canUseManualCapture = $canUseManualCapture;
}

public function execute(OrderInterface $order, Payment $molliePayment): bool
{
if (!$this->config->useManualCapture($order->getStoreId()) ||
$order->getPayment()->getMethod() != Creditcard::CODE
) {
if (!$this->canUseManualCapture->execute($order)) {
return true;
}

Expand Down
43 changes: 43 additions & 0 deletions Service/Mollie/Order/CanUseManualCapture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Mollie\Order;

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Methods\ApplePay;
use Mollie\Payment\Model\Methods\Creditcard;

class CanUseManualCapture
{
/**
* @var Config
*/
private $config;

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

public function execute(OrderInterface $order): bool
{
if (!$this->config->useManualCapture((int)$order->getStoreId())) {
return false;
}

$method = $order->getPayment()->getMethod();
$supportedMethods = [ApplePay::CODE, Creditcard::CODE];
if (!in_array($method, $supportedMethods)) {
return false;
}

return true;
}
}
24 changes: 13 additions & 11 deletions Service/Mollie/Order/CreateInvoiceOnShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
namespace Mollie\Payment\Service\Mollie\Order;

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Config;

class CreateInvoiceOnShipment
{
/**
* @var Config
* @var CanUseManualCapture
*/
private $config;
private $canUseManualCapture;
/**
* @var UsedMollieApi
*/
private $usedMollieApi;

public function __construct(
Config $config
CanUseManualCapture $canUseManualCapture,
UsedMollieApi $usedMollieApi
) {
$this->config = $config;
$this->canUseManualCapture = $canUseManualCapture;
$this->usedMollieApi = $usedMollieApi;
}

public function execute(OrderInterface $order): bool
Expand All @@ -32,13 +37,10 @@ public function execute(OrderInterface $order): bool
return true;
}

$transactionId = $order->getMollieTransactionId() ?? '';
$api = substr($transactionId, 0, 4) == 'ord_' ? 'orders' : 'payments';
if ($methodCode == 'mollie_methods_creditcard' &&
$this->config->useManualCapture($order->getStoreId()) &&
$api == 'payments'
if ($this->usedMollieApi->execute($order) == UsedMollieApi::TYPE_PAYMENTS &&
$this->canUseManualCapture->execute($order)
) {
return false;
return true;
}

return false;
Expand Down
23 changes: 23 additions & 0 deletions Service/Mollie/Order/UsedMollieApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Mollie\Order;

use Magento\Sales\Api\Data\OrderInterface;

class UsedMollieApi
{
const TYPE_ORDERS = 'orders';
const TYPE_PAYMENTS = 'payments';

public function execute(OrderInterface $order): string
{
$transactionId = $order->getMollieTransactionId() ?? '';
return substr($transactionId, 0, 4) == 'ord_' ? self::TYPE_ORDERS : self::TYPE_PAYMENTS;
}
}
15 changes: 6 additions & 9 deletions Service/Order/TransactionPart/CaptureMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
namespace Mollie\Payment\Service\Order\TransactionPart;

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Client\Orders;
use Mollie\Payment\Model\Client\Payments;
use Mollie\Payment\Service\Mollie\Order\CanUseManualCapture;
use Mollie\Payment\Service\Order\TransactionPartInterface;

class CaptureMode implements TransactionPartInterface
{
/**
* @var Config
* @var CanUseManualCapture
*/
private $config;
private $canUseManualCapture;

public function __construct(
Config $config
CanUseManualCapture $canUseManualCapture
) {
$this->config = $config;
$this->canUseManualCapture = $canUseManualCapture;
}

public function process(OrderInterface $order, $apiMethod, array $transaction)
Expand All @@ -27,9 +26,7 @@ public function process(OrderInterface $order, $apiMethod, array $transaction)
return $transaction;
}

if ($order->getPayment()->getMethod() != 'mollie_methods_creditcard' ||
!$this->config->useManualCapture($order->getStoreId())
) {
if (!$this->canUseManualCapture->execute($order)) {
return $transaction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function testCanCaptureWhenEnabledButNotCreditcard(): void
public function testCannotCaptureWhenEnabledAndCreditcard(): void
{
$order = $this->loadOrderById('100000001');
$order->getPayment()->setMethod('mollie_methods_creditcard');
$methods = ['mollie_methods_applepay', 'mollie_methods_creditcard'];
$order->getPayment()->setMethod($methods[array_rand($methods)]);

/** @var CanRegisterCaptureNotification $instance */
$instance = $this->objectManager->create(CanRegisterCaptureNotification::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function testIsEnabledByMethod(string $method): void
public function testIsDisabledWhenCreatePaymentAuthorizationIsEnabledAndApiIsPayments(): void
{
$order = $this->loadOrderById('100000001');
$order->getPayment()->setMethod('mollie_methods_creditcard');
$methods = ['mollie_methods_applepay', 'mollie_methods_creditcard'];
$order->getPayment()->setMethod($methods[array_rand($methods)]);
$order->setMollieTransactionId('tr_1234567890');

/** @var CreateInvoiceOnShipment $instance */
Expand All @@ -62,7 +63,8 @@ public function testIsDisabledWhenCreatePaymentAuthorizationIsEnabledAndApiIsPay
public function testIsDisabledWhenCreatePaymentAuthorizationIsEnabledAndApiIsOrders(): void
{
$order = $this->loadOrderById('100000001');
$order->getPayment()->setMethod('mollie_methods_creditcard');
$methods = ['mollie_methods_applepay', 'mollie_methods_creditcard'];
$order->getPayment()->setMethod($methods[array_rand($methods)]);
$order->setMollieTransactionId('ord_1234567890');

/** @var CreateInvoiceOnShipment $instance */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mollie\Payment\Model\Client\Orders;
use Mollie\Payment\Model\Client\Payments;
use Mollie\Payment\Model\Methods\ApplePay;
use Mollie\Payment\Model\Methods\Creditcard;
use Mollie\Payment\Model\Methods\Ideal;
use Mollie\Payment\Service\Order\TransactionPart\CaptureMode;
Expand Down Expand Up @@ -32,7 +33,7 @@ public function testDoesNothingWhenTheApiMethodIsOrders(): void
* @magentoDataFixture Magento/Sales/_files/order.php
* @return void
*/
public function testDoesNothingWhenThePaymentMethodIsNotCreditcard(): void
public function testDoesNothingWhenThePaymentMethodIsNotCreditCardOrApplePay(): void
{
$order = $this->loadOrderById('100000001');
$order->getPayment()->setMethod(Ideal::CODE);
Expand Down Expand Up @@ -72,7 +73,8 @@ public function testDoesNothingWhenNotEnabled(): void
public function testSetsTheModeWhenApplicable(): void
{
$order = $this->loadOrderById('100000001');
$order->getPayment()->setMethod(Creditcard::CODE);
$methods = [ApplePay::CODE, Creditcard::CODE];
$order->getPayment()->setMethod($methods[array_rand($methods)]);

/** @var CaptureMode $instance */
$instance = $this->objectManager->create(CaptureMode::class);
Expand Down

0 comments on commit 48cffdf

Please sign in to comment.