From c072e8550a8d4647c311e83250ff7b06f1c2ca62 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Thu, 18 Nov 2021 14:45:34 +0100 Subject: [PATCH 1/2] Add retry counter meta to order to avoid duplicate invoice error on consequent tries --- .../src/Factory/PurchaseUnitFactory.php | 6 ++-- .../src/Gateway/ProcessPaymentTrait.php | 6 ++++ .../src/Processor/OrderProcessor.php | 2 -- .../Factory/PurchaseUnitFactoryTest.php | 3 ++ .../WcGateway/Gateway/WcGatewayTest.php | 14 +++++++- .../Processor/OrderProcessorTest.php | 35 ------------------- 6 files changed, 26 insertions(+), 40 deletions(-) diff --git a/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php b/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php index ce97bb5bc..c8c677b13 100644 --- a/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php +++ b/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php @@ -121,9 +121,11 @@ public function from_wc_order( \WC_Order $order ): PurchaseUnit { $payee = $this->payee_repository->payee(); $wc_order_id = $order->get_order_number(); $custom_id = $this->prefix . $wc_order_id; - $invoice_id = $this->prefix . $wc_order_id; + $retry = $order->get_meta( 'ppcp-retry' ) ? '-' . $order->get_meta( 'ppcp-retry' ) : ''; + $invoice_id = $this->prefix . $wc_order_id . $retry; $soft_descriptor = ''; - $purchase_unit = new PurchaseUnit( + + $purchase_unit = new PurchaseUnit( $amount, $items, $shipping, diff --git a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php index de1e0a70c..cfe84e45d 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/ProcessPaymentTrait.php @@ -253,13 +253,19 @@ function ( Authorization $authorization ): bool { } } + // Adds retry counter meta to avoid duplicate invoice id error on consequent tries. + $wc_order->update_meta_data( 'ppcp-retry', (int) $wc_order->get_meta( 'ppcp-retry' ) + 1 ); + $wc_order->save_meta_data(); + $this->session_handler->destroy_session_data(); wc_add_notice( $error_message, 'error' ); return $failure_data; } + WC()->cart->empty_cart(); $this->session_handler->destroy_session_data(); + return array( 'result' => 'success', 'redirect' => $this->get_return_url( $wc_order ), diff --git a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php index dc4796f42..22b06ae1a 100644 --- a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -189,8 +189,6 @@ public function process( \WC_Order $wc_order ): bool { $wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'true' ); $wc_order->update_status( 'processing' ); } - WC()->cart->empty_cart(); - $this->session_handler->destroy_session_data(); $this->last_error = ''; return true; } diff --git a/tests/PHPUnit/ApiClient/Factory/PurchaseUnitFactoryTest.php b/tests/PHPUnit/ApiClient/Factory/PurchaseUnitFactoryTest.php index 199bf13d8..b68cdd685 100644 --- a/tests/PHPUnit/ApiClient/Factory/PurchaseUnitFactoryTest.php +++ b/tests/PHPUnit/ApiClient/Factory/PurchaseUnitFactoryTest.php @@ -25,6 +25,7 @@ public function testWcOrderDefault() $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder ->expects('get_order_number')->andReturn($wcOrderId); + $wcOrder->expects('get_meta')->andReturn(''); $amount = Mockery::mock(Amount::class); $amountFactory = Mockery::mock(AmountFactory::class); $amountFactory @@ -89,6 +90,7 @@ public function testWcOrderShippingGetsDroppedWhenNoPostalCode() $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder ->expects('get_order_number')->andReturn(1); + $wcOrder->expects('get_meta')->andReturn(''); $amount = Mockery::mock(Amount::class); $amountFactory = Mockery::mock(AmountFactory::class); $amountFactory @@ -144,6 +146,7 @@ public function testWcOrderShippingGetsDroppedWhenNoCountryCode() $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder ->expects('get_order_number')->andReturn(1); + $wcOrder->expects('get_meta')->andReturn(''); $amount = Mockery::mock(Amount::class); $amountFactory = Mockery::mock(AmountFactory::class); $amountFactory diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index edfaf2251..6fd2b3195 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -42,7 +42,7 @@ public function testProcessPaymentSuccess() { $orderId = 1; $wcOrder = Mockery::mock(\WC_Order::class); $wcOrder->shouldReceive('get_customer_id')->andReturn(1); - + $wcOrder->shouldReceive('get_meta')->andReturn(''); $settingsRenderer = Mockery::mock(SettingsRenderer::class); $orderProcessor = Mockery::mock(OrderProcessor::class); $orderProcessor @@ -106,6 +106,18 @@ function(\WC_Order $order) use ($wcOrder) : bool { when('wc_get_checkout_url') ->justReturn('test'); + $woocommerce = Mockery::mock(\WooCommerce::class); + $session = Mockery::mock(\WC_Session::class); + when('WC')->justReturn($woocommerce); + $woocommerce->session = $session; + $session->shouldReceive('get')->andReturn([]); + + $woocommerce = Mockery::mock(\WooCommerce::class); + $cart = Mockery::mock(\WC_Cart::class); + when('WC')->justReturn($woocommerce); + $woocommerce->cart = $cart; + $cart->shouldReceive('empty_cart'); + $result = $testee->process_payment($orderId); $this->assertIsArray($result); diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index addf60602..68433295a 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -88,8 +88,6 @@ public function testAuthorize() { $sessionHandler ->expects('order') ->andReturn($currentOrder); - $sessionHandler - ->expects('destroy_session_data'); $orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint @@ -129,15 +127,6 @@ public function testAuthorize() { $this->environment ); - $cart = Mockery::mock(\WC_Cart::class); - $cart - ->expects('empty_cart'); - $woocommerce = Mockery::mock(\WooCommerce::class); - when('WC') - ->justReturn($woocommerce); - - $woocommerce->cart = $cart; - $wcOrder ->expects('update_meta_data') ->with( @@ -211,8 +200,6 @@ public function testCapture() { $sessionHandler ->expects('order') ->andReturn($currentOrder); - $sessionHandler - ->expects('destroy_session_data'); $orderEndpoint = Mockery::mock(OrderEndpoint::class); $orderEndpoint ->expects('patch_order_with') @@ -234,21 +221,8 @@ public function testCapture() { ->shouldReceive('has') ->andReturnFalse(); - $cart = Mockery::mock(\WC_Cart::class); - $cart - ->shouldReceive('empty_cart'); - - $woocommerce = Mockery::Mock(\Woocommerce::class); - $woocommerce - ->shouldReceive('__get') - ->with('cart') - ->set('cart', $cart); - when('WC') - ->justReturn($woocommerce); - $logger = Mockery::mock(LoggerInterface::class); - $testee = new OrderProcessor( $sessionHandler, $orderEndpoint, @@ -260,15 +234,6 @@ public function testCapture() { $this->environment ); - $cart = Mockery::mock(\WC_Cart::class); - $cart - ->expects('empty_cart'); - $woocommerce = Mockery::mock(\WooCommerce::class); - $woocommerce->cart = $cart; - - when('WC') - ->justReturn($woocommerce); - $wcOrder ->expects('update_meta_data') ->with( From 626f4804cbe07787eb108e3611f5e15eb56337fb Mon Sep 17 00:00:00 2001 From: dinamiko Date: Thu, 18 Nov 2021 14:49:42 +0100 Subject: [PATCH 2/2] Remove unused stuff on test --- tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index 6fd2b3195..42300db66 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -106,12 +106,6 @@ function(\WC_Order $order) use ($wcOrder) : bool { when('wc_get_checkout_url') ->justReturn('test'); - $woocommerce = Mockery::mock(\WooCommerce::class); - $session = Mockery::mock(\WC_Session::class); - when('WC')->justReturn($woocommerce); - $woocommerce->session = $session; - $session->shouldReceive('get')->andReturn([]); - $woocommerce = Mockery::mock(\WooCommerce::class); $cart = Mockery::mock(\WC_Cart::class); when('WC')->justReturn($woocommerce);