Skip to content

Credit Card Payment Usage

Zia Ulhaq edited this page Sep 12, 2017 · 14 revisions

Credit Card Payment

Credit card payment has several steps to do the charging.

  1. Tokenize - Wrap and encrypt your secure credit card information to one time token that will be used for charging.
  2. Charging - Use the token in step 1 to do the payment.

Card Type details

There are 3 types of flow for credit card payments.

  1. One click : Allows merchants to securely tokenize the credit card, and use it for transactions later, without users entering the card details again.
  2. Two click : Allows merchants to securely tokenize the credit card and use it for the later transactions, with the user entering the CVV.
  3. Normal : This is the simple case where user enters his credit card information every time a transaction is needed to be done.

Notes

  • These features are available on special MID's from Banks and may not be available to all merchants. We will go in detail about these flows ahead.
  • For normal flow we can do secure and Insecure way.
    • Secure : With 3d secure validation.
    • Insecure : Without 3d secure validation.
  • One click and two click can only be done in secure flow only(ie. 3DS validation)

To set the card click type and the secure mode of the transaction use code below.

CreditCard ccOptions = new CreditCard();
// True when using two clicks or one click
ccOptions.setSaveCard(true);
// set secure= true to enable 3ds secure optional
// one oneclick mode this is mandatory
ccOptions.setSecure(true);
// Set bank name when using MIGS channel
creditCardOptions.setBank(BankType.BANK_NAME);
// Set MIGS channel (ONLY for BCA, BRI and Maybank Acquiring bank)
creditCardOptions.setChannel(CreditCard.MIGS);

// Set credit card options into transaction request
MidtransSDK.getInstance().getTransactionRequest().setCreditCard(ccOptions);
  

Create Tokenize Request

You must create a tokenize request by using customer credit card details before doing the tokenize.

// CardTokenRequest class contains card details required for getting token.
CardTokenRequest cardTokenRequest = new CardTokenRequest(CARD_NUMBER, CARD_CVV_NUMBER, EXPIRY_MONTH, EXPIRY_YEAR, CLIENT_KEY);
// Optional mode
cardTokenRequest.setSecure(IS_SECURE); // Set to true if this token will need 3DS authorization
cardTokenRequest.setIsSaved(IS_SAVED); // Set to true if this token will be used later (one click or two click)
cardTokenRequest.setTwoClick(IS_TWO_CLICK); // Set to true if using two click
cardTokenRequest.setSavedTokenId(SAVED_TOKEN_ID); // Set the saved token id if using two click
cardTokenRequest.setBank(BankType.MANDIRI); // Set acquiring bank (Optional)

Tokenize Credit Card Details

Using tokenize request created in previous step you can proceed to do the tokenize.

MidtransSDK.getInstance().getCardToken(cardTokenRequest, new CardTokenCallback() {
    @Override
    public void onSuccess(TokenDetailsResponse response) {
        // Card token will be used to charge the payment
        String cardToken = response.getTokenId();
        // Success action here
    }

    @Override
    public void onFailure(TokenDetailsResponse response, String reason) {
        // Failure action here
    }

    @Override
    public void onError(Throwable error) {
        // Error action here
    }
});
Handle 3DS (If using secure mode)

If you use secure mode when doing tokenize, you will get redirect URL on TokenDetailsResponse.

To get the redirect URL, use following code.

String redirectUrl = response.getRedirectUrl();

Then you need a webview with a custom client to capture redirect URL if the authorization was completed.

For example:

// Setup a custom `WebViewClient` to capture completed 3D secure authorization
class VeritransWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // Check if redirect URL has "/token/callback/" to ensure authorization was completed
            if (url.contains("/token/callback/")) {
               // Authorization was completed
               // Handle the charging here
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
}

// Set the VeritransWebViewClient on your WebView
webView.setWebViewClient(new VeritransWebViewClient());
// Enable JavaScript on your WebView
webView.getSettings().setJavaScriptEnabled(true);
// Load redirect URL on your WebView
webView.loadUrl(redirectUrl);

Charge Payment

Using Card Token from previous step and checkout token from this step you can start charging the transaction.

CreditCardPaymentModel payment = new CreditCardPaymentModel(CARD_TOKEN, IS_SAVE_CARD);
MidtransSDK.getInstance().paymentUsingCard(CHECKOUT_TOKEN, payment, new TransactionCallback() {
    @Override
    public void onSuccess(TransactionResponse response) {
        // Success Action here
    }

    @Override
    public void onFailure(TransactionResponse response, String reason) {
        // Failure Action here
    }

    @Override
    public void onError(Throwable error) {
        // Error Action here
    }
});
Clone this wiki locally