微信支付报错问题解决
支付回调各种报错,openID、APPID等等原因,打印 console.log(typeof(res.jsApiParameters));
重点把string转化为Object就ok了。
每次支付请求会生成订单,根据对应订单号,修改数据库订单状态才可以完成。
{
"appId":"wx1b35c*********3",
"nonceStr":"nam97dundxjiisasumrevju*******",
"package":"prepay_id=wx***",
"signType":"MD5",
"timeStamp":"1**",
"paySign":"7CD01*****9"
}
//获取充值套餐
$.ajax({
type: 'post',
url: api.domain + api.recharge_package,
data: {
"access_token": access
},
success : function(res) {
if (res.status == 1) {
var n = res.data.length;
var menu = [];
if(n < 1){
return false;
}
for(var i = 0;i < n;i++){
menu.push(res.data[i].id);
var selected;
//设置默认套餐与价格
if(i == 0){
selected = ' btn-active';
$('#money').html(res.data[i].price);
} else {
selected = '';
}
var $list = $('<span class="weui-btn btn-check js-pay ' + selected + '" title="' +
res.data[i].id + '" price="' + res.data[i].price + '">' + res.data[i].name + '</span>');
$list.appendTo($('#parent'));
}
$('#count').val(menu);
} else {
$.toptip(res.msg,'cancel');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('XMLHttpRequest.status = ' + XMLHttpRequest.status);
console.log('XMLHttpRequest.readyState = ' + XMLHttpRequest.readyState);
console.log('textStatus = ' + textStatus);
}
});
//点击套餐
$('body').on('click', '.js-pay', function(){
var param_b = $(this).attr('title');
var menu = $('#count').val().split(',');
for(var p = 0; p < menu.length;p++) {
if(menu[p] == param_b) {
$('span[title="' + menu[p] + '"]').addClass('btn-active');
} else {
$('span[title="' + menu[p] + '"]').removeClass('btn-active');
}
}
$('#money').html($(this).attr('price'));
});
//充值付费
$("#pay").click(function() {
var package = $('.btn-active').attr('title');
$.ajax({
type: 'post',
url: '/recharge/pay',
data: {
"access_token": access,
"package_id": package
},
success : function(res) {
console.log(typeof(res));
console.log(res.jsApiParameters);
console.log(typeof(res.jsApiParameters));
if (res.status == 1) {
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall($.parseJSON(res.jsApiParameters),res.order_no);
}
} else {
$.toptip(res.msg,'cancel');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('XMLHttpRequest.status = ' + XMLHttpRequest.status);
console.log('XMLHttpRequest.readyState = ' + XMLHttpRequest.readyState);
console.log('textStatus = ' + textStatus);
}
});
});
//调用微信JS api 支付
function jsApiCall(n,order_no){
console.log(n);
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
n,
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_code+res.err_desc+res.err_msg);
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
paySuccess(order_no);
}else if(res.err_msg == "get_brand_wcpay_request:cancel"){
$.toast("支付取消", "cancel");
}else{
$.toast("支付失败", "forbidden");
} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}
);
}
//微信回调成功,跳转成功页面
function paySuccess(n){
$.ajax({
type: 'post',
url: api.domain + api,
data: {
"access_token": access,
"order_no": n
},
success : function(res) {
if (res.status == 1) {
location.href = '/recharge/success';
} else {
$.toptip(res.msg,'cancel');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('XMLHttpRequest.status = ' + XMLHttpRequest.status);
console.log('XMLHttpRequest.readyState = ' + XMLHttpRequest.readyState);
console.log('textStatus = ' + textStatus);
}
});
}