Skip to content

Commit

Permalink
Merge pull request #51 from veritrans/wip-2.22
Browse files Browse the repository at this point in the history
Release version 2.22
  • Loading branch information
rizdaprasetya authored Apr 27, 2021
2 parents 78bf303 + 5f00d3d commit 9bd6102
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 47 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ Note: This section is optional and only for advanced usage.
Available for customization from plugin config:
- Payment text label of the payment options
- Payment text description of the payment options
- You can also input html tags as the text, to insert something like image
- On both configuration fields above can also input html tags as the text, to insert something like image. For example you can input like this to show images:

```html
Online Payment via Midtrans <img src="https://docs.midtrans.com/asset/image/main/midtrans-logo.png">
```

You can change the image, like if you want to show the logo of banks or payment providers that you are accepting.

Additional payment options (radio button) can be activated:
- Installment
Expand Down
2 changes: 1 addition & 1 deletion class/class.midtrans-gateway-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static function createRefund( $order_id, $params, $plugin_id="midtrans" )
* Get Midtrans Notification.
* @return object Midtrans Notification response.
*/
public static function getMidtransNotif( $plugin_id="midtrans") {
public static function getStatusFromMidtransNotif( $plugin_id="midtrans") {
self::fetchAndSetMidtransApiConfig( $plugin_id );
return new Midtrans\Notification();
}
Expand Down
56 changes: 41 additions & 15 deletions class/class.midtrans-gateway-notif-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
/**
* WC_Gateway_Midtrans_Notif_Handler class.
* Handles responses from Midtrans Notification.
* @todo : refactor, this shouldn't be a class
* maybe just a bunch of function to include in main class
* to avoid complex param & config value passing
*/
class WC_Gateway_Midtrans_Notif_Handler
// extends WC_Gateway_
{
/**
* Constructor.
*
* @param bool $is_production Use production or not.
* @param string $server_key ServerKey to receive HTTP notification from Midtrans.
*
*/
public function __construct() {
// Register hook for handling HTTP notification (HTTP call to `http://[your web]/?wc-api=WC_Gateway_Midtrans`)
Expand Down Expand Up @@ -49,6 +50,22 @@ public function doEarlyAckResponse() {
return $raw_notification;
}

/**
* getPluginOptions
* @param string $plugin_id plugin id of the paid order
* @return array plugin options
*/
public function getPluginOptions($plugin_id = 'midtrans'){
// Get current plugin options
$plugin_options = array();
try {
$plugin_options = get_option( 'woocommerce_' . $plugin_id . '_settings' );
} catch (Exception $e) {
WC_Midtrans_Logger::log( 'Fail to getPluginOptions', 'midtrans-error' );
};
return $plugin_options;
}

/**
* Called by hook function when HTTP notification / API call received
* Handle Midtrans payment notification
Expand All @@ -69,8 +86,9 @@ public function handleMidtransNotificationRequest() {
isset($_POST['response'])? sanitize_text_field($_POST['response']): null;

// check whether the request is POST or GET,
// if request == POST, request is for payment notification, then update the payment status
if(empty($sanitized['order_id']) && empty($sanitizedPost['id']) && empty($sanitized['id']) && empty($sanitizedPost['response'])) { // Check if POST, then create new notification
// @TODO: refactor this conditions, this doesn't quite represent conditions for a POST request
if(empty($sanitized['order_id']) && empty($sanitizedPost['id']) && empty($sanitized['id']) && empty($sanitizedPost['response'])) {
// Request is POST, proceed to create new notification, then update the payment status
$raw_notification = $this->doEarlyAckResponse();
// Handle pdf url update
$this->handlePendingPaymentPdfUrlUpdate();
Expand All @@ -81,20 +99,20 @@ public function handleMidtransNotificationRequest() {
WC_Midtrans_Logger::log( 'Can\'t find order id' . $raw_notification['order_id'] . ' on WooCommerce dashboard', 'midtrans-error' );
exit;
}
// Get plugin id
// Get current plugin id
else $plugin_id = $wcorder->get_payment_method();
// Verify Midtrans notification
$midtrans_notification = WC_Midtrans_API::getMidtransNotif( $plugin_id );
$midtrans_notification = WC_Midtrans_API::getStatusFromMidtransNotif( $plugin_id );
// If notification verified, handle it
if (in_array($midtrans_notification->status_code, array(200, 201, 202, 407))) {
if (wc_get_order($midtrans_notification->order_id) != false) {
do_action( "midtrans-handle-valid-notification", $midtrans_notification );
do_action( "midtrans-handle-valid-notification", $midtrans_notification, $plugin_id );
}
}
exit;
}
// if request == GET, request is for finish OR failed URL, then redirect to WooCommerce's order complete/failed
else {
// The request == GET, this will handle redirect url from Snap finish OR failed, proceed to redirect to WooCommerce's order complete/failed page
$sanitized['transaction_status'] =
isset($_GET['transaction_status'])? sanitize_text_field($_GET['transaction_status']): null;
$sanitized['status_code'] =
Expand All @@ -111,13 +129,17 @@ public function handleMidtransNotificationRequest() {
}
// if or pending/challenge
else if( !empty($sanitized['order_id']) && !empty($sanitized['transaction_status']) && $sanitized['status_code'] == 201) {
// @FIXME: $this->ignore_pending_status is broken, it doesn't refer to plugin class
if(property_exists($this,'ignore_pending_status') && $this->ignore_pending_status == 'yes'){
$order_id = $sanitized['order_id'];
$order = new WC_Order( $order_id );
$plugin_id = $order->get_payment_method();

$plugin_options = $this->getPluginOptions($plugin_id);
if( array_key_exists('ignore_pending_status',$plugin_options)
&& $plugin_options['ignore_pending_status'] == 'yes'
){
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit;
}
$order_id = $sanitized['order_id'];
$order = new WC_Order( $order_id );
wp_redirect($order->get_checkout_order_received_url());
}
//if deny, redirect to order checkout page again
Expand Down Expand Up @@ -223,7 +245,7 @@ public function handlePendingPaymentPdfUrlUpdate(){
* notification
* @return void
*/
public function handleMidtransValidNotificationRequest( $midtrans_notification ) {
public function handleMidtransValidNotificationRequest( $midtrans_notification, $plugin_id = 'midtrans' ) {
global $woocommerce;

$order = new WC_Order( $midtrans_notification->order_id );
Expand Down Expand Up @@ -265,10 +287,14 @@ public function handleMidtransValidNotificationRequest( $midtrans_notification )
$order->update_meta_data('_mt_payment_transaction_id',$midtrans_notification->transaction_id);
$order->save();

if(property_exists($this,'ignore_pending_status') && $this->ignore_pending_status == 'yes'){
$plugin_options = $this->getPluginOptions($plugin_id);
if( array_key_exists('ignore_pending_status',$plugin_options)
&& $plugin_options['ignore_pending_status'] == 'yes'
){
exit;
}
$order->update_status('on-hold',__('Awaiting payment: Midtrans-'.$midtrans_notification->payment_type,'midtrans-woocommerce'));
error_log($plugin_options['ignore_pending_status']);
}
else if ($midtrans_notification->transaction_status == 'refund' || $midtrans_notification->transaction_status == 'partial_refund') {
$refund_request = $this->validateRefundNotif( $midtrans_notification );
Expand Down
2 changes: 1 addition & 1 deletion class/class.midtrans-gateway-paymentrequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function init_form_fields() {
'title' => __( 'Acquiring Bank', 'midtrans-woocommerce'),
'type' => 'text',
'label' => __( 'Acquiring Bank', 'midtrans-woocommerce' ),
'description' => __( 'Leave blank for default. </br> Specify your acquiring bank for this payment option. </br> Options: BCA, BRI, DANAMON, MAYBANK, BNI, MANDIRI, CIMB, etc (Only choose 1 bank).' , 'midtrans-woocommerce' ),
'description' => __( 'You should leave it empty, it will be auto configured. </br> Alternatively may specify your card-payment acquiring bank for this payment option. </br> Options: BCA, BRI, DANAMON, MAYBANK, BNI, MANDIRI, CIMB, etc (Only choose 1 bank).' , 'midtrans-woocommerce' ),
'default' => ''
),
'bin_number' => array(
Expand Down
2 changes: 1 addition & 1 deletion class/class.midtrans-gateway-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function init_form_fields() {
'title' => __( 'Acquiring Bank', 'midtrans-woocommerce'),
'type' => 'text',
'label' => __( 'Acquiring Bank', 'midtrans-woocommerce' ),
'description' => __( 'Leave blank for default. </br> Specify your acquiring bank for this payment option. </br> Options: BCA, BRI, DANAMON, MAYBANK, BNI, MANDIRI, CIMB, etc (Only choose 1 bank).' , 'midtrans-woocommerce' ),
'description' => __( 'You should leave it empty, it will be auto configured. </br> Alternatively may specify your card-payment acquiring bank for this payment option. </br> Options: BCA, BRI, DANAMON, MAYBANK, BNI, MANDIRI, CIMB, etc (Only choose 1 bank).' , 'midtrans-woocommerce' ),
'default' => ''
),
'bin_number' => array(
Expand Down
2 changes: 1 addition & 1 deletion class/class.midtrans-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function init_form_fields() {
'title' => __( 'Acquiring Bank', 'midtrans-woocommerce'),
'type' => 'text',
'label' => __( 'Acquiring Bank', 'midtrans-woocommerce' ),
'description' => __( 'Leave blank for default. </br> Specify your acquiring bank for this payment option. </br> Options: BCA, BRI, DANAMON, MAYBANK, BNI, MANDIRI, CIMB, etc (Only choose 1 bank).' , 'midtrans-woocommerce' ),
'description' => __( 'You should leave it empty, it will be auto configured. </br> Alternatively may specify your card-payment acquiring bank for this payment option. </br> Options: BCA, BRI, DANAMON, MAYBANK, BNI, MANDIRI, CIMB, etc (Only choose 1 bank).' , 'midtrans-woocommerce' ),
'default' => ''
)
));
Expand Down
44 changes: 25 additions & 19 deletions class/midtrans-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
'label' => __( 'Enable Midtrans Payment', 'midtrans-woocommerce' ),
'default' => 'no'
),
'title' => array(
'title' => __( 'Title', 'midtrans-woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'midtrans-woocommerce' ),
'default' => $this->getDefaultTitle(),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Customer Message', 'midtrans-woocommerce' ),
'type' => 'textarea',
'description' => __( 'This controls the description which the user sees during checkout', 'midtrans-woocommerce' ),
'default' => $this->getDefaultDescription(),
),
'merchant_id' => array(
'title' => __("Merchant ID", 'midtrans-woocommerce'),
'type' => 'text',
Expand Down Expand Up @@ -74,11 +61,29 @@
'default' => '',
'class' => 'production_settings toggle-midtrans'
),
'title' => array(
'title' => __( 'Payment Title', 'midtrans-woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the payment label title which the user sees during checkout. <a href="https://github.com/veritrans/SNAP-Woocommerce#configurables" target="_blank">This support HTML tags</a> like &lt;img&gt; tag, if you want to include images.', 'midtrans-woocommerce' ),
'default' => $this->getDefaultTitle(),
// 'desc_tip' => true,
),
'description' => array(
'title' => __( 'Payment Description', 'midtrans-woocommerce' ),
'type' => 'textarea',
'description' => __( 'You can customize here the expanded description which the user sees during checkout when they choose this payment. <a href="https://github.com/veritrans/SNAP-Woocommerce#configurables" target="_blank">This support HTML tags</a> like &lt;img&gt; tag, if you want to include images.', 'midtrans-woocommerce' ),
'default' => $this->getDefaultDescription(),
),
'advanced_config_separator' => array(
'title' => __( 'Advanced Config Section - Optional, you can leave them default.', 'midtrans-woocommerce' ),
'type' => 'title',
'description' => __( '-- Configurations below is optional and don\'t need to be changed, you can leave them default. Unless you know you want advanced configuration --','midtrans-woocommerce'),
),
'enable_3d_secure' => array(
'title' => __( 'Enable 3D Secure', 'midtrans-woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable 3D Secure?', 'midtrans-woocommerce' ),
'description' => __( 'You must enable 3D Secure.
'description' => __( 'You should enable 3D Secure.
Please contact us if you wish to disable this feature in the Production environment.', 'midtrans-woocommerce' ),
'default' => 'yes'
),
Expand All @@ -91,10 +96,10 @@
'default' => 'no'
),
'enable_redirect' => array(
'title' => __( 'Redirect payment page', 'midtrans-woocommerce' ),
'title' => __( 'Redirect payment mode', 'midtrans-woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable payment page redirection?', 'midtrans-woocommerce' ),
'description' => __( 'This will redirect customer to Midtrans hosted payment page instead of popup payment page on your website. <br>Leave it disabled if you are not sure', 'midtrans-woocommerce' ),
'label' => __( 'Enable redirection for payment page?', 'midtrans-woocommerce' ),
'description' => __( 'This will redirect customer to Midtrans hosted payment page instead of popup payment page on your website. <br>Useful if you encounter issue with payment page on your website.', 'midtrans-woocommerce' ),
'class' => 'toggle-advanced',
'default' => 'no'
),
Expand All @@ -114,7 +119,7 @@
'title' => __( 'Use Dashboard Finish url', 'midtrans-woocommerce' ),
'type' => 'checkbox',
'label' => 'Use dashboard configured payment finish url?',
'description' => __( 'This will allow use of Dashboard configured payment finish url instead of auto configured url', 'midtrans-woocommerce' ),
'description' => __( 'This will alternatively redirect customer to Dashboard configured payment finish url instead of auto configured url, after payment is completed', 'midtrans-woocommerce' ),
'default' => 'no'
),
'ganalytics_id' => array(
Expand All @@ -130,11 +135,12 @@
'description' => __( 'By default, item stock only reduced if payment status on Midtrans reach pending/success (customer choose payment channel and click pay on payment pop-up). Enable this if you want to immediately reduce item stock when payment pop-up generated/displayed.', 'midtrans-woocommerce' ),
'default' => 'no'
),
// @Note: only main plugin class config will be applied on notif handler, sub plugin class config will not affect it, check gateway-notif-handler.php class to fix
'ignore_pending_status' => array(
'title' => __( 'Ignore Midtrans Transaction Pending Status', 'midtrans-woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Ignore Midtrans Transaction Pending Status?', 'midtrans-woocommerce' ),
'description' => __( 'This will prevent customer for being redirected to "order received" page, on unpaid async payment type. <br>Backend pending notification will also ignored, and not trigger change to "on-hold". <br>Leave it disabled if you are not sure', 'midtrans-woocommerce' ),
'description' => __( 'This will prevent customer for being redirected to "order received" page, on unpaid async payment type. <br>Backend pending notification will also ignored, and will not change to "on-hold" status. <br>Leave it disabled if you are not sure', 'midtrans-woocommerce' ),
'class' => 'toggle-advanced',
'default' => 'no'
),
Expand Down
2 changes: 1 addition & 1 deletion midtrans-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Midtrans - WooCommerce Payment Gateway
Plugin URI: https://github.com/veritrans/SNAP-Woocommerce
Description: Accept all payment directly on your WooCommerce site in a seamless and secure checkout environment with <a target="_blank" href="https://midtrans.com/">Midtrans</a>
Version: 2.21.0
Version: 2.22.0
Author: Midtrans
Author URI: http://midtrans.co.id
License: GPLv2 or later
Expand Down
19 changes: 13 additions & 6 deletions public/js/midtrans-payment-page-main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// wc_midtrans var is passed from payment-page backend via inline script.

var payButton = document.getElementById("pay-button");
;(function( $, window, document ) {
var payButton = document.getElementById("pay-button");

document.addEventListener("DOMContentLoaded", function(event) {
function MixpanelTrackResult(token, merchant_id, cms_name, cms_version, plugin_name, plugin_version, status, result) {
var eventNames = {
pay: 'pg-pay',
Expand Down Expand Up @@ -50,8 +50,9 @@ document.addEventListener("DOMContentLoaded", function(event) {
var retryCount = 0;
var snapExecuted = false;
var intervalFunction = 0;
// Continously retry to execute SNAP popup if fail, with 1000ms delay between retry

function execSnapCont(ccDetails){
// Continously retry to execute SNAP popup if fail, periodically w/ 1000ms delay between retry
intervalFunction = setInterval(function() {
try{
snap.pay(SNAP_TOKEN,
Expand Down Expand Up @@ -131,10 +132,12 @@ document.addEventListener("DOMContentLoaded", function(event) {
} catch (e){
retryCount++;
if(retryCount >= 10){
location.reload(); payButton.innerHTML = "Loading..."; return;
// stop retrying, let the pay button trigger page refresh
payButton.innerHTML = "Proceed To Payment";
return 0;
}
console.log(e);
console.log("Snap not ready yet... Retrying in 1000ms!");
console.log("Snap.pay() fail to execute... Retrying in 1000ms!");
} finally {
if (snapExecuted) {
clearInterval(intervalFunction);
Expand Down Expand Up @@ -181,6 +184,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
var clickCount = 0;
function handlePayAction() {
if(clickCount >= 2){
// refresh page, hoping reloading all frontend state will fix Snap fail to open
location.reload();
payButton.innerHTML = "Loading...";
return;
Expand All @@ -189,6 +193,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
var isPaymentRequestPlugin = wc_midtrans.is_payment_request_plugin;
// Check if this is paymentRequest sub-plugin & paymentRequest is supported
if(isPaymentRequestPlugin && window.PaymentRequest){
// utilize Chrome in-built paymentRequest browser feature for Card txn
var payRequest = createPaymentRequest();
payRequest
.show()
Expand All @@ -210,6 +215,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
execSnapCont(ccDetails);
})
} else {
// execute snap normally
execSnapCont(ccDetails);
}
clickCount++;
Expand All @@ -225,4 +231,5 @@ document.addEventListener("DOMContentLoaded", function(event) {

handlePayAction();
payButton.innerHTML = "Proceed To Payment";
});
})( jQuery, window, document );
// well jQuery is not actually used, just for formality
Loading

0 comments on commit 9bd6102

Please sign in to comment.