//php页面----yii2.0框架
public function actionJsapi(){
$this->layout=false;
$total_fee=0.02;//$_POST['total_fee'];
$body="常奥康";//$_Get['body'];
//①、获取用户openid
$tools = new \JsApiPay();
$access_token=$this->access_token;
$openId = $tools->GetOpenid($access_token);
//②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody($body);//商品名称
$input->SetAttach("test");
$input->SetOut_trade_no(\WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee($total_fee*100);//商品价格
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
// $input->SetGoods_tag("test");
$input->SetNotify_url(\WxPayConfig::NOTIFY_URL);
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = \WxPayApi::unifiedOrder($input);
$jsApiParameters =$tools->GetJsApiParameters($order);
// $arr=json_decode($jsApiParameters,true);
// $signature="jsapi_ticket=".$this->ticket."&noncestr=".$arr['nonceStr']."×tamp=".$arr['timeStamp']."&url=http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// $arr['sigNature']=sha1($signature);
// $jsApiParameters=json_encode($arr);
// $url="http://app.mj0408.com/p3-10-3-1-1payment.html?p=".$jsApiParameters;
// header("Location:".$url);
return $this->render('jsapi',array('jsApiParameters'=>$jsApiParameters,'total_fee'=>$total_fee));
}
//html页面
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>微信支付</title>
<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<?php echo $jsApiParameters; ?>,//php处理后接收的json数据
function(res){
WeixinJSBridge.log(res.err_msg);
if(res.err_msg=='get_brand_wcpay_request:ok'){//如果支付成功
location.href="http://app..com/p3-7-2pay_success.html";
}else{//如果支付失败
location.href="http://admin..com/weixin/wxpay/jsapi.html";
}
// alert(res.err_msg);//cancel/ok;;;;;;;;get_brand_wcpay_request:ok
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == "undefined"){
alert(document.addEventListener);return;
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>
<br/>
<font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px"><?=$total_fee?></span>元</b></font><br/><br/>
<div align="center">
<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" οnclick="callpay()" >立即支付</button>
</div>
</body>
</html>