Home >Web Front-end >uni-app >How uniapp implements payment function
Uniapp’s method of implementing the payment function: first obtain the available payment environment; then determine whether the user has an Alipay payment environment; then obtain relevant data from the back-end interface and configure it into orderInfo; finally obtain the back-end return data Then call the relevant payment function.
The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, thinkpad t480 computer.
Recommendation (free): uni-app development tutorial
How to implement payment function in uniapp:
//支付宝支付 zfbPay(){ uni.getProvider({ //获取可用的支付环境 service: 'payment', success: res=>{ if (~res.provider.indexOf('alipay')) { //先判断用户是否有支付宝支付环境 uni.showLoading({title: '正在调起支付宝支付'}) let params={ //给后端传什么参数看你的后端需要什么 money:this.moneyCount, ispc:3 } uni.request({ //再从后端接口获取相关数据配置到orderInfo里,这个接口由后端配置好了,返回结果见下图1-支付宝 url: `${this.$baseUrl}/api-order/amstc/userRechargeAccountByAliPay`, method: 'POST', header: { "Token":this.userToken, "Content-Type":"application/x-www-form-urlencoded" }, data: params, success: res => { if(res.data.code==200){ let payInfo=res.data.data //拿到后端返回数据后调用下面支付函数 uni.requestPayment({ provider: 'alipay', orderInfo: payInfo, //支付宝订单数据(string类型) success: res=>{ uni.hideLoading(); uni.showToast({title: '支付成功',icon:'none'}) }, fail:err=>{ uni.hideLoading(); uni.showToast({title: '支付失败,请稍后再试',icon:'none'}) } }); } }, fail: () => { uni.hideLoading(); uni.showToast({title: '服务器开小差了呢,请您稍后再试',icon:'none'}) } }); }else{ uni.showToast({title: '获取支付宝通道失败,请检查您的支付宝是否正常启用',icon:'none'}) } } }); }, //微信支付 wxPay(){ uni.getProvider({ service: 'payment', success: res=>{ if (~res.provider.indexOf('wxpay')) { //先判断用户是否有微信支付环境(是否安装了微信app) uni.showLoading({title: '正在调起微信支付'}) let params={ money:this.moneyCount, bs:4 } uni.request({ //再从后端接口获取相关数据配置到orderInfo里,这个接口由后端配置好了,返回结果见下图2-微信 url: `${this.$baseUrl}/api-order/amstc/userRechargeAccountByWx`, method: 'POST', header: { "Token":this.userToken, "Content-Type":"application/x-www-form-urlencoded" }, data: params, success: res => { if(res.data.code==200){ let resobj=JSON.parse(res.data.data) let payInfo={ appid: resobj.appid, noncestr: resobj.nonce_str, package:"Sign=WXPay", partnerid: resobj.mch_id, prepayid: resobj.prepay_id, timestamp: resobj.time_stamp, sign: resobj.sign, } uni.requestPayment({ provider: 'wxpay', orderInfo: payInfo, //微信订单数据(Object类型) success: res=>{ uni.hideLoading(); uni.showToast({title: '支付成功',icon:'none'}) }, fail:err=>{ uni.hideLoading(); uni.showToast({title: '支付失败,请稍后再试',icon:'none'}) } }); } }, fail: () => { uni.hideLoading(); uni.showToast({title: '服务器开小差了呢,请您稍后再试',icon:'none'}) } }); }else{ uni.showToast({title: '获取微信通道失败,请检查您的微信是否正常启用',icon:'none'}) } } }); },
Alipay’s interface for obtaining orderInfo
WeChat’s interface for obtaining orderInfo
Related free learning recommendations: php programming (video)
The above is the detailed content of How uniapp implements payment function. For more information, please follow other related articles on the PHP Chinese website!