Wednesday, 31 March 2021

Android Stripe Payment Integration

 How to make Payment with Stripe , Here we are doing the Stripe Payment process integration.


Card card = new Card("4242424242424242", 12, 2022, "123");
// Card card = new Card(cardNumber, cardExpMonth, cardExpYear, cardCVC);
boolean validation = card.validateCard();
if (validation) {
Stripe stripe = new Stripe(mContext);
// Stripe stripe = new Stripe(Pay_With_Bank.this);
stripe.createToken(
card,
userPublishableKey,
new TokenCallback() {
public void onSuccess(Token token) {
if (checkInternetConnection()) {
Log.d("tokendataid", token.toString());
dialog = new ProgressDialog(mContext);
dialog.setMessage("Please Wait...");
dialog.show();

new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
sendCardToInfoToStripe(token.getId());
}
});
}
}).start();
} else {
dialog.dismiss();
Toast.makeText(mContext, "enable Internet", Toast.LENGTH_LONG).show();
}
}
public void onError(Exception error) {
dialog.dismiss();
Toast.makeText(mContext, error.getMessage(), Toast.LENGTH_LONG).show();
// myUtility.setAlertMessage("Error", error.getMessage());
}
});
return true;
} else if (!card.validateNumber()) {
dialog.dismiss();
Toast.makeText(mContext, "The card number that you entered is invalid.", Toast.LENGTH_SHORT).show();
} else if (!card.validateExpiryDate()) {
dialog.dismiss();
Toast.makeText(mContext, "The expiration date that you entered is invalid.", Toast.LENGTH_SHORT).show();
} else if (!card.validateCVC()) {
dialog.dismiss();
Toast.makeText(mContext, "The CVC code that you entered is invalid.", Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
Toast.makeText(mContext, "The card details that you entered are invalid.", Toast.LENGTH_SHORT).show();
}




Here , we are giving static card details .


private boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager) mContext.getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
private void sendCardToInfoToStripe(String token) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build();
StrictMode.setThreadPolicy(policy);

try {
com.stripe.Stripe.apiKey = "";
Map<String, Object> params = new HashMap<>();
int pricedata = Integer.parseInt(price) * 100;
price = String.valueOf(pricedata);
params.put("amount", price);
params.put("currency", "usd");
params.put("description", "Discription");
params.put("source", token);
Log.d("asdf", params.toString());
Charge charge = Charge.create(params);
Log.d("token_id", charge.getStatus());
Log.d("payment_id", charge.getId());
Toast.makeText(mContext, charge.getStatus(), Toast.LENGTH_SHORT).show();
bank_payment_type = "bank";
bankpayment_status = charge.getStatus();
if (bankpayment_status.equalsIgnoreCase("succeeded")) {
bankpayment_status = "completed";
Toast.makeText(mContext, "Payment is complted", Toast.LENGTH_SHORT).show();
} else {
bankpayment_status = "cancelled";
}
bank_payment_id = charge.getId();
try {
dialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
if (appoint.equalsIgnoreCase("appointment")) {
var = "1";
} else {
}
Log.d("log..", "status" + charge.getStatus());
} catch (Exception e) {
dialog.dismiss();
Toast.makeText(mContext, "error occur"+e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}



This is the code how to use Stripe Payment in Android.

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...