小程序支付及H5支付前端代码小结

小程序支付和H5支付前端都不需要引入其他的js , 只需要后台将相关的参数

timeStamp: \'\',
nonceStr: \'\',
package: \'\',
signType: \'MD5\',
paySign: \'\',

返回来就可以发起微信支付。

小程序支付:

wx.requestPayment({
  timeStamp: \'\',
  nonceStr: \'\',
  package: \'\',
  signType: \'MD5\',
  paySign: \'\',
  success (res) { },
  fail (res) { }
})

H5支付:

 1  callpay(needData) {
 2    let that = this;
 3     if (typeof WeixinJSBridge == "undefined") {
 4           if (document.addEventListener) {
 5                document.addEventListener(\'WeixinJSBridgeReady\', that.jsApiCall, false);
 6           } else if (document.attachEvent) {
 7               document.attachEvent(\'WeixinJSBridgeReady\', that.jsApiCall);
 8               document.attachEvent(\'onWeixinJSBridgeReady\', that.jsApiCall);
 9            }
10            } else {
11               that.jsApiCall(needData);
12          }
13   },
14    jsApiCall(needData) {
15         let that = this;
16         WeixinJSBridge.invoke(
17             \'getBrandWCPayRequest\', {
18               "appId": needData.appId,
19              "timeStamp": needData.timeStamp,
20                "nonceStr": needData.nonceStr,
21                 "package": needData.package,
22                 "signType": needData.signType,
23                  "paySign": needData.paySign,
24                   },
25                   function (res) {
26                       
27                             if (res.err_msg == "get_brand_wcpay_request:ok") {
28                                 // alert(\'微信支付成功!\');
29                                 if (that.alonePay == true) { //单独购买
30                                     window.location.href = "courseList.html" + "?goods_id=" + that
31                                         .goods_id;
32                                 } else { //拼团购买
33                                     window.location.href = "groupDetail.html" + "?group_id=" + that
34                                         .group_id + "&goods_id=" + that.goods_id;
35                                 }
36                                 // window.location.href = "index.html";
37                                 //                        window.location.href="http://h5.taotiangou.cn";
38                             } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
39                                 alert(\'已取消微信支付!\');
40                                 // window.location.href = "index.html";
41                                 //                        window.location.href="http://h5.taotiangou.cn";
42                             } else if (res.err_msg == "get_brand_wcpay_request:fail") {
43                                 alert(\'微信支付失败!\');
44                                 // window.location.href = "index.html";
45 
46                                 //                        window.location.href="http://h5.taotiangou.cn";
47                             }
48                         }
49                     );
50                 },