Snap JS

snap has 3 public functions: pay, show & hide .




pay(snapToken, options)


Start Snap payment page.


snap.pay('YOUR_SNAP_TOKEN', {
  onSuccess: function(result){console.log('success');console.log(result);},
  onPending: function(result){console.log('pending');console.log(result);},
  onError: function(result){console.log('error');console.log(result);},
  onClose: function(){console.log('customer closed the popup without finishing the payment');}
})
NameDescription
snapToken
String (required)
Snap token acquired from backend integration
enabledPayments
Array (optional)
List of payment types to be displayed. This will filter out enabled payments from backend integration.
options.onSuccess
Function (optional)
Payment success callback (200 status_code)
options.onPending
Function (optional)
Payment pending callback (201 status_code)
options.onError
Function (optional)
Payment error callback (4xx or 5xx status_code)
options.onClose
Function (optional)
Called if customer has closed the payment popup prematurely without finishing the payment
options.language
String (optional)
Sets the language. This will override language setting on Merchant Administration Portal. Supported values are en (English) and id (Bahasa Indonesia). Defaults to id
options.autoCloseDelay
Integer (optional)
Auto closes the last page of Indomaret and Bank Transfer payments after the specified time delay. The time delay is specified in seconds. Setting it to 0 will disable this feature. Defaults to 0.
options.selectedPaymentType
String (optional)
Skips order summary and select payment page to directly select a specific payment type. Supported values are credit_card, cimb_clicks, bca_klikbca, bca_klikpay, bri_epay, telkomsel_cash, echannel, indosat_dompetku, permata_va, other_va, bca_va, bni_va, kioson, indomaret, gci, and danamon_online.
options.uiMode
String (optional)
Choose the UI mode for GoPay, ShopeePay . Supported values are deeplink, qr, and auto. Set to auto by default.

onSuccess, onPending, & onError function accept one parameter which is Transaction Result object.




show()


Show snap loading page. Helper function if you want to show instant loading feedback while getting SNAP_TOKEN using AJAX.

If AJAX success, call snap.pay to continue payment process. Else, call snap.hide to end loading page.




hide()


Hide active snap page. Complementary function of snap.show. Helper function if you want to show instant loading feedback while getting SNAP_TOKEN using AJAX.


function ajaxGetToken(transactionData, callback){
    var snapToken;
    // Request get token to your server & save result to snapToken variable

    if(snapToken){
      callback(null, snapToken);
    } else {
      callback(new Error('Failed to fetch snap token'),null);
    }
}

payButton.onclick(function(){
  snap.show();
  ajaxGetToken(transactionData, function(error, snapToken){
    if(error){
      snap.hide();
    } else {
      snap.pay(snapToken);
    }
  });
});