🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` <?php session_start(); /** * * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用 * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重 * 请勿直接直接使用样例对外提供服务 * **/ //print_r($_SESSION['think']['wxpaydata']['price']);exit(); error_reporting(E_ERROR); require_once "../lib/WxPay.Api.php"; require_once "WxPay.JsApiPay.php"; require_once "WxPay.Config.php"; require_once 'log.php'; //初始化日志 $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log'); $log = Log::Init($logHandler, 15); //打印输出数组信息 /*function printf_info($data) { foreach($data as $key=>$value){ echo "<font color='#00ff55;'>$key</font> : ".htmlspecialchars($value, ENT_QUOTES)." <br/>"; } }*/ //①、获取用户openid try{ $tools = new JsApiPay(); $openId = $tools->GetOpenid(); //②、统一下单 $input = new WxPayUnifiedOrder(); $input->SetBody("会员充值"); $input->SetAttach("会员充值"); $input->SetOut_trade_no($_SESSION['think']['wxpaydata']['out_trade_no']); $input->SetTotal_fee($_SESSION['think']['wxpaydata']['price']*100); //$input->SetTotal_fee(1); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("JSAPI"); $input->SetNotify_url("http://www.xkhcmb.cn/api/wxpay/callback/"); $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $config = new WxPayConfig(); $order = WxPayApi::unifiedOrder($config, $input); //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>'; //printf_info($order); $jsApiParameters = $tools->GetJsApiParameters($order); //获取共享收货地址js函数参数 $editAddress = $tools->GetEditAddressParameters(); } catch(Exception $e) { Log::ERROR(json_encode($e)); } //③、在支持成功回调通知中处理成功之后的事宜,见 notify.php /** * 注意: * 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认支付是否成功 * 2、jsapi支付时需要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信公众平台“网页授权接口”, * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.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>微信JSPAI支付</title> <script type="text/javascript"> callpay(); //调用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $jsApiParameters; ?>, 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" ) { //alert('支付成功'); location.href="http://www.xkhcmb.cn/api/wxpay/wxpaywxok"; }else{ //alert('支付失败'); location.href="http://www.xkhcmb.cn/index/user/index.html"; } /*************/ } ); } 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> <script type="text/javascript"> //获取共享地址 function editAddress() { WeixinJSBridge.invoke( 'editAddress', <?php echo $editAddress; ?>, function(res){ var value1 = res.proviceFirstStageName; var value2 = res.addressCitySecondStageName; var value3 = res.addressCountiesThirdStageName; var value4 = res.addressDetailInfo; var tel = res.telNumber; alert(value1 + value2 + value3 + value4 + ":" + tel); } ); } window.onload = function(){ if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ // document.addEventListener('WeixinJSBridgeReady', editAddress, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', editAddress); document.attachEvent('onWeixinJSBridgeReady', editAddress); } }else{ editAddress(); } }; </script> </head> <body> <br/> <!-- <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px"><?php echo $_SESSION['think']['wxpaydata']['price']*100;?>分</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" onclick="callpay()" >立即支付</button> </div>--> </body> </html> ```