include_once("./module/payment/lib/WxPayPubHelper/WxPayPubHelper.php");
//使用jsapi接口
$jsApi = new JsApi_pub();
//=========步骤1:通过code获得openid============
$openid = $_SESSION['openid'];
//=========步骤2:使用统一支付接口,获取prepay_id============
$unifiedOrder = new UnifiedOrder_pub();
$unifiedOrder->setParameter("openid", $openid);//商品描述
$unifiedOrder->setParameter("body", "test");//商品描述
//自定义订单号,此处仅作举例
$timeStamp = time();
$out_trade_no = WxPayConf_pub::APPID . "$timeStamp";
$unifiedOrder->setParameter("out_trade_no", $out_trade_no);//商户订单号
$unifiedOrder->setParameter("total_fee", 100);//总金额
$unifiedOrder->setParameter("notify_url", WxPayConf_pub::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型
//非必填参数,商户可根据实际情况选填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
//$unifiedOrder->setParameter("device_info","XXXX");//设备号
$unifiedOrder->setParameter("attach", $re_wx['id'] . "|" . $order_id);//附加数据
$prepay_id = $unifiedOrder->getPrepayId();
//=========步骤3:使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($jsApiParameters));
$.ajax({
type: "post",
url: url,
dataType: "json",
data: {price:price,order_id:id},
success: function(msg){
var data = $.parseJSON(msg);
$('#wx').attr('data-appid',data.appId);
$('#wx').attr('data-time',data.timeStamp);
$('#wx').attr('data-nonce',data.nonceStr);
$('#wx').attr('data-pack',data.package);
$('#wx').attr('data-sign',data.paySign);
callpay();
}
});
<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke('getBrandWCPayRequest',{
"appId":$('#wx').attr('data-appid'),
"timeStamp":$('#wx').attr('data-time'),
"nonceStr":$('#wx').attr('data-nonce'),
"package":$('#wx').attr('data-pack'),
"signType":"MD5",
"paySign":$('#wx').attr('data-sign')
},function(res){
// WeixinJSBridge.log(res.err_msg);
if(res.err_msg == "get_brand_wcpay_request:ok")// 支付成功
{
var order_id=$('#order_id').val();
location.href = "<{$config.web_url}>/pay/?m=fenhong&s=evaluate&order_id="+order_id;
}
});
}
function callpay()
{
//$("#form1").submit();
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();
}
}
</script>
<div id="wx" data-appid="" data-time="" data-nonce="" data-pack="" data-sign="" style="display:none"></div>