uniapp APP微信支付 微信内浏览器支付(公众号jsapi支付)

APP支付

                     // 支付按钮
                     async payBtn(e) {   
                        // 提交给后端的东西,以获取到微信支付用到的参数
	                    const res = await buyOrder({
	                        bizId: this.goodsId,
	                        payType: e,
	                        remark: this.beizhu,
	                        type: '1'
	                    })
						const ass = res.data.payFormString
						console.log(ass);
                        // 发起微信支付需要用到的各种参数
						this.appid = ass.appid
						this.partnerid = ass.partnerId
						this.prepayid = ass.prepayId
						this.noncestr = ass.nonceStr
						this.sign = ass.sign
						this.timestamp = ass.timestamp
					    // ---------------------------
					    // 发起微信支付
						uni.requestPayment({
							provider: 'wxpay',
							"orderInfo": {
								"appid": this.appid, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
								"noncestr": this.noncestr, // 随机字符串
								"package": "Sign=WXPay", // 固定值!!!!
								"partnerid": this.partnerid, // 微信支付商户号
								"prepayid": this.prepayid, // 统一下单订单号 
								"timestamp": this.timestamp, // 时间戳(单位:秒)
								"sign": this.sign // 签名,这里用的 MD5/RSA 签名
							},
							 success(res) {
							 	console.log(res);
							 },
							 fail(e) {
							 	console.log(e);
							 }

						})
                    }
					

微信内浏览器支付(公众号支付)

注释:先获取code,把code给后端,让后端拿code 去换 openId,然后就可以发起支付请求了。

准备工作

一、添加开发者

因为要在微信开发者工具  公众号网页调试, 要把你加入到开发者,才能调试

步骤:

1、首先登录微信公众平台 微信公众平台 (qq.com)

 2、

3、 

 二、如出现redirect_uri参数错误

  如出现这个报错,需要到微信公众号 微信公众平台 (qq.com)

 1、添加网页授权域名 这个就是下面用到的 123123.cn (下面注释也有提示) 

2、域名不变 123123.cn   需要后端把这个文件放一下 

 三、支付的时候显示当前页面url未注册

 如出现这个报错,需到  这里设置一下 微信支付 - 中国领先的第三方支付平台 | 微信支付提供安全快捷的支付方式

  还是那个域名  http://123123.cn

 

正式开始 

一、首页

		onShow() {
			this.getCode()
		},
		methods: {

			getCode() { // 非静默授权,第一次有弹框  静默授权的话,scope=snsapi_base
				var local = 'http://123123.cn' //当前页面的地址(首页地址) 这个地址需要添加到公众号的一个设置里面去!!!
				//  const local = window.location.href;
				var appid = 'wxf123123123123' //公众号id 公众号里有自己查,不会找的查一下百度
				this.code = this.getUrlCode().code // 截取code	
				// 判断地址栏参数有无code,如果没有code,页面地址就跳转到微信提供的获取code的链接
				if (this.code == null || this.code == '') {
					console.log(1116666)
					location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
						"&redirect_uri=" +
						encodeURIComponent(local) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"

				} else {
					// 获取code后自己的业务逻辑 code只能用一次,后序会报错,注意
					this.getWechatCodeFn(this.code)  // 把code传给后端,以获取unionid 
				}
			},
			// 截取url中的code方法
			getUrlCode() {
				var url = location.search

				var theRequest = new Object()
				if (url.indexOf("?") != -1) {
					var str = url.substr(1)
					var strs = str.split("&")
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
					}
				}
				console.log(theRequest, '111')
				return theRequest
			},

            // 给后端code
			async getWechatCodeFn(code) {
				const res = await getWechatCode({
					code: code
				})
				this.unionid = res.data.unionid
				uni.setStorageSync('unionid',this.unionid) // 我这里需要存到浏览器,支付页要把这个在给后端,如果你不需要可以不存
			},
}

二、支付页

onLoad(parms) {
			this.unionid = uni.getStorageSync('unionid')  // 获取在首页存到缓存的unionid
               }
methods: {
			        // 支付按钮
			        async payBtn(e) {
                        // 提交给后端的东西,以获取到微信支付用到的参数
						const res = await buyOrder({
							bizId: this.goodsId,
							payType: e,
							remark: this.beizhu,
							type: '1',
							unionid: this.unionid  // 这个是在首页的时候通过获取用户的code,然后给后端 后端返回一个unionid(openid)
						})
						const ass = res.data.payFormString
						console.log(ass);
                        // 微信jsapi支付需要用到的参数
						this.appId = ass.appId
						console.log(this.appId);
						this.noncestr = ass.nonceStr
						this.package = ass.packageVal
						this.timestamp = ass.timestamp
						this.signType = ass.signType
						this.paySign = ass.paySign
                        //------------------------
					    // 发起微信支付
						WeixinJSBridge.invoke('getBrandWCPayRequest', {
								"appId": this.appId, //公众号ID,由商户传入     
								"timeStamp": this.timestamp, //时间戳,自1970年以来的秒数     
								"nonceStr": this.noncestr, //随机串     
								"package": this.package,
								"signType": this.signType, //微信签名方式:     
								"paySign": this.paySign //微信签名 
							},
							function(res) {
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									// 使用以上方式判断前端返回,微信团队郑重提示:
									//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
								}
							});
						

                }
        }
					
					

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值