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

下载地址 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​​​​​​​

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

发布了34 篇原创文章 · 获赞 28 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/a1439226817/article/details/103896299