UniApp H5 WeChat Pay

The first step, if NPM is not integrated, it is recommended to integrate NPM package management tools first

Can refer to another article of the author

UniApp integrated NPM

The second step is to integrate jweixin-module according to Uniapp official website

npm install jweixin-module --save

UniApp H5 WeChat Pay

The third step is to set up on the WeChat public platform-Official Account Settings-Function Settings-Web Authorized Domain Name, configure your own H5 domain name, and use Web page authorization to obtain openId (required for payment signatures, here is a function that needs to be done in the background) At the same time, because the official account and WeChat merchant account are two accounts, they need to be bound to the WeChat merchant account.

 

The fourth step is to bind the payment authorization directory on the WeChat merchant platform, and use the top-level domain name directly instead of the detailed address

WeChat Merchant Website

The fifth step, in H5, when the user logs in, get the authorization, get the openId, and then submit it to the background for saving, which can be obtained directly through the link.

 

The sixth step is to place an order and obtain data such as signatures and timestamps generated in the background through the order. These need to be referenced on the official WeChat website of the background.

WeChat jsAPI document

The seventh step, after obtaining the signature, is the front-end work, and the second step library is introduced into the script of the page that needs to be paid

var jweixin = require('jweixin-module');

Step 8: In the call method, implement the config method and the ready method in turn, including the checkJsApi method and the chooseWXPay method. Just pass in the corresponding parameters. As follows:

 

payRequest:function(){
				var self = this;
				
				jweixin.config({
					debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
					appId:AppId, // 必填,公众号的唯一标识
					timestamp:self.rtData.timeStamp, // 必填,生成签名的时间戳
					nonceStr:self.rtData.nonceStr, // 必填,生成签名的随机串
					signature:self.rtData.paySign, // 必填,签名,见附录1
					jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
				});
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
						success: function(res) {
							console.log('checkjsapi Success')
							console.log(res);
						},
						fail:function(res) {
							console.log('res')
							console.log(res);
						}
					});
				    jweixin.chooseWXPay({
				        timestamp: self.rtData.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
				        nonceStr: self.rtData.nonceStr, // 支付签名随机串,不长于 32 位
				        package: self.rtData.packageValue, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
						signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
				        paySign:self.rtData.paySign, // 支付签名
				        success:function(res) {
				            // 支付成功后的回调函数
							
							console.log('paysuccess')
							console.log(res)
							var route =  'payResult' + '?type=1'
							uni.navigateTo({
							  url:route
							});
							
				        },
						cancel: function(r) {
						   var route =  'payResult' + '?type=2'
						   uni.navigateTo({
						     url:route
						   });
						},
				        fail:function(res) {
				           
							console.log('payfail')
							console.log(res)
							var route =  'payResult' + '?type=0' 
							uni.navigateTo({
							  url:route
							});
							
							
				        }
				    });
				});
				 
				jweixin.error(function(res) {
					console.log('error')
					console.log(res)
					uni.showToast({
						icon: 'none',
						title: '支付失败了',
						duration: 4000
					});
				        // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
				        /*alert("config信息验证失败");*/
				});
				
				
				
			},

Questions and responses:

1. Here you need to use the jweixin object to call related methods, don't use the wx object to call, remember to change it if you copy the code directly from the official WeChat website, the author suffered here.

2. Turn on the debug. If you see the pop-up box config: ok, you can basically confirm that the signature is correct. Of course, pay attention to capitalization and appId when signing.

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/bitcser/article/details/104837019