Table of Contents
Preface
Climb the pit: There is a high probability that Android will fail to activate WeChat payment (failed to introduce WeChat's js-sdk package)
Solution:
Vue introduces WeChat js-sdk Package
Attached is my demo
Summary:
Home Web Front-end H5 Tutorial H5 WeChat payment solution to the failure of introducing WeChat's js-sdk package

H5 WeChat payment solution to the failure of introducing WeChat's js-sdk package

Jul 24, 2018 am 10:18 AM
html5 javascript vue.js WeChat Pay

The content shared with you in this article is about the solution to the failure of introducing WeChat’s js-sdk package in H5 WeChat payment. The content is of great reference value and I hope it can help friends in need.

Preface

In the development of WeChat public account activities, since I have not been exposed to WeChat-related development before, I fell into pits and climbed into pits. However, it also made me understand WeChat public and WeChat official Familiarity with documentation is greatly increased.

Climb the pit: There is a high probability that Android will fail to activate WeChat payment (failed to introduce WeChat's js-sdk package)

In the official document of WeChat: https://pay.weixin.qq. com/wik...
There is such a DEMO:
function onBridgeReady(){
   WeixinJSBridge.invoke(
      'getBrandWCPayRequest', {
         "appId":"wx2421b1c4370ec43b",     //公众号名称,由商户传入     
         "timeStamp":"1395712654",         //时间戳,自1970年以来的秒数     
         "nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串     
         "package":"prepay_id=u802345jgfjsdfgsdg888",     
         "signType":"MD5",         //微信签名方式:     
         "paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
      },
      function(res){
      if(res.err_msg == "get_brand_wcpay_request:ok" ){
      // 使用以上方式判断前端返回,微信团队郑重提示:
            //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
      } 
   }); 
}
if (typeof WeixinJSBridge == "undefined"){
   if( document.addEventListener ){
       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
   }else if (document.attachEvent){
       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
   }
}else{
   onBridgeReady();
}
Copy after login
After the back-end colleague successfully authorizes it, introduce this code on the activity page, be happy, build and submit for testing. Apple is fine. , Android seems to be fine, but sometimes Goose Android cannot adjust the payment. I initially thought it was due to the WeChat version, etc. However, the probability of successful adjustment is too low. It can only be adjusted once every 10 times. , it must be the code reason. Change it.

Solution:

Open the WeChat developer tools, log, and finally find that at this step if (typeof WeixinJSBridge == "undefined")
1.ios can activate WeChat Browser's js-sdk
2. Most Androids go to undefined
Actually, I don't know the reason here. Personally, I feel it is a problem with the built-in browser version of WeChat Android and the WeixinJSBridge method. (I hope someone can answer it)
Since js-sdk cannot be adjusted, then introduce the configuration manually //So sometimes being lazy is more troublesome, learn lessons
if (typeof WeixinJSBridge == "undefined"){
    console.log( WeixinJSBridge);
   if( document.addEventListener ){
       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
   }else if (document.attachEvent){
       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
   }
}else{
   onBridgeReady();
}
Copy after login

Vue introduces WeChat js-sdk Package

npm i -S weixin-js-sdk
Copy after login

Introduce module

import wx from 'weixin-js-sdk'
Copy after login

configuration on the page that needs to be introduced (refer to WeChat official documentation: https://mp.weixin.qq.com/wiki...):

wx.config({
    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: '', // 必填,公众号的唯一标识
    timestamp: , // 必填,生成签名的时间戳
    nonceStr: '', // 必填,生成签名的随机串
    signature: '',// 必填,签名
    jsApiList: [] // 必填,需要使用的JS接口列表 比如我要调用支付接口 那么就是 [chooseWXPay]
});

这里timestamp是小写 s 是小写,数据类型是 int 类型
Copy after login

Now that the configuration is successful, let’s continue reading the official documentation
The official documentation says that there is a ready method. After the config verification is successful, put the interface in it to ensure execution.

wx.ready(function(){
    // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
Copy after login

Introduction parameters in ready (pay attention to the data type, and cooperate well with back-end colleagues - -)

wx.chooseWXPay({
timestamp: 0, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: '', // 支付签名随机串,不长于 32 位
package: '', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
signType: '', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: '', // 支付签名
success: function (res) {
// 支付成功后的回调函数
}
});
Copy after login

Attached is my demo

Use Vue data in ready The data in WXPay accidentally fell into the pit pointed by this. If you do not add bind, the parameters in wx.chooseWXPay cannot get the data returned from the backend request. This here does not point to VueComponent, so naturally the request cannot be obtained. Finally, I assigned the data of this.wx_config array object.
getConfig(){
            wx.config({
                debug: this.wx_config.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: this.wx_config.appId, // 必填,公众号的唯一标识
                timestamp: this.wx_config.timestamp, // 必填,生成签名的时间戳
                nonceStr: this.wx_config.nonceStr, // 必填,生成签名的随机串
                signature:this.wx_config.signature,// 必填,签名
                jsApiList: this.wx_config.jsApiList // 必填,需要使用的JS接口列表
            });
            //微信支付
            wx.ready(function() {
                // console.log(this.jsApiCall());
                wx.chooseWXPay({
                    timestamp: this.wechat_code.timestamp,
                    nonceStr:this.wechat_code.nonceStr,
                    package: this.wechat_code.package,
                    signType: this.wechat_code.signType,
                    paySign: this.wechat_code.paySign,
                    success: function () {
                        // 支付成功后的回调函数
                        alert("支付成功");
                        window.location.href = "/hd/becomevip";
                    },
                    cancel: function() {
                        alert("支付失败");
                    }
                });
            }.bind(this));
        },
Copy after login

Summary:

It is always inevitable to step into pitfalls. To summarize, don’t do the right thing because you are afraid of trouble~

Related recommendations:

About the solution to the 1px border in the mobile H5 page

About the sharing introduction of the html5 canvas WeChat poster

The above is the detailed content of H5 WeChat payment solution to the failure of introducing WeChat's js-sdk package. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles