[API] Refactored the orders endpoint to use the WC_API_Exception class #6138
This commit is contained in:
parent
a74efe61a9
commit
e14a5b0624
|
@ -305,17 +305,21 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
*/
|
*/
|
||||||
public function get_orders_count( $status = null, $filter = array() ) {
|
public function get_orders_count( $status = null, $filter = array() ) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
if ( ! current_user_can( 'read_private_shop_orders' ) ) {
|
||||||
|
throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_orders_count', __( 'You do not have permission to read the orders count', 'woocommerce' ), 401 );
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! empty( $status ) ) {
|
if ( ! empty( $status ) ) {
|
||||||
$filter['status'] = $status;
|
$filter['status'] = $status;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = $this->query_orders( $filter );
|
$query = $this->query_orders( $filter );
|
||||||
|
|
||||||
if ( ! current_user_can( 'read_private_shop_orders' ) ) {
|
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_read_orders_count', __( 'You do not have permission to read the orders count', 'woocommerce' ), array( 'status' => 401 ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
return array( 'count' => (int) $query->found_posts );
|
return array( 'count' => (int) $query->found_posts );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1143,8 +1147,8 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function get_order_note( $order_id, $id, $fields = null ) {
|
public function get_order_note( $order_id, $id, $fields = null ) {
|
||||||
|
try {
|
||||||
// validate order ID
|
// Validate order ID
|
||||||
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
|
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
|
||||||
|
|
||||||
if ( is_wp_error( $order_id ) ) {
|
if ( is_wp_error( $order_id ) ) {
|
||||||
|
@ -1154,13 +1158,13 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
if ( empty( $id ) ) {
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$note = get_comment( $id );
|
$note = get_comment( $id );
|
||||||
|
|
||||||
if ( is_null( $note ) ) {
|
if ( is_null( $note ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$order_note = array(
|
$order_note = array(
|
||||||
|
@ -1171,6 +1175,9 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
);
|
);
|
||||||
|
|
||||||
return array( 'order_note' => apply_filters( 'woocommerce_api_order_note_response', $order_note, $id, $fields, $note, $order_id, $this ) );
|
return array( 'order_note' => apply_filters( 'woocommerce_api_order_note_response', $order_note, $id, $fields, $note, $order_id, $this ) );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1182,18 +1189,18 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return WP_Error|array error or created note response data
|
* @return WP_Error|array error or created note response data
|
||||||
*/
|
*/
|
||||||
public function create_order_note( $order_id, $data ) {
|
public function create_order_note( $order_id, $data ) {
|
||||||
|
try {
|
||||||
$data = isset( $data['order_note'] ) ? $data['order_note'] : array();
|
$data = isset( $data['order_note'] ) ? $data['order_note'] : array();
|
||||||
|
|
||||||
// permission check
|
// permission check
|
||||||
if ( ! current_user_can( 'publish_shop_orders' ) ) {
|
if ( ! current_user_can( 'publish_shop_orders' ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_create_order_note', __( 'You do not have permission to create order notes', 'woocommerce' ), array( 'status' => 401 ) );
|
throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_note', __( 'You do not have permission to create order notes', 'woocommerce' ), 401 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$order_id = absint( $order_id );
|
$order_id = absint( $order_id );
|
||||||
|
|
||||||
if ( empty( $order_id ) ) {
|
if ( empty( $order_id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$order = wc_get_order( $order_id );
|
$order = wc_get_order( $order_id );
|
||||||
|
@ -1202,7 +1209,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
|
|
||||||
// note content is required
|
// note content is required
|
||||||
if ( ! isset( $data['note'] ) ) {
|
if ( ! isset( $data['note'] ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note', __( 'Order note is required', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note', __( 'Order note is required', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$is_customer_note = ( isset( $data['customer_note'] ) && true === $data['customer_note'] );
|
$is_customer_note = ( isset( $data['customer_note'] ) && true === $data['customer_note'] );
|
||||||
|
@ -1211,7 +1218,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
$note_id = $order->add_order_note( $data['note'], $is_customer_note );
|
$note_id = $order->add_order_note( $data['note'], $is_customer_note );
|
||||||
|
|
||||||
if ( ! $note_id ) {
|
if ( ! $note_id ) {
|
||||||
return new WP_Error( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again', 'woocommerce' ), array( 'status' => 500 ) );
|
throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again', 'woocommerce' ), 500 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP 201 Created
|
// HTTP 201 Created
|
||||||
|
@ -1220,6 +1227,9 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
do_action( 'woocommerce_api_create_order_note', $note_id, $order_id, $this );
|
do_action( 'woocommerce_api_create_order_note', $note_id, $order_id, $this );
|
||||||
|
|
||||||
return $this->get_order_note( $order->id, $note_id );
|
return $this->get_order_note( $order->id, $note_id );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1232,10 +1242,10 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return WP_Error|array error or edited note response data
|
* @return WP_Error|array error or edited note response data
|
||||||
*/
|
*/
|
||||||
public function edit_order_note( $order_id, $id, $data ) {
|
public function edit_order_note( $order_id, $id, $data ) {
|
||||||
|
try {
|
||||||
$data = isset( $data['order_note'] ) ? $data['order_note'] : array();
|
$data = isset( $data['order_note'] ) ? $data['order_note'] : array();
|
||||||
|
|
||||||
// validate order ID
|
// Validate order ID
|
||||||
$order_id = $this->validate_request( $order_id, 'shop_order', 'edit' );
|
$order_id = $this->validate_request( $order_id, 'shop_order', 'edit' );
|
||||||
|
|
||||||
if ( is_wp_error( $order_id ) ) {
|
if ( is_wp_error( $order_id ) ) {
|
||||||
|
@ -1244,28 +1254,28 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
|
|
||||||
$order = wc_get_order( $order_id );
|
$order = wc_get_order( $order_id );
|
||||||
|
|
||||||
// validate note ID
|
// Validate note ID
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
if ( empty( $id ) ) {
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure note ID is valid
|
// Ensure note ID is valid
|
||||||
$note = get_comment( $id );
|
$note = get_comment( $id );
|
||||||
|
|
||||||
if ( is_null( $note ) ) {
|
if ( is_null( $note ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure note ID is associated with given order
|
// Ensure note ID is associated with given order
|
||||||
if ( $note->comment_post_ID != $order->id ) {
|
if ( $note->comment_post_ID != $order->id ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = apply_filters( 'woocommerce_api_edit_order_note_data', $data, $note->comment_ID, $order->id, $this );
|
$data = apply_filters( 'woocommerce_api_edit_order_note_data', $data, $note->comment_ID, $order->id, $this );
|
||||||
|
|
||||||
// note content
|
// Note content
|
||||||
if ( isset( $data['note'] ) ) {
|
if ( isset( $data['note'] ) ) {
|
||||||
|
|
||||||
wp_update_comment(
|
wp_update_comment(
|
||||||
|
@ -1276,7 +1286,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// customer note
|
// Customer note
|
||||||
if ( isset( $data['customer_note'] ) ) {
|
if ( isset( $data['customer_note'] ) ) {
|
||||||
|
|
||||||
update_comment_meta( $note->comment_ID, 'is_customer_note', true === $data['customer_note'] );
|
update_comment_meta( $note->comment_ID, 'is_customer_note', true === $data['customer_note'] );
|
||||||
|
@ -1285,6 +1295,9 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
do_action( 'woocommerce_api_edit_order_note', $note->comment_ID, $order->id, $this );
|
do_action( 'woocommerce_api_edit_order_note', $note->comment_ID, $order->id, $this );
|
||||||
|
|
||||||
return $this->get_order_note( $order->id, $note->comment_ID );
|
return $this->get_order_note( $order->id, $note->comment_ID );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1296,44 +1309,44 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return WP_Error|array error or deleted message
|
* @return WP_Error|array error or deleted message
|
||||||
*/
|
*/
|
||||||
public function delete_order_note( $order_id, $id ) {
|
public function delete_order_note( $order_id, $id ) {
|
||||||
|
try {
|
||||||
$order_id = $this->validate_request( $order_id, 'shop_order', 'delete' );
|
$order_id = $this->validate_request( $order_id, 'shop_order', 'delete' );
|
||||||
|
|
||||||
if ( is_wp_error( $order_id ) ) {
|
if ( is_wp_error( $order_id ) ) {
|
||||||
return $order_id;
|
return $order_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate note ID
|
// Validate note ID
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
if ( empty( $id ) ) {
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure note ID is valid
|
// Ensure note ID is valid
|
||||||
$note = get_comment( $id );
|
$note = get_comment( $id );
|
||||||
|
|
||||||
if ( is_null( $note) ) {
|
if ( is_null( $note ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure note ID is associated with given order
|
// Ensure note ID is associated with given order
|
||||||
if ( $note->comment_post_ID != $order_id ) {
|
if ( $note->comment_post_ID != $order_id ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// force delete since trashed order notes could not be managed through comments list table
|
// Force delete since trashed order notes could not be managed through comments list table
|
||||||
$result = wp_delete_comment( $note->comment_ID, true );
|
$result = wp_delete_comment( $note->comment_ID, true );
|
||||||
|
|
||||||
if ( $result ) {
|
if ( ! $result ) {
|
||||||
|
throw new WC_API_Exception( 'woocommerce_api_cannot_delete_order_note', __( 'This order note cannot be deleted', 'woocommerce' ), 500 );
|
||||||
|
}
|
||||||
|
|
||||||
do_action( 'woocommerce_api_delete_order_note', $note->comment_ID, $order_id, $this );
|
do_action( 'woocommerce_api_delete_order_note', $note->comment_ID, $order_id, $this );
|
||||||
|
|
||||||
return array( 'message' => __( 'Permanently deleted order note', 'woocommerce' ) );
|
return array( 'message' => __( 'Permanently deleted order note', 'woocommerce' ) );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
} else {
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
|
||||||
return new WP_Error( 'woocommerce_api_cannot_delete_order_note', __( 'This order note cannot be deleted', 'woocommerce' ), array( 'status' => 500 ) );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1381,7 +1394,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function get_order_refund( $order_id, $id, $fields = null ) {
|
public function get_order_refund( $order_id, $id, $fields = null ) {
|
||||||
|
try {
|
||||||
// Validate order ID
|
// Validate order ID
|
||||||
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
|
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
|
||||||
|
|
||||||
|
@ -1392,14 +1405,14 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
if ( empty( $id ) ) {
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$order = wc_get_order( $id );
|
$order = wc_get_order( $id );
|
||||||
$refund = wc_get_order( $id );
|
$refund = wc_get_order( $id );
|
||||||
|
|
||||||
if ( ! $refund ) {
|
if ( ! $refund ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$line_items = array();
|
$line_items = array();
|
||||||
|
@ -1444,6 +1457,9 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
);
|
);
|
||||||
|
|
||||||
return array( 'order_refund' => apply_filters( 'woocommerce_api_order_refund_response', $order_refund, $id, $fields, $refund, $order_id, $this ) );
|
return array( 'order_refund' => apply_filters( 'woocommerce_api_order_refund_response', $order_refund, $id, $fields, $refund, $order_id, $this ) );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1456,27 +1472,27 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return WP_Error|array error or created refund response data
|
* @return WP_Error|array error or created refund response data
|
||||||
*/
|
*/
|
||||||
public function create_order_refund( $order_id, $data, $api_refund = true ) {
|
public function create_order_refund( $order_id, $data, $api_refund = true ) {
|
||||||
|
try {
|
||||||
$data = isset( $data['order_refund'] ) ? $data['order_refund'] : array();
|
$data = isset( $data['order_refund'] ) ? $data['order_refund'] : array();
|
||||||
|
|
||||||
// Permission check
|
// Permission check
|
||||||
if ( ! current_user_can( 'publish_shop_orders' ) ) {
|
if ( ! current_user_can( 'publish_shop_orders' ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_user_cannot_create_order_refund', __( 'You do not have permission to create order refunds', 'woocommerce' ), array( 'status' => 401 ) );
|
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 );
|
$order_id = absint( $order_id );
|
||||||
|
|
||||||
if ( empty( $order_id ) ) {
|
if ( empty( $order_id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), array( 'status' => 400 ) );
|
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 );
|
$data = apply_filters( 'woocommerce_api_create_order_refund_data', $data, $order_id, $this );
|
||||||
|
|
||||||
// Refund amount is required
|
// Refund amount is required
|
||||||
if ( ! isset( $data['amount'] ) ) {
|
if ( ! isset( $data['amount'] ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund', __( 'Refund amount is required', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount is required', 'woocommerce' ), 400 );
|
||||||
} elseif ( 0 > $data['amount'] ) {
|
} elseif ( 0 > $data['amount'] ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund', __( 'Refund amount must be positive', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount must be positive', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$data['order_id'] = $order_id;
|
$data['order_id'] = $order_id;
|
||||||
|
@ -1486,7 +1502,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
$refund = wc_create_refund( $data );
|
$refund = wc_create_refund( $data );
|
||||||
|
|
||||||
if ( ! $refund ) {
|
if ( ! $refund ) {
|
||||||
return new WP_Error( 'woocommerce_api_cannot_create_order_refund', __( 'Cannot create order refund, please try again', 'woocommerce' ), array( 'status' => 500 ) );
|
throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_refund', __( 'Cannot create order refund, please try again', 'woocommerce' ), 500 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refund via API
|
// Refund via API
|
||||||
|
@ -1503,7 +1519,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
if ( is_wp_error( $result ) ) {
|
if ( is_wp_error( $result ) ) {
|
||||||
return $result;
|
return $result;
|
||||||
} elseif ( ! $result ) {
|
} elseif ( ! $result ) {
|
||||||
return new WP_Error( 'woocommerce_api_create_order_refund_api_failed', __( 'An error occurred while attempting to create the refund using the payment gateway API', 'woocommerce' ), array( 'status' => 500 ) );
|
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 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1514,6 +1530,9 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
do_action( 'woocommerce_api_create_order_refund', $refund->id, $order_id, $this );
|
do_action( 'woocommerce_api_create_order_refund', $refund->id, $order_id, $this );
|
||||||
|
|
||||||
return $this->get_order_refund( $order_id, $refund->id );
|
return $this->get_order_refund( $order_id, $refund->id );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1526,7 +1545,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return WP_Error|array error or edited refund response data
|
* @return WP_Error|array error or edited refund response data
|
||||||
*/
|
*/
|
||||||
public function edit_order_refund( $order_id, $id, $data ) {
|
public function edit_order_refund( $order_id, $id, $data ) {
|
||||||
|
try {
|
||||||
$data = isset( $data['order_refund'] ) ? $data['order_refund'] : array();
|
$data = isset( $data['order_refund'] ) ? $data['order_refund'] : array();
|
||||||
|
|
||||||
// Validate order ID
|
// Validate order ID
|
||||||
|
@ -1540,19 +1559,19 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
if ( empty( $id ) ) {
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure order ID is valid
|
// Ensure order ID is valid
|
||||||
$refund = get_post( $id );
|
$refund = get_post( $id );
|
||||||
|
|
||||||
if ( ! $refund ) {
|
if ( ! $refund ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure refund ID is associated with given order
|
// Ensure refund ID is associated with given order
|
||||||
if ( $refund->post_parent != $order_id ) {
|
if ( $refund->post_parent != $order_id ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = apply_filters( 'woocommerce_api_edit_order_refund_data', $data, $refund->ID, $order_id, $this );
|
$data = apply_filters( 'woocommerce_api_edit_order_refund_data', $data, $refund->ID, $order_id, $this );
|
||||||
|
@ -1574,6 +1593,9 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
do_action( 'woocommerce_api_edit_order_refund', $refund->ID, $order_id, $this );
|
do_action( 'woocommerce_api_edit_order_refund', $refund->ID, $order_id, $this );
|
||||||
|
|
||||||
return $this->get_order_refund( $order_id, $refund->ID );
|
return $this->get_order_refund( $order_id, $refund->ID );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1585,7 +1607,7 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
* @return WP_Error|array error or deleted message
|
* @return WP_Error|array error or deleted message
|
||||||
*/
|
*/
|
||||||
public function delete_order_refund( $order_id, $id ) {
|
public function delete_order_refund( $order_id, $id ) {
|
||||||
|
try {
|
||||||
$order_id = $this->validate_request( $order_id, 'shop_order', 'delete' );
|
$order_id = $this->validate_request( $order_id, 'shop_order', 'delete' );
|
||||||
|
|
||||||
if ( is_wp_error( $order_id ) ) {
|
if ( is_wp_error( $order_id ) ) {
|
||||||
|
@ -1596,19 +1618,19 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
$id = absint( $id );
|
$id = absint( $id );
|
||||||
|
|
||||||
if ( empty( $id ) ) {
|
if ( empty( $id ) ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure refund ID is valid
|
// Ensure refund ID is valid
|
||||||
$refund = get_post( $id );
|
$refund = get_post( $id );
|
||||||
|
|
||||||
if ( ! $refund ) {
|
if ( ! $refund ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found', 'woocommerce' ), array( 'status' => 404 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found', 'woocommerce' ), 404 );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure refund ID is associated with given order
|
// Ensure refund ID is associated with given order
|
||||||
if ( $refund->post_parent != $order_id ) {
|
if ( $refund->post_parent != $order_id ) {
|
||||||
return new WP_Error( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order', 'woocommerce' ), array( 'status' => 400 ) );
|
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
wc_delete_shop_order_transients( $order_id );
|
wc_delete_shop_order_transients( $order_id );
|
||||||
|
@ -1616,5 +1638,8 @@ class WC_API_Orders extends WC_API_Resource {
|
||||||
do_action( 'woocommerce_api_delete_order_refund', $refund->ID, $order_id, $this );
|
do_action( 'woocommerce_api_delete_order_refund', $refund->ID, $order_id, $this );
|
||||||
|
|
||||||
return $this->delete( $refund->ID, 'order', true );
|
return $this->delete( $refund->ID, 'order', true );
|
||||||
|
} catch ( WC_API_Exception $e ) {
|
||||||
|
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue