微信,支付宝支付
码农天地 -微信公众号支付通过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`
);
}
通过微信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微信支付通过重定向URL 对接微信支付 地址一般为后端返回 前端需要重定向window.location.replace('后端返回的地址');
H5支付宝支付支付宝支付是通过动态生成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();
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。