From d40384aa1a50df8cff789a0b3473cf0b1ec2f76d Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 14 Oct 2021 16:34:03 +0300 Subject: [PATCH] Set transaction id for authorization --- .../src/Processor/OrderProcessor.php | 36 ++++++++++--------- .../Processor/OrderProcessorTest.php | 3 +- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php index fdcf95076..02e05c335 100644 --- a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -176,7 +176,7 @@ class OrderProcessor { $transaction_id = $this->get_paypal_order_transaction_id( $order ); - if ( '' !== $transaction_id ) { + if ( $transaction_id ) { $this->set_order_transaction_id( $transaction_id, $wc_order ); } @@ -216,32 +216,34 @@ class OrderProcessor { } /** - * Retrieve transaction id from PayPal order. + * Retrieves transaction id from PayPal order. * - * @param Order $order Order to get transaction id from. + * @param Order $order The order to get transaction id from. * - * @return string + * @return string|null */ - private function get_paypal_order_transaction_id( Order $order ): string { - $purchase_units = $order->purchase_units(); - - if ( ! isset( $purchase_units[0] ) ) { - return ''; + private function get_paypal_order_transaction_id( Order $order ): ?string { + $purchase_unit = $order->purchase_units()[0] ?? null; + if ( ! $purchase_unit ) { + return null; } - $payments = $purchase_units[0]->payments(); - + $payments = $purchase_unit->payments(); if ( null === $payments ) { - return ''; + return null; } - $captures = $payments->captures(); - - if ( isset( $captures[0] ) ) { - return $captures[0]->id(); + $capture = $payments->captures()[0] ?? null; + if ( $capture ) { + return $capture->id(); } - return ''; + $authorization = $payments->authorizations()[0] ?? null; + if ( $authorization ) { + return $authorization->id(); + } + + return null; } /** diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index 9872019a3..cab412b47 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -159,7 +159,8 @@ class OrderProcessorTest extends TestCase $wcOrder ->expects('update_status') ->with('on-hold', 'Awaiting payment.'); - + $wcOrder->expects('set_transaction_id') + ->with($transactionId); $this->assertTrue($testee->process($wcOrder)); }