最近在微信H5頁面內集成微信JSAPI支付,遇到很多問題,現將集成步驟及遇到的問題記錄以下:php
1.官方下載SDK,下載地址:https://pay.weixin.qq.com/wiki/doc/api/download/WxpayAPI_php_v3.zipjson
2.下載以後,只保留存放證書的文件cert(從微信公衆平臺「API安全」中下載),庫文件lib,日誌文件logs,以及example裏面的notify.php,jsapi.php文件api
3.配置WxPay.Config.php文件(注意:商戶支付密鑰key是在微信支付平臺「API安全」中設置,地址:https://pay.weixin.qq.com/index.php/account/api_cert,公衆帳號secret即APPSECRET是在微信公衆平臺中設置,地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN)。安全
4.進入微信公衆平臺,設置微信支付受權目錄(通常指的是發起微信支付的上一級目錄)及網頁受權(開發者中心-》接口權限-》網頁受權,填寫訪問域名便可)。微信
5.構建微信支付地址app
private $snsapi_base_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'; $param = array( 'appid' => '你的APPID', 'redirect_uri' => '回調地址', //地址指向官方文檔中的jsapi.php.做用:調用微信客服端,發起支付。 'response_type' => 'code', 'scope' => 'snsapi_base', 'state' => ’訂單號‘ ); $url = $this->snsapi_base_url.http_build_query($param).'#wechat_redirect';
6.打開jsapi.php(注意:從這裏開始,微信官方文檔就開始坑了)微信公衆平臺
//注意事項 //1.獲取用戶openid 官方文檔的寫法以下 $tools = new JsApiPay(); $openId = $tools->GetOpenid(); //改爲以下 $tools = new JsApiPay(); $openId = $tools->GetOpenidFromMp($_GET['code']); //若是是liunx系統,注意區分大小寫 (官方都是不區分大小寫的) //2. $input = new WxPayUnifiedOrder(); $input->SetBody("test"); //商品描述 $input->SetAttach("test"); //附加信息 $input->SetOut_trade_no($order_sn); //商品訂單號 $input->SetTotal_fee("1"); //商品費用 注意:以’分‘爲單位 $input->SetTime_start(date("YmdHis")); //$input->SetTime_expire(date("YmdHis", time() + 600)); 直接去掉吧 $input->SetGoods_tag("test"); //商品標記 $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php"); //通知地址,官方文檔中的notify.php,做用:處理支付成功後的訂單狀態及相關信息。 $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input);
//3.注意引用文件的路徑
7.打開notify.php函數
1、將官方文檔中的Log::所有改爲Logwx::(由於官方定義的類是Logwx,但在這裏引用的時候變成了Log,坑啊)。微信支付
2、注意引用文件的路徑(注:下載的官方文檔,須要更改路徑的文件:jsapi.php,notify.php,WxPay.Config.php)。ui
3、
<?php ini_set('date.timezone','Asia/Shanghai'); error_reporting(E_ERROR); require_once "lib/WxPay.Api.php"; require_once 'lib/WxPay.Notify.php'; require_once 'log.php'; //初始化日誌 $logHandler= new CLogFileHandler("logs/".date('Y-m-d').'.log'); $log = Logwx::Init($logHandler, 15); class PayNotifyCallBack extends WxPayNotify { //查詢訂單 public function Queryorder($transaction_id) { $input = new WxPayOrderQuery(); $input->SetTransaction_id($transaction_id); $result = WxPayApi::orderQuery($input); Logwx::DEBUG("query:" . json_encode($result)); if(array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") { return true; } return false; } //重寫回調處理函數 public function NotifyProcess($data, &$msg) { Logwx::DEBUG("call back:" . json_encode($data)); $notfiyOutput = array(); if(!array_key_exists("transaction_id", $data)){ $msg = "輸入參數不正確"; return false; } //查詢訂單,判斷訂單真實性 if(!$this->Queryorder($data["transaction_id"])){ $msg = "訂單查詢失敗"; return false; } //經過$data['out_trade_no'],在這裏處理訂單狀態 return true; } } $xml = $GLOBALS['HTTP_RAW_POST_DATA']; //微信返回的數據,格式(XML) Logwx::DEBUG("begin notify"); Logwx::DEBUG("xml:".$xml); $notify = new PayNotifyCallBack(); $notify->Handle(false);