php微信公眾號介面實現發紅包的方法

來源:互聯網
上載者:User
這篇文章主要介紹了php版公眾號介面實現發紅包的方法,結合執行個體形式分析了php版公眾號實現發紅包的介面調用方法與相關使用注意事項,需要的朋友可以參考下

本文執行個體講述了php版公眾號介面實現發紅包的方法。分享給大家供大家參考,具體如下:

最近接到一個任務,需要用來給使用者自動發紅包。要完成這個任務需要這麼已經一些物料

商戶號,已申請支付

商戶號主體下面的公眾號

先看一下

只需要完成後面幾步就可以了。

在公眾號伺服器上面調用紅包代碼

/***紅包功能*/public function sendredpack(){ $re_openid = $this->_pg('re_openid');  $inputObj = new sendredpack_pub();if(!$re_openid){  return "紅包功能,收紅包使用者不可為空";}  $inputObj->setParameter('re_openid',$re_openid); //收紅包的使用者的openid  $inputObj->setParameter('send_name',"汽配一號鋪"); //紅包寄件者名稱  $inputObj->setParameter('total_amount',"100"); //收紅包的使用者的金額,精確到分  $inputObj->setParameter('total_num',"1"); //收紅包的個數  $inputObj->setParameter('wishing',"恭喜發財,謝謝支援,小小心意"); //收紅包的使用者的openid  $inputObj->setParameter('client_ip',"121.40.157.243"); //調用介面的IP  $inputObj->setParameter('act_name',"小鄧感恩紅包"); //紅包主題  $inputObj->setParameter('remark',"謝謝大家一路一來的支援"); //備忘  $response = $inputObj->getResult();  return $response;}

在支付協助工具輔助層加一個類,來完成紅包功能

/*** 發紅包介面**/class sendredpack_pub extends Wxpay_client_pub{   function __construct() {  //設定介面連結   $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";  //設定curl逾時時間  $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;}/*** 產生介面參數xml*/function createXml(){  try {  $this->parameters["mch_billno"] = WxPayConf_pub::MCHID.createUnique();//商戶訂單號  $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公眾帳號ID  $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號  $this->parameters["nonce_str"] = $this->createNoncestr();//隨機字串  $this->parameters["sign"] = $this->getSign($this->parameters);//簽名  return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) {   die($e->errorMessage()); }}/*** 作用:擷取結果,使用認證通訊*/function getResult(){  $this->postXmlSSL();  $this->result = $this->xmlToArray($this->response);  return $this->result;}}

然後部署返支付的服務上面,就可以了!!然後在做公眾號(這個公眾號)的服務上面加入“紅包”。就能達到上面的效果了

根據文檔進行開發

請您仔細閱讀介面文檔,參照文檔進行開發,請注意,為了保證商戶資金安全,介面強校正商戶號與appid之間的綁定關係,以及appid與openid之間的對應關係(如果商戶號與appid之間沒有綁定關係,即appid沒有申請支付或者申請支付商戶號不匹配,或者openid歸屬appid錯誤,介面會返回報錯)。

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.