diff --git a/class/class.midtrans-gateway-installment.php b/class/class.midtrans-gateway-installment.php index 8b3699e..a5b9b68 100755 --- a/class/class.midtrans-gateway-installment.php +++ b/class/class.midtrans-gateway-installment.php @@ -509,35 +509,12 @@ function process_payment( $order_id ) { * @param float $amount * @return bool */ - function process_refund($order_id, $amount = null, $reason = '', $duplicated = false){ - require_once(dirname(__FILE__) . '/../lib/midtrans/Midtrans.php'); - require_once(dirname(__FILE__) . '/class.midtrans-utils.php'); - - \Midtrans\Config::$isProduction = ($this->environment == 'production') ? true : false; - \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $this->server_key_v2_production : $this->server_key_v2_sandbox; - - $order = wc_get_order( $order_id ); - $params = array( - 'refund_key' => 'RefundID' . $order_id . '-' . current_time('timestamp'), - 'amount' => $amount, - 'reason' => $reason - ); - - try { - $response = \Midtrans\Transaction::refund($order_id, $params); - } catch (Exception $e) { - $error_message = strpos($e->getMessage(), '412') ? $e->getMessage() . ' Note: Refund via Midtrans only for specific payment method, please consult to your midtrans PIC for more information' : $e->getMessage(); - return new WP_Error( 'midtrans_refund_error', $error_message ); - return false; - } - - if (is_wp_error($response)) { - return false; - } elseif ($response->status_code == 200) { - $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-midtrans'), wc_price($response->refund_amount), $response->refund_key, $reason); - $order->add_order_note($refund_message); - return true; - } + function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); + + if ($refundResponse == '200') return true; + else return new WP_Error( 'midtrans_refund_error', $refundResponse); } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway-installmentoff.php b/class/class.midtrans-gateway-installmentoff.php index d90d9be..135851d 100755 --- a/class/class.midtrans-gateway-installmentoff.php +++ b/class/class.midtrans-gateway-installmentoff.php @@ -533,35 +533,12 @@ function process_payment( $order_id ) { * @param float $amount * @return bool */ - function process_refund($order_id, $amount = null, $reason = '', $duplicated = false){ - require_once(dirname(__FILE__) . '/../lib/midtrans/Midtrans.php'); - require_once(dirname(__FILE__) . '/class.midtrans-utils.php'); - - \Midtrans\Config::$isProduction = ($this->environment == 'production') ? true : false; - \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $this->server_key_v2_production : $this->server_key_v2_sandbox; - - $order = wc_get_order( $order_id ); - $params = array( - 'refund_key' => 'RefundID' . $order_id . '-' . current_time('timestamp'), - 'amount' => $amount, - 'reason' => $reason - ); - - try { - $response = \Midtrans\Transaction::refund($order_id, $params); - } catch (Exception $e) { - $error_message = strpos($e->getMessage(), '412') ? $e->getMessage() . ' Note: Refund via Midtrans only for specific payment method, please consult to your midtrans PIC for more information' : $e->getMessage(); - return new WP_Error( 'midtrans_refund_error', $error_message ); - return false; - } - - if (is_wp_error($response)) { - return false; - } elseif ($response->status_code == 200) { - $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-midtrans'), wc_price($response->refund_amount), $response->refund_key, $reason); - $order->add_order_note($refund_message); - return true; - } + function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); + + if ($refundResponse == '200') return true; + else return new WP_Error( 'midtrans_refund_error', $refundResponse); } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway-paymentrequest.php b/class/class.midtrans-gateway-paymentrequest.php index 915e29d..fa847e1 100755 --- a/class/class.midtrans-gateway-paymentrequest.php +++ b/class/class.midtrans-gateway-paymentrequest.php @@ -441,35 +441,12 @@ function process_payment( $order_id ) { * @param float $amount * @return bool */ - function process_refund($order_id, $amount = null, $reason = '', $duplicated = false){ - require_once(dirname(__FILE__) . '/../lib/midtrans/Midtrans.php'); - require_once(dirname(__FILE__) . '/class.midtrans-utils.php'); - - \Midtrans\Config::$isProduction = ($this->environment == 'production') ? true : false; - \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $this->server_key_v2_production : $this->server_key_v2_sandbox; - - $order = wc_get_order( $order_id ); - $params = array( - 'refund_key' => 'RefundID' . $order_id . '-' . current_time('timestamp'), - 'amount' => $amount, - 'reason' => $reason - ); - - try { - $response = \Midtrans\Transaction::refund($order_id, $params); - } catch (Exception $e) { - $error_message = strpos($e->getMessage(), '412') ? $e->getMessage() . ' Note: Refund via Midtrans only for specific payment method, please consult to your midtrans PIC for more information' : $e->getMessage(); - return new WP_Error( 'midtrans_refund_error', $error_message ); - return false; - } - - if (is_wp_error($response)) { - return false; - } elseif ($response->status_code == 200) { - $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-midtrans'), wc_price($response->refund_amount), $response->refund_key, $reason); - $order->add_order_note($refund_message); - return true; - } + function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); + + if ($refundResponse == '200') return true; + else return new WP_Error( 'midtrans_refund_error', $refundResponse); } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway-promo.php b/class/class.midtrans-gateway-promo.php index 47f5cef..99a4d95 100755 --- a/class/class.midtrans-gateway-promo.php +++ b/class/class.midtrans-gateway-promo.php @@ -222,7 +222,7 @@ function init_form_fields() { 'promo_code' => array( 'title' => __( 'Promo Code', 'woocommerce' ), 'type' => 'text', - 'description' => __( 'Promo Code that would be used for discount. Leave blank if you are not sure.', 'woocommerce' ), + 'description' => __( 'Promo Code that would be used for discount. Leave blank if you are not using promo code.', 'woocommerce' ), 'default' => '' ), 'enable_map_finish_url' => array( @@ -275,13 +275,12 @@ function create_snap_transaction( $order_id,$order){ $order_items = array(); $cart = $woocommerce->cart; - // add discount with coupon named `onlinepromo` or admin defined `promo_code` str - $coupon_code = 'onlinepromo'; - if ( strlen($this->promo_code) > 0 ) + if ( strlen($this->promo_code) > 0 ) { $coupon_code = $this->promo_code; + // add coupon to $cart for discount + $cart->add_discount($coupon_code); + } - // add coupon to $cart for discount - $cart->add_discount($coupon_code); // $order->add_coupon( 'onlinepromo', WC()->cart->get_coupon_discount_amount( 'onlinepromo' ), WC()->cart->get_coupon_discount_tax_amount( 'onlinepromo' ) ); $order->set_shipping_total( WC()->cart->shipping_total ); $order->set_discount_total( WC()->cart->get_cart_discount_total() ); @@ -535,35 +534,12 @@ function process_payment( $order_id ) { * @param float $amount * @return bool */ - function process_refund($order_id, $amount = null, $reason = '', $duplicated = false){ - require_once(dirname(__FILE__) . '/../lib/midtrans/Midtrans.php'); - require_once(dirname(__FILE__) . '/class.midtrans-utils.php'); - - \Midtrans\Config::$isProduction = ($this->environment == 'production') ? true : false; - \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $this->server_key_v2_production : $this->server_key_v2_sandbox; - - $order = wc_get_order( $order_id ); - $params = array( - 'refund_key' => 'RefundID' . $order_id . '-' . current_time('timestamp'), - 'amount' => $amount, - 'reason' => $reason - ); - - try { - $response = \Midtrans\Transaction::refund($order_id, $params); - } catch (Exception $e) { - $error_message = strpos($e->getMessage(), '412') ? $e->getMessage() . ' Note: Refund via Midtrans only for specific payment method, please consult to your midtrans PIC for more information' : $e->getMessage(); - return new WP_Error( 'midtrans_refund_error', $error_message ); - return false; - } - - if (is_wp_error($response)) { - return false; - } elseif ($response->status_code == 200) { - $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-midtrans'), wc_price($response->refund_amount), $response->refund_key, $reason); - $order->add_order_note($refund_message); - return true; - } + function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); + + if ($refundResponse == '200') return true; + else return new WP_Error( 'midtrans_refund_error', $refundResponse); } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway.php b/class/class.midtrans-gateway.php index e1cd95b..a3b13e3 100755 --- a/class/class.midtrans-gateway.php +++ b/class/class.midtrans-gateway.php @@ -561,35 +561,12 @@ function process_payment( $order_id ) { * @param float $amount * @return bool */ - function process_refund($order_id, $amount = null, $reason = '', $duplicated = false){ - require_once(dirname(__FILE__) . '/../lib/midtrans/Midtrans.php'); - require_once(dirname(__FILE__) . '/class.midtrans-utils.php'); - - \Midtrans\Config::$isProduction = ($this->environment == 'production') ? true : false; - \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $this->server_key_v2_production : $this->server_key_v2_sandbox; - - $order = wc_get_order( $order_id ); - $params = array( - 'refund_key' => 'RefundID' . $order_id . '-' . current_time('timestamp'), - 'amount' => $amount, - 'reason' => $reason - ); - - try { - $response = \Midtrans\Transaction::refund($order_id, $params); - } catch (Exception $e) { - $error_message = strpos($e->getMessage(), '412') ? $e->getMessage() . ' Note: Refund via Midtrans only for specific payment method, please consult to your midtrans PIC for more information' : $e->getMessage(); - return new WP_Error( 'midtrans_refund_error', $error_message ); - return false; - } - - if (is_wp_error($response)) { - return false; - } elseif ($response->status_code == 200) { - $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-midtrans'), wc_price($response->refund_amount), $response->refund_key, $reason); - $order->add_order_note($refund_message); - return true; - } + function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); + + if ($refundResponse == '200') return true; + else return new WP_Error( 'midtrans_refund_error', $refundResponse); } /** diff --git a/class/refund-option.php b/class/refund-option.php new file mode 100644 index 0000000..175fbb4 --- /dev/null +++ b/class/refund-option.php @@ -0,0 +1,28 @@ +environment == 'production') ? true : false; + \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $dependencies->server_key_v2_production : $dependencies->server_key_v2_sandbox; + + $order = wc_get_order( $order_id ); + $params = array( + 'refund_key' => 'RefundID' . $order_id . '-' . current_time('timestamp'), + 'amount' => $amount, + 'reason' => $reason + ); + + try { + $response = \Midtrans\Transaction::refund($order_id, $params); + } catch (Exception $e) { + $error_message = strpos($e->getMessage(), '412') ? $e->getMessage() . ' Note: Refund via Midtrans only for specific payment method, please consult to your midtrans PIC for more information' : $e->getMessage(); + return $error_message; + } + + if ($response->status_code == 200) { + $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-midtrans'), wc_price($response->refund_amount), $response->refund_key, $reason); + $order->add_order_note($refund_message); + return $response->status_code; + } +} \ No newline at end of file