Skip to content

Commit

Permalink
Merge pull request #33 from aalexanderkevin/master
Browse files Browse the repository at this point in the history
improve refund and promo code
  • Loading branch information
rizdaprasetya authored Feb 20, 2020
2 parents 8521286 + e9d63b6 commit 017bce7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 151 deletions.
35 changes: 6 additions & 29 deletions class/class.midtrans-gateway-installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
35 changes: 6 additions & 29 deletions class/class.midtrans-gateway-installmentoff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
35 changes: 6 additions & 29 deletions class/class.midtrans-gateway-paymentrequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
46 changes: 11 additions & 35 deletions class/class.midtrans-gateway-promo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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 ) {
Expand Down
35 changes: 6 additions & 29 deletions class/class.midtrans-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions class/refund-option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
function refund_option($order_id, $amount, $reason, $dependencies) {
require_once(dirname(__FILE__) . '/../lib/midtrans/Midtrans.php');
require_once(dirname(__FILE__) . '/class.midtrans-utils.php');

\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(
'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;
}
}

0 comments on commit 017bce7

Please sign in to comment.