zoukankan      html  css  js  c++  java
  • 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  //拿到后端返回数据后调用下面支付函数(若接口返回obj,需先转成string类型)
    								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)  //(这里注意后端返回的数据类型是string还是object,需转成object类型)
    									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'})
    					}
    				 }
    			});
    		},
    

    支付宝获取orderInfo的接口

    微信获取orderInfo的接口

  • 相关阅读:
    React Native 项目运行在 Web 浏览器上面
    iOS:CYLTabBarController【低耦合集成TabBarController】
    iOS原生项目中集成React Native
    iOS根据Url 获取图片尺寸
    iOS关于html缓存
    swift约束框架SnapKit使用
    swift
    Swift关于Any,AnyObject,AnyClass的区别与联系
    swift三方库
    React Native
  • 原文地址:https://www.cnblogs.com/huihuihero/p/13535158.html
Copyright © 2011-2022 走看看