0
点赞
收藏
分享

微信扫一扫

uniapp如何实现多端支付?支付宝微信支付


今天说一下uniapp实现多端支付,涉及微信公众号,小程序,APP,支付宝APP,支付宝h5支付,多端支付

有疑问可以留言

//点击支付的按钮
			goPay() {
				let userId = this.$queue.getData('userId');
				uni.showLoading({
					title: '支付中'
				});
				//在data里面自己定义
				if (this.openWay === 0) { //微信支付
					let that = this;
					
					//微信小程序支付,请求后台接口,传对应参数,
					// #ifdef MP-WEIXIN
					this.$Request.postJson('/pay/wxPay?payNum=' + this.payNum + '&userId=' + userId + '&payMoney=' + this.payMoney).then(
						res => {
							//调用api接口 传入对应参数 支付
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.timeStamp,
								nonceStr: res.nonceStr,
								package: res.package,
								signType: res.signType,
								paySign: res.paySign,
								success: function(res) {
									uni.showLoading({
										title: '支付成功'
									});
									uni.hideLoading();
									setTimeout(() => {
										uni.redirectTo({
											url: '../my/myList'
										});
									}, 1000);
								},
								fail: function(err) {
									uni.hideLoading();
									that.$queue.showToast('支付失败');
								}
							});
						});
					// #endif


					// #ifdef H5
					//公众号支付
					this.$Request.postJson('/pay/wxPayWeb?payNum=' + this.payNum + '&userId=' + userId + '&payMoney=' + this.payMoney)
						.then(
							res => {
								//请求公众号方法
								that.callPay(res);
							});
					// #endif
					
					// #ifdef APP-PLUS
					this.$Request.postJson('/pay/wxPayApp?payNum=' + this.payNum + '&userId=' + userId + '&payMoney=' + this.payMoney)
						.then(
							res => {
								console.log(JSON.stringify(res))
								this.setPayment('wxpay', JSON.stringify(res));
							});
					// #endif
				} else if (this.openWay === 1) { //支付宝支付
					// #ifdef H5
					this.$Request.getT('/aliPay/payH5?payNum=' + this.payNum + '&userId=' + userId + '&payMoney=' + this.payMoney).then(
						res => {
							const div = document.createElement('div')
							div.innerHTML = res.data //此处form就是后台返回接收到的数据
							document.body.appendChild(div)
							document.forms[0].submit()
						});
					// #endif
					// #ifdef APP-PLUS
					this.$Request.getT('/aliPay/payApp?payNum=' + this.payNum + '&userId=' + userId + '&payMoney=' + this.payMoney).then(
						res => {
							this.setPayment('alipay', res.data);
						});
					// #endif
				} else if (this.openWay === 2) { //零钱支付 自己开发的 忽略
					if (this.lingqian > 0) {
						this.$Request.getT('/orders/changePay?ordersId=' + this.id).then(res => {
							if (res.status === 0) {
								uni.hideLoading();
								this.$queue.showToast('支付成功');
								setTimeout(function() {
									uni.hideLoading();
									uni.redirectTo({
										url: './myList'
									});
								}, 1000);
							} else {
								uni.hideLoading();
								this.$queue.showToast(res.msg);
							}
						});
					} else {
						this.$queue.showToast('零钱余额为空,请前往微信打开进行支付');
					}
				} else if (this.openWay === 3) { //零钱支付
					if (this.total > 0) {
						this.$Request.getT('/orders/jiFenPay?ordersId=' + this.id).then(res => {
							if (res.status === 0) {
								uni.hideLoading();
								this.$queue.showToast('支付成功');
								setTimeout(function() {
									uni.hideLoading();
									uni.redirectTo({
										url: './myList'
									});
								}, 1000);
							} else {
								uni.hideLoading();
								this.$queue.showToast(res.msg);
							}
						});
					} else {
						this.$queue.showToast('积分余额为空!');
					}
				}
			},
			
			//公众号支付处理方式
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
					}
				} else {
					this.onBridgeReady(response);
				}
			},
			//公众号支付处理方式
			onBridgeReady: function(response) {
				let that = this;
				if (!response.package) {
					return;
				}
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称,由商户传入
						"timeStamp": response.timestamp, //时间戳,自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式:
						"paySign": response.sign //微信签名
					},
					function(res) {
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示:
							//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
							uni.showLoading({
								title: '支付成功'
							});
							setTimeout(function() {
								uni.hideLoading();
								uni.redirectTo({
									url: './myList'
								});
							}, 1000);
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			
			//APP统一拉起支付方法
			setPayment(name, order) {
				let that = this;
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						uni.hideLoading();
						uni.showLoading({
							title: '支付成功'
						});
						//处理支付成功 跳转页面
						setTimeout(function() {
							uni.hideLoading();
							uni.redirectTo({
								url: '../member/orderdetail?id=' + that.id
							});
						}, 1000);
					},
					fail: function(err) {
						uni.hideLoading();
						console.log(12)
					}
				});
			}

举报

相关推荐

0 条评论