stripePay

来自于:官方立即使用

openCardView getToken addPaymentCardTextField submitPayment deviceSupportsApplePay applePay setApplePayStatus

概述

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 文件

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

  1. ```js
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  4. <plist version="1.0">
  5. <dict>
  6. <key>com.apple.developer.healthkit</key>
  7. <true/>
  8. <key>com.apple.developer.in-app-payments</key>
  9. <array>
  10. <string>merchant.com.apicloud.stripePay</string>
  11. </array>
  12. </dict>
  13. </plist>
  14. ```

其中 merchant.com.apicloud.stripePay 为第一步申请的 Apple Merchant ID。”\com.apple.developer.healthkit\ \“ 是开通健康功能,与本模块无关,若开发者同时使用了多个需要配置 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
  • 配置示例:
  1. <feature name="stripePay">
  2. <param name="publishableKey" value="wxd0d84bbf23b4a0e4"/>
  3. <param name="accentColor" value="#1E90FF"/>
  4. <param name="appleMerchantIdentifier" value="*********"/>
  5. </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 支付流程:

  4. 移动端收集支付信息

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

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

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

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

  9. 设置支付结果状态(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 identifierSTPToken(py)tokenId) to your server from the client.

Create a charge with the token

openCardView

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

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

openCardView(callback(ret))

callback(ret)

ret:

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

示例代码

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

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

getToken

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

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

params

number:

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

expMonth:

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

expYear:

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

cvc:

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

callback(ret, err)

ret:

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

err:

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

示例代码

  1. var stripePay = api.require('stripePay');
  2. stripePay.getToken({
  3. number: '',
  4. expMonth: '',
  5. expYear: '',
  6. cvc: ''
  7. }, function(ret, err) {
  8. if (ret.status) {
  9. //获取 token 成功
  10. } else {
  11. alert(err.code);
  12. }
  13. });

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

addPaymentCardTextField

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

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

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

params

rect:

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

fixedOn:

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

fixed:

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

callback(ret)

ret:

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

示例代码

  1. var stripePay = api.require('stripePay');
  2. stripePay.addPaymentCardTextField({
  3. retct: {
  4. x: ,
  5. y: ,
  6. w: ,
  7. h:
  8. },
  9. fixedOn: '',
  10. fixed:
  11. }, function(ret, err) {
  12. if (ret.isValid) {
  13. alert('配置商户支付参数成功');
  14. }
  15. });

可用性

仅支持 iOS 平台

可提供的1.0.0及更高版本

submitPayment

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

submitPayment(callback(ret, err))

callback(ret, err)

ret:

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

err:

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

示例代码

  1. var stripePay = api.require('stripePay');
  2. stripePay.submitPayment(function(ret, err) {
  3. if (ret.status) {
  4. alert(JSON.stingify(ret));
  5. } else {
  6. alert(err.code);
  7. }
  8. });

可用性

仅支持 iOS 平台

可提供的 1.0.0 及更高版本

deviceSupportsApplePay

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

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

deviceSupportsApplePay(callback(ret))

callback(ret)

ret:

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

示例代码

  1. var stripePay = api.require('stripePay');
  2. stripePay.deviceSupportsApplePay(function(ret) {
  3. if (ret.status) {
  4. alert('支持');
  5. } else {
  6. alert('不支持');
  7. }
  8. });

可用性

仅支持 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:

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

callback(ret, err)

ret:

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

err:

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

示例代码

  1. var stripePay = api.require('stripePay');
  2. stripePay.applePay({
  3. country: '',
  4. currency: '',
  5. paymentItems: [{
  6. label: 'Hat',
  7. amount: '50.00'
  8. },{
  9. label: 'T-shirt',
  10. amount: '50.00'
  11. }]
  12. }, function(ret, err) {
  13. api.alert({msg:JSON.stringify(ret)});
  14. });

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本

setApplePayStatus

设置苹果支付状态

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

params

status:

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

示例代码

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

可用性

iOS 系统,Android 系统

可提供的 1.0.0 及更高版本