Moved logic to wc_create_refund

This commit is contained in:
Mike Jolley 2017-02-08 16:20:42 +00:00
parent 52291e003c
commit 11ef37880b
2 changed files with 20 additions and 88 deletions

View File

@ -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 );

View File

@ -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 ) {