ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Android】Stripe支付

2021-03-05 19:04:25  阅读:1077  来源: 互联网

标签:PaymentIntent 支付 secret stripe key activity Android Stripe


提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

前言

一、Stripe是什么?

二、使用步骤

1.引入库

2.配置publishable key 来和Stripe通讯

3.创建订单,从服务端获取client secret

4.获取银行卡信息

5. 开始支付

6.支付结果回调

 

总结


 

前言

最近公司做海外项目用到了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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有