From 7a994f8d28c6e1778bd572bc81b34f4d01442045 Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 17 Mar 2023 17:23:33 +0200 Subject: [PATCH] Do not save refund id after ppcp_refund_order --- api/order-functions.php | 8 +++++--- .../ppcp-wc-gateway/src/Processor/RefundProcessor.php | 11 ++++++----- tests/PHPUnit/Api/OrderRefundTest.php | 4 +++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/api/order-functions.php b/api/order-functions.php index c0851e77a..f477651fd 100644 --- a/api/order-functions.php +++ b/api/order-functions.php @@ -73,21 +73,23 @@ function ppcp_capture_order( WC_Order $wc_order ): void { } /** - * Refunds the order. + * Refunds the PayPal order. + * Note that you can use wc_refund_payment() to trigger the refund in WC and PayPal. * * @param WC_Order $wc_order The WC order. * @param float $amount The refund amount. * @param string $reason The reason for the refund. + * @return string The PayPal refund ID. * @throws InvalidArgumentException When the order cannot be refunded. * @throws Exception When the operation fails. */ -function ppcp_refund_order( WC_Order $wc_order, float $amount, string $reason = '' ): void { +function ppcp_refund_order( WC_Order $wc_order, float $amount, string $reason = '' ): string { $order = ppcp_get_paypal_order( $wc_order ); $refund_processor = PPCP::container()->get( 'wcgateway.processor.refunds' ); assert( $refund_processor instanceof RefundProcessor ); - $refund_processor->refund( $order, $wc_order, $amount, $reason ); + return $refund_processor->refund( $order, $wc_order, $amount, $reason ); } /** diff --git a/modules/ppcp-wc-gateway/src/Processor/RefundProcessor.php b/modules/ppcp-wc-gateway/src/Processor/RefundProcessor.php index cafea380a..0cfea30ad 100644 --- a/modules/ppcp-wc-gateway/src/Processor/RefundProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/RefundProcessor.php @@ -103,7 +103,9 @@ class RefundProcessor { switch ( $mode ) { case self::REFUND_MODE_REFUND: - $this->refund( $order, $wc_order, $amount, $reason ); + $refund_id = $this->refund( $order, $wc_order, $amount, $reason ); + + $this->add_refund_to_meta( $wc_order, $refund_id ); break; case self::REFUND_MODE_VOID: @@ -133,13 +135,14 @@ class RefundProcessor { * @param string $reason The reason for the refund. * * @throws RuntimeException When operation fails. + * @return string The PayPal refund ID. */ public function refund( Order $order, WC_Order $wc_order, float $amount, string $reason = '' - ): void { + ): string { $payments = $this->get_payments( $order ); $captures = $payments->captures(); @@ -157,9 +160,7 @@ class RefundProcessor { ) ); - $refund_id = $this->payments_endpoint->refund( $refund ); - - $this->add_refund_to_meta( $wc_order, $refund_id ); + return $this->payments_endpoint->refund( $refund ); } /** diff --git a/tests/PHPUnit/Api/OrderRefundTest.php b/tests/PHPUnit/Api/OrderRefundTest.php index 53b0e2792..aa3d4fc4e 100644 --- a/tests/PHPUnit/Api/OrderRefundTest.php +++ b/tests/PHPUnit/Api/OrderRefundTest.php @@ -48,9 +48,11 @@ class OrderRefundTest extends ModularTestCase $this->refundProcessor ->expects('refund') + ->andReturn('456qwe') ->once(); - ppcp_refund_order($wcOrder, 42.0, 'reason'); + $refund_id = ppcp_refund_order($wcOrder, 42.0, 'reason'); + $this->assertEquals('456qwe', $refund_id); } public function testOrderWithoutId(): void {