WordPress at Your Fingertips
function is not described

WC_API_Orders::create_order_refund() public WC 2.2

Create a new order refund for the given order

{} It's a method of the class: WC_API_Orders{}

Return

WP_Error|Array. error or created refund response data

Usage

$WC_API_Orders = new WC_API_Orders();
$WC_API_Orders->create_order_refund( $order_id, $data, $api_refund );
$order_id(string) (required)
order ID
$data(array) (required)
raw request data
$api_refund(true|false)
do refund using a payment gateway API
Default: true

Changelog

Since 2.2 Introduced.

Code of WC_API_Orders::create_order_refund() WC 5.7.1

public function create_order_refund( $order_id, $data, $api_refund = true ) {
	try {
		if ( ! isset( $data['order_refund'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_order_refund_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'order_refund' ), 400 );
		}

		$data = $data['order_refund'];

		// Permission check
		if ( ! current_user_can( 'publish_shop_orders' ) ) {
			throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_refund', __( 'You do not have permission to create order refunds', 'woocommerce' ), 401 );
		}

		$order_id = absint( $order_id );

		if ( empty( $order_id ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), 400 );
		}

		$data = apply_filters( 'woocommerce_api_create_order_refund_data', $data, $order_id, $this );

		// Refund amount is required
		if ( ! isset( $data['amount'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount is required.', 'woocommerce' ), 400 );
		} elseif ( 0 > $data['amount'] ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount must be positive.', 'woocommerce' ), 400 );
		}

		$data['order_id']  = $order_id;
		$data['refund_id'] = 0;

		// Create the refund
		$refund = wc_create_refund( $data );

		if ( ! $refund ) {
			throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 );
		}

		// Refund via API
		if ( $api_refund ) {
			if ( WC()->payment_gateways() ) {
				$payment_gateways = WC()->payment_gateways->payment_gateways();
			}

			$order = wc_get_order( $order_id );

			if ( isset( $payment_gateways[ $order->get_payment_method() ] ) && $payment_gateways[ $order->get_payment_method() ]->supports( 'refunds' ) ) {
				$result = $payment_gateways[ $order->get_payment_method() ]->process_refund( $order_id, $refund->get_amount(), $refund->get_reason() );

				if ( is_wp_error( $result ) ) {
					return $result;
				} elseif ( ! $result ) {
					throw new WC_API_Exception( 'woocommerce_api_create_order_refund_api_failed', __( 'An error occurred while attempting to create the refund using the payment gateway API.', 'woocommerce' ), 500 );
				}
			}
		}

		// HTTP 201 Created
		$this->server->send_status( 201 );

		do_action( 'woocommerce_api_create_order_refund', $refund->get_id(), $order_id, $this );

		return $this->get_order_refund( $order_id, $refund->get_id() );
	} catch ( WC_Data_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}