wc_refund_payment()
Try to refund the payment for an order via the gateway.
No Hooks.
Return
true|false|WP_Error
.
Usage
wc_refund_payment( $order, $amount, $reason );
- $order(WC_Order) (required)
- Order instance.
- $amount(string) (required)
- Amount to refund.
- $reason(string)
- Refund reason.
Default: ''
Changelog
Since 3.0.0 | Introduced. |
wc_refund_payment() wc refund payment code WC 9.4.2
function wc_refund_payment( $order, $amount, $reason = '' ) { try { if ( ! is_a( $order, 'WC_Order' ) ) { throw new Exception( __( 'Invalid order.', 'woocommerce' ) ); } $gateway_controller = WC_Payment_Gateways::instance(); $all_gateways = $gateway_controller->payment_gateways(); $payment_method = $order->get_payment_method(); $gateway = isset( $all_gateways[ $payment_method ] ) ? $all_gateways[ $payment_method ] : false; if ( ! $gateway ) { throw new Exception( __( 'The payment gateway for this order does not exist.', 'woocommerce' ) ); } if ( ! $gateway->supports( 'refunds' ) ) { throw new Exception( __( 'The payment gateway for this order does not support automatic refunds.', 'woocommerce' ) ); } $result = $gateway->process_refund( $order->get_id(), $amount, $reason ); if ( ! $result ) { throw new Exception( __( 'An error occurred while attempting to create the refund using the payment gateway API.', 'woocommerce' ) ); } if ( is_wp_error( $result ) ) { throw new Exception( $result->get_error_message() ); } return true; } catch ( Exception $e ) { return new WP_Error( 'error', $e->getMessage() ); } }