WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Купить персональные IPV4 и IPV6 прокси
функция не описана

WC_API_Orders::create_order_refund() WC 2.2

Create a new order refund for the given order

Это метод класса: WC_API_Orders

Возвращает

WP_Error/Массив. error or created refund response data

Использование

$WC_API_Orders = new WC_API_Orders();
$var = $WC_API_Orders->create_order_refund( $order_id, $data, $api_refund );
$order_id(строка) (обязательный)
order ID
$data(массив) (обязательный)
raw request data
$api_refund(true/false)
do refund using a payment gateway API
По умолчанию: true

Список изменений

С версии 2.2 Введена.

Код WC API Orders::create order refund: woocommerce/includes/legacy/api/v2/class-wc-api-orders.php WC 3.7.0

<?php
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() ) );
	}
}