process orders when credit card is approved

This commit is contained in:
David Remer 2020-07-16 10:16:43 +03:00
parent f9dac40c47
commit 241a5e170d
5 changed files with 36 additions and 10 deletions

View file

@ -99,10 +99,10 @@ return [
$requestData = $container->get('button.request-data');
$apiClient = $container->get('api.endpoint.order');
$sessionHandler = $container->get('session.handler');
$threeDSecure = $container->get('button.endpoint.helper.three-d-secure');
$threeDSecure = $container->get('button.helper.three-d-secure');
return new ApproveOrderEndpoint($requestData, $apiClient, $sessionHandler, $threeDSecure);
},
'button.endpoint.helper.three-d-secure' => static function (ContainerInterface $container): ThreeDSecure {
'button.helper.three-d-secure' => static function (ContainerInterface $container): ThreeDSecure {
return new ThreeDSecure();
},
];

View file

@ -58,7 +58,8 @@ class SmartButton implements SmartButtonInterface
$buttonRenderer = static function () {
$product = wc_get_product();
if (! is_checkout() && is_a($product, \WC_Product::class)
if (
! is_checkout() && is_a($product, \WC_Product::class)
&& (
$product->is_type(['external', 'grouped'])
|| ! $product->is_in_stock()
@ -75,7 +76,8 @@ class SmartButton implements SmartButtonInterface
return;
}
$product = wc_get_product();
if (! is_checkout() && is_a($product, \WC_Product::class)
if (
! is_checkout() && is_a($product, \WC_Product::class)
&& (
$product->is_type(['external', 'grouped'])
|| ! $product->is_in_stock()

View file

@ -77,12 +77,14 @@ return [
$orderEndpoint = $container->get('api.endpoint.order');
$paymentsEndpoint = $container->get('api.endpoint.payments');
$orderFactory = $container->get('api.factory.order');
$threeDsecure = $container->get('button.helper.three-d-secure');
return new OrderProcessor(
$sessionHandler,
$cartRepository,
$orderEndpoint,
$paymentsEndpoint,
$orderFactory
$orderFactory,
$threeDsecure
);
},
'wcgateway.processor.authorized-payments' => static function (ContainerInterface $container): AuthorizedPaymentsProcessor {

View file

@ -10,6 +10,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
@ -20,6 +21,7 @@ class OrderProcessor
private $orderEndpoint;
private $paymentsEndpoint;
private $orderFactory;
private $threedSecure;
private $lastError = '';
@ -28,7 +30,8 @@ class OrderProcessor
CartRepository $cartRepository,
OrderEndpoint $orderEndpoint,
PaymentsEndpoint $paymentsEndpoint,
OrderFactory $orderFactory
OrderFactory $orderFactory,
ThreeDSecure $threedSecure
) {
$this->sessionHandler = $sessionHandler;
@ -36,6 +39,7 @@ class OrderProcessor
$this->orderEndpoint = $orderEndpoint;
$this->paymentsEndpoint = $paymentsEndpoint;
$this->orderFactory = $orderFactory;
$this->threedSecure = $threedSecure;
}
public function process(\WC_Order $wcOrder, \WooCommerce $woocommerce): bool
@ -45,7 +49,7 @@ class OrderProcessor
$wcOrder->update_meta_data(WcGateway::INTENT_META_KEY, $order->intent());
$errorMessage = null;
if (!$order || !$order->status()->is(OrderStatus::APPROVED)) {
if (!$order || ! $this->orderIsApproved($order)) {
$errorMessage = __(
'The payment has not been approved yet.',
'woocommerce-paypal-commerce-gateway'
@ -98,4 +102,14 @@ class OrderProcessor
$order = $this->orderEndpoint->patchOrderWith($order, $updatedOrder);
return $order;
}
private function orderIsApproved(Order $order): bool
{
if ($order->status()->is(OrderStatus::APPROVED)) {
return true;
}
return $this->threedSecure->proceedWithOrder($order) === ThreeDSecure::PROCCEED;
}
}

View file

@ -10,6 +10,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\TestCase;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway;
@ -64,13 +65,15 @@ class OrderProcessorTest extends TestCase
->expects('fromWcOrder')
->with($wcOrder, $currentOrder)
->andReturn($currentOrder);
$threeDSecure = Mockery::mock(ThreeDSecure::class);
$testee = new OrderProcessor(
$sessionHandler,
$cartRepository,
$orderEndpoint,
$paymentsEndpoint,
$orderFactory
$orderFactory,
$threeDSecure
);
$cart = Mockery::mock(\WC_Cart::class);
@ -148,13 +151,15 @@ class OrderProcessorTest extends TestCase
->expects('fromWcOrder')
->with($wcOrder, $currentOrder)
->andReturn($currentOrder);
$threeDSecure = Mockery::mock(ThreeDSecure::class);
$testee = new OrderProcessor(
$sessionHandler,
$cartRepository,
$orderEndpoint,
$paymentsEndpoint,
$orderFactory
$orderFactory,
$threeDSecure
);
$cart = Mockery::mock(\WC_Cart::class);
@ -211,13 +216,16 @@ class OrderProcessorTest extends TestCase
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$orderFactory = Mockery::mock(OrderFactory::class);
$threeDSecure = Mockery::mock(ThreeDSecure::class);
$threeDSecure->expects('proceedWithOrder')->andReturn(ThreeDSecure::NO_DECISION);
$testee = new OrderProcessor(
$sessionHandler,
$cartRepository,
$orderEndpoint,
$paymentsEndpoint,
$orderFactory
$orderFactory,
$threeDSecure
);
$cart = Mockery::mock(\WC_Cart::class);