1. 程式人生 > >ThinkPHP整合微信支付之發現金紅包

ThinkPHP整合微信支付之發現金紅包

大家好,微信支付系列教程四種方式已經結束,如果你以為結束了就錯了,有同學跟我提到微信還有紅包功能,我開始也沒注意這一塊,於是看了下微信商戶平臺上有講到這一塊,微信支付平臺上也早就有了,於是趁熱打鐵,研究了下,繼續發出關於微信紅包的教程文章。接下來請看微信支付發紅包之現金紅包教程!
現在微信商戶可以向指定的openid傳送紅包,目前紅包分兩種:現金紅包和裂變紅包。本教程是關於現金紅包的。

在貼程式碼之前,先講幾個注意點:1.去商戶平臺裡,給你的商戶充錢,沒錢是發不了紅包噠! 2.微信紅包需要證書支援,所以請大家到商戶平臺下去下載好證書後放到安全資料夾下,並且需要在配置檔案中指定好證書路徑!


step1:老樣子,還是介紹配置檔案WxPayConf_pub.php,看過之前微信支付教程的同學應該很清楚這一塊了,這裡我將程式碼截圖出來,配置好後進行下一步!


step2:下載你的證書,放到一個目錄下,對應配置檔案中,記得這裡是絕對路徑!


step3:之前的微信支付的demo微信官方已經幫我們寫好了WxPayHelper.php這個類庫,我們可以很方便的呼叫就夠了,而微信紅包目前還沒有官方demo,所以這裡我們得自己在WxPayHelper.php檔案下寫自己的紅包支付方法:
  1. /**
  2.  * 現金紅包介面
  3.  * @author gaoyl101
  4.  */
  5. class Redpack_pub extends Wxpay_client_pub
  6. {
  7.     var $code;//code碼,用以獲取openid
  8.     var $openid;//使用者的openid
  9.     
  10.     function __construct
    ()
  11.     {
  12.         //設定介面連結
  13.         $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
  14.         //設定curl超時時間
  15.         $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  16.     }
  17.     /**
  18.      * 生成介面引數xml
  19.      */
  20.     function createXml()
  21.     {
  22.         try
  23.         {
  24.             //檢測必填引數
  25.             if($this->parameters["mch_billno"] == null)
  26.             {
  27.                 throw new SDKRuntimeException("缺少發紅包介面必填引數mch_billno!"."<br>");
  28.             }elseif($this->parameters["nick_name"] == null){
  29.                 throw new SDKRuntimeException("缺少發紅包介面必填引數nick_name!"."<br>");
  30.             }elseif ($this->parameters["send_name"] == null ) {
  31.                 throw new SDKRuntimeException("缺少發紅包介面必填引數send_name!"."<br>");
  32.             }elseif ($this->parameters["total_amount"] == null) {
  33.                 throw new SDKRuntimeException("缺少發紅包介面必填引數total_amount!"."<br>");
  34.             }elseif($this->parameters["min_value"] == null){
  35.                 throw new SDKRuntimeException("缺少發紅包介面必填引數min_value!"."<br>");
  36.             }elseif ($this->parameters["max_value"] == null ) {
  37.                 throw new SDKRuntimeException("缺少發紅包介面必填引數max_value!"."<br>");
  38.             }elseif ($this->parameters["total_num"] == null) {
  39.                 throw new SDKRuntimeException("缺少發紅包介面必填引數total_num!"."<br>");
  40.             }elseif ($this->parameters["wishing"] == null) {
  41.                 throw new SDKRuntimeException("缺少發紅包介面必填引數wishing!"."<br>");
  42.             }elseif ($this->parameters["act_name"] == null) {
  43.                 throw new SDKRuntimeException("缺少發紅包介面必填引數act_name!"."<br>");
  44.             }elseif ($this->parameters["remark"] == null) {
  45.                 throw new SDKRuntimeException("缺少發紅包介面必填引數remark!"."<br>");
  46.             }
  47.             $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公眾賬號ID
  48.             $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號
  49.             $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//終端ip
  50.             $this->parameters["nonce_str"] = $this->createNoncestr();//隨機字串
  51.             $this->parameters["re_openid"] = $this->openid;//使用者openid
  52.             $this->parameters["sign"] = $this->getSign($this->parameters);//簽名
  53.             return  $this->arrayToXml($this->parameters);
  54.         }catch (SDKRuntimeException $e)
  55.         {
  56.             die($e->errorMessage());
  57.         }
  58.     }
  59.     
  60.     
  61.     function sendRedpack()
  62.     {
  63.         $this->postXmlSSL();
  64.         $this->result = $this->xmlToArray($this->response);
  65.         return $this->result;
  66.     }
  67.     
  68.     
  69.     
  70.     /**
  71.      *     作用:生成可以獲得code的url
  72.      */
  73.     function createOauthUrlForCode($redirectUrl)
  74.     {
  75.         $urlObj["appid"] = WxPayConf_pub::APPID;
  76.         $urlObj["redirect_uri"] = "$redirectUrl";
  77.         $urlObj["response_type"] = "code";
  78.         $urlObj["scope"] = "snsapi_base";
  79.         $urlObj["state"] = "STATE"."#wechat_redirect";
  80.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  81.         return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  82.     }
  83.     
  84.     
  85.     
  86.     /**
  87.      *     作用:生成可以獲得openid的url
  88.      */
  89.     function createOauthUrlForOpenid()
  90.     {
  91.         $urlObj["appid"] = WxPayConf_pub::APPID;
  92.         $urlObj["secret"] = WxPayConf_pub::APPSECRET;
  93.         $urlObj["code"] = $this->code;
  94.         $urlObj["grant_type"] = "authorization_code";
  95.         $bizString = $this->formatBizQueryParaMap($urlObj, false);
  96.         return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  97.     }
  98.     
  99.     /**
  100.      *     作用:通過curl向微信提交code,以獲取openid
  101.      */
  102.     function getOpenid()
  103.     {
  104.         $url = $this