app-payment-stripe.md 6.1 KB
Newer Older
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
### 开通  
- [登录Stripe](https://dashboard.stripe.com/login)注册账号
* 注册账号后可获取开发测试的API密钥(公钥、私钥),注意:需[激活账户](https://dashboard.stripe.com/account/onboarding)获取正式的API密钥
* 设置[支付方式](https://dashboard.stripe.com/settings/payment_methods)

更多信息详见[申请开通Stripe操作指南](https://uniapp.dcloud.io/app-payment-stripe-open)

**注意**
- iOS系统仅支持iOS13.0及以上版本

### 配置  
在manifest.json文件“App模块配置”项的“Payment(支付)”下,勾选“paypal支付”项并配置相关参数
![](https://native-res.dcloud.net.cn/images/uniapp/payment/stripe_setup_manifest_info.png)

**参数说明**  
- returnURL  
Android平台使用,格式为"your-app://stripe"(示例 io.dcloud.test://stripe),'your-app'为应用的bundle id或其它自定义scheme,参考:[配置一个自定义页面内跳转协议 (URL Scheme)](https://ask.dcloud.net.cn/article/64)


### 服务器生成订单
在 App 端调用支付前,需在业务服务器生成[PaymentIntent](https://stripe.com/docs/api/payment_intents),详情可参考Stripe官方文档:[Add an endpoint](https://stripe.com/docs/payments/accept-a-payment?platform=android&ui=payment-sheet#add-server-endpoint)

激活账户前可通过POST请求Stripe官方沙盒服务器[https://stripe.com/docs/payments/accept-a-payment](https://stripe.com/docs/payments/accept-a-payment),生成测试PaymentIntent,示例如下:

```  js
uni.request({
    url: 'https://stripe-mobile-payment-sheet.glitch.me/checkout',//仅为示例
    method: "POST", 
    success:(res) => {
        console.log("订单信息" + res.data);
        var publishKey = res.data.publishableKey;
        var paymentIntent = res.data.paymentIntent; 
        var customer = res.data.customer;
        var ephemeralKey = res.data.ephemeralKey;
    }
});
```


### 应用内发起支付

- uni-app项目  
调用 [uni.requestPayment(OBJECT)](https://uniapp.dcloud.io/api/plugins/payment?id=requestpayment) 发起支付,OBJECT参数中provider属性值固定为`stripe`、orderInfo属性值为订单对象
- 5+ App项目  
调用 [plus.payment.request(channel, orderInfo, successCB, errorCB)](https://www.html5plus.org/doc/zh_cn/payment.html#plus.payment.request) 发起支付, channel参数为stripe支付对象,orderInfo参数为订单对象


#### 订单对象参数说明  
Object对象类型

| 属性 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| customer | String | 否 | Stripe的[Customer](https://stripe.com/docs/api/customers)对象,如为同一用户执行定期重复性收款,并跟踪多笔收款 |
| ephemeralKey | String | 否 | Stripe的Customer Ephemeral Key,用于临时访问Customer |
| isAllowDelay | String | 否 | 是否支持延迟支付,默认不支持(将 isAllowDelay 设置为 true 后可使用一些较慢的支付方式,例如 SEPA 借记和 Sofort 对于这些支付方式,只有当 PaymentSheet 完成后才能知道最终的付款状态是成功还是失败。如果您允许这样,则通知客户您已确认他们的订单,但收到付款后才能履行(例如,发货)订单。) |
| merchantName | String | 是 | 商户名称 |
| paymentIntent | String | 是 | Stripe的[PaymentIntent](https://stripe.com/docs/api/payment_intents)对象,对应服务器生成的支付订单 |
| publishKey | String | 是 | 公钥,在Stripe注册账号后可获取 |

> 注意:customer与ephemeralKey必须成对出现,只传其一无效


#### 示例代码  
- uni-app项目  
``` js
//订单对象
var orderInfo = {
    "customer": "Stripe的Customer",                    //Customer
    "ephemeralKey": "Stripe的Customer Ephemeral Key",  //临时访问Customer的Key
    "isAllowDelay": true,                              //是否支持延迟支付  默认false
    "merchantName": "DCloud",                          //商户名
    "paymentIntent": "Stripe的PaymentIntent",          //订单信息
    "publishKey": "Public Key",                        //公钥
};
// 从Stripe测试服务器获取订单数据
uni.request({
    url: 'https://stripe-mobile-payment-sheet.glitch.me/checkout',
    method: "POST",
    success: (res) => {
        orderInfo = {
            "customer": res.data.customer,
            "ephemeralKey": res.data.ephemeralKey,
            "isAllowDelay": true,
            "merchantName": "DCloud",
            "paymentIntent": res.data.paymentIntent,
            "publishKey": res.data.publishableKey,
        };
    }
});
//...
//发起支付
uni.getProvider({
    service: 'payment',
    success: function(res) {
        if (~res.provider.indexOf('stripe')) {
            uni.requestPayment({
                "provider": "stripe",
                "orderInfo": orderInfo,
                success(res) {
                    console.log("requestPayment Success: "+JSON.stringify(res));
                },
                fail(e) {
                    console.log("requestPayment failed: "+JSON.stringify(e));
                }
            });
        }
    }
});
```  

- 5+ App项目  
``` js
//订单对象,从服务器获取
var orderInfo = {
    "customer": "Stripe的Customer",                    //Customer
    "ephemeralKey": "Stripe的Customer Ephemeral Key",  //临时访问Customer的Key
    "isAllowDelay": true,                              //是否支持延迟支付  默认false
    "merchantName": "DCloud",                          //商户名
    "paymentIntent": "Stripe的PaymentIntent",          //订单信息
    "publishKey": "Public Key",                        //公钥
};
//获取支付渠道
var stripeSev = null;
plus.payment.getChannels(function(channels){
    for (var i in channels) {
        var channel = channels[i];
        if (channel.id === 'stripe') {
            stripeSev = channel;
        }
    }
    //发起支付
    plus.payment.request(stripeSev, orderInfo, function(result) {
         var rawdata = JSON.parse(result.rawdata);
         console.log("支付成功");
    }, function(e) {
         console.log("支付失败:" + JSON.stringify(e));
    });
  }, function(e){
      console.log("获取支付渠道失败:" + JSON.stringify(e));
});
```