-
Notifications
You must be signed in to change notification settings - Fork 54
Credit Card One Click
Zia Ulhaq edited this page Feb 2, 2017
·
2 revisions
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.
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.
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
}
});
// 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 saved card on first transaction by providing SDK same user_id
as before when doing checkout.
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
}
});
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
}
});