joken-前端工程师

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

唤起微信支付核心代码

async weixinPayApp() {
			const self = this;
			let postApi = '';

			if (this.checkWexin) {
				postApi = 'getOpenWeixinSignOrderInfo';
			} else if (this.checkAliPay) {
				postApi = 'getAlipaySignOrderInfoForApp';
			}

			let option = '';
			if (this.entry) {
				// 如果是从订单列表跳转  取orderId
				option = {
					orderId: this.orderId
				};
				if (this.isSpell) {
					option.fightgroupOrderId = this.orderId;
				}
			} else { 
				// 走下单流程 去 orderGroudid
				option = {
					orderId: this.payOrderId
				};
				if (this.isSpell) {
					option.fightgroupOrderId = this.payOrderId;
				}
			}

			this.$u.api.pay[postApi](option).then(res => {
				console.log('PAY', res);

				let orderInfo = '';
				if (this.checkWexin) {
					const { appId, nonceStr, partnerId, prepayId, sign, timeStamp } = res.signOrderInfo;
					orderInfo = {
						appid: appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
						noncestr: nonceStr, // 随机字符串
						package: res.signOrderInfo.package, // 固定值
						partnerid: partnerId, // 微信支付商户号
						prepayid: prepayId, // 统一下单订单号
						timestamp: timeStamp, // 时间戳(单位:秒)
						sign: sign // 签名,这里用的 MD5 签名
					};
				} else if (this.checkAliPay) {
					orderInfo = res.orderString;
				}

				uni.requestPayment({
					provider: this.checkWexin ? 'wxpay' : 'alipay',
					orderInfo: orderInfo,
					success: function(res) {
						uni.showModal({
							title: '提示',
							content: '支付成功',
							showCancel: false,
							success: ({ confirm, cancel }) => {
								self.$go(`pages/pay/success?orderId=${self.orderId}`, {
									type: 'redirectTo'
								});
							}
						});
						console.log('success:' + JSON.stringify(res));
					},
					fail: function(err) {
						console.log('fail:' + JSON.stringify(err));
						self.$go(`pages/pay/fail?orderId=${self.orderId}`, {
							type: 'redirectTo'
						});
					}
				});
			});

		},

上面代码核心就是从服务端获取支付信息,然后通过uni.requestPayment 唤起支付即可

uniapp hbuild配置

image

image

image

appid:
image
需要微信后台申请

ios Universal Links :
一个链接代表本app标识,hbuild可以自动生成,直接点击自动生成按钮
生成方式参考:

https://zh.uniapp.dcloud.io/api/plugins/universal-links.html

微信后台配置

image

应用签名就是应用包名的32位md5哈希值

参考文章

https://juejin.cn/post/6965038146773843976

安卓一个app唤另外一个app示例代码:https://developer.aliyun.com/article/12533

posted on 2024-06-17 19:27  joken1310  阅读(9)  评论(0编辑  收藏  举报