From 62ae94d93e3f471dd5de259062d3ec03892b0b3d Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Wed, 11 Nov 2020 18:47:48 +0530 Subject: [PATCH] Add shipping, tax and fee lines to refund response for better information. --- ...ss-wc-rest-order-refunds-v2-controller.php | 13 +++- .../helpers/class-wc-helper-order.php | 29 +++++++++ ...-rest-order-refunds-v2-controller-test.php | 59 +++++++++++++++++++ ...-wc-rest-order-refunds-controller-test.php | 59 +++++++++++++++++++ 4 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller-test.php create mode 100644 tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-order-refunds-controller-test.php diff --git a/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller.php b/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller.php index 7630dae2ecd..d08e86b410b 100644 --- a/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller.php +++ b/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller.php @@ -58,7 +58,9 @@ class WC_REST_Order_Refunds_V2_Controller extends WC_REST_Orders_V2_Controller { */ public function register_routes() { register_rest_route( - $this->namespace, '/' . $this->rest_base, array( + $this->namespace, + '/' . $this->rest_base, + array( 'args' => array( 'order_id' => array( 'description' => __( 'The order ID.', 'woocommerce' ), @@ -82,7 +84,9 @@ class WC_REST_Order_Refunds_V2_Controller extends WC_REST_Orders_V2_Controller { ); register_rest_route( - $this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( + $this->namespace, + '/' . $this->rest_base . '/(?P[\d]+)', + array( 'args' => array( 'order_id' => array( 'description' => __( 'The order ID.', 'woocommerce' ), @@ -140,7 +144,7 @@ class WC_REST_Order_Refunds_V2_Controller extends WC_REST_Orders_V2_Controller { $data = $object->get_data(); $format_decimal = array( 'amount' ); $format_date = array( 'date_created' ); - $format_line_items = array( 'line_items' ); + $format_line_items = array( 'line_items', 'shipping_lines', 'tax_lines', 'fee_lines' ); // Format decimal values. foreach ( $format_decimal as $key ) { @@ -169,6 +173,9 @@ class WC_REST_Order_Refunds_V2_Controller extends WC_REST_Orders_V2_Controller { 'refunded_payment' => $data['refunded_payment'], 'meta_data' => $data['meta_data'], 'line_items' => $data['line_items'], + 'shipping_lines' => $data['shipping_lines'], + 'tax_lines' => $data['tax_lines'], + 'fee_lines' => $data['fee_lines'], ); } diff --git a/tests/legacy/framework/helpers/class-wc-helper-order.php b/tests/legacy/framework/helpers/class-wc-helper-order.php index 457a37d011f..52db052a917 100644 --- a/tests/legacy/framework/helpers/class-wc-helper-order.php +++ b/tests/legacy/framework/helpers/class-wc-helper-order.php @@ -119,4 +119,33 @@ class WC_Helper_Order { return $order; } + + /** + * Helper function to create order with fees and shipping objects. + * + * @param int $customer_id The ID of the customer the order is for. + * @param WC_Product $product The product to add to the order. + * + * @return WC_Order + */ + public static function create_order_with_fees_and_shipping( $customer_id = 1, $product = null ) { + $order = self::create_order( $customer_id, $product ); + + $fee_item = new WC_Order_Item_Fee(); + $fee_item->set_order_id( $order->get_id() ); + $fee_item->set_name( 'Testing fees' ); + $fee_item->set_total( 100 ); + + $shipping_item = new WC_Order_Item_Shipping(); + $shipping_item->set_order_id( $order->get_id() ); + $shipping_item->set_name( 'Flat shipping' ); + $shipping_item->set_total( 25 ); + + $order->add_item( $fee_item ); + $order->add_item( $shipping_item ); + $order->save(); + $order->calculate_totals( true ); + + return $order; + } } diff --git a/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller-test.php b/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller-test.php new file mode 100644 index 00000000000..27fbc017dac --- /dev/null +++ b/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-order-refunds-v2-controller-test.php @@ -0,0 +1,59 @@ +get_items( 'line_item' ) ); + $fee_item = current( $order->get_items( 'fee' ) ); + $shipping_item = current( $order->get_items( 'shipping' ) ); + + $refund = wc_create_refund( + array( + 'order_id' => $order->get_id(), + 'reason' => 'testing', + 'line_items' => array( + $product_item->get_id() => + array( + 'qty' => 1, + 'refund_total' => 1, + ), + $fee_item->get_id() => + array( + 'refund_total' => 10, + ), + $shipping_item->get_id() => + array( + 'refund_total' => 20, + ), + ), + ) + ); + + $this->assertNotWPError( $refund ); + + $request = new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() . '/refunds/' . $refund->get_id() ); + + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + + $this->assertContains( 'line_items', array_keys( $data ) ); + $this->assertEquals( -1, $data['line_items'][0]['total'] ); + + $this->assertContains( 'fee_lines', array_keys( $data ) ); + $this->assertEquals( -10, $data['fee_lines'][0]['total'] ); + + $this->assertContains( 'shipping_lines', array_keys( $data ) ); + $this->assertEquals( -20, $data['shipping_lines'][0]['total'] ); + } +} diff --git a/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-order-refunds-controller-test.php b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-order-refunds-controller-test.php new file mode 100644 index 00000000000..c306efcfa71 --- /dev/null +++ b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-order-refunds-controller-test.php @@ -0,0 +1,59 @@ +get_items( 'line_item' ) ); + $fee_item = current( $order->get_items( 'fee' ) ); + $shipping_item = current( $order->get_items( 'shipping' ) ); + + $refund = wc_create_refund( + array( + 'order_id' => $order->get_id(), + 'reason' => 'testing', + 'line_items' => array( + $product_item->get_id() => + array( + 'qty' => 1, + 'refund_total' => 1, + ), + $fee_item->get_id() => + array( + 'refund_total' => 10, + ), + $shipping_item->get_id() => + array( + 'refund_total' => 20, + ), + ), + ) + ); + + $this->assertNotWPError( $refund ); + + $request = new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() . '/refunds/' . $refund->get_id() ); + + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + + $this->assertContains( 'line_items', array_keys( $data ) ); + $this->assertEquals( -1, $data['line_items'][0]['total'] ); + + $this->assertContains( 'fee_lines', array_keys( $data ) ); + $this->assertEquals( -10, $data['fee_lines'][0]['total'] ); + + $this->assertContains( 'shipping_lines', array_keys( $data ) ); + $this->assertEquals( -20, $data['shipping_lines'][0]['total'] ); + } +}