PHP小程序發放紅包接口

微信文檔:

https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9&index=2

PHP調用接口需要證書(curl用到):

微信商戶平臺(pay.weixin.qq.com)-->賬戶中心-->賬戶設置-->API安全-->下載證書

需要支付密鑰:

key設置路徑:微信商戶平臺(pay.weixin.qq.com)-->賬戶設置-->API安全-->密鑰設置

設置允許服務IP白名單:

微信商戶平臺(pay.weixin.qq.com)-->產品中心-->現金紅包-->產品設置-->設置服務器IP白名單

封裝類:

/*
 * 微信支付:小程序發放紅包接口
 */
class WeixinPay {

    private $sendurl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb';//發放紅包接口
    private $mch_billno;//商戶訂單號
    private $mch_id;//商戶號
    private $wxappid;//公衆賬號appid
    private $send_name;//商戶名稱
    private $re_openid;//用戶openid
    private $total_amount;//付款金額,單位分
    private $total_num;//紅包發放總人數
    private $wishing;//紅包祝福語
    private $client_ip;//Ip地址
    private $act_name;//活動名稱
    private $remark;//備註
    private $notify_way;//通知用戶形式
    private $scene_id;//發放紅包使用場景,紅包金額大於200時必傳
    private $key;//商戶號支付鑰匙

    function __construct($mch_billno, $mch_id, $wxappid, $send_name,$re_openid,$total_amount,$total_num,$wishing,$client_ip,$act_name,$remark,$notify_way,$scene_id,$key) {
        $this->mch_billno = $mch_billno;
        $this->mch_id = $mch_id;
        $this->wxappid = $wxappid;
        $this->send_name = $send_name;
        $this->re_openid = $re_openid;
        $this->total_amount = $total_amount;
        $this->total_num = $total_num;
        $this->wishing = $wishing;
        $this->client_ip = $client_ip;
        $this->act_name = $act_name;
        $this->remark = $remark;
        $this->notify_way = $notify_way;
        $this->scene_id = $scene_id;
        $this->key = $key;
    }

    public function sendhb(){
        //隨機字符串
        $nonce_str = $this->createNoncestr();
        //商戶訂單號
        $mch_billno = $this->mch_billno;
        //商戶號
        $mch_id = $this->mch_id;
        //公衆賬號appid
        $wxappid = $this->wxappid;
        //商戶名稱
        $send_name = $this->send_name;
        //用戶openid
        $re_openid = $this->re_openid;
        //付款金額,單位分
        $total_amount = $this->total_amount;
        //紅包發放總人數
        $total_num = $this->total_num;
        //紅包祝福語
        $wishing = $this->wishing;
        //Ip地址
        $client_ip = $this->client_ip;
        //活動名稱
        $act_name = $this->act_name;
        //備註
        $remark = $this->remark;
        //通知用戶形式
        $notify_way = $this->notify_way;
        //發放紅包使用場景,紅包金額大於200時必傳
        $scene_id = $this->scene_id;

        $parameters = array(
            'nonce_str' => $nonce_str,
            'mch_billno' => $mch_billno,
            'mch_id' => $mch_id,
            'wxappid' => $wxappid,
            'send_name' => $send_name,
            're_openid' => $re_openid,
            'total_amount' => $total_amount,
            'total_num' => $total_num,
            'wishing' => $wishing,
            'client_ip' => $client_ip,
            'act_name' => $act_name,
            'remark' => $remark,
            'notify_way' => $notify_way,
            'scene_id' => $scene_id
        );
        //生成簽名,所有參數+key然後MD5
        $parameters['sign'] = $this->getSign($parameters);

        $xmlData = $this->arrayToXml($parameters);
//        var_dump($xmlData);
        $curlres = $this->postXmlCurl($xmlData, $this->sendurl);
//        var_dump($curlres);
        $res = $this->xmlToArray($curlres);
//        var_dump($res);
        return $res;
    }


    private function postXmlCurl($xml, $url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        //設置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求結果爲字符串且輸出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //證書的位置
        curl_setopt($ch, CURLOPT_SSLCERT, __DIR__ . '/cert/apiclient_cert.pem');
        //證書key的位置
        curl_setopt($ch, CURLOPT_SSLKEY, __DIR__ . '/cert/apiclient_key.pem');

        //運行curl
        $data = curl_exec($ch);
        //返回結果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            curl_close($ch);
            throw new Exception("curl出錯,錯誤碼:$error");
        }
    }

    //數組轉換成xml
    private function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                $xml .= "<" . $key . ">" . $this->arrayToXml($val) . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }

    //xml轉換成數組
    private function xmlToArray($xml) {
        //禁止引用外部xml實體
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring), true);
        return $val;
    }

    //作用:產生隨機字符串,不長於32位
    private function createNoncestr($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //作用:生成簽名
    private function getSign($Obj) {
        foreach ($Obj as $k => $v) {
            //不爲空的參數才參與簽名
            if(!empty($v)){
                $Parameters[$k] = $v;
            }
        }
        //簽名步驟一:按字典序排序參數
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //簽名步驟二:在string後加入KEY
        $String = $String . "&key=" . $this->key;
        //簽名步驟三:MD5加密
        $String = md5($String);
        //簽名步驟四:所有字符轉爲大寫
        $result = strtoupper($String);
        return $result;
    }

    //作用:格式化參數,簽名過程需要使用
    private function formatBizQueryParaMap($paraMap, $urlencode) {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }

}

調用示範:

$mch_billno = '1000000000201907031234567890';//商戶訂單號
$mch_id = '1000000000';//商戶號
$wxappid = 'wx**************ff';//小程序賬號appid
$send_name = '某某企業';//商戶名稱
$re_openid = 'oq*************************Is';//用戶openid
$total_amount = 100;//付款金額,單位分
$total_num = 1;//紅包發放總人數
$wishing = '恭喜';//紅包祝福語
$client_ip = '120.0.1.120';//調用接口的服務器Ip地址
$act_name = '紅包活動';//活動名稱
$remark = '快來搶';//備註
$notify_way = '';//通知用戶形式JSAPI
$scene_id = '';//發放紅包使用場景,紅包金額大於200時必傳
$key = 'aany**************************nt';//商戶號支付鑰匙

$weixinpay = new WeixinPay($mch_billno, $mch_id, $wxappid, $send_name,$re_openid,$total_amount,$total_num,$wishing,$client_ip,$act_name,$remark,$notify_way,$scene_id,$key);
$res=$weixinpay->sendhb();

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章