add below gradle for client and server sdk

// Stripe Android server SDK
implementation ‘com.stripe:stripe-java:14.0.0’
// Stripe Android client SDK
implementation ‘com.stripe:stripe-android:14.1.1’

xml design


<com.stripe.android.view.CardInputWidget
android:id=”@+id/cardInputWidget”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginLeft=”20dp”
android:layout_marginRight=”20dp”
app:layout_constraintTop_toTopOf=”parent”/>

Java file


private String paymentIntentClientSecret;
private Stripe stripe;
CardInputWidget cardInputWidget;

protected void onCreate(Bundle savedInstanceState) {

PaymentConfiguration.init(getApplicationContext(),
“pk_test_******************”);

}

private class ServerCall extends AsyncTask<Void,Void,Void>{

@Override
protected Void doInBackground(Void… voids) {
serverSide();
return null;
}
}

public void serverSide(){
try {
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
com.stripe.Stripe.apiKey = “sk_test_*************************”;

PaymentIntentCreateParams createParams = new PaymentIntentCreateParams.Builder()
.setCurrency(“inr”).setAmount(1099L).setDescription(“LGMTest”)
.build();

com.stripe.model.PaymentIntent intent = com.stripe.model.PaymentIntent.create(createParams);
String clientSecret = intent.getClientSecret();
// Pass the client secret to the client
paymentIntentClientSecret = clientSecret;

Log.e(“ClientSecretKey”,”=>”+paymentIntentClientSecret);
}catch (Exception e){
e.printStackTrace();
}
}

private void startCheckout() {
// Request a PaymentIntent from your server and store its client secret in paymentIntentClientSecret
// Click Open on GitHub to see a full implementation

// Hook up the pay button to the card widget and stripe instance
Button payButton = findViewById(R.id.payButton);
payButton.setOnClickListener((View view) -> {
PaymentMethodCreateParams params = cardInputWidget.getPaymentMethodCreateParams();
if (params != null) {
ConfirmPaymentIntentParams confirmParams = ConfirmPaymentIntentParams
.createWithPaymentMethodCreateParams(params, paymentIntentClientSecret);
final Context context = getApplicationContext();
stripe = new Stripe(
context,
PaymentConfiguration.getInstance(context).getPublishableKey()
);
stripe.confirmPayment(this, confirmParams);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

// Handle the result of stripe.confirmPayment
stripe.onPaymentResult(requestCode, data, new PaymentResultCallback(this));
}

private static final class PaymentResultCallback
implements ApiResultCallback<PaymentIntentResult> {
@NonNull
private final WeakReference<MainActivity> activityRef;

PaymentResultCallback(@NonNull MainActivity activity) {
activityRef = new WeakReference<>(activity);
}

@Override
public void onSuccess(@NonNull PaymentIntentResult result) {
final MainActivity activity = activityRef.get();
if (activity == null) {
return;
}

PaymentIntent paymentIntent = result.getIntent();
PaymentIntent.Status status = paymentIntent.getStatus();
if (status == PaymentIntent.Status.Succeeded) {
// Payment completed successfully
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Log.e(“Success”,gson.toJson(paymentIntent));
} else if (status == PaymentIntent.Status.RequiresPaymentMethod) {
// Payment failed
Log.e(“Error 1”,Objects.requireNonNull(paymentIntent.getLastPaymentError()).getMessage());

}
}

@Override
public void onError(@NonNull Exception e) {
final MainActivity activity = activityRef.get();
if (activity == null) {
return;
}

Log.e(“Error 2”,e.toString());

}
}

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...