uniapp微信支付

//这是封装的
//引入api文件
api = app.requirejs('api');
//
sub_order: function() {
				let ta = this,
					mation = ta.mation,
					info = ta.info;
				let e = {
					currentTarget: {
						dataset: {

						}
					}
				}
				if (!mation.arti) {
					e.currentTarget.dataset.type = 'goods_content';
					ta.eject(e);
					return false;
				} else if (!ta.treaty) {
					core.alert('请先同意服务协议');
					return false;
				}

				if (ta.status == 1) {
					ta.toBuy();
				} else if (ta.status == 2) {
					api.yidu_pay(ta, ta.order_info.order_id, 'order');
				}
			},

//ta代表 上面的 let ta = this , order_id代表ta.order_info.order_id,order_type 代表'order'这里是固定的所以直接写'order' order_type是后端请求过来的参数    原这样写ta.order_info.order_type
yidu_pay(ta, order_id, order_type) {
		if (ta.running) {
			return false
		}
		ta.setData({
			running: true
		});
		//调用接口 里面的参数是从后端请求过来的
		core.get('pay/createWxPay', {
			order_id: order_id,
			order_type: order_type,
			buyer_id: getApp().globalData.getCache('userinfo').uid
		//请求成功后返回参数
		}, function(t) {
			ta.setData({
				running: false
			});
			//对code进行判断
			if (t.code == 0) {
			//调取返回的数据传到uniapp支付api的uni.requestPayment
				uni.requestPayment({
					provider: 'wxpay',//服务提供商
					timeStamp: t.data.timeStamp,
					nonceStr: t.data.nonceStr,
					'package': t.data.package,
					signType: t.data.signType,
					paySign: t.data.paySign,
				//然后获取返回参数进行判断
					success: function(res) {
						if (res.errMsg == "requestPayment:ok") {
							if (typeof ta.success_res == 'function') {
								ta.success_res(1, "支付成功");
							} else {
								getApp().yidu.msg('支付成功');
							}
						} else {
							getApp().yidu.msg('支付失败!')
						}
					},
					fail: function(res) {
						if (typeof ta.success_res == 'function') {
							ta.success_res(2, "机不可失,不要让宝贝等太久哦");
						} else {
							getApp().yidu.msg('机不可失,不要让宝贝等太久哦')
						}
					}
				})
			} else if (t.code == 2) {
				if (typeof ta.success_res == 'function') {
					ta.success_res(1, "支付成功");
				} else {
					getApp().yidu.msg('支付成功');
				}
			} else {
				if (typeof ta.success_res == 'function') {
					ta.success_res(2, t.msg);
				} else {
					getApp().yidu.msg(t.msg);
				}

			}
		}, true)
	},
//微信充值 //支付接口测试 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
发出的红包

打赏作者

呱嗨喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值