微信小程序支付纯前端

百度许久才找到这位大佬的不走后台的小程序支付
https://blog.csdn.net/yu17310133443/article/details/89470887

/*统一支付接口*/
			unitedPayRequest() {
				var that = this;
				//统一支付签名
				var appid = 'xxx'; //appid必填
				var body = '12345'; //商品名必填,没有编码不能用中文
				var mch_id = 'xxx'; //商户号必填
				var openid = 'xxx'
				var nonce_str = that.randomString(); //随机字符串,不长于32位。  
				var notify_url = 'https://www.weixin.qq.com/wxpay/pay.php'; //通知地址必填
				var total_fee = parseInt(0.01 * 100); //价格,这是一分钱
				var trade_type = "JSAPI";
				var key = 'xxx'; //商户key必填,在商户后台获得
				var out_trade_no = that.randomString1(); //自定义订单号必填

				var unifiedPayment = 'appid=' + appid + '&body=' + body + '&mch_id=' + mch_id + '&nonce_str=' + nonce_str +
					'&notify_url=' + notify_url + '&openid=' + openid + '&out_trade_no=' + out_trade_no + '&total_fee=' +
					total_fee + '&trade_type=' + trade_type + '&key=' + key;
				console.log("unifiedPayment", unifiedPayment);
				var sign = md5(unifiedPayment).toUpperCase();
				console.log("签名md5", sign);

				//封装统一支付xml参数
				var formData = "<xml>";
				formData += "<appid>" + appid + "</appid>";
				formData += "<body>" + body + "</body>";
				formData += "<mch_id>" + mch_id + "</mch_id>";
				formData += "<nonce_str>" + nonce_str + "</nonce_str>";
				formData += "<notify_url>" + notify_url + "</notify_url>";
				formData += "<openid>" + openid + "</openid>";
				formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>";
				formData += "<total_fee>" + total_fee + "</total_fee>";
				formData += "<trade_type>" + trade_type + "</trade_type>";
				formData += "<sign>" + sign + "</sign>";
				formData += "</xml>";
				console.log("formData", formData);
				//统一支付
				wx.request({
					url: 'https://api.mch.weixin.qq.com/pay/unifiedorder', //别忘了把api.mch.weixin.qq.com域名加入小程序request白名单,这个目前可以加
					method: 'POST',
					head: 'application/json',
					data: formData, //设置请求的 header
					success: function(res) {
						console.log("返回商户", res.data);
						var result_code = that.getXMLNodeValue('result_code', res.data.toString("utf-8"));
						var resultCode = result_code.split('[')[2].split(']')[0];
						if (resultCode == 'FAIL') {
							var err_code_des = that.getXMLNodeValue('err_code_des', res.data.toString(
								"utf-8"));
							var errDes = err_code_des.split('[')[2].split(']')[0];
							wx.showToast({
								title: errDes,
								icon: 'none',
								duration: 3000
							})
						} else {
							//发起支付
							var prepay_id = that.getXMLNodeValue('prepay_id', res.data.toString("utf-8"));
							var tmp = prepay_id.split('[');
							var tmp1 = tmp[2].split(']');
							//签名  
							var key = 'xxx'; //商户key必填,在商户后台获得
							var appId = 'xxx'; //appid必填
							var timeStamp = that.createTimeStamp();
							var nonceStr = that.randomString();
							var stringSignTemp = "appId=" + appId + "&nonceStr=" + nonceStr +
								"&package=prepay_id=" + tmp1[0] + "&signType=MD5&timeStamp=" + timeStamp +
								"&key=" + key;
							console.log("签名字符串", stringSignTemp);
							var sign = md5(stringSignTemp).toUpperCase();
							console.log("签名", sign);
							var param = {
								"timeStamp": timeStamp,
								"package": 'prepay_id=' + tmp1[0],
								"paySign": sign,
								"signType": "MD5",
								"nonceStr": nonceStr
							}
							console.log("param小程序支付接口参数", param);
							that.processPay(param);
						}

					},
				})

			},


			/* 小程序支付 */

			processPay(param) {
				wx.requestPayment({
					timeStamp: param.timeStamp,
					nonceStr: param.nonceStr,
					package: param.package,
					signType: param.signType,
					paySign: param.paySign,
					success: function(res) {
						// success
						console.log("wx.requestPayment返回信息", res);
						wx.showModal({
							title: '支付成功',
							content: '您将在“微信支付”官方号中收到支付凭证',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {} else if (res.cancel) {}
							}
						})
					},
					fail: function() {
						console.log("支付失败");
					},
					complete: function() {
						console.log("支付完成(成功或失败都为完成)");
					}
				})
			},
			/* 时间戳产生函数   */

			createTimeStamp() {
				return parseInt(new Date().getTime() / 1000) + ''
			},
			/* 随机数 */
			randomString() {
				var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; //默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
				var maxPos = chars.length;
				var pwd = '';
				for (var i = 0; i < 32; i++) {
					pwd += chars.charAt(Math.floor(Math.random() * maxPos));
				}
				return pwd;
			},
			//订单号
			randomString1() {
				var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; //默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
				var maxPos = chars.length;
				var pwd = '';
				for (var i = 0; i < 30; i++) {
					pwd += chars.charAt(Math.floor(Math.random() * maxPos));
				}
				return pwd;
			},
			/* 获取XML节点信息 */
			getXMLNodeValue(node_name, xml) {
				console.log(xml)
				var tmp = xml.split("<" + node_name + ">")
				var _tmp = tmp[1].split("</" + node_name + ">")
				return _tmp[0]
			},
		
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值