Skip to content

Commit

Permalink
Expose WCPay refund transaction id for refund endpoint (#7932)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Paun <[email protected]>
Co-authored-by: Cvetan Cvetanov <[email protected]>
  • Loading branch information
3 people authored Jan 5, 2024
1 parent fc29bee commit 439ec44
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog/add-transaction-id-to-refund-endpoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Expose the refund transaction ID in WooCommerce Order Refund API
1 change: 1 addition & 0 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
21 changes: 21 additions & 0 deletions includes/class-wc-payments-order-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test-class-wc-payments-order-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 439ec44

Please sign in to comment.