# uni.requestPayment(options)

请求支付

# 参数

名称 类型 必填 默认值 描述
options RequestPaymentOptions -
名称 类型 必备 默认值 描述
provider string - 支付服务提供商,通过 uni.getProvider 获取,目前支持支付宝支付(alipay),微信支付(wxpay)
orderInfo string - 订单数据
success (result: RequestPaymentSuccess) => void | null - 接口调用成功的回调函数
fail (result: IRequestPaymentFail) => void | null - 接口调用失败的回调函数
complete (result: any) => void | null - 接口调用结束的回调函数(调用成功、失败都会执行)
# RequestPaymentSuccess 的属性值
名称 类型 必备 默认值 描述
data any | null -
# IRequestPaymentFail 的属性值
名称 类型 必备 默认值 描述
errCode 700710 | 700711 | 700712 | 700713 | 700714 | 700715 | 700716 - 错误码
- 700710 正在处理中,支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态
- 700711 订单支付失败。
- 700712 重复请求。
- 700713 用户中途取消。
- 700714 网络连接出错。
- 700715 支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态。
- 700716 其它支付错误。
errSubject string - 统一错误主题(模块)名称
data any | null - 错误信息中包含的数据
cause Error | null - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - -

# orderInfo参数说明

  • 支付宝
    App 支付请求参数字符串,主要包含商家的订单信息,key=value 形式,以 & 连接。示例
app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%
22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%
91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22
%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=R
SA2&timestamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552
EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJd
hIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D<br>

[更多详情参考支付宝官方文档]

  • 微信
    App 支付请求参数字符串。示例
{
 "appid":"wxd569c7238830733b",
 "noncestr":"6N47VnR42bqIm4xq",
 "package":"Sign=WXPay",
 "partnerid":"1230636401",
 "prepayid":"wx26174750316675ac54b89c224eb3250000",
 "timestamp":1711446470,
 "sign":"EE987459B9CFF6462462147130110D31"
}

[更多详情参考微信官方文档]

# requestPayment 兼容性

Android iOS web
4.02 x x

# 参见

相关 Bug

# cause支付sdk错误码汇总

# 支付宝支付

支付宝错误码 错误信息
-9000 订单支付成功
-8000 正在处理中,支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态
-4000 订单支付失败
-5000 重复请求
-6001 用户中途取消
-6002 网络连接出错
-6004 支付结果未知(有可能已经支付成功),请查询商家订单列表中订单的支付状态
其它 其它支付错误

注意:

  • App平台开发支付宝支付,无需自定义基座,真机运行可直接开发
  • 可通过编写uts插件判断微信是否安装,如android中可通过引入微信自带的api进行判断
   import WXAPIFactory from 'com.tencent.mm.opensdk.openapi.WXAPIFactory';
   let api = WXAPIFactory.createWXAPI(UTSAndroid.getTopPageActivity(), '');
   api!.isWXAppInstalled()
  • 需要在根目录的manifest.json文件中,对所使用的支付进行配置如
{
   "app": {
      "distribute": {
         "modules": {
            "uni-payment": {
               "alipay": {},//支付宝支付
               "wxpay": {}//微信支付
            }
         }
      }
   }
}

[更多详情参考模块配置]

  • app-android平台微信支付需要4.11及以上版本

# 示例

hello uni-app x

<template>
 <page-head title="发起支付"></page-head>

 <template v-if="providerList.length > 0">
   <button style="margin-top: 20px;" type="primary" v-for="(item,index) in providerList" :key="index"
     @click="requestPayment(item)">{{item.name}}支付</button>
 </template>
</template>

<script>
 export type PayItem = { id : string, name : string }
 export default {
   data() {
     return {
       btnText: "支付宝支付",
       btnType: "primary",
       orderInfo: "",
       errorCode: 0,
       errorMsg: "",
       complete: false,
       providerList: [] as PayItem[]
     }
   },
   onLoad: function () {
     uni.getProvider({
       service: "payment",
       success: (e) => {
         console.log("payment success:" + JSON.stringify(e));
         let array = e.provider as string[]
         array.forEach((value : string) => {
           switch (value) {
             case 'alipay':
               this.providerList.push({
                 name: '支付宝',
                 id: "alipay",
               } as PayItem);
               break;
             case 'wxpay':
               this.providerList.push({
                 name: '微信',
                 id: "wxpay",
               } as PayItem);
               break;
             default:
               break;
           }
         })
       },
       fail: (e) => {
         console.log("获取支付通道失败:", e);
       }
     });
   },
   methods: {
     requestPayment(e : PayItem) {
       const provider = e.id
       if (provider == "alipay") {
         this.payAli()
       } else if (provider == "wxpay") {
         this.payWX()
       }
     },
     payAli() {
       uni.showLoading({
         title: "请求中..."
       })
       uni.request({
         url: 'https://demo.dcloud.net.cn/payment/alipay/?total=0.01',
         method: 'GET',
         timeout: 6000,
         success: (res) => {
           this.orderInfo = JSON.stringify(res.data);
           console.log("====" + this.orderInfo)
           uni.hideLoading()
           uni.requestPayment({
             provider: "alipay",
             orderInfo: res.data as string,
             fail: (res) => {
               console.log(JSON.stringify(res))
               this.errorCode = res.errCode
               uni.showToast({
                 icon: 'error',
                 title: 'errorCode:' + this.errorCode
               });
             },
             success: (res) => {
               console.log(JSON.stringify(res))
               uni.showToast({
                 icon: 'success',
                 title: '支付成功'
               });
             }
           })
         },
         fail: (e) => {
           console.log(e)
           uni.hideLoading()
         },
       });
     },
     payWX() {
       uni.showLoading({
         title: "请求中..."
       })
       let url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__uniappx/?total=1'
       const res = uni.getAppBaseInfo();
       if (res.packageName == 'io.dcloud.hellouniappx') {//hello uniappx
         url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=1'
       }
       uni.request({
         url: url,
         method: 'GET',
         timeout: 6000,
         header: {
           "Content-Type": "application/json"
         } as UTSJSONObject,
         success: (res) => {
           console.log(res.data)
           uni.hideLoading()
           uni.requestPayment({
             provider: "wxpay",
             orderInfo: JSON.stringify(res.data),
             fail: (res) => {
               console.log(JSON.stringify(res))
               this.errorCode = res.errCode
               uni.showToast({
                 duration: 5000,
                 icon: 'error',
                 title: 'errorCode:' + this.errorCode,
               });
             },
             success: (res) => {
               console.log(JSON.stringify(res))
               uni.showToast({
                 duration: 5000,
                 icon: 'success',
                 title: '支付成功'
               });
             }
           })
         },
         fail: (res) => {
           uni.hideLoading()
           console.log(res)
         },
       });
     },

     //自动化测试使用
     jest_pay() {
       uni.requestPayment({
         provider: "alipay",
         orderInfo: this.orderInfo,
         fail: (res : RequestPaymentFail) => {
           this.errorCode = res.errCode
           this.complete = true
         },
         success: (res : RequestPaymentSuccess) => {
           console.log(JSON.stringify(res))
           this.complete = true
         }
       } as RequestPaymentOptions)
     }
   }
 }
</script>

<style>

</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 描述
errMsg string - 错误信息