Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose WCPay refund transaction id for refund endpoint #7932

Merged
merged 10 commits into from
Jan 5, 2024
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 WooCommerce transaction id for a refund on refund API endpoint
anu-rock marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1877,6 +1877,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_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_TRANSACTION_ID_META_KEY = '_refund_transaction_id';
anu-rock marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 mixed $order The order.
* @param string $wcpay_transaction_id The value to be set.
*
* @throws Order_Not_Found_Exception
*/
public function set_wcpay_transaction_id_for_order( $order, $wcpay_transaction_id ) {
anu-rock marked this conversation as resolved.
Show resolved Hide resolved
$order = $this->get_order( $order );
$order->update_meta_data( self::WCPAY_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_transaction_id_for_order() {
$wcpay_refund_transaction_id = 'txn_mock';
$this->order_service->set_wcpay_transaction_id_for_order( $this->order, $wcpay_refund_transaction_id );
$this->assertSame( $this->order->get_meta( WC_Payments_Order_Service::WCPAY_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
Loading