The development of ThinkPHP5 WeChat cash red envelopes

Source: Internet
Author: User
Tags openid urlencode vars
This article mainly introduced about the ThinkPHP5 cash red envelope development, has certain reference value, now shares to everybody, has the need friend can refer to

Controller:

    /** * Release cash red envelopes */public function Payluckymoney () {$obj 2 = array ();    AppID $obj 2[' wxappid ' = config (' wx_gzh.appid ');    Merchant ID $obj 2[' mch_id ' = config (' wx_sh.mchid '); Combined into 28 bits, according to the official development document, you can set your own $obj 2[' mch_billno ' = config (' Wx_sh.mchid '). Date (' Ymdhis ').    RAND (1000, 9999);    The machine IP address of the calling interface $obj 2[' client_ip ') = $_server[' remote_addr '];    Receive red envelopes OpenID $obj 2[' Re_openid ' = Session (' OpenID ');     /* Payment amount set to start, randomly distributed according to probability settings. * Between 1-200 yuan, Unit points.    Here set 95% probability of 1-2 yuan, 5% probability of 2-10 yuan */$n = rand (1, 100);    if ($n <=) {$obj 2[' total_amount ') = rand (100, 200);    } else {$obj 2[' total_amount ') = rand (200, 1000);    }//$obj 2[' total_amount ') = 100;    /* Payment amount Set END *///Bonus number $obj 2[' total_num ') = 1;    Merchant name $obj 2[' send_name '] = "small Door too";    Red Envelope Blessing Language $obj 2[' wishing '] = "Congratulations on the rich, prosperous";    Activity name $obj 2[' act_name '] = "small door too certification collar red envelopes";    Remark $obj 2[' remark '] = "small door too red envelope"; /* The following variables are not documented in the Li Fulin blog. There's no problem with commenting.    No need. $obj 2[' min_value ') = $money    $obj 2[' max_value ') = $money;    $obj 2[' nick_name '] = ' small door too red envelopes ';    */$url = "Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";    $isPay = Pay_lucky_money ($url, $obj 2);    $res = Xml_to_array ($isPay);        Successful issuance of the red packet data into the database if ($res [' return_msg '] = = ' success ') {//issuance succeeded, logical processing} else {//issue failed, return failure reason    return $res [' return_msg ']; }}

The common.php function (you can also have a common class to invoke):

----The following is the area of the cash red envelopes start----///** * Issued cash red envelopes core function, call this function directly issued red envelopes. * @param $url Cash RED envelope request Address * @param $obj * @return mixed */function Pay_lucky_money ($url, $obj) {//Create random string (32 bit) $obj [' N    Once_str '] = Str_rand ();    Create Signature $sign = Get_sign ($obj, false);    Halt ($sign);    $obj [' sign '] = $sign;    Incoming signature to array $postXml = Array_to_xml ($obj);    Convert parameters to XML format//halt ($POSTXML);    $RESPONSEXML = Curl_post_ssl ($url, $postXml);    Submit Request//halt ($RESPONSEXML); return $responseXml;} /** * @param $arr generate the preceding parameters * @param $urlencode * @return String returns the encrypted signature */function get_sign ($arr, $urlencode) {$buff =    "";    The contents of the incoming array parameter are sorted alphabetically, a in front, z in the Last (dictionary order) Ksort ($arr);            foreach ($arr as $k + $v) {if (null! = $v && "null"! = $v && "sign"! = $k) {//signature do not transcode            if ($urlencode) {$v = UrlEncode ($v); } $buff. = $k. "=" . $v.        "&"; }}//Remove the end symbol "&" without this if, because the length must be greater than 0 if (strlen ($buff) > 0) {       $stringA = substr ($buff, 0, strlen ($buff)-1); }//Signature stitching API $stringSignTemp = $stringA. "&key=".    Config (' Wx_sh.key ');    The signature is encrypted and capitalized $sign = Strtoupper (MD5 ($stringSignTemp)); return $sign;}    The POST request site requires a certificate function Curl_post_ssl ($url, $vars, $second = A, $aHeader = Array ()) {$ch = Curl_init ();    Time-out curl_setopt ($ch, Curlopt_timeout, $second);    curl_setopt ($ch, Curlopt_returntransfer, 1);    Here set the agent, if any, curl_setopt ($ch, Curlopt_url, $url);    curl_setopt ($ch, Curlopt_ssl_verifypeer, false);    curl_setopt ($ch, Curlopt_ssl_verifyhost, false);    Cert and key belong to two. Pem files//Please ensure that your Libcurl version supports two-way authentication with a version higher than 7.20.1, which is equivalent to the two PEM certificate files under "Current file directory/cert/wxpay/" with Curl verification. curl_setopt ($ch, Curlopt_sslcert, DirName (__file__).        Directory_separator. ' Cert '. Directory_separator. ' Wxpay '. Directory_separator.    ' Apiclient_cert.pem '); curl_setopt ($ch, Curlopt_sslkey, DirName (__file__).        Directory_separator. ' Cert '. Directory_separator. ' Wxpay '. Directory_separator.    ' Apiclient_key.pem '); curl_setopt ($ch, Curlopt_cainfo,dirname (__file__).    Directory_separator. ' Cert '.    Directory_separator. ' Rootca.pem ');    This is not necessary because most of the operating systems have built-in ROOTCA.PEM certificates, which are common CA certificates.    if (count ($aHeader) >= 1) {curl_setopt ($ch, Curlopt_httpheader, $aHeader);    } curl_setopt ($ch, Curlopt_post, 1);    curl_setopt ($ch, Curlopt_postfields, $vars);    $data = curl_exec ($ch);        if ($data) {curl_close ($ch);    return $data;        } else {$error = Curl_errno ($ch);        echo "Call Faild, ErrorCode: $error \ n";        Curl_close ($ch);    return false; }}//----Below is the area of cash red envelopes end----//

———— above, because my code is written under Common.php, and common.php is in the application directory, so the certificate is placed under the application/cert/wxpay/directory

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.