woocommerce/includes/api/class-wc-rest-order-refunds...

584 lines
18 KiB
PHP
Raw Normal View History

2016-02-17 19:29:09 +00:00
<?php
/**
* REST API Order Refunds controller
*
* Handles requests to the /orders/<order_id>/refunds endpoint.
*
2018-03-06 18:04:58 +00:00
* @package WooCommerce/API
* @since 2.6.0
2016-02-17 19:29:09 +00:00
*/
2018-03-06 18:04:58 +00:00
defined( 'ABSPATH' ) || exit;
2016-02-17 19:29:09 +00:00
/**
* REST API Order Refunds controller class.
*
* @package WooCommerce/API
* @extends WC_REST_Orders_Controller
2016-02-17 19:29:09 +00:00
*/
2016-08-22 10:00:31 +00:00
class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller {
2016-02-17 19:29:09 +00:00
/**
* Endpoint namespace.
*
* @var string
*/
2017-02-09 17:06:13 +00:00
protected $namespace = 'wc/v2';
2016-02-17 19:29:09 +00:00
/**
* Route base.
*
* @var string
*/
2016-02-22 18:49:38 +00:00
protected $rest_base = 'orders/(?P<order_id>[\d]+)/refunds';
2016-02-17 19:29:09 +00:00
/**
* Post type.
2016-02-17 19:29:09 +00:00
*
* @var string
*/
protected $post_type = 'shop_order_refund';
2016-02-17 19:29:09 +00:00
2016-08-22 10:00:31 +00:00
/**
* Stores the request.
*
2016-08-22 10:00:31 +00:00
* @var array
*/
protected $request = array();
/**
* Order refunds actions.
*/
public function __construct() {
2017-02-16 23:45:12 +00:00
add_filter( "woocommerce_rest_{$this->post_type}_object_trashable", '__return_false' );
}
2016-02-17 19:29:09 +00:00
/**
* Register the routes for order refunds.
*/
public function register_routes() {
2018-01-25 16:04:11 +00:00
register_rest_route(
$this->namespace, '/' . $this->rest_base, array(
'args' => array(
'order_id' => array(
'description' => __( 'The order ID.', 'woocommerce' ),
'type' => 'integer',
),
2017-01-26 20:33:39 +00:00
),
2018-01-25 16:04:11 +00:00
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
2017-01-26 20:33:39 +00:00
),
2018-01-25 16:04:11 +00:00
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
2017-01-26 19:22:57 +00:00
),
2018-01-25 16:04:11 +00:00
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
'args' => array(
'order_id' => array(
'description' => __( 'The order ID.', 'woocommerce' ),
'type' => 'integer',
),
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'integer',
),
),
2018-01-25 16:04:11 +00:00
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
2018-01-25 16:04:11 +00:00
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'default' => true,
'type' => 'boolean',
'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
2016-02-17 19:29:09 +00:00
}
/**
2017-02-16 23:45:12 +00:00
* Get object.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-02-16 23:45:12 +00:00
* @param int $id Object ID.
* @return WC_Data
*/
protected function get_object( $id ) {
return wc_get_order( $id );
}
/**
* Get formatted item data.
2017-02-16 23:45:12 +00:00
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param WC_Data $object WC_Data instance.
* @return array
*/
protected function get_formatted_item_data( $object ) {
2017-02-16 23:45:12 +00:00
$data = $object->get_data();
2016-08-22 12:04:57 +00:00
$format_decimal = array( 'amount' );
$format_date = array( 'date_created' );
2016-08-22 10:00:31 +00:00
$format_line_items = array( 'line_items' );
// Format decimal values.
foreach ( $format_decimal as $key ) {
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
}
// Format date values.
foreach ( $format_date as $key ) {
2017-03-10 18:54:59 +00:00
$datetime = $data[ $key ];
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
2016-08-22 10:00:31 +00:00
}
// Format line items.
foreach ( $format_line_items as $key ) {
$data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) );
}
return array(
'id' => $object->get_id(),
'date_created' => $data['date_created'],
'date_created_gmt' => $data['date_created_gmt'],
'amount' => $data['amount'],
'reason' => $data['reason'],
'refunded_by' => $data['refunded_by'],
'refunded_payment' => $data['refunded_payment'],
'meta_data' => $data['meta_data'],
'line_items' => $data['line_items'],
);
}
/**
* Prepare a single order output for response.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*
* @param WC_Data $object Object data.
* @param WP_REST_Request $request Request object.
*
* @return WP_Error|WP_REST_Response
*/
public function prepare_object_for_response( $object, $request ) {
$this->request = $request;
2017-11-09 19:34:25 +00:00
$this->request['dp'] = is_null( $this->request['dp'] ) ? wc_get_price_decimals() : absint( $this->request['dp'] );
$order = wc_get_order( (int) $request['order_id'] );
if ( ! $order ) {
return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
}
if ( ! $object || $object->get_parent_id() !== $order->get_id() ) {
return new WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
}
2017-03-10 19:05:32 +00:00
$data = $this->get_formatted_item_data( $object );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
2017-02-16 23:45:12 +00:00
$response->add_links( $this->prepare_links( $object, $request ) );
/**
* Filter the data for a response.
*
2017-02-16 23:45:12 +00:00
* The dynamic portion of the hook name, $this->post_type,
* refers to object type being prepared for the response.
*
2017-02-16 23:45:12 +00:00
* @param WP_REST_Response $response The response object.
* @param WC_Data $object Object data.
* @param WP_REST_Request $request Request object.
*/
2017-02-16 23:45:12 +00:00
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request );
}
/**
* Prepare links for the request.
*
2017-02-16 23:45:12 +00:00
* @param WC_Data $object Object data.
* @param WP_REST_Request $request Request object.
2017-02-16 23:45:12 +00:00
* @return array Links for the given post.
*/
2017-02-16 23:45:12 +00:00
protected function prepare_links( $object, $request ) {
2018-01-25 16:04:11 +00:00
$base = str_replace( '(?P<order_id>[\d]+)', $object->get_parent_id(), $this->rest_base );
$links = array(
'self' => array(
2017-02-16 23:45:12 +00:00
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $object->get_id() ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
),
2018-01-25 16:04:11 +00:00
'up' => array(
2017-02-16 23:45:12 +00:00
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object->get_parent_id() ) ),
),
);
return $links;
}
/**
2017-02-16 23:45:12 +00:00
* Prepare objects query.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-02-16 23:45:12 +00:00
* @param WP_REST_Request $request Full details about the request.
* @return array
*/
2017-02-16 23:45:12 +00:00
protected function prepare_objects_query( $request ) {
$args = parent::prepare_objects_query( $request );
$args['post_status'] = array_keys( wc_get_order_statuses() );
2016-08-22 10:00:31 +00:00
$args['post_parent__in'] = array( absint( $request['order_id'] ) );
2017-02-16 03:27:44 +00:00
return $args;
}
2016-03-28 20:33:13 +00:00
/**
2017-02-16 23:45:12 +00:00
* Prepares one object for create or update operation.
2016-03-28 20:33:13 +00:00
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-02-16 23:45:12 +00:00
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
* @return WP_Error|WC_Data The prepared item, or WP_Error object on failure.
2016-03-28 20:33:13 +00:00
*/
2017-02-16 23:45:12 +00:00
protected function prepare_object_for_database( $request, $creating = false ) {
$order = wc_get_order( (int) $request['order_id'] );
2016-03-28 20:33:13 +00:00
2017-02-16 23:45:12 +00:00
if ( ! $order ) {
return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
2016-03-28 20:33:13 +00:00
}
if ( 0 > $request['amount'] ) {
return new WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 );
}
// Create the refund.
2018-01-25 16:04:11 +00:00
$refund = wc_create_refund(
array(
'order_id' => $order->get_id(),
'amount' => $request['amount'],
'reason' => empty( $request['reason'] ) ? null : $request['reason'],
'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true,
'restock_items' => true,
)
);
2016-03-28 20:33:13 +00:00
2017-02-08 16:20:42 +00:00
if ( is_wp_error( $refund ) ) {
return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 );
2016-03-28 20:33:13 +00:00
}
2017-02-08 16:20:42 +00:00
if ( ! $refund ) {
return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 );
2016-03-28 20:33:13 +00:00
}
2017-09-01 10:02:11 +00:00
if ( ! empty( $request['meta_data'] ) && is_array( $request['meta_data'] ) ) {
foreach ( $request['meta_data'] as $meta ) {
$refund->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
}
$refund->save_meta_data();
}
2016-03-28 20:33:13 +00:00
/**
2017-02-16 23:45:12 +00:00
* Filters an object before it is inserted via the REST API.
2016-03-28 20:33:13 +00:00
*
2017-02-16 23:45:12 +00:00
* The dynamic portion of the hook name, `$this->post_type`,
* refers to the object type slug.
*
* @param WC_Data $coupon Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
2016-03-28 20:33:13 +00:00
*/
2017-02-16 23:45:12 +00:00
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating );
}
2016-03-28 20:33:13 +00:00
2017-02-16 23:45:12 +00:00
/**
* Save an object data.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-02-16 23:45:12 +00:00
* @param WP_REST_Request $request Full details about the request.
* @param bool $creating If is creating a new object.
* @return WC_Data|WP_Error
*/
protected function save_object( $request, $creating = false ) {
try {
$object = $this->prepare_object_for_database( $request, $creating );
if ( is_wp_error( $object ) ) {
return $object;
}
2017-02-17 03:46:42 +00:00
return $this->get_object( $object->get_id() );
2017-02-16 23:45:12 +00:00
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
2016-03-28 20:33:13 +00:00
}
/**
* Get the Order's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => $this->post_type,
'type' => 'object',
'properties' => array(
2018-01-25 16:04:11 +00:00
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'date_created' => array(
2017-03-10 19:30:07 +00:00
'description' => __( "The date the order refund was created, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'date_created_gmt' => array(
'description' => __( 'The date the order refund was created, as GMT.', 'woocommerce' ),
'type' => 'date-time',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'amount' => array(
'description' => __( 'Refund amount.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
2018-01-25 16:04:11 +00:00
'reason' => array(
'description' => __( 'Reason for refund.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
2018-01-25 16:04:11 +00:00
'refunded_by' => array(
2016-08-22 12:04:57 +00:00
'description' => __( 'User ID of user who created the refund.', 'woocommerce' ),
2016-12-07 18:13:17 +00:00
'type' => 'integer',
'context' => array( 'view', 'edit' ),
2016-08-22 12:04:57 +00:00
),
'refunded_payment' => array(
'description' => __( 'If the payment was refunded via the API.', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view' ),
),
2018-01-25 16:04:11 +00:00
'meta_data' => array(
'description' => __( 'Meta data.', 'woocommerce' ),
2016-12-07 12:20:56 +00:00
'type' => 'array',
2016-08-22 12:04:57 +00:00
'context' => array( 'view', 'edit' ),
2016-12-07 14:24:44 +00:00
'items' => array(
'type' => 'object',
'properties' => array(
2018-01-25 16:04:11 +00:00
'id' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Meta ID.', 'woocommerce' ),
2016-12-07 18:13:17 +00:00
'type' => 'integer',
2016-12-07 14:24:44 +00:00
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'key' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Meta key.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'value' => array(
'description' => __( 'Meta value.', 'woocommerce' ),
'type' => 'mixed',
2016-12-07 14:24:44 +00:00
'context' => array( 'view', 'edit' ),
),
2016-08-22 12:04:57 +00:00
),
),
),
2018-01-25 16:04:11 +00:00
'line_items' => array(
2016-04-04 19:13:38 +00:00
'description' => __( 'Line items data.', 'woocommerce' ),
2016-12-07 12:20:56 +00:00
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
'items' => array(
'type' => 'object',
'properties' => array(
2018-01-25 16:04:11 +00:00
'id' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Item ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-08-22 12:04:57 +00:00
),
2018-01-25 16:04:11 +00:00
'name' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Product name.', 'woocommerce' ),
'type' => 'mixed',
2016-12-07 14:24:44 +00:00
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'product_id' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Product ID.', 'woocommerce' ),
'type' => 'mixed',
2016-12-07 14:24:44 +00:00
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
'variation_id' => array(
'description' => __( 'Variation ID, if applicable.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'quantity' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Quantity ordered.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'tax_class' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Tax class of product.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'subtotal' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Line subtotal (before discounts).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
'subtotal_tax' => array(
'description' => __( 'Line subtotal tax (before discounts).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'total' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Line total (after discounts).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'total_tax' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'taxes' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Line taxes.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
2018-01-25 16:04:11 +00:00
'id' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Tax rate ID.', 'woocommerce' ),
'type' => 'integer',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'total' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Tax total.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
'subtotal' => array(
'description' => __( 'Tax subtotal.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
),
),
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'meta_data' => array(
'description' => __( 'Meta data.', 'woocommerce' ),
2016-12-07 14:24:44 +00:00
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
'items' => array(
'type' => 'object',
'properties' => array(
2018-01-25 16:04:11 +00:00
'id' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Meta ID.', 'woocommerce' ),
2016-12-07 18:13:17 +00:00
'type' => 'integer',
2016-12-07 14:24:44 +00:00
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'key' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Meta key.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
'value' => array(
'description' => __( 'Meta value.', 'woocommerce' ),
'type' => 'mixed',
2016-12-07 14:24:44 +00:00
'context' => array( 'view', 'edit' ),
'readonly' => true,
2016-12-07 14:24:44 +00:00
),
),
),
2016-12-07 14:24:44 +00:00
),
2018-01-25 16:04:11 +00:00
'sku' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Product SKU.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
2018-01-25 16:04:11 +00:00
'price' => array(
2016-12-07 14:24:44 +00:00
'description' => __( 'Product price.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
),
),
),
2018-01-25 16:04:11 +00:00
'api_refund' => array(
2017-03-22 13:39:23 +00:00
'description' => __( 'When true, the payment gateway API is used to generate the refund.', 'woocommerce' ),
2017-03-21 20:07:01 +00:00
'type' => 'boolean',
'context' => array( 'edit' ),
'default' => true,
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
2017-02-16 23:45:12 +00:00
unset( $params['status'], $params['customer'], $params['product'] );
return $params;
}
2016-02-17 19:29:09 +00:00
}