jsapi微信支付 -----適用於微信瀏覽器支付
阿新 • • 發佈:2019-01-01
**
* jsapi支付 只限於微信瀏覽器使用
*/
public function wxjspay(){
$notify_url = ""; //非同步通知地址
$order_number = I('order_number'); //訂單編號
$money = I('orderPrice'); //訂單金額
//呼叫微信支付函式
Vendor('Wxpay.example.WxPay#JsApiPay');
Vendor('Wxpay.lib.WxPay#Api');
Vendor('Wxpay.lib.WxPay#Data');
Vendor('Wxpay.lib.WxPay#Config');
Vendor('Wxpay.lib.WxPay#Exception');
$notify = new \JsApiPay();
$openid = $notify->GetOpenid(); //獲取openid 要配置微信公眾平臺,還要填寫APPSECRET引數
$input = new \WxPayUnifiedOrder();
$input->SetBody('訂單號:'.$order_number); //描述
$input->SetOut_trade_no($order_number); //訂單號
$input->SetTotal_fee($money*100); //單位為分,只能傳整數
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 60));
$input->SetNotify_url($notify_url);
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openid);
$order = \WxPayApi::unifiedOrder($input);
if($order['err_code'] == 'INVALID_REQUEST'){
$this->error('該訂單已過期,請重新下單');
}
$jsApiParameters = $notify->GetJsApiParameters($order);
$this->assign('jsApiParameters',$jsApiParameters); //將返回結果傳到前臺頁面
$this->display();
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>weixin</title>
<script type="text/javascript">
//呼叫微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsApiParameters}, //通過js調起微信支付
function(res){
WeixinJSBridge.log(res.err_msg);
alert(res.err_code+res.err_desc+res.err_msg);
}
);
}
function callpay()
{
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>
</head>
<body>
<div align="center">
<button type="button" onclick="callpay()" >立即支付</button>
</div>
</body>
</html>