PHPcms微信支付

基本上按照官方给的sdk修改的没有改动太多东西
微信支付有些麻烦 需要在微信公众平台上设置-支付授权目录(重要)和-[网页授权获取用户基本信息](重要)
流程已经走通 因为各个项目的不同仅供参考吧

有好的建议请留言



1.去官网下载




https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

2.重新建立目录结构



3.在模块方法中建立方法


注微信支付中获取code和openid时容易丢失传递值 可以考虑cookie传值

[php]  view plain  copy
  1. public function wx_pay() {  
  2.     include PHPCMS_PATH . '/wxpay/lib/WxPay.Api.php';  
  3.     include PHPCMS_PATH . '/wxpay/WxPay.JsApiPay.php';  
  4.     include PHPCMS_PATH . '/wxpay/log.php';  
  5.     $logHandler = new CLogFileHandler(PHPCMS_PATH . "'/wxpay/logs/" . date('Y-m-d') . '.log');  
  6.     $log = Log::Init($logHandler, 15);  
  7.     //①、获取用户openid  
  8.     $tools = new JsApiPay();  
  9.     $openId = $tools->GetOpenid();  
  10.     $list['cate_name'] = param::get_cookie('cate_name');  
  11.     $list['amount'] = param::get_cookie('amount');  
  12.     $list['order_sn'] = param::get_cookie('order_sn');  
  13.     //②、统一下单  
  14.     $input = new WxPayUnifiedOrder();  
  15.     $input->SetBody($list['cate_name']);  
  16.     $input->SetAttach($list['cate_name']);  
  17.     $input->SetOut_trade_no($list['order_sn']);  
  18.     $input->SetTotal_fee($list['amount'] * 100);  
  19.     $input->SetTime_start(date("YmdHis"));  
  20.     $input->SetTime_expire(date("YmdHis", time() + 600));  
  21.     $input->SetGoods_tag("test");  
  22.     $input->SetNotify_url("http://www.www.com/wxpay/notify.php");  
  23.     $input->SetTrade_type("JSAPI");  
  24.     $input->SetOpenid($openId);  
  25.     $order = WxPayApi::unifiedOrder($input);  
  26.     $list['jsApiParameters'] = $tools->GetJsApiParameters($order);  
  27.     //var_dump($list);exit;  
  28.     include template('xxx''xxx''default');  
  29. }  

4.前台页面


[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1"/>   
  7.     <title>支付</title>  
  8.     <script type="text/javascript">  
  9.         //调用微信JS api 支付  
  10.         function jsApiCall() {  
  11.             WeixinJSBridge.invoke(  
  12.                 'getBrandWCPayRequest',   
  13.                 {$list[jsApiParameters]},  
  14.                 function(res) {  
  15.                     WeixinJSBridge.log(res.err_msg);  
  16.                     if (res.err_msg == "get_brand_wcpay_request:ok") {  
  17.                         window.location.href = "";  
  18.                     } else {  
  19.                         //返回跳转到订单详情页面  
  20.                         alert(支付失败);  
  21.                         window.location.href = "";  
  22.                     }  
  23.                 }  
  24.             );  
  25.         }  
  26.   
  27.         function callpay() {  
  28.             if (typeof WeixinJSBridge == "undefined") {  
  29.                 if (document.addEventListener) {  
  30.                     document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);  
  31.                 } else if (document.attachEvent) {  
  32.                     document.attachEvent('WeixinJSBridgeReady', jsApiCall);  
  33.                     document.attachEvent('onWeixinJSBridgeReady', jsApiCall);  
  34.                 }  
  35.             } else {  
  36.                 jsApiCall();  
  37.             }  
  38.         }  
  39.     </script>  
  40. </head>  
  41.   
  42. <body>  
  43.     <div class="receive">  
  44.         <h2>微信支付</h2>  
  45.         <ul class="form-list">  
  46.   
  47.             <p class="submit" type="button" onclick="callpay()">立即支付</p>  
  48.         </ul>  
  49.     </div>  
  50. </body>  
  51.   
  52. </html>  


5.回调函数

经测试回调链接不支持拼接链接 因为赶时间没有进行其他的实现方法
回调函数 /wxpay/notify.php



[php]  view plain  copy
  1. <?php  
  2.   
  3. ini_set('date.timezone','Asia/Shanghai');  
  4. error_reporting(E_ERROR);  
  5. require_once "lib/WxPay.Api.php";  
  6. require_once 'lib/WxPay.Notify.php';  
  7. require_once 'log.php';  
  8.   
  9. //初始化日志  
  10. $logHandlernew CLogFileHandler("logs/".date('Y-m-d').'.log');  
  11. $log = Log::Init($logHandler, 15);  
  12.   
  13. class PayNotifyCallBack extends WxPayNotify  
  14. {  
  15. function __construct()  
  16. {  
  17.   
  18.     // $hostname = "localhost";  
  19.     // $database = "cmsv";  
  20.     // $username = "root";  
  21.     // $password = "root";  
  22.   
  23.   
  24.     $conn = mysql_connect($hostname$username$passwordor trigger_error(mysql_error(),E_USER_ERROR);   
  25.     mysql_select_db($database$conn);  
  26. }  
  27. //查询订单  
  28. public function Queryorder($transaction_id)  
  29. {  
  30.     $input = new WxPayOrderQuery();  
  31.     $input->SetTransaction_id($transaction_id);  
  32.     $result = WxPayApi::orderQuery($input);  
  33.     Log::DEBUG("query:" . json_encode($result));  
  34.     if(array_key_exists("return_code"$result)  
  35.         && array_key_exists("result_code"$result)  
  36.         && $result["return_code"] == "SUCCESS"  
  37.         && $result["result_code"] == "SUCCESS")  
  38.     {  
  39.         return true;  
  40.     }  
  41.     return false;  
  42. }  
  43.   
  44. //重写回调处理函数  
  45. public function NotifyProcess($data, &$msg)  
  46. {  
  47.     Log::DEBUG("call back:" . json_encode($data));  
  48.     $notfiyOutput = array();  
  49.   
  50.     if(!array_key_exists("transaction_id"$data)){  
  51.         $msg = "输入参数不正确";  
  52.         return false;  
  53.     }  
  54.     //查询订单,判断订单真实性  
  55.     if(!$this->Queryorder($data["transaction_id"])){  
  56.         $msg = "订单查询失败";  
  57.         return false;  
  58.     }  
  59.     $order_sn = $data["out_trade_no"];  
  60.             $transaction_id = $data["transaction_id"];  
  61.     $amount = $data["total_fee"]/100;  
  62.     $query = "SELECT ...";  
  63.     $result = mysql_query($query);   
  64.     $row = mysql_fetch_array($result);  
  65.     $time = time();  
  66.     Log::DEBUG("v2_code_order:" . json_encode($row));  
  67.     if($amount == $row['amount']){  
  68.         $sql2 = "SELECT id,code_sn FROM ...";  
  69.         $re = mysql_query($sql2);   
  70.         $code = mysql_fetch_array($re);  
  71.         Log::DEBUG("code_ll:" . json_encode($code));  
  72.         $sql_code="UPDATE v2_code SET..";  
  73.         $code_up = mysql_query($sql_code);  
  74.   
  75.         $sql_order="UPDATE v2_code_order SET ...'";  
  76.         $order_up = mysql_query($sql_order);  
  77.         Log::DEBUG("code_up:" . json_encode($code_up));  
  78.         Log::DEBUG("order_up:" . json_encode($order_up));  
  79.   
  80.     }else{  
  81.         return false;  
  82.     }  
  83.   
  84.   
  85.     return true;  
  86. }  
  87. }  
  88.   
  89. Log::DEBUG("begin notify");  
  90. $notify = new PayNotifyCallBack();  
  91. $notify->Handle(false);

猜你喜欢

转载自blog.csdn.net/qq_39702981/article/details/79522751