【Android】Stripe支付
作者:互联网
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
2.配置publishable key 来和Stripe通讯
前言
最近公司做海外项目用到了Stripe支付,这里就安卓端集成Stripe支付流程记录一下。总体来说接入集成Stripe还是比较简单的。
一、Stripe是什么?
Stripe是由20多岁的两兄弟Patrick Collison和John Collison创办的Stripe为公司提供网上支付的解决方案。Stripe向服务的公司收取每笔交易的2.9%加上30美分的手续费。
Stripe官网: https://stripe.com/
二、使用步骤
1.引入库
在app模块build.gradle里加入。最新版本:https://github.com/stripe/stripe-android/releases
implementation 'com.stripe:stripe-android:16.3.0'
2.配置publishable key 来和Stripe通讯
在你的Application里配置
import com.stripe.android.PaymentConfiguration;
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
PaymentConfiguration.init(
getApplicationContext(),
"pk_test_TYooMQauvdEDq54NiTphI7jx"
);
}
}
publishable key需要在Stripe官网里面申请。
3.创建订单,从服务端获取client secret
客户端创建订单,从服务器返回临时的client secret。这个是用来和银行卡信息绑定在一起进行支付。
服务端获取client secret的方法
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
PaymentIntentCreateParams params =
PaymentIntentCreateParams.builder()
.setAmount(1099L)
.setCurrency("usd")
.build();
PaymentIntent intent = PaymentIntent.create(params);
String clientSecret = intent.getClientSecret();
// Pass the client secret to the client
4.获取银行卡信息
创建界面获取用户输入的银行卡信息,包含卡号,日期,CVV码。
可以使用Stripe定义好的控件CardInputWidget来轻松获取。CardInputWidget控件会对输入的内容进行校验。如果是自己的布局实现的,需要调用API来校验。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:showIn="@layout/activity_checkout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入银行卡信息"
android:textSize="16sp" />
<com.stripe.android.view.CardInputWidget
android:id="@+id/cardInputWidget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10" />
<Button
android:id="@+id/payButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="@android:color/holo_green_light"
android:text="@string/pay" />
</LinearLayout>
5. 开始支付
CardInputWidget控件获取到参数,然后和Client Secret绑定在一起。创建Stripe对象来确认支付。
Button payButton = findViewById(R.id.payButton);
payButton.setOnClickListener((View view) -> {
CardInputWidget cardInputWidget = findViewById(R.id.cardInputWidget);
PaymentMethodCreateParams params = cardInputWidget.getPaymentMethodCreateParams();
if (params != null) {
if (paymentIntentClientSecret == null) {
return;
}
ConfirmPaymentIntentParams confirmParams = ConfirmPaymentIntentParams
.createWithPaymentMethodCreateParams(params, paymentIntentClientSecret);
String stripePublishableKey = PaymentConfiguration.getInstance(this).getPublishableKey();
LogUtils.d("stripePublishableKey==" + stripePublishableKey);
// Configure the SDK with your Stripe publishable key so that it can make requests to the Stripe API
stripe = new Stripe(
getApplicationContext(),
Objects.requireNonNull(stripePublishableKey)
);
stripe.confirmPayment(this, confirmParams);
}
});
6.支付结果回调
在onActivityResult里获取支付的结果回调。
@Override
protected void onActivityResult(int requestCode, int resultCode, 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<StripPaymentActivity> activityRef;
PaymentResultCallback(@NonNull StripPaymentActivity activity) {
activityRef = new WeakReference<>(activity);
}
@Override
public void onSuccess(@NonNull PaymentIntentResult result) {
final StripPaymentActivity 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();
String toJson = gson.toJson(paymentIntent);
activity.displayAlert(
activity.getString(R.string.payment_successful),
"",
true
);*/
activity.showDialogPaySuccess();
} else if (status == PaymentIntent.Status.RequiresPaymentMethod) {
// Payment failed – allow retrying using a different payment method
activity.displayAlert(
activity.getString(R.string.payment_failed),
Objects.requireNonNull(paymentIntent.getLastPaymentError()).getMessage(),
false
);
}
}
@Override
public void one rror(@NonNull Exception e) {
final StripPaymentActivity activity = activityRef.get();
if (activity == null) {
return;
}
// Payment request failed – allow retrying using the same payment method
activity.displayAlert(activity.getString(R.string.payment_failed), e.getMessage(), false);
}
}
总结
Stripe的支付流程就是这样。具体的使用可以查看官方文档。文档里面还有保存银行卡的功能。这个实现起来会比较富足,可以根据具体业务来添加。
标签:PaymentIntent,支付,secret,stripe,key,activity,Android,Stripe 来源: https://blog.csdn.net/u010689161/article/details/114406439