微信支付JSAPI模式及退款CodeIgniter集成篇


微信支付接口文檔:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1

首先你得知道這個jsapi是不能離開微信進行調用支付的,明白了這個道理我們好下手,頁面是在微信內顯示並通過jsapi調用微信支付組件進行支付。

可以看看我們上一篇文章,主要是Native掃碼支付模式二

我們仍然繼續使用wechatpay.php這個支付集成類,簡單方便好理解,不過如果應用jsapi的話這個類有個bug

在我們構造jsapi需要的參數時有個時間戳,我們用time()生成的,會報微信支付調用JSAPI缺少參數:timeStamp

修改如下:

	/**
	 * 獲取js支付使用的第二個參數
	 */
	public function get_package($prepay_id) {
		$data = array();
		$data["appId"] = $this->_config["appid"];
          //改動地方,把它變成字符串
		$time=time();
		$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;
	}

 其實這個方法就是獲取jsapi的支付參數了

一、微信JSAPI支付

不能忘記配置授權目錄,調用jsapi我是在http://xxx.com/index.php/home下我配置了這個

首先我們還是要調用統一下單接口,獲取我們要的參數(如果此類的配置放置位置等不會的請參考上篇文章),此為pay方法,在調用統一下單接口的時候我們需要知道需要哪些參數

1、要獲取openid,這個我是項目用了一個微信API的類庫,https://github.com/dodgepudding/wechat-php-sdk,主要是用了這里面的方法

此項目有朋友專門的對接了CodeIgniter框架的擴展類庫,可以直接用,目錄結構,我們直接上代碼吧

    public function __construct()
    {
        parent::__construct();
        $this->load->library('CI_Wechat');//由於我的項目是時刻都跟微信綁在一起,所以直接加載在構造函數里了,不用每個方法都加載了。
        $this->load->library('pagination');
    }

 CI_Model內容大家看下上面的類庫源碼,還有里面如何配置的,下面我們看看如何獲取openid

 

    function oauthurl()
    {
        $oauth_url = $this->ci_wechat->getOauthRedirect(base_url() . 'index.php/home/oauth', 1);
        header('Location: ' . $oauth_url);
        exit();
    }

    function oauth()
    {
        if (!isset($_GET['code'])) {

            //觸發微信返回code碼
            $baseUrl = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING']);
            $url = $this->__CreateOauthUrlForCode($baseUrl);
            Header("Location: $url");
            exit();

        } else {
            $json = $this->ci_wechat->getOauthAccessToken();
            $openid = $json['openid'];
            //注冊用戶,成功后可以搶單
            //return $this->_isRegistered($_SESSION['user']['openid']);
            return $openid;

        }
    }

 以上兩個方法就是獲取openid的,獲取之后我是保存在session里的,我每個頁面都判斷是否獲取了openid如果沒有獲取直接

$this->session->set_userdata('openid', $this->oauth());

 這樣保證一直能得到openid

2、構造JSAPI支付所需參數(統一下單的參數構造)

                $this->load->model('publist');//獲取訂單信息
                $pub = $this->publist->GetList(array('id' => $_SESSION['orderid']));
                //微信支付配置的參數配置讀取
                $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);
                //商戶交易單號
                $out_trade_no = $pub->listno;
                $total_fee=$pub->fee;
                $openid=$_SESSION['openid'];
                $param['body']="黑人牙膏";
                $param['attach']=$pub->id;
                $param['detail']="黑人牙膏-".$out_trade_no;
                $param['out_trade_no']=$out_trade_no;
                $param['total_fee']=$total_fee*100;
                $param["spbill_create_ip"] =$_SERVER['REMOTE_ADDR'];
                $param["time_start"] = date("YmdHis");
                $param["time_expire"] =date("YmdHis", time() + 600);
                $param["goods_tag"] = "黑人牙膏";
                $param["notify_url"] = base_url()."index.php/home/notify";
                $param["trade_type"] = "JSAPI";
                $param["openid"] = $openid;

                //統一下單,獲取結果,結果是為了構造jsapi調用微信支付組件所需參數
                $result=$this->wechatpay->unifiedOrder($param);
                
                //如果結果是成功的我們才能構造所需參數,首要判斷預支付id

                if (isset($result["prepay_id"]) && !empty($result["prepay_id"])) {
                    //調用支付類里的get_package方法,得到構造的參數
                    $data['parameters']=json_encode($this->wechatpay->get_package($result['prepay_id']));
                    $data['notifyurl']=$param["notify_url"];
                    $data['fee']=$total_fee;
                    $data['pubid']=$_SESSION['orderid'];

                    $this->load->view('home/header');
                    //要有個頁面將以上數據傳遞過去並展示給用戶
                    $this->load->view('home/pay', $data);
                    $this->load->view('home/footer');
                }        

 3、支付頁面,views視圖pay.php

<?php
$jsApiParameters = $parameters;//參數賦值
?>

<script type="text/javascript">
    //調用微信JS api 支付
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            <?php echo $jsApiParameters; ?>,
            function(res){
                WeixinJSBridge.log(res.err_msg);
                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    $.alert('支付成功');
                    //我在這里選擇了前台只要支付成功將單號傳遞更新數據
                    $.ajax({
                        url:'<?php  echo $notifyurl.'/'.$pubid;?>',
                        dataType:'json',
                        success : function(ret){
                            if(ret==1){
                                //成功后返回我的訂單頁面
                                location.href="<?php echo base_url().'index.php/home/myorder';?>";
                            }
                        }
                    });
                }else
                {
                    //$.alert('支付失敗');
                }
                //alert(res.err_code+res.err_desc+res.err_msg);
            }
        );
    }

    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>
<div class="hd">
    <h1 class="page_title">支付佣金</h1>
    <p class="page_desc">請認真核對佣金金額</p>
</div>
<div class="weui_cells">
    <div class="weui_cell">
        <div class="weui_cell_hd weui_cell_primary">
            該筆訂單支付金額為<span style="color:#f00;font-size:50px"><?php echo $fee; ?></span>元錢
        </div>
    </div>
</div>
<button class="weui_btn weui_btn_primary" type="button" onclick="callpay()" >立即支付</button>

 以上代碼可以用微信web開發者工具,使用方式自己看看吧,有了這個工具調試不再難

4、支付成功跳轉頁面,我們看notify方法

    function notify()
    {
        $id = $this->uri->segment(3);
        if (isset($_SESSION['openid'])) {
            $this->load->model('publist');//更新業務邏輯
            $rs = $this->publist->UpdateList(array('id' => $id, 'feestatus' => 1));
            if ($rs > 0) {
                echo 1;
                exit;
            } else {
                echo 0;
                exit;
            }
        }
    }

 這樣我們的支付流程就徹底走完了。

二、當我們支付完之后,有些單子可以退單的,如何將款項也退回呢

以上場景要弄明白了

我們申請退款需要參數有哪些?我們看看支付類里的退款方法

	/**
	 * 申請退款 - 使用商戶訂單號
	 * @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;
	}

 商戶訂單號,商戶提供的退單號,付款金額,退款金額(不能退的比實際付款的多),操作員(一般商戶號)

控制器內寫退款方法

    //申請退款
    function refund($id="")
    {
        if($id==""){
            //方便我手動調用退單
            $id = $this->uri->segment(3);
        }
        if (isset($id) && $id != "") {
            $this->load->model('publist');
            //1、取消訂單可以退款。2、失敗訂單可以退款
            $pub = $this->publist->GetList(array('id' => $id));
            if ($pub->liststatus == 3 || $pub->liststatus == 4) {
                $listno = $pub->listno;
                $fee = $pub->fee * 100;

                $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);

                if (isset($listno) && $listno != "") {
                    $out_trade_no = $listno;
                    $total_fee = $fee;
                    $refund_fee = $fee;
                    //自定義商戶退單號
                    $out_refund_no=$wxconfig['mch_id'].date("YmdHis");
                        $result=$this->wechatpay->refund($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$wxconfig['mch_id']);

                    log::DEBUG(json_encode($result));
                    if (isset($result["return_code"]) && $result["return_code"]="SUCCESS"&&isset($result["result_code"]) && $result["result_code"]="SUCCESS") {
                        echo "<script>$.toast('退款成功')</script>";
                    }
                    //佣金狀態更改為已退款
                    $this->publist->UpdateList(array('id'=>$id,'liststatus'=>3,'listoutno'=>$out_refund_no));
                    redirect('home/myorder');
                }
            }
        }
    }

 試試就好了,很快就可以接到退款消息

以上是這幾天摸索出來的東西,分享給大家。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM