From 439ec445d7cf323da6a0276553ed0c73d480e6cd Mon Sep 17 00:00:00 2001 From: Dan Paun <82826872+dpaun1985@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:23:11 +0200 Subject: [PATCH] Expose WCPay refund transaction id for refund endpoint (#7932) Co-authored-by: Dan Paun Co-authored-by: Cvetan Cvetanov --- .../add-transaction-id-to-refund-endpoint | 4 ++++ includes/class-wc-payment-gateway-wcpay.php | 1 + includes/class-wc-payments-order-service.php | 21 +++++++++++++++++++ .../test-class-wc-payments-order-service.php | 6 ++++++ 4 files changed, 32 insertions(+) create mode 100644 changelog/add-transaction-id-to-refund-endpoint diff --git a/changelog/add-transaction-id-to-refund-endpoint b/changelog/add-transaction-id-to-refund-endpoint new file mode 100644 index 00000000000..c99092fb388 --- /dev/null +++ b/changelog/add-transaction-id-to-refund-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Expose the refund transaction ID in WooCommerce Order Refund API diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index 6331101bd25..7e55bda63b3 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -2285,6 +2285,7 @@ static function ( $refund ) use ( $refund_amount ) { $wc_last_refund = WC_Payments_Utils::get_last_refund_from_order_id( $order_id ); if ( $wc_last_refund ) { $this->order_service->set_wcpay_refund_id_for_order( $wc_last_refund, $refund['id'] ); + $this->order_service->set_wcpay_refund_transaction_id_for_order( $wc_last_refund, $refund['balance_transaction'] ); $wc_last_refund->save_meta_data(); } diff --git a/includes/class-wc-payments-order-service.php b/includes/class-wc-payments-order-service.php index eab529f6d92..04ea3f9cad7 100644 --- a/includes/class-wc-payments-order-service.php +++ b/includes/class-wc-payments-order-service.php @@ -83,6 +83,13 @@ class WC_Payments_Order_Service { */ const WCPAY_REFUND_ID_META_KEY = '_wcpay_refund_id'; + /** + * Meta key used to store WCPay refund transaction id. + * + * @const string + */ + const WCPAY_REFUND_TRANSACTION_ID_META_KEY = '_wcpay_refund_transaction_id'; + /** * Meta key used to store WCPay refund status. * @@ -651,6 +658,20 @@ public function set_wcpay_refund_id_for_order( $order, $wcpay_refund_id ) { $order->save_meta_data(); } + /** + * Set the payment metadata for refund transaction id. + * + * @param WC_Order_Refund $order The order. + * @param string $wcpay_transaction_id The value to be set. + * + * @throws Order_Not_Found_Exception + */ + public function set_wcpay_refund_transaction_id_for_order( WC_Order_Refund $order, string $wcpay_transaction_id ) { + $order = $this->get_order( $order ); + $order->update_meta_data( self::WCPAY_REFUND_TRANSACTION_ID_META_KEY, $wcpay_transaction_id ); + $order->save_meta_data(); + } + /** * Get the payment metadata for refund id. * diff --git a/tests/unit/test-class-wc-payments-order-service.php b/tests/unit/test-class-wc-payments-order-service.php index 1107c94fcd5..83ea7139ed9 100644 --- a/tests/unit/test-class-wc-payments-order-service.php +++ b/tests/unit/test-class-wc-payments-order-service.php @@ -1155,6 +1155,12 @@ public function test_set_wcpay_refund_id() { $this->assertEquals( $this->order->get_meta( '_wcpay_refund_id', true ), $wcpay_refund_id ); } + public function set_wcpay_refund_transaction_id_for_order() { + $wcpay_refund_transaction_id = 'txn_mock'; + $this->order_service->set_wcpay_refund_transaction_id_for_order( $this->order, $wcpay_refund_transaction_id ); + $this->assertSame( $this->order->get_meta( WC_Payments_Order_Service::WCPAY_REFUND_TRANSACTION_ID_META_KEY, true ), $wcpay_refund_transaction_id ); + } + public function test_get_wcpay_refund_id() { $wcpay_refund_id = 'ri_1234'; $this->order->update_meta_data( '_wcpay_refund_id', $wcpay_refund_id );