Skip to content

Credit Card One Click

Zia Ulhaq edited this page Feb 2, 2017 · 2 revisions

Overview

For one click implementation, merchant server implementation is not required.

Note:

  • Checkout still require merchant server.
  • Snap updated their engine so now they have feature to save card.

To implement one click, please follow below steps.

Implementation

Creating TransactionRequest

TransactionRequest request = new TransactionRequest(ORDER_ID, GROSS_AMOUNT);
... // Set another parameters like item details or customer details for transaction request here

// Set credit card option parameters
CreditCard creditCard = new CreditCard();
creditCard.setSaveCard(true); // True if save card is enabled (to get the savedTokenId)
creditCard.setSecure(true); // True if 3D Secure was enabled. This is also required to enable one click

request.setCreditCard(creditCard);

MidtransSDK.getInstance().setTransactionRequest(request);

MidtransSDK.getInstance().checkout(USER_ID, new CheckoutCallback() {
    @Override
    public void onSuccess(Token token) {
         // Handle success checkout
         // Checkout token
         String token = token.getTokenId();
    }

    @Override
    public void onFailure(Token token, String reason) {
         // Handle failure when checkout            
    }

    @Override
    public void onError(Throwable error) {
         // Handle error when checkout
    }
});

Note: You must give SDK USER_ID so SDK can differentiate the card's owner.

Get Transaction Details (Including Saved Card)

this step will be used to get transaction information (snap token, transaction details, enabled payment type, info merchant and information about creditcard.

MidtransSDK.getInstance().getTransactionOptions(tokenId, new TransactionOptionsCallback() {
            @Override
            public void onSuccess(Transaction transaction) {
                // Handle success here
                // Get Saved Card
                List<SavedToken> savedTokenList = transaction.getCreditCard().getSavedTokens();
            }

            @Override
            public void onFailure(Transaction transaction, String reason) {
                // Handle failure here
            }

            @Override
            public void onError(Throwable error) {
                // Handle error here
            }
        });

Get Card Token

// Create CardTokenRequest 
CardTokenRequest cardTokenRequest = new CardTokenRequest(CARD_NUMBER, CARD_CVV_NUMBER, EXPIRY_MONTH, EXPIRY_YEAR, CLIENT_KEY);
// This is if you're using 3D secure
cardTokenRequest.setSecure(true);
cardTokenRequest.setGrossAmount(GROSS_AMOUNT);

// Start getting token
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
        // If using 3DS then show the authorization page using WebView. When authorization completed then start the payment
        // Else start to charge the transaction
    }

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

    @Override
    public void onError(Throwable error) {
        // Error action here
    }
});

Charge Transaction

At the charge transaction, when charging you must set IS_SAVE_CARD parameter to true.

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
    }
});

Sequential Transaction

You can use saved card on first transaction by providing SDK same user_id as before when doing checkout.

Checkout using USER_ID

MidtransSDK.getInstance().checkout(USER_ID, new CheckoutCallback() {
    @Override
    public void onSuccess(Token token) {
         // Handle success checkout
         // Checkout token
         String token = token.getTokenId();
    }

    @Override
    public void onFailure(Token token, String reason) {
         // Handle failure when checkout            
    }

    @Override
    public void onError(Throwable error) {
         // Handle error when checkout
    }
});

Get Transaction Details (Including Saved Card)

MidtransSDK.getInstance().getTransactionOptions(tokenId, new TransactionOptionsCallback() {
            @Override
            public void onSuccess(Transaction transaction) {
                // Handle success here
                // Get Saved Card
                List<SavedToken> savedTokenList = transaction.getCreditCard().getSavedTokens();
            }

            @Override
            public void onFailure(Transaction transaction, String reason) {
                // Handle failure here
            }

            @Override
            public void onError(Throwable error) {
                // Handle error here
            }
        });

Charge Payment

At one click payment, you don't need to get card token. Instead you can directly pay using masked_card obtained on saved card at previous step.

// Create payment model using masked card number from saved token object obtained in previous step
CreditCardPaymentModel payment = new CreditCardPaymentModel(MASKED_CARD_NUMBER);

MidtransSDK.getInstance().paymentUsingCard(CHECKOUT_TOKEN, payment, new TransactionCallback() {
            @Override
            public void onSuccess(TransactionResponse response) {
                // Handle payment success
                
            }

            @Override
            public void onFailure(TransactionResponse response, String reason) {
                // Handle payment failure
            }

            @Override
            public void onError(Throwable error) {
                // Handle payment error
            }
        });
Clone this wiki locally