Create refunds and process via gateway

This commit is contained in:
Mike Jolley 2014-07-10 14:49:04 +01:00
parent d88279fe53
commit 280b884c28
4 changed files with 47 additions and 6 deletions

View File

@ -447,8 +447,10 @@ jQuery( function($){
};
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
console.log( response );
if ( response ) {
if ( response === true ) {
window.location.reload();
} else if ( response.error ) {
alert( response.error );
}
$('#woocommerce-order-items').unblock();
});

File diff suppressed because one or more lines are too long

View File

@ -1641,9 +1641,50 @@ class WC_AJAX {
$refund_amount = sanitize_text_field( $_POST['refund_amount'] );
$refund_reason = sanitize_text_field( $_POST['refund_reason'] );
$refund_qty = json_decode( sanitize_text_field( stripslashes( $_POST['refund_qty'] ) ) );
$api_refund = $_POST['api_refund'] ? true : false;
$api_refund = $_POST['api_refund'] === 'true' ? true : false;
wp_send_json( true );
try {
// Validate that the refund can occur
$order = get_order( $order_id );
$max_refund = $order->get_total() - $order->get_total_refunded();
if ( ! $refund_amount || $max_refund < $refund_amount ) {
throw new exception( __( 'Invalid refund amount', 'woocommerce' ) );
}
// Create the refund object
$refund = wc_create_refund( array(
'amount' => $refund_amount,
'reason' => $refund_reason,
'order_id' => $order_id
) );
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->payment_method ] ) && $payment_gateways[ $order->payment_method ]->supports( 'refunds' ) ) {
$result = $payment_gateways[ $order->payment_method ]->process_refund( $order_id, $refund_amount );
if ( is_wp_error( $result ) ) {
throw new exception( $result->get_error_message() );
} elseif ( ! $result ) {
throw new exception( __( 'Refund failed', 'woocommerce' ) );
}
}
}
wp_send_json( true );
} catch ( Exception $e ) {
wp_send_json( array( 'error' => $e->getMessage() ) );
}
}
}

View File

@ -408,7 +408,6 @@ function wc_create_refund( $args = array() ) {
$default_args = array(
'amount' => '',
'reason' => null,
'tax' => '',
'order_id' => 0,
'refund_id' => 0
);
@ -447,7 +446,6 @@ function wc_create_refund( $args = array() ) {
// Default refund meta data
if ( ! $updating ) {
update_post_meta( $refund_id, '_refund_amount', wc_format_decimal( $args['amount'] ) );
update_post_meta( $refund_id, '_refund_tax', wc_format_decimal( $args['tax'] ) );
// Set the order type.
wp_set_object_terms( $refund_id, 'refund', 'order_type' );