From 565dc4727d9e759d3e38f917fe0d12672373f9f7 Mon Sep 17 00:00:00 2001 From: Alexander Kevin Date: Wed, 19 Feb 2020 14:50:17 +0700 Subject: [PATCH 1/4] ignore inject promo code if promo_code field gets blank on Midtrans Promo Payment --- class/class.midtrans-gateway-promo.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/class/class.midtrans-gateway-promo.php b/class/class.midtrans-gateway-promo.php index 47f5cef..4ee4152 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 use 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() ); From b4ca93f033b5cf11a0ea286a3679f08bb554a54c Mon Sep 17 00:00:00 2001 From: Alexander Kevin Date: Wed, 19 Feb 2020 15:01:38 +0700 Subject: [PATCH 2/4] add refund-option.php for improve refund via woocommerce code --- class/class.midtrans-gateway-installment.php | 33 +++---------------- .../class.midtrans-gateway-installmentoff.php | 33 +++---------------- .../class.midtrans-gateway-paymentrequest.php | 33 +++---------------- class/class.midtrans-gateway-promo.php | 33 +++---------------- class/class.midtrans-gateway.php | 33 +++---------------- class/refund-option.php | 26 +++++++++++++++ 6 files changed, 46 insertions(+), 145 deletions(-) create mode 100644 class/refund-option.php diff --git a/class/class.midtrans-gateway-installment.php b/class/class.midtrans-gateway-installment.php index 8b3699e..17ba985 100755 --- a/class/class.midtrans-gateway-installment.php +++ b/class/class.midtrans-gateway-installment.php @@ -509,35 +509,10 @@ 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) { + $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + + return $refundResponse; } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway-installmentoff.php b/class/class.midtrans-gateway-installmentoff.php index d90d9be..5aa744f 100755 --- a/class/class.midtrans-gateway-installmentoff.php +++ b/class/class.midtrans-gateway-installmentoff.php @@ -533,35 +533,10 @@ 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) { + $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + + return $refundResponse; } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway-paymentrequest.php b/class/class.midtrans-gateway-paymentrequest.php index 915e29d..6aac35f 100755 --- a/class/class.midtrans-gateway-paymentrequest.php +++ b/class/class.midtrans-gateway-paymentrequest.php @@ -441,35 +441,10 @@ 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) { + $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + + return $refundResponse; } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway-promo.php b/class/class.midtrans-gateway-promo.php index 4ee4152..2cdc9a7 100755 --- a/class/class.midtrans-gateway-promo.php +++ b/class/class.midtrans-gateway-promo.php @@ -534,35 +534,10 @@ 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) { + $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + + return $refundResponse; } function receipt_page( $order_id ) { diff --git a/class/class.midtrans-gateway.php b/class/class.midtrans-gateway.php index e1cd95b..4c33cb7 100755 --- a/class/class.midtrans-gateway.php +++ b/class/class.midtrans-gateway.php @@ -561,35 +561,10 @@ 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) { + $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + + return $refundResponse; } /** diff --git a/class/refund-option.php b/class/refund-option.php new file mode 100644 index 0000000..d6dddc8 --- /dev/null +++ b/class/refund-option.php @@ -0,0 +1,26 @@ +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 false; + } + + 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 true; + } \ No newline at end of file From bb5c708beed5c87291e62d857feef38a5006a676 Mon Sep 17 00:00:00 2001 From: Alexander Kevin Date: Wed, 19 Feb 2020 15:16:25 +0700 Subject: [PATCH 3/4] change the wording on promo code description --- class/class.midtrans-gateway-promo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class/class.midtrans-gateway-promo.php b/class/class.midtrans-gateway-promo.php index 2cdc9a7..30ebb1f 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 use promo code.', '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( From e9d63b60412fae59d071f7852ebd29b8e2ddda3d Mon Sep 17 00:00:00 2001 From: Alexander Kevin Date: Wed, 19 Feb 2020 17:11:01 +0700 Subject: [PATCH 4/4] refactor function process_refund --- class/class.midtrans-gateway-installment.php | 6 ++++-- class/class.midtrans-gateway-installmentoff.php | 6 ++++-- class/class.midtrans-gateway-paymentrequest.php | 6 ++++-- class/class.midtrans-gateway-promo.php | 6 ++++-- class/class.midtrans-gateway.php | 6 ++++-- class/refund-option.php | 12 +++++++----- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/class/class.midtrans-gateway-installment.php b/class/class.midtrans-gateway-installment.php index 17ba985..a5b9b68 100755 --- a/class/class.midtrans-gateway-installment.php +++ b/class/class.midtrans-gateway-installment.php @@ -510,9 +510,11 @@ function process_payment( $order_id ) { * @return bool */ function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { - $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); - return $refundResponse; + 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 5aa744f..135851d 100755 --- a/class/class.midtrans-gateway-installmentoff.php +++ b/class/class.midtrans-gateway-installmentoff.php @@ -534,9 +534,11 @@ function process_payment( $order_id ) { * @return bool */ function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { - $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); - return $refundResponse; + 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 6aac35f..fa847e1 100755 --- a/class/class.midtrans-gateway-paymentrequest.php +++ b/class/class.midtrans-gateway-paymentrequest.php @@ -442,9 +442,11 @@ function process_payment( $order_id ) { * @return bool */ function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { - $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); - return $refundResponse; + 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 30ebb1f..99a4d95 100755 --- a/class/class.midtrans-gateway-promo.php +++ b/class/class.midtrans-gateway-promo.php @@ -535,9 +535,11 @@ function process_payment( $order_id ) { * @return bool */ function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { - $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); - return $refundResponse; + 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 4c33cb7..a3b13e3 100755 --- a/class/class.midtrans-gateway.php +++ b/class/class.midtrans-gateway.php @@ -562,9 +562,11 @@ function process_payment( $order_id ) { * @return bool */ function process_refund($order_id, $amount = null, $reason = '', $duplicated = false) { - $refundResponse = (require_once(dirname(__FILE__) . '/refund-option.php')) ? true : new WP_Error( 'midtrans_refund_error', $error_message ); + require_once(dirname(__FILE__) . '/refund-option.php'); + $refundResponse = refund_option($order_id, $amount, $reason, $this); - return $refundResponse; + 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 index d6dddc8..175fbb4 100644 --- a/class/refund-option.php +++ b/class/refund-option.php @@ -1,9 +1,10 @@ environment == 'production') ? true : false; - \Midtrans\Config::$serverKey = (\Midtrans\Config::$isProduction) ? $this->server_key_v2_production : $this->server_key_v2_sandbox; + \Midtrans\Config::$isProduction = ($dependencies->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( @@ -16,11 +17,12 @@ $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 false; + 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 true; - } \ No newline at end of file + return $response->status_code; + } +} \ No newline at end of file