diff --git a/classes/wc-gateway-braintree-angelleye.php b/classes/wc-gateway-braintree-angelleye.php index 3cefc2a13..8ac52bb38 100644 --- a/classes/wc-gateway-braintree-angelleye.php +++ b/classes/wc-gateway-braintree-angelleye.php @@ -73,6 +73,9 @@ function __construct() { add_filter( 'clean_url', array( $this, 'adjust_fraud_script_tag' ) ); add_action( 'wp_print_footer_scripts', array( $this, 'render_fraud_js' ), 1 ); } + + add_action('woocommerce_admin_order_data_after_order_details', array($this, 'woocommerce_admin_order_data_after_order_details'), 10, 1); + } /** @@ -1709,4 +1712,38 @@ public function angelleye_reload_gateway_credentials_for_woo_subscription_renewa } } } + + public function woocommerce_admin_order_data_after_order_details($order) { + $payment_method = version_compare( WC_VERSION, '3.0', '<' ) ? $order->payment_method : $order->get_payment_method(); + if( 'braintree' == $payment_method && $order->get_status() != 'refunded') { + $this->angelleye_braintree_lib(); + $order_id = version_compare(WC_VERSION, '3.0', '<') ? $order->id : $order->get_id(); + $transaction_id = $order->get_transaction_id(); + $transaction = Braintree_Transaction::find($transaction_id); + if( !empty($transaction->refundIds) ) { + foreach ($transaction->refundIds as $key => $value) { + $braintree_refunded_id = get_post_meta($order_id, 'braintree_refunded_id', true); + if( empty($braintree_refunded_id) ) { + $braintree_refunded_id = array(); + } + if(!in_array($value, $braintree_refunded_id)) { + $refund_transaction = Braintree_Transaction::find($value); + $default_args = array( + 'amount' => $refund_transaction->amount, + 'reason' => 'Data Synchronization from Braintree', + 'order_id' => $order_id, + 'refund_id' => 0, + 'line_items' => array(), + 'refund_payment' => false, + 'restock_items' => false, + ); + wc_create_refund( $default_args ); + $order->add_order_note(sprintf(__('Refunded %s - Transaction ID: %s', 'paypal-for-woocommerce'), wc_price(number_format($refund_transaction->amount, 2, '.', '')), $value)); + $braintree_refunded_id[$value] = $value; + update_post_meta($order_id, 'braintree_refunded_id', $braintree_refunded_id); + } + } + } + } + } } \ No newline at end of file