华南中天论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
华南中天论坛 门户 网络技术 大数据 查看内容

uniapp 让支付触手可及,封装了微信支付、QQ支付、支付宝支付、京东支付、银联支付常用的支付方式以及各种常用的接口

2020-1-11 00:12| 发布者: 七彩数码影像| 查看: 135| 评论: 0

摘要: 下载地址 https://gitee.com/zscat/mallplus 1.uniapp 接入各种h5支付 // 用户点击支付方式处理 async toPayHandler(code) { let params = {'orderId':this.orderId}; let data = { payment_c

下载地址 https://gitee.com/zscat/mallplus

1.uniapp 接入各种h5支付

// 用户点击支付方式处理
			async toPayHandler(code) {
				let params = {'orderId':this.orderId};
				let data = {
					payment_code: code,
					payment_type: this.type
				}
				data['orderId'] = (this.type == 1 || this.type == 5 || this.type == 6) ? this.orderId : this.uid
				switch (code) {
					case 'alipay':
						/**
						 * 支付宝支付需要模拟GET提交数据
						 */
						if (this.type == 1 && this.orderId) {
							data['params'] = {
								trade_type: 'WAP',
								return_url: baseUrl +
									'wap/#/pages/order/payment/result'
							}
						} else if (this.type == 2 && this.recharge) {
							data['params'] = {
								money: this.recharge,
								return_url: baseUrl + 'wap/#/pages/order/payment/result'
							}
						} else if ((this.type == 5 || this.type == 6) && this.recharge) {
							data['params'] = {
								orderId: this.orderId
							}
						}

						let res = await Api.apiCall('get',Api.order.aliWapPay,params);
						console.log('============================')
						console.log(res);
						console.log('success:' + JSON.stringify(res));
						if (res) {

							document.body.appendChild(JSON.stringify(res))
							let testForm = document.getElementsByName('punchout_form')
								
							testForm.dispatchEvent(new Event('submit'))
							    console.log(tempForm);
							testForm.submit()
								document.body.removeChild(testForm)

						}
						break
					case 'wechatpay':

						/**
						 * 微信支付有两种
						 * 判断是否在微信浏览器
						 * 	微信jsapi支付
						 */
						let isWeiXin = this.$common.isWeiXinBrowser()

						if (isWeiXin) {
							var transitUrl =
								baseUrl +
								'wap/#/pages/order/payment/auth?order_id=' +
								this.orderId +
								'&type=' +
								this.type;

							if (this.type == 1 && this.orderId) {
								
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									url: transitUrl
								}
							} else if (this.type == 2 && this.recharge) {
								data['params'] = {
									trade_type: 'JSAPI_OFFICIAL',
									money: this.recharge,
									url: transitUrl + '&uid=' + this.uid + '&money=' + this.recharge
								}
								
							} else if ((this.type == 5 || this.type == 6) && this.recharge) {
								data['params'] = {
									orderId: this.orderId
								}
							}
							let res = await Api.apiCall('get',Api.order.webPay,params);
							console.log(res);
							if (res) {
								const data = res.data
								this.checkWXJSBridge(data)
							}else{
									window.location.href = res.msg
									return;
							}
						} else {
							// 微信 H5支付
							if (this.type == 1 && this.orderId) {
								data['params'] = {
									trade_type: 'MWEB',
									return_url: baseUrl +
										'wap/#/pages/order/payment/result'
								}
							} else if (this.type == 2 && this.recharge) {
								data['params'] = {
									trade_type: 'MWEB',
									money: this.recharge,
									return_url: baseUrl + 'wap/#/pages/order/payment/result'
								}
							} else if ((this.type == 5 || this.type == 6) && this.recharge) {
								data['params'] = {
									orderId: this.orderId
								}
							}
							console.log(data);
							// 微信h5支付
							let res = await Api.apiCall('get',Api.order.wapPay,params);
							console.log(res);
							if (res) {

								if (res) {
									location.href = res
								} else {
									this.$common.errorToShow(res)
								}
							}
						}
						break
					case 'balancepay':
						/**
						 *  用户余额支付
						 *
						 */
						if ((this.type == 5 || this.type == 6) && this.recharge) {
							data['params'] = {
								orderId: this.orderId
							}
						}
						let params1 = {'orderId':this.orderId};
						let data1 = await Api.apiCall('post',Api.order.balancePay,params1);
						console.log(data1)
						if (data1) {
							uni.redirectTo({
								url: '/pages/order/payment/result?order=' + JSON.stringify(data1)
							})
						}else {
							this.$api.msg('余额支付失败');
						}

						break
					case 'offline':
						/**
						 * 线下支付
						 */
						this.$common.modelShow(
							'线下支付说明',
							'请联系客服进行线下支付qq:951449465',
							() => {},
							false,
							'取消',
							'确定'
						)
						break
				}

2.uniapp支持各种微信环境的支付

// 用户点击支付方式处理
		async toPayHandler (code) {

			let params = {'orderId':this.orderId,
				payment_type: this.type};


			// 判断订单支付类型
			if (this.type == 2 && this.recharge) {

			}else if ((this.type == 5 || this.type == 6) && this.recharge) {

			}
			let _this = this
			switch (code) {
				case 'wechatpay':

					let res = await Api.apiCall('post',Api.order.weixinAppletPay,params);
					console.log(res);
					if (res) {

						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.timeStamp,
							nonceStr: res.nonceStr,
							package: res.package,
							signType: res.signType,
							paySign: res.paySign,
							success: function (e) {
								if (e.errMsg === 'requestPayment:ok') {
									_this.$common.successToShow(res.msg, () => {
										_this.$common.redirectTo('/pages/order/payment/result?id=' + res.id)
									})
								}
							}
						});
					} else {
						this.$common.errorToShow(res.msg)
					}

				break
				case 'balancepay':
				/**
				 *  用户余额支付
				 *
				 */
				let params1 = {'orderId':this.orderId};
					let data1 = await Api.apiCall('post',Api.order.balancePay,params1);
					console.log(data1)
					if (data1) {
						uni.redirectTo({
							url: '/pages/order/payment/result?order=' + JSON.stringify(data1)
						})
					}else {
						this.$api.msg('余额支付失败');
					}
				break
				case 'offline':
				/**
				 * 线下支付
				 */
				this.$common.modelShow('线下支付说明', '请联系客服进行线下支付', () => {},false, '取消', '确定')
				break
			}

3.uniapp支持app环境的支付方式

// 用户点击支付方式处理
		async toPayHandler (code) {
			let _this = this
			let params = {'orderId':this.orderId};
			let data = {
				payment_code: code,
				payment_type: _this.type
			}

			data['ids'] = (this.type == 1 || this.type == 5 || this.type == 6) ? this.orderId : this.uid
			 if ((this.type == 5 || this.type == 6) && this.recharge) {
				data['params'] = {
					trade_type: 'APP',
					formid: this.orderId
				}
			}
			switch (code) {
				case 'alipay':
				/**
				 * 支付宝支付需要模拟GET提交数据
				 */
				if (_this.type == 1 && _this.orderId) {
					data['params'] = {
						trade_type: 'APP'
					}
				} else if (_this.type == 2 && _this.recharge) {
					data['params'] = {
						trade_type: 'APP',
						money: _this.recharge
					}
				}

					let res = await Api.apiCall('get',Api.order.aliAppPay,params);
					console.log(res);
					if (res) {

						uni.requestPayment({
							provider: "alipay",
							orderInfo: res,
							success: function(data){
								console.log(data);
								_this.$common.successToShow('支付成功', () => {
									_this.redirectHandler(res.data.payment_id)
								})
							}
						});
					} else {
						_this.$comon.errorToShow(res.msg)
					}

				break
				case 'wechatpay':
					// 微信 H5支付
					if (_this.type == 1 && _this.orderId) {
						data['params'] = {
							trade_type: 'APP'
						}
					} else if (_this.type == 2 && _this.recharge) {
						data['params'] = {
							trade_type: 'APP',
							money: _this.recharge
						}
					}

					// 微信app支付
					let res1 = await Api.apiCall('get',Api.order.appPay,params);

					if (res1) {
							// 调用微信支付
							uni.requestPayment({
								provider: "wxpay",
								orderInfo: res1,
								success: function(data){
									_this.$common.successToShow('支付成功', () => {
										_this.redirectHandler(data)
									})
								},
								fail:function(res){
									console.log(JSON.stringify(res));
								}
							});
						} else {
							_this.$common.errorToShow(res.msg)
						}

					break
				case 'balancepay':
					/**
					 *  用户余额支付
					 *
					 */
					let params1 = {'orderId':this.orderId};
					let data1 = await Api.apiCall('post',Api.order.balancePay,params1);
					console.log(data1)
					if (data1) {
						uni.redirectTo({
							url: '/pages/order/payment/result?order=' + JSON.stringify(data1)
						})
					}else {
						this.$api.msg('余额支付失败');
					}
					break
				case 'offline':
					/**
					 * 线下支付
					 */
					_this.$common.modelShow('线下支付说明', '请联系客服进行线下支付',() => {}, false, '取消', '确定')
					break
				}
		},

 

4.uniapp支持各种 支付宝环境的支付方式

// 用户点击支付方式处理
		async toPayHandler (code) {
			let params = {'orderId':this.orderId};
			let data = {
				payment_code: code,
				payment_type: this.type
			}
			data['ids'] = (this.type == 1 || this.type == 5 || this.type == 6) ? this.orderId : this.uid

			// 判断订单支付类型
			if (this.type == 2 && this.recharge) {
				data['params'] = {
					money: this.recharge,
					trade_type: 'JSAPI'
				}
			} else if ((this.type == 5 || this.type == 6) && this.recharge) {
				data['params'] = {
					trade_type: 'JSAPI',
					formid: this.orderId
				}
			}else {
				data['params'] = {
					trade_type: 'JSAPI'
				}
			}


			let _this = this;
			switch (code) {
				case 'alipay':
					let res = await Api.apiCall('get',Api.order.webPay,params);
					console.log(res);
					if (res) {

							uni.requestPayment({
								provider: 'alipay',
								tradeNO:res.data.trade_no,
								success: function (e) {
									if (e.errMsg === 'requestPayment:ok') {
										_this.$common.successToShow(res.msg, () => {
											_this.$common.redirectTo('/pages/order/payment/result?id=' + res.data.id);
										});
									}
								}
							});
						} else {
							this.$common.errorToShow(res.msg);

					}
					break
				case 'balancepay':
					//用户余额支付
					let params1 = {'orderId':this.orderId};
					let data1 = await Api.apiCall('post',Api.order.balancePay,params1);
					console.log(data1)
					if (data1) {
						uni.redirectTo({
							url: '/pages/order/payment/result?order=' + JSON.stringify(data1)
						})
					}else {
						this.$api.msg('余额支付失败');
					}
					break;
				case 'offline':
					//线下支付
					this.$common.modelShow('线下支付说明', '请联系客服进行线下支付',() => {}, false, '取消', '确定')
					break;
			}

		}

下载 地址 https://gitee.com/zscat/mallplus​​​​​​​

关注公众号获取微服务版本下载和部署方式

zscat-me 发布了34 篇原创文章 · 获赞 28 · 访问量 10万+ 私信 关注
来源:https://blog.csdn.net/a1439226817/article/details/103896299
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

鲜花

握手

雷人

路过

鸡蛋

最新评论

QQ|Archiver|手机版|小黑屋|南华中天社区 ( 粤ICP备11019662号 )

GMT+8, 2020-8-5 21:51 , Processed in 0.035302 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

返回顶部