CodeIgniter微信扫码支付(模式二)

官方demo:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1

1、首先下载官方demo,cert/、logs/ 、example/log.php放在libraries/Wxpay/目录下

2、在ci配置文件config/ 下新建 wxpay_config.php 并完成配置。

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

	/**
	 * TODO: 修改这里配置为您自己申请的商户信息
	 * 微信公众号信息配置
	 * 
	 * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
	 * 
	 * MCHID:商户号(必须配置,开户邮件中可查看)
	 * 
	 * KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
	 * 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
	 * 
	 * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
	 * 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
	 * @var string
	 */

	$config['appid'] = '';
	 
	$config['mch_id'] = '';
	 
	$config['apikey'] = '';
	 
	$config['appsecret'] = '';
	 
	$config['sslcertPath'] =  APPPATH.'libraries/Wxpay/cert/apiclient_cert.pem';
	 
	$config['sslkeyPath'] = APPPATH.'libraries/Wxpay/cert/apiclient_key.pem';

3、在libraries/ 下新建 CI_Wechatpay.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// 加载微信支付
require_once APPPATH.'libraries/Wxpay/Wechatpay.php';

/**
 * 为CI扩展微信支付类
 */
class CI_Wechatpay extends WechatPay {

}

4、在libraries/Wxpay 下新建Wechatpay.php   这是前辈封装好的。尽情的享用吧

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

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;
    }
}

5、接下来我是在控制器 controller/Wxpay.php 文件下封装个方法   这里也用到了log.php ,注意看使用方法。在调bug时非常好用。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Wxpay extends CI_Controller {  /**   * Index Page for this controller.   *   * Maps to the following URL   *  http://example.com/index.php/welcome   * - or -   *  http://example.com/index.php/welcome/index   * - or -   * Since this controller is set as the default controller in   * config/routes.php, it's displayed at http://example.com/   *   * So any other public methods not prefixed with an underscore will   * map to /index.php/welcome/<method_name>   * @see https://codeigniter.com/user_guide/general/urls.html   */  public function __construct()
    {
        parent::__construct();
        $this->load->config('config');
        $this->load->helper('url');
        $this->load->model('Consult_model');
        $this->load->model('Payment_model');
    }  public function index()  {  $base_url = $this->config->item('base_url');  $to_id = "26";  $consult_info = $this->Consult_model->consult_info(array('consult_id'=>$to_id));  print_r($consult_info);  }          /**
         * wxPay 微信支付接口
         * @param $Id 订单ID
         * @author lyne
         */
        public function wxPay(){
         $base_url = $this->config->item('base_url');
            require_once (APPPATH.'libraries/Wxpay/log.php');
            //初始化日志
            $logHandler= new CLogFileHandler(APPPATH."libraries/Wxpay/logs/".date('Y-m-d').'.log');
            Log::Init($logHandler, 15);

            
            // 调用微信扫码支付接口配置信息
            $this->load->config('wxpay_config');
            $wxconfig['appid']=$this->config->item('appid');
            $wxconfig['mch_id']=$this->config->item('mch_id');
            $wxconfig['apikey']=$this->config->item('apikey');
            $wxconfig['appsecret']=$this->config->item('appsecret');
            $wxconfig['sslcertPath']=$this->config->item('sslcertPath');
            $wxconfig['sslkeyPath']=$this->config->item('sslkeyPath');
            //由于此类库构造函数需要传参,我们初始化类库就传参数给他吧
            $this->load->library('CI_Wechatpay',$wxconfig);

            $o = array('goods_name'=>$_GET['goods_name'],'id'=>$_GET['id'],'order_code'=>$_GET['order_code'],'order_amount'=>$_GET['order_amount'],'goods_id'=>$_GET['goods_id']);
            $param['body']=$o['goods_name']; //"商品名称(自行看文档具体填什么)";
            $param['attach']=$o['id']; // "我有个参数要传我就穿了个id过来,这里不要有空格避免出错";
            $param['detail']=$o['order_code'];  //"我填了商品名称加订单号";
            $param['out_trade_no']=$o['order_code']; //"商户订单号";
            $param['total_fee']=$o['order_amount']*100; //"金额,记得乘以100,微信支付单位默认分";//如$total_fee*100
            $param["spbill_create_ip"] =$_SERVER['REMOTE_ADDR'];//客户端IP地址
            $param["time_start"] = date("YmdHis");//请求开始时间
            $param["time_expire"] = date("YmdHis", time() + 600);//请求超时时间 10分钟
            $param["goods_tag"] = urldecode('运费:') . "0"; //商品标签,自行填写
            $param["notify_url"] = $base_url."wxpay/wxnotify"; //自行定义异步通知url
            $param["trade_type"] = "NATIVE";//扫码支付模式二
            $param["product_id"] = $o['goods_id']; //正好有产品id就传了个,看文档说自己定义
            //调用统一下单API接口
            $result=$this->ci_wechatpay->unifiedOrder($param);//这里可以加日志输出,
            // 写入日志
            log::debug(json_encode($result));
            //成功(return_code和result_code都为SUCCESS)就会返回含有带支付二维码链接的数据
            $data=array();
            if (isset($result["code_url"]) && !empty($result["code_url"])) {//二维码图片链接
                $data['wxurl'] = $result["code_url"];
                //这里传递商户订单号到扫码视图,是因为我想做跳转,根据商户号去查询订单是否支付成功,如果成功了就跳转,定时轮询微信服务器(这个谁有好的方法可以分享给我啊,表示感谢啦)
                $data['orderno'] = $param['out_trade_no'];
                // 写入日志
                
                
                echo 'http://paysdk.weixin.qq.com/example/qrcode.php?data='.urlencode($data['wxurl']);
                //echo '<img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data='.urlencode($data['wxurl']).'" width="150"/>';
                /*$this->assign('time_expire',strtotime($param["time_expire"])); //订单失效时间
                $this->assign('order_no',$data['orderno']);
                $this->assign('wxurl',urlencode($data['wxurl'])); // 获取到的二维码地址
                $this->display('wxpay/index.html');*/
            }
        }
        //微信异步通知
    function wxnotify(){
//$postStr = file_get_contents("php://input");//因为很多都设置了register_globals禁止,不能用$GLOBALS["HTTP_RAW_POST_DATA']     //这部分困扰了好久用上面这种一直接受不到数据,或者接受了解析不正确,最终用下面的正常了,有哪位愿意指点的可以告知一二
        //$xml = $GLOBALS['HTTP_RAW_POST_DATA'];//这个需要开启;always_populate_raw_post_data = On
        $xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
        $this->load->config('wxpay_config');
        $wxconfig['appid']=$this->config->item('appid');
        $wxconfig['mch_id']=$this->config->item('mch_id');
        $wxconfig['apikey']=$this->config->item('apikey');
        $wxconfig['appsecret']=$this->config->item('appsecret');
        $wxconfig['sslcertPath']=$this->config->item('sslcertPath');
        $wxconfig['sslkeyPath']=$this->config->item('sslkeyPath');
        //$this->load->library('Wechatpay',$wxconfig);
        $this->load->library('CI_Wechatpay',$wxconfig);
        
        libxml_disable_entity_loader(true);
        $array= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        //log::debug($xml);
        //log::debug(json_encode($array));
        if($array!=null){
            $out_trade_no = $array['out_trade_no'];
            $total_fee = $array['total_fee'];
            
           
            $arr = explode("to",$out_trade_no);      $to_id = $arr[1];      $service_id = $arr[2];      if($service_id==1){       $consult_info = $this->Consult_model->consult_info(array('consult_id'=>$to_id));       $this->Payment_model->add_payment(array('user_id'=>$consult_info['user_id'],'service_id'=>$service_id,'to_id'=>$to_id,'payment_type'=>"微信",'payment_price'=>$total_fee,'payment_time'=>time()));       if($this->Consult_model->edit_consult(array('state'=>0),$to_id)){       $this->ci_wechatpay->response_back();       }else{       $this->ci_wechatpay->response_back("FAIL");//告知微信我失败了继续发       }       //echo '<script>window.location.href="'.site_url('Patient/consult_info').'?consult_id='.$to_id.'"</script>';      }
        }
    }
}

发表评论

邮箱地址不会被公开。 必填项已用*标注