1. 程式人生 > >使用php的ci框架,實現微信支付(jsapi方式+完整程式碼)

使用php的ci框架,實現微信支付(jsapi方式+完整程式碼)

看了一天的微信文件,花了一天的時候除錯問題,一共花了差不多2天時間,可算是把微信支付做出來了,在這裡把微信支付的開發過程記錄一下。

微信後臺配置

要注意的是支付授權目錄,應該填寫你實際支付頁面的上一級!

那麼你的授權目錄應該是:www.baidu.com/abc/order/

注意要帶上‘/’

1.  檔案準備

 由於我用的ci框架,在微信支付官方文件下載支付的demo,不是php的CI框架,所以並不適用。隨後在網上找到了一位熱心網友大神的程式碼,將官網的demo整合到了CI框架裡,連結http://www.cnblogs.com/24la/p/wxpay-jsapi-refund.html。感謝大神!大家可以看看連結中的說明,寫的很清楚。

這裡貼一下大佬封裝的api類,很好用!

class WechatPay {
    const TRADETYPE_JSAPI = 'JSAPI',TRADETYPE_NATIVE = 'NATIVE',TRADETYPE_APP = 'APP';
    const URL_UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    const URL_ORDERQUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
    const URL_CLOSEORDER = 'https://api.mch.weixin.qq.com/pay/closeorder';
    const URL_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
    const URL_REFUNDQUERY = 'https://api.mch.weixin.qq.com/pay/refundquery';
    const URL_DOWNLOADBILL = 'https://api.mch.weixin.qq.com/pay/downloadbill';
    const URL_REPORT = 'https://api.mch.weixin.qq.com/payitil/report';
    const URL_SHORTURL = 'https://api.mch.weixin.qq.com/tools/shorturl';
    const URL_MICROPAY = 'https://api.mch.weixin.qq.com/pay/micropay';
    /**
     * 錯誤資訊
     */
    public $error = null;
    /**
     * 錯誤資訊XML
     */
    public $errorXML = null;
    /**
     * 微信支付配置陣列
     * appid        公眾賬號appid
     * mch_id       商戶號
     * apikey       加密key
     * appsecret    公眾號appsecret
     * sslcertPath  證書路徑(apiclient_cert.pem)
     * sslkeyPath   金鑰路徑(apiclient_key.pem)
     */
    private $_config;
    /**
     * @param $config 微信支付配置陣列
     */
    public function __construct($config) {
        $this->_config = $config;
    }
    /**
     * JSAPI獲取prepay_id
     * @param $body
     * @param $out_trade_no
     * @param $total_fee
     * @param $notify_url
     * @param $openid
     * @return null
     */
    public function getPrepayId($body,$out_trade_no,$total_fee,$notify_url,$openid) {
        $data = array();
        $data["nonce_str"]    = $this->get_nonce_string();
        $data["body"]         = $body;
        $data["out_trade_no"] = $out_trade_no;
        $data["total_fee"]    = $total_fee;
        $data["spbill_create_ip"] = $_SERVER["REMOTE_ADDR"];
        $data["notify_url"]   = $notify_url;
        $data["trade_type"]   = self::TRADETYPE_JSAPI;
        $data["openid"]   = $openid;
        $result = $this->unifiedOrder($data);
        if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
            return $result["prepay_id"];
        } else {
            $this->error = $result["return_code"] == "SUCCESS" ? $result["err_code_des"] : $result["return_msg"];
            $this->errorXML = $this->array2xml($result);
            return null;
        }
    }
    private function get_nonce_string() {
        return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"),0,32);
    }
    /**
     * 統一下單介面
     */
    public function unifiedOrder($params) {
        $data = array();
        $data["appid"] = $this->_config["appid"];
        $data["mch_id"] = $this->_config["mch_id"];
        $data["device_info"] = (isset($params['device_info'])&&trim($params['device_info'])!='')?$params['device_info']:null;
        $data["nonce_str"] = $this->get_nonce_string();
        $data["body"] = $params['body'];
        $data["detail"] = isset($params['detail'])?$params['detail']:null;//optional
        $data["attach"] = isset($params['attach'])?$params['attach']:null;//optional
        $data["out_trade_no"] = isset($params['out_trade_no'])?$params['out_trade_no']:null;
        $data["fee_type"] = isset($params['fee_type'])?$params['fee_type']:'CNY';
        $data["total_fee"]    = $params['total_fee'];
        $data["spbill_create_ip"] = $params['spbill_create_ip'];
        $data["time_start"] = isset($params['time_start'])?$params['time_start']:null;//optional
        $data["time_expire"] = isset($params['time_expire'])?$params['time_expire']:null;//optional
        $data["goods_tag"] = isset($params['goods_tag'])?$params['goods_tag']:null;
        $data["notify_url"] = $params['notify_url'];
        $data["trade_type"] = $params['trade_type'];
        $data["product_id"] = isset($params['product_id'])?$params['product_id']:null;//required when trade_type = NATIVE
        $data["openid"] = isset($params['openid'])?$params['openid']:null;//required when trade_type = JSAPI
        $result = $this->post(self::URL_UNIFIEDORDER, $data);
        return $result;
    }
    private function post($url, $data,$cert = false) {
        $data["sign"] = $this->sign($data);
        $xml = $this->array2xml($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        if($cert == true){
            //使用證書:cert 與 key 分別屬於兩個.pem檔案
            curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
            curl_setopt($ch,CURLOPT_SSLCERT, $this->_config['sslcertPath']);
            curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
            curl_setopt($ch,CURLOPT_SSLKEY, $this->_config['sslkeyPath']);
        }
        $content = curl_exec($ch);
        $array = $this->xml2array($content);
        return $array;
    }
    /**
     * 資料簽名
     * @param $data
     * @return string
     */
    private function sign($data) {
        ksort($data);
        $string1 = "";
        foreach ($data as $k => $v) {
            if ($v && trim($v)!='') {
                $string1 .= "$k=$v&";
            }
        }
        $stringSignTemp = $string1 . "key=" . $this->_config["apikey"];
        $sign = strtoupper(md5($stringSignTemp));
        return $sign;
    }
    private function array2xml($array) {
        $xml = "<xml>" . PHP_EOL;
        foreach ($array as $k => $v) {
            if($v && trim($v)!='')
                $xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL;
        }
        $xml .= "</xml>";
        return $xml;
    }
    private function xml2array($xml) {
        $array = array();
        $tmp = null;
        try{
            $tmp = (array) simplexml_load_string($xml);
        }catch(Exception $e){}
        if($tmp && is_array($tmp)){
            foreach ( $tmp as $k => $v) {
                $array[$k] = (string) $v;
            }
        }
        return $array;
    }
    /**
     * 掃碼支付(模式二)獲取支付二維碼
     * @param $body
     * @param $out_trade_no
     * @param $total_fee
     * @param $notify_url
     * @param $product_id
     * @return null
     */
    public function getCodeUrl($body,$out_trade_no,$total_fee,$notify_url,$product_id){
        $data = array();
        $data["nonce_str"]    = $this->get_nonce_string();
        $data["body"]         = $body;
        $data["out_trade_no"] = $out_trade_no;
        $data["total_fee"]    = $total_fee;
        $data["spbill_create_ip"] = $_SERVER["SERVER_ADDR"];
        $data["notify_url"]   = $notify_url;
        $data["trade_type"]   = self::TRADETYPE_NATIVE;
        $data["product_id"]   = $product_id;
        $result = $this->unifiedOrder($data);
        if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
            return $result["code_url"];
        } else {
            $this->error = $result["return_code"] == "SUCCESS" ? $result["err_code_des"] : $result["return_msg"];
            return null;
        }
    }
    /**
     * 查詢訂單
     * @param $transaction_id
     * @param $out_trade_no
     * @return array
     */
    public function orderQuery($transaction_id,$out_trade_no){
        $data = array();
        $data["appid"] = $this->_config["appid"];
        $data["mch_id"] = $this->_config["mch_id"];
        $data["transaction_id"] = $transaction_id;
        $data["out_trade_no"] = $out_trade_no;
        $data["nonce_str"] = $this->get_nonce_string();
        $result = $this->post(self::URL_ORDERQUERY, $data);
        return $result;
    }
    /**
     * 關閉訂單
     * @param $out_trade_no
     * @return array
     */
    public function closeOrder($out_trade_no){
        $data = array();
        $data["appid"] = $this->_config["appid"];
        $data["mch_id"] = $this->_config["mch_id"];
        $data["out_trade_no"] = $out_trade_no;
        $data["nonce_str"] = $this->get_nonce_string();
        $result = $this->post(self::URL_CLOSEORDER, $data);
        return $result;
    }
    /**
     * 申請退款 - 使用商戶訂單號
     * @param $out_trade_no 商戶訂單號
     * @param $out_refund_no 退款單號
     * @param $total_fee 總金額(單位:分)
     * @param $refund_fee 退款金額(單位:分)
     * @param $op_user_id 操作員賬號
     * @return array
     */
    public function refund($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$op_user_id){
        $data = array();
        $data["appid"] = $this->_config["appid"];
        $data["mch_id"] = $this->_config["mch_id"];
        $data["nonce_str"] = $this->get_nonce_string();
        $data["out_trade_no"] = $out_trade_no;
        $data["out_refund_no"] = $out_refund_no;
        $data["total_fee"] = $total_fee;
        $data["refund_fee"] = $refund_fee;
        $data["op_user_id"] = $op_user_id;
        $result = $this->post(self::URL_REFUND, $data,true);
        return $result;
    }
    /**
     * 申請退款 - 使用微信訂單號
     * @param $out_trade_no 商戶訂單號
     * @param $out_refund_no 退款單號
     * @param $total_fee 總金額(單位:分)
     * @param $refund_fee 退款金額(單位:分)
     * @param $op_user_id 操作員賬號
     * @return array
     */
    public function refundByTransId($transaction_id,$out_refund_no,$total_fee,$refund_fee,$op_user_id){
        $data = array();
        $data["appid"] = $this->_config["appid"];
        $data["mch_id"] = $this->_config["mch_id"];
        $data["nonce_str"] = $this->get_nonce_string();
        $data["transaction_id"] = $transaction_id;
        $data["out_refund_no"] = $out_refund_no;
        $data["total_fee"] = $total_fee;
        $data["refund_fee"] = $refund_fee;
        $data["op_user_id"] = $op_user_id;
        $result = $this->post(self::URL_REFUND, $data,true);
        return $result;
    }
    /**
     * 下載對賬單
     * @param $bill_date 下載對賬單的日期,格式:20140603
     * @param $bill_type 型別
     * @return array
     */
    public function downloadBill($bill_date,$bill_type = 'ALL'){
        $data = array();
        $data["appid"] = $this->_config["appid"];
        $data["mch_id"] = $this->_config["mch_id"];
        $data["bill_date"] = $bill_date;
        $data["bill_type"] = $bill_type;
        $data["nonce_str"] = $this->get_nonce_string();
        $result = $this->post(self::URL_DOWNLOADBILL, $data);
        return $result;
    }
    /**
     * 獲取js支付使用的第二個引數
     */
    public function get_package($prepay_id) {
        $data = array();
        $data["appId"] = $this->_config["appid"];
        $data["timeStamp"] = time();
        $data["nonceStr"]  = $this->get_nonce_string();
        $data["package"]   = "prepay_id=$prepay_id";
        $data["signType"]  = "MD5";
        $data["paySign"]   = $this->sign($data);
        return $data;
    }
    /**
     * 獲取傳送到通知地址的資料(在通知地址內使用)
     * @return 結果陣列,如果不是微信伺服器傳送的資料返回null
     *          appid
     *          bank_type
     *          cash_fee
     *          fee_type
     *          is_subscribe
     *          mch_id
     *          nonce_str
     *          openid
     *          out_trade_no    商戶訂單號
     *          result_code
     *          return_code
     *          sign
     *          time_end
     *          total_fee       總金額
     *          trade_type
     *          transaction_id  微信支付訂單號
     */
    public function get_back_data() {
        $xml = file_get_contents("php://input");
        $data = $this->xml2array($xml);
        if ($this->validate($data)) {
            return $data;
        } else {
            return null;
        }
    }
    /**
     * 驗證資料簽名
     * @param $data 資料陣列
     * @return 資料校驗結果
     */
    public function validate($data) {
        if (!isset($data["sign"])) {
            return false;
        }
        $sign = $data["sign"];
        unset($data["sign"]);
        return $this->sign($data) == $sign;
    }
    /**
     * 響應微信支付後臺通知
     * @param $return_code 返回狀態碼 SUCCESS/FAIL
     * @param $return_msg  返回資訊
     */
    public function response_back($return_code="SUCCESS", $return_msg=null) {
        $data = array();
        $data["return_code"] = $return_code;
        if ($return_msg) {
            $data["return_msg"] = $return_msg;
        }
        $xml = $this->array2xml($data);
        print $xml;
    }
}


根據文章,需要對檔案做一下改動:

1.      新建一個類檔案Wechatpay.php 放到application\libraries裡

2.      將證書,日誌之類的檔案放到wechatpay.php 統計目錄下

3.      新建配置檔案wxpay_config.php 儲存商戶號、appid、AppSecret、API key。將該檔案放到application\config目錄下

2.  控制器裡呼叫

連結裡有講的十分詳細該怎麼寫。

其中有關於openid,因為我之前有獲取過openid並把它放到session,所以在呼叫的時候直接從session獲取就行了,十分方便。大家也可是嘗試這種方法,

tips:在開發中遇到了一個問題,做到統一下單的時候,微信返回的報錯資訊是許可權不足,請到商戶後臺配置。網上搜了下,很多人都遇到過這種問題,百度了一圈也沒有實際解決方案。

最後,要吐槽一下騰訊!花了半天時間確認商戶後臺配置沒問題,各個引數都沒有問題,結果還是失敗返回許可權不足。結果聯絡騰訊,給了各個資料對方說稍等,半個小時後後就沒問題了!