微信,支付寶支付

微信公衆號支付
  1. 經過url重定向方式拿到微信所需的code
//獲取微信code  微信公衆號環境

function getCode() {

 const appId = "微信appid";
 //注意 必定要轉義url
 const REDIRECT_URI = encodeURIComponent(

 window.location.href

 );

 window.location.replace(

 `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=snsapi_base&state=null#wechat_redirect`

 );

}
  1. 經過微信jssdk的方式調起支付
//調起微信支付 JSAPI

function onBridgeReady(data) {

 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 {

 WeixinJSBridge.invoke(

 "getBrandWCPayRequest",

 {

 appId: data.appId, //公衆號名稱,由商戶傳入

 timeStamp: data.timeStamp, //時間戳,自1970年以來的秒數

 nonceStr: data.nonceStr, //隨機串

 package: data.package,

 signType: "MD5", //微信簽名方式:

 paySign: data.paySign, //微信簽名

 },

 function (res) {

 /**

 * 狀態爲 cancel 時說明用戶取消了支付 toast提示

 * 狀態不爲 cancel 時 重定向到內部支付後的狀態頁

 */

 if (res.err_msg == "get_brand_wcpay_request:cancel") {

 Toast("支付取消");

 } else {
//注意 微信公衆號支付 須要前端本身跳轉支付狀態頁面
 window.location.replace();

 }

 }

 );

 }

}
H5微信支付
  1. 經過重定向URL 對接微信支付 地址通常爲後端返回 前端須要重定向
window.location.replace('後端返回的地址');
H5支付寶支付
  1. 支付寶支付是經過動態生成form表單來對接支付的,須要注意 不要傳入多於參數,不然回報錯
var temp = document.createElement("form");

 temp.action = data.prepay.form_url;

 temp.method = "post";

 temp.style.display = "none";

 let params = data.prepay;

 for (let v in params) {

 if (v != "form_url") {

 var opt = document.createElement("input");

 opt.name = v;

 opt.value = decodeURIComponent(params[v]);

 temp.appendChild(opt);

 }

 }

 document.body.appendChild(temp);

 temp.submit();