From 11ef37880b449083d0662c7b04ec97fdfdfb5512 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 8 Feb 2017 16:20:42 +0000 Subject: [PATCH] Moved logic to wc_create_refund --- ...class-wc-rest-order-refunds-controller.php | 43 ++++-------- includes/class-wc-ajax.php | 65 +++---------------- 2 files changed, 20 insertions(+), 88 deletions(-) diff --git a/includes/api/class-wc-rest-order-refunds-controller.php b/includes/api/class-wc-rest-order-refunds-controller.php index aeb83aeb372..d97d88638c8 100644 --- a/includes/api/class-wc-rest-order-refunds-controller.php +++ b/includes/api/class-wc-rest-order-refunds-controller.php @@ -252,43 +252,24 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller { return new WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 ); } - $api_refund = is_bool( $request['api_refund'] ) ? $request['api_refund'] : true; - - $data = array( - 'order_id' => $order_data->ID, - 'amount' => $request['amount'], - 'reason' => empty( $request['reason'] ) ? null : $request['reason'], - 'line_items' => $request['line_items'], - ); - // Create the refund. - $refund = wc_create_refund( $data ); + $refund = wc_create_refund( array( + 'order_id' => $order_data->ID, + 'amount' => $request['amount'], + 'reason' => empty( $request['reason'] ) ? null : $request['reason'], + 'line_items' => $request['line_items'], + 'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true, + 'restock_items' => true, + ) ); + + if ( is_wp_error( $refund ) ) { + return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 ); + } if ( ! $refund ) { return new WP_Error( 'woocommerce_rest_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_data ); - - 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->get_id(), $refund->get_amount(), $refund->get_reason() ); - - if ( ! $result || is_wp_error( $result ) ) { - $refund->delete(); - - $message = is_wp_error( $result ) ? $result : new WP_Error( 'woocommerce_rest_create_order_refund_api_failed', __( 'An error occurred while attempting to create the refund using the payment gateway API.', 'woocommerce' ), 500 ); - - return $message; - } - } - } - $post = get_post( $refund->get_id() ); $this->update_additional_fields_for_object( $post, $request ); diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 0a4955503ef..6190a77c21b 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -1365,73 +1365,24 @@ class WC_AJAX { $line_items[ $item_id ]['refund_tax'] = array_map( 'wc_format_decimal', $tax_totals ); } - // Create the refund object + // Create the refund object. $refund = wc_create_refund( array( - 'amount' => $refund_amount, - 'reason' => $refund_reason, - 'order_id' => $order_id, - 'line_items' => $line_items, + 'amount' => $refund_amount, + 'reason' => $refund_reason, + 'order_id' => $order_id, + 'line_items' => $line_items, + 'refund_payment' => $api_refund, + 'restock_items' => $restock_refunded_items, ) ); if ( is_wp_error( $refund ) ) { throw new Exception( $refund->get_error_message() ); } - // Refund via API - if ( $api_refund ) { - if ( WC()->payment_gateways() ) { - $payment_gateways = WC()->payment_gateways->payment_gateways(); - } - 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_amount, $refund_reason ); - - do_action( 'woocommerce_refund_processed', $refund, $result ); - - if ( is_wp_error( $result ) ) { - throw new Exception( $result->get_error_message() ); - } elseif ( ! $result ) { - throw new Exception( __( 'Refund failed', 'woocommerce' ) ); - } - } - } - - // restock items - foreach ( $line_item_qtys as $item_id => $qty ) { - if ( $restock_refunded_items && $qty && isset( $order_items[ $item_id ] ) ) { - $order_item = $order_items[ $item_id ]; - $_product = $order_item->get_product(); - - if ( $_product && $_product->exists() && $_product->managing_stock() ) { - $old_stock = wc_stock_amount( $_product->stock ); - $new_quantity = $_product->increase_stock( $qty ); - - $order->add_order_note( sprintf( __( 'Item #%1$s stock increased from %2$s to %3$s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_quantity ) ); - - do_action( 'woocommerce_restock_refunded_item', $_product->get_id(), $old_stock, $new_quantity, $order, $_product ); - } - } - } - - // Trigger notifications and status changes - if ( $order->get_remaining_refund_amount() > 0 || ( $order->has_free_item() && $order->get_remaining_refund_items() > 0 ) ) { - /** - * woocommerce_order_partially_refunded. - * - * @since 2.4.0 - * Note: 3rd arg was added in err. Kept for bw compat. 2.4.3. - */ - do_action( 'woocommerce_order_partially_refunded', $order_id, $refund->get_id(), $refund->get_id() ); - } else { - do_action( 'woocommerce_order_fully_refunded', $order_id, $refund->get_id() ); - - $order->update_status( apply_filters( 'woocommerce_order_fully_refunded_status', 'refunded', $order_id, $refund->get_id() ) ); + if ( did_action( 'woocommerce_order_fully_refunded' ) ) { $response_data['status'] = 'fully_refunded'; } - do_action( 'woocommerce_order_refunded', $order_id, $refund->get_id() ); - - // Clear transients - wc_delete_shop_order_transients( $order_id ); wp_send_json_success( $response_data ); } catch ( Exception $e ) {