Credit Card Two Clicks - veritrans/veritrans-android GitHub Wiki
For two clicks
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 two clicks please follow steps below.
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).
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
}
});
// 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
}
});
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
}
});
You can use the savedTokenId
obtained in first transaction to proceed the payment without require your customers to input their credit card details again. They just need to input the CVV
.
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
}
});
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
}
});
After user select which card want to be used on next payment, they have need to input CVV
again then proceed into get card token using this parameters.
CardTokenRequest cardTokenRequest = new CardTokenRequest();
cardTokenRequest.setCardCVV(CVV);
// SAVED_TOKEN_ID included in SavedToken object obtained in previous step
cardTokenRequest.setSavedTokenId(SAVED_TOKEN_ID);
cardTokenRequest.setTwoClick(true);
cardTokenRequest.setSecure(true);
cardTokenRequest.setGrossAmount(GROSS_AMOUNT);
cardTokenRequest.setClientKey(VT_CLIENT_KEY);
midtransSDK.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) {
// Handle failure
}
@Override
public void onError(Throwable error) {
// Handle error
}
});
// Create payment model using masked card number from saved token object obtained in previous step
CreditCardPaymentModel payment = new CreditCardPaymentModel(CARD_TOKEN, IS_SAVE_CARD);
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
}
});