stripePay

概述

** Stripe 支付简介**

Stripe是由20多岁的两兄弟Patrick Collison和John Collison创办的Stripe为公司提供网上支付的解决方案。Stripe向服务的公司收取每笔交易的2.9%加上30美分的手续费。

在线支付服务商Stripe据传正在进行新一轮融资,这一轮融资将使公司估值达到50亿美元,不过目前融资的具体数额不明。Stripe在 2016年12月份刚获得7000万融资,当时的估值为35亿美元。在之前的几轮融资中Stripe已经获得了1.9亿美元的融资。

功能服务

The Stripe iOS SDK make it easy to collect your users’ credit card details inside your iOS app.

Our SDK is compatible with iOS apps supporting iOS 8.0 and above. It requires Xcode 8.0+ to build the source.

首先去 stripe 官网 注册一个账号

然后登录该账号,并在账号内申请创建 key,https://dashboard.stripe.com/account/apikeys.

最后将获得的 publishableKey 配置到config 文件内。

Apple Pay(苹果支付)

什么是苹果支付?

苹果支付 不同于苹果内购,苹果内购是用户购买虚拟物品的支付手段。苹果支付类似于国内的支付宝支付、微信支付。可以理解为一个第三方支付平台。用户使用时,需要首先把银行卡添加到自己苹果手机的wallet 这个app 内,然后在集成了该功能的商家app上购买物(非虚拟物品)时可选择使用苹果支付功能。

本插件的苹果支付相关接口既封装了该功能,开发者通过调用本插件可集成苹果支付到自己的app 内。

使用苹果支付的条件

设备要求: iPhone 6 or 6+, iPad Air 2, or iPad mini 3 系统要求: iOS 8.1 or later

集成苹果支付的步骤

一. Registering for an Apple Merchant ID

First, you'll need to obtain an Apple Merchant ID. Start by heading to the Registering a Merchant ID page on the Apple Developer website. Fill out the form with a description and identifier. Your description is for your own records and can be modified in the future (we recommend just using the name of your app). The idenfitier must be unique (across all apps, not just yours) and can't be changed later (although you can always make another one). We recommend using merchant.com.{your_app_name}. Save this value for later use when developing your app.

二. Creating a new Apple Pay certificate

Creating a new Apple Pay certificate You need to include a certificate in your app to encrypt outgoing payment data. This involves 3 steps:

  • Obtain a CSR (certificate signing request) file from Stripe
  • Use this CSR to generate a certificate through Apple
  • Upload the certificate back to Stripe

First, head to the Apple Pay section of the Account Settings page in the Dashboard. Choose "Create New Certificate" and a .certSigningRequest file will automatically download.

Next, back on the Apple Developer site, visit the Add iOS Certificate page. Choose "Apple Pay Certificate" from the options and click "Continue". On the next page, choose the Merchant ID you created earlier from the dropdown and continue.

The next page explains that you can obtain a CSR from your Payment Provider (which at this point you've done already) or create one manually. Important note: you must use the CSR provided by Stripe - creating your own won't work. So ignore the directions at the bottom of this page and continue on.

You'll be prompted to upload a .certSigningRequest file. Choose the file you downloaded from the Dashboard and continue. You'll see a success page, with an option to download your certificate. Download it. Finally, return to the Dashboard and upload this .cer file to Stripe.

三.创建 entitlements 文件

YonBuilder移动开发 平台上的开发者,可通过 创建 entitlements 文件 告诉编译服务器开通苹果支付功能。配置方法参考论坛帖子。配置示例如下:

```js
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>com.apple.developer.healthkit</key>
        <true/>
        <key>com.apple.developer.in-app-payments</key>
        <array>
            <string>merchant.com.apicloud.stripePay</string>
        </array>
    </dict>
    </plist>
```

其中 merchant.com.apicloud.stripePay 为第一步申请的 Apple Merchant ID。"<key>com.apple.developer.healthkit</key> <true/>" 是开通健康功能,与本插件无关,若开发者同时使用了多个需要配置 entitlements 文件的插件,可如上述示例继续添加。

集成苹果支付的常见问题

If you're seeing errors from the Stripe API when attempting to create tokens, there's likely something wrong with your Apple Pay Certificate. You'll need to generate a new certificate and upload it to Stripe, as described above. Make sure you use a CSR obtained from your Dashboard and not one you generated yourself. Xcode often incorrectly caches old certificates, so in addition to generating a new certificate, we recommend creating a new Apple Merchant ID as well.

If you receive the error "You haven't added your Apple merchant account to Stripe", it's likely your app is sending data encrypted with a previous (non-Stripe) CSR/Certificate. Make sure any certs generated by non-Stripe CSRs are revoked under your Apple Merchant ID. If this does not resolve the issue, delete the merchant ID in your Apple account and recreate it. Then, create a new certificate based on the same (Stripe-provided) CSR that was previously used. You do not need to re-upload this new certificate to Stripe. Once done, toggle the Apple Pay Credentials off and on in your app to make sure they refresh properly.

使用此插件之前需先配置 config.xml 文件,方法如下

  • 名称:stripePay
  • 参数:urlScheme、apiKey、apiSecret
  • 配置示例:
  <feature name="stripePay">
    <param name="publishableKey" value="wxd0d84bbf23b4a0e4"/>
    <param name="accentColor" value="#1E90FF"/>
    <param name="appleMerchantIdentifier" value="*********"/>
  </feature>
  • 字段描述:

    publishableKey:在 stripe 账号内申请创建的 key。

    accentColor:(可选配置)弹出信用卡信息输入界面(通过 openCardView 接口打开的)的风格色值,支持 rgb、rgba、#;默认:'#1E90FF'

    appleMerchantIdentifier:(必须配置)上述步骤中在苹果开发者中心创建的商户 id 号,其格式一般为 merchant.com.app名。

注意:使用苹果支付功能时,最低适配版本是iOS 8.0

插件支付流程概况:

Stripe 支付流程:

  1. 移动端收集支付信息

  2. 移动端获取支付 token

  3. 移动端发送 token 到商户服务器,商户服务器端进行扣款

  4. 移动端获取商户服务器扣款结果(成功/失败),并展示给用户

本插件封装实现了过程 1 和 2(openCardView、getToken、addPaymentCardTextField+submitPayment)。过程 3、4 自行实现。

三种获取 token 的接口:

  1. openCardView:插件打开一个窗口(相当于 window),用户自己输入信用卡信息

  2. getToken:通过信用卡信息直接获取 token

  3. addPaymentCardTextField 和 submitPayment

Stripe-ApplePay 支付流程:

  1. 移动端收集支付信息

  2. 判断当前设备是否支持苹果支付(deviceSupportsApplePay接口)

  3. 移动端获取支付 token(applePay接口)

  4. 移动端发送 token 到商户服务器,商户服务器端进行扣款---自行实现

  5. 移动端获取商户服务器扣款结果(成功/失败)---自行实现

  6. 设置支付结果状态(setApplePayStatus接口)

Next steps (Server-side)

Learn how to add code to your backend to charge a user based on the Stripe token。

Server-side

Send the Token’s identifier to your server from the client.

Create a charge with the token

openCardView

打开输入信用卡信息界面,

注意:本界面是 stripe 移动端 SDK 实现的,类似于 open 一个 window

openCardView(callback(ret))

callback(ret)

ret:

  • 类型:JSON 对象
  • 内部字段:
{
     eventType: 'cancel',    //字符串类型;交互事件的类型,取值范围:
                             //cancel:用户取消
                             //completion:完成
     token: {                //JSON 对象;获取的token信息,仅当 eventType 为 completion 时有值
         tokenId: '',        //字符串类型;The value of the token.
         livemode: ,         //字符串类型;Whether or not this token was created in livemode
         created: '',        //字符串类型;When the token was created. 格式为:yyyy-MM-dd HH:mm:ss
         details: {          //JSON对象;信用卡详情
            last4: '',       //字符串类型; The last 4 digits of the card.
            expMonth: '',    //字符串类型;The card's expiration month. 1-indexed (i.e. 1 == January)
            expYear: '',     //字符串类型;The card's expiration year.
            name: '',        //字符串类型;The cardholder's name.
            cardId: '',      //字符串类型;The Stripe ID for the card.
            funding: '',     //字符串类型;The funding source for the card (credit, debit, prepaid, or other)
            country: '',     //字符串类型;国家
            currency: '',    //字符串类型;币种
            brand: ''        //字符串类型;The issuer of the card(visa, amex, masterCard, discover, JCB, dinersClub, unknown)
         }
     }
}

示例代码

var stripePay = api.require('stripePay');
stripePay.openCardView(function(ret) {
   api.alert({msg:JSON.stringify(ret)});
});

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

getToken

根据信用卡信息,直接获取 token

getToken({params}, callback(ret, err))

params

number:

  • 类型:字符串
  • 描述:卡号

expMonth:

  • 类型:数字
  • 描述:过期月份,如10

expYear:

  • 类型:数字
  • 描述:过期年份,如:2018

cvc:

  • 类型:字符串
  • 描述:卡背面的安全码

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
{
    status: true,           //布尔型;true||false
    token: {                //JSON 对象;获取的token信息
         tokenId: '',        //字符串类型;The value of the token.
         livemode: ,         //字符串类型;Whether or not this token was created in livemode
         created: '',        //字符串类型;When the token was created.
         details: {          //JSON对象;信用卡详情
            last4: '',       //字符串类型; The last 4 digits of the card.
            expMonth: '',    //字符串类型;The card's expiration month. 1-indexed (i.e. 1 == January)
            expYear: '',     //字符串类型;The card's expiration year.
            name: '',        //字符串类型;The cardholder's name.
            cardId: '',      //字符串类型;The issuer of the card(visa, amex, masterCard, discover, JCB, DinersClub, unknown)
            funding: '',     //字符串类型;The funding source for the card (credit, debit, prepaid, or other)
            country: '',     //字符串类型;国家
            currency: '',    //字符串类型;币种
            brand: ''        //字符串类型;The issuer of the card.
         }
     }
}

err:

  • 类型:JSON 对象
  • 内部字段:
{
    code: 1 ,       //数字类型;错误码
    msg: ''         //字符串类型;错误信息
}

示例代码

var stripePay = api.require('stripePay');
stripePay.getToken({
    number: '',
    expMonth: '',
    expYear: '',
    cvc: ''
}, function(ret, err) {
    if (ret.status) {
        //获取 token 成功
    } else {
        alert(err.code);
    }
});

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

addPaymentCardTextField

添加一个 stripe 移动端 SDK 生成的输入框

测试卡号:4242 4242 4242 4242 — 12/19 — 123

addPaymentCardTextField({params}, callback(ret, err))

params

rect:

  • 类型:JSON 对象
  • 描述:(可选项)插件的位置及尺寸
  • 内部字段:
{
    x: 0,   //(可选项)数字类型;插件左上角的 x 坐标(相对于所属的 Window 或 Frame);默认值:0
    y: 0,   //(可选项)数字类型;插件左上角的 y 坐标(相对于所属的 Window 或 Frame);默认值:0
    w: 80,  //(可选项)数字类型;插件的宽度;默认值:80
    h: 50   //(可选项)数字类型;插件的高度;默认值:50
}

fixedOn:

  • 类型:字符串类型
  • 描述:(可选项)插件视图添加到指定 frame 的名字(只指 frame,传 window 无效)
  • 默认:插件依附于当前 window

fixed:

  • 类型:布尔
  • 描述:(可选项)是否将插件视图固定到窗口上(不跟随窗口上下滚动)
  • 默认值:true

callback(ret)

ret:

  • 类型:JSON 对象
  • 内部字段:
{
    eventType: 'show',  //字符串类型;交互事件类型
                        //show:打开成功,并显示在指定窗口上
                        //textFieldDidChange:输入框文本改变事件
    isValid: true       //布尔类型;Whether or not the form currently contains a valid card number, expiration date, and CVC.                 
}

示例代码

var stripePay = api.require('stripePay');
stripePay.addPaymentCardTextField({
    rect: {
       x: ,
       y: ,
       w: ,
       h: 
    },
    fixedOn: '',
    fixed: 
}, function(ret, err) {
    if (ret.isValid) {
        alert('配置商户支付参数成功');
    }
});

可用性

仅支持 iOS 平台

可提供的1.0.0及更高版本

submitPayment

提交通过 addPaymentCardTextField 接口打开的输入框获取的信用卡信息,从而获取 token

submitPayment(callback(ret, err))

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
{
    status: true,           //布尔型;true||false
    token: {                //JSON 对象;获取的token信息
         tokenId: '',        //字符串类型;The value of the token.
         livemode: ,         //字符串类型;Whether or not this token was created in livemode
         created: '',        //字符串类型;When the token was created.
         details: {          //JSON对象;信用卡详情
            last4: '',       //字符串类型; The last 4 digits of the card.
            expMonth: '',    //字符串类型;The card's expiration month. 1-indexed (i.e. 1 == January)
            expYear: '',     //字符串类型;The card's expiration year.
            name: '',        //字符串类型;The cardholder's name.
            cardId: '',      //字符串类型;The issuer of the card(visa, amex, masterCard, discover, JCB, DinersClub, unknown)
            funding: '',     //字符串类型;The funding source for the card (credit, debit, prepaid, or other)
            country: '',     //字符串类型;国家
            currency: '',    //字符串类型;币种
            brand: ''        //字符串类型;The issuer of the card.
         }
     }
}

err:

  • 类型:JSON 对象
  • 内部字段:
{
    code: 1     //数字类型;错误码
    msg:''      //字符串类型;错误信息
}

示例代码

var stripePay = api.require('stripePay');
stripePay.submitPayment(function(ret, err) {
    if (ret.status) {
        alert(JSON.stingify(ret));
    } else {
        alert(err.code);
    }
});

可用性

仅支持 iOS 平台

可提供的 1.0.0 及更高版本

deviceSupportsApplePay

判断当前设备是否支持苹果支付

1,Visa:维萨卡 2,MasterCard:万事达卡 3,AmericanExpress:美国运通卡 4,DiscoverCard:发现卡

deviceSupportsApplePay(callback(ret))

callback(ret)

ret:

  • 类型:JSON 对象
  • 内部字段:
{
    status: true          //布尔型;true||false
}

示例代码

var stripePay = api.require('stripePay');
stripePay.deviceSupportsApplePay(function(ret) {
    if (ret.status) {
        alert('支持');
    } else {
        alert('不支持');
    }
});

可用性

仅支持 iOS 平台

可提供的 1.0.0 及更高版本

applePay

调用苹果支付,获取 token

applePay({params}, callback(ret, err))

params

country:

  • 类型:字符串
  • 描述:The two-letter code for the country where the payment will be processed. This should be the country of your Stripe account

currency:

  • 类型:字符串
  • 描述:The three-letter code for the currency used by this payment request. Apple Pay interprets the amounts provided by the summary items attached to this request as amounts in this currency.

paymentItems:

  • 类型:数组
  • 描述:要支付的实物信息组成的数组
  • 内部字段:
[{
    label: '',         //字符串类型;实物标签
    amount: ''         //字符串类型;实物价钱,如50.00
},{},{}]

callback(ret, err)

ret:

  • 类型:JSON 对象
  • 内部字段:
{
    eventType: 'err',       //字符串类型;交互事件类型,取值范围如下:
                            //err: 支付发生错误
                            //didAuthorize: 获取token成功
                            //didFinish: 完成 This may occur when the user cancels the request, or after the PKPaymentAuthorizationStatus parameter of the didAuthorize has been shown to the user.
    token: {                //JSON 对象;获取的token信息
         tokenId: '',        //字符串类型;The value of the token.
         livemode: ,         //字符串类型;Whether or not this token was created in livemode
         created: '',        //字符串类型;When the token was created.
         details: {          //JSON对象;信用卡详情
            last4: '',       //字符串类型; The last 4 digits of the card.
            expMonth: '',    //字符串类型;The card's expiration month. 1-indexed (i.e. 1 == January)
            expYear: '',     //字符串类型;The card's expiration year.
            name: '',        //字符串类型;The cardholder's name.
            cardId: '',      //字符串类型;The issuer of the card(visa, amex, masterCard, discover, JCB, DinersClub, unknown)
            funding: '',     //字符串类型;The funding source for the card (credit, debit, prepaid, or other)
            country: '',     //字符串类型;国家
            currency: '',    //字符串类型;币种
            brand: ''        //字符串类型;The issuer of the card.
         }
     }
}

err:

  • 类型:JSON 对象
  • 内部字段:
{
    code: 1 ,       //数字类型;错误码
    msg: ''         //字符串类型;错误信息
}

示例代码

var stripePay = api.require('stripePay');
stripePay.applePay({
    country: '',
    currency: '',
    paymentItems: [{
      label: 'Hat',
      amount: '50.00'
    },{
      label: 'T-shirt',
      amount: '50.00'
    }]
}, function(ret, err) {
    api.alert({msg:JSON.stringify(ret)});
});

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

setApplePayStatus

设置苹果支付状态

setApplePayStatus({params}, callback(ret, err))

params

status:

  • 类型:字符串
  • 描述:支付状态
  • 内部字段:
    • success:成功
    • failure:失败

示例代码

var stripePay = api.require('stripePay');
stripePay.setApplePayStatus({
    status: 'success'
});

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

是否仍需要帮助? 请保持联络!
最后更新于 2024/10/21