ThinkPHP整合微信支付之發現金紅包
阿新 • • 發佈:2018-11-08
大家好,微信支付系列教程四種方式已經結束,如果你以為結束了就錯了,有同學跟我提到微信還有紅包功能,我開始也沒注意這一塊,於是看了下微信商戶平臺上有講到這一塊,微信支付平臺上也早就有了,於是趁熱打鐵,研究了下,繼續發出關於微信紅包的教程文章。接下來請看微信支付發紅包之現金紅包教程!
現在微信商戶可以向指定的openid傳送紅包,目前紅包分兩種:現金紅包和裂變紅包。本教程是關於現金紅包的。
在貼程式碼之前,先講幾個注意點:1.去商戶平臺裡,給你的商戶充錢,沒錢是發不了紅包噠! 2.微信紅包需要證書支援,所以請大家到商戶平臺下去下載好證書後放到安全資料夾下,並且需要在配置檔案中指定好證書路徑!
step1:老樣子,還是介紹配置檔案WxPayConf_pub.php,看過之前微信支付教程的同學應該很清楚這一塊了,這裡我將程式碼截圖出來,配置好後進行下一步!
step2:下載你的證書,放到一個目錄下,對應配置檔案中,記得這裡是絕對路徑!
step3:之前的微信支付的demo微信官方已經幫我們寫好了WxPayHelper.php這個類庫,我們可以很方便的呼叫就夠了,而微信紅包目前還沒有官方demo,所以這裡我們得自己在WxPayHelper.php檔案下寫自己的紅包支付方法:
現在微信商戶可以向指定的openid傳送紅包,目前紅包分兩種:現金紅包和裂變紅包。本教程是關於現金紅包的。
在貼程式碼之前,先講幾個注意點:1.去商戶平臺裡,給你的商戶充錢,沒錢是發不了紅包噠! 2.微信紅包需要證書支援,所以請大家到商戶平臺下去下載好證書後放到安全資料夾下,並且需要在配置檔案中指定好證書路徑!
step1:老樣子,還是介紹配置檔案WxPayConf_pub.php,看過之前微信支付教程的同學應該很清楚這一塊了,這裡我將程式碼截圖出來,配置好後進行下一步!
step2:下載你的證書,放到一個目錄下,對應配置檔案中,記得這裡是絕對路徑!
step3:之前的微信支付的demo微信官方已經幫我們寫好了WxPayHelper.php這個類庫,我們可以很方便的呼叫就夠了,而微信紅包目前還沒有官方demo,所以這裡我們得自己在WxPayHelper.php檔案下寫自己的紅包支付方法:
- /**
- * 現金紅包介面
- * @author gaoyl101
- */
- class Redpack_pub extends Wxpay_client_pub
- {
- var $code;//code碼,用以獲取openid
- var $openid;//使用者的openid
- function __construct
- {
- //設定介面連結
- $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
- //設定curl超時時間
- $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
- }
- /**
- * 生成介面引數xml
- */
- function createXml()
- {
- try
- {
- //檢測必填引數
- if($this->parameters["mch_billno"] == null)
- {
- throw new SDKRuntimeException("缺少發紅包介面必填引數mch_billno!"."<br>");
- }elseif($this->parameters["nick_name"] == null){
- throw new SDKRuntimeException("缺少發紅包介面必填引數nick_name!"."<br>");
- }elseif ($this->parameters["send_name"] == null ) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數send_name!"."<br>");
- }elseif ($this->parameters["total_amount"] == null) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數total_amount!"."<br>");
- }elseif($this->parameters["min_value"] == null){
- throw new SDKRuntimeException("缺少發紅包介面必填引數min_value!"."<br>");
- }elseif ($this->parameters["max_value"] == null ) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數max_value!"."<br>");
- }elseif ($this->parameters["total_num"] == null) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數total_num!"."<br>");
- }elseif ($this->parameters["wishing"] == null) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數wishing!"."<br>");
- }elseif ($this->parameters["act_name"] == null) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數act_name!"."<br>");
- }elseif ($this->parameters["remark"] == null) {
- throw new SDKRuntimeException("缺少發紅包介面必填引數remark!"."<br>");
- }
- $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公眾賬號ID
- $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號
- $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//終端ip
- $this->parameters["nonce_str"] = $this->createNoncestr();//隨機字串
- $this->parameters["re_openid"] = $this->openid;//使用者openid
- $this->parameters["sign"] = $this->getSign($this->parameters);//簽名
- return $this->arrayToXml($this->parameters);
- }catch (SDKRuntimeException $e)
- {
- die($e->errorMessage());
- }
- }
- function sendRedpack()
- {
- $this->postXmlSSL();
- $this->result = $this->xmlToArray($this->response);
- return $this->result;
- }
- /**
- * 作用:生成可以獲得code的url
- */
- function createOauthUrlForCode($redirectUrl)
- {
- $urlObj["appid"] = WxPayConf_pub::APPID;
- $urlObj["redirect_uri"] = "$redirectUrl";
- $urlObj["response_type"] = "code";
- $urlObj["scope"] = "snsapi_base";
- $urlObj["state"] = "STATE"."#wechat_redirect";
- $bizString = $this->formatBizQueryParaMap($urlObj, false);
- return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
- }
- /**
- * 作用:生成可以獲得openid的url
- */
- function createOauthUrlForOpenid()
- {
- $urlObj["appid"] = WxPayConf_pub::APPID;
- $urlObj["secret"] = WxPayConf_pub::APPSECRET;
- $urlObj["code"] = $this->code;
- $urlObj["grant_type"] = "authorization_code";
- $bizString = $this->formatBizQueryParaMap($urlObj, false);
- return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
- }
- /**
- * 作用:通過curl向微信提交code,以獲取openid
- */
- function getOpenid()
- {
- $url = $this