当前位置: 首页 > 工具软件 > go.stripe > 使用案例 >

国外stripe支付,超简单几行代码搞定

茅秦斩
2023-12-01

公司在做海外的项目 需要stripe支付 很简单 几行代码 

先加入依赖:

compile 'com.stripe:stripe-android:6.1.2'

总共两种方式 一种是自己定义控件支付 一种是使用stripe提供的控件支付 两种方式代码差别也很小

1.自己定义的控件支付

//调用stripe支付方法
    private void goStripe() {
        //第一个参数: 银行卡卡号   二:月   三:年  四:CVC码
       Card card = new Card(tvCard.getText().toString()+"", Integer.parseInt(tvMm.getText().toString()),
                Integer.parseInt(tvYy.getText().toString()),tvCvv.getText().toString()+"");
        //该Card实例包含验证卡号是否通过Luhn检查,到期日期是未来,并且CVC看起来有效。一次验证这三件事情
        if (!card.validateCard()) {
            // Show errors
            //验证错误
        }else{
            //pk_test_xxxxxxxxxxx  //测试环境
            //pk_live_xxxxxxxxxxx  //生产环境
            //创建stripe对象  环境要与服务端的stripe环境一致 是测试还是生产(正式)
            Stripe stripe = new Stripe(mContext, "pk_test_xxxxxxxxxx");
            stripe.createToken(
                    card,
                    new TokenCallback() {
                        public void onSuccess(Token token) {
                            // Send token to your server
                            //成功创建令牌  在此发起支付接口请求
                        }

                        public void onError(Exception error) {
                            // Show localized error message
                        }
                    }
            );
        }

    }

2.使用stripe提供的控件支付

//stripe提供的控件
<com.stripe.android.view.CardInputWidget
  android:id="@+id/card_input_widget"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/CardWidgetStyle"
/>
      //stripe的控件提供了直接获取输入的信息
      //获取输入框银行卡信息
        Card cardToSave = card_input_widget.getCard();
       //验证是否错误
        if (cardToSave == null) {
            //验证错误
        } else {
            //创建stripe对象
            Stripe stripe = new Stripe(mContext, "pk_text_xxxxxxxxx");
            stripe.createToken(
                    cardToSave,
                    new TokenCallback() {
                        public void onSuccess(Token token) {
                            // Send token to your server
                            //成功创建令牌 
                            //发起支付的请求接口
                        }

                        public void onError(Exception error) {
                            // Show localized error message

                        }
                    }
            );
        }
stripe官网在此 点击打开链接


 类似资料: