Stripe is the simple and best way to accept mobile payments inside any android app.
Steps are as below.
1) First you need to import Stripe library and add it to your project.
On stripe.com you will get the stripe library but it will be a gr-addle support library, you need to convert it to Android project structure.
2) Add android SDK with API level-17 and android-support-v4 to you SDK if it's not added.
3) Create the Dialog to retrieve Card information from user with the Cancel and Done buttons.
Required information for card constructor is
Card No :- 16 digit, Expiry Date:- MM/YY and Card CVC No :- 3 digit.
For testing you can use
Card No;- 4242 4242 4242 4242
Expiry Date:- 12/25 (any date grater from current date)
CVC No :- 123 (any three digits)
if user clicks on the done button of Dialog then get the card info and assign it to the Following fields,
/*It's better if you create all these fields globally in class*/
private int cardExpMonth,cardExpYear;
private String cardNumber = "",cardCVC = "";
private static String publishKey = "";
private static Card card;
In my class I have created this method top retrieve card info from Payment-dialog
private void getCardInfo()
{
cardNumber = firstFourDigit.getText().toString() + secondFourDigit.getText().toString()
+ thirdFourDigit.getText().toString() + fourthFourDigit.getText().toString();
cardCVC = ccvCode.getText().toString();
String [] dateMonth = expiryDate.getText().toString().split("/");
if(!dateMonth[0].isEmpty())
{
cardExpMonth = Integer.parseInt(dateMonth[0]);
if(dateMonth.length == 2 && !dateMonth[1].isEmpty())
{
cardExpYear = Integer.parseInt(dateMonth[1]);
}
}
}
After Successfully retrieving the Card info you need to Validate the Card info creating the card constructor like this
private boolean ValidateCard()
{
//boolean status = false;
card = new Card(
cardNumber, // It's String type
cardExpMonth, //It's int type 2digit
cardExpYear, //It's int type 2 digit
cardCVC // It's String type
);
/*card.validateNumber() will simply validate the 16 digit no provided by the user returns true if the card no is correct false if card no is wrong.*/
if(!card.validateNumber())
{
/*showToast is the method which will Toast the passed String, you can simply call the Toast.makeToast.... as well */
showToast(R.string.invaliCardNo);
return false;
}else if(!card.validateExpiryDate())
{
showToast(R.string.invalideCardDate);
return false;
}else if(!card.validateCVC())
{
showToast(R.string.invalideCardCvc);
return false;
}
return true;
}
4) Get the Publish_key from you server, the publish key is the Stripe account info key. Normally client will provide this key.
create the Stripe instance and call create Token methods like this.
try {
Stripe stripe = new Stripe(publishKey);
stripe.createToken(card,
new TokenCallback() {
@Override
public void onSuccess(Token token) {
Log.i("This is the stripe token here", ""+token);
/*If you enter into onSuccess then Stripe token is created successfully, Next pass this token to your Server with server required information and if you got success result from you server then payment is done successfully.*/
}
@Override
public void onError(Exception error) {
// Show localized error message
Toast.makeText(getContext(), R.string.transactionError, Toast.LENGTH_SHORT).show();
}
});
} catch (AuthenticationException e) {
e.printStackTrace();
}
No comments:
Post a Comment