According to the micro-credit premium red envelope interface, develop the PHP version of the API interface, now for the main code analysis
Red Packet Interface Call request code, all request parameters are required parameters corresponding to the document:
Class Wxapi {
Private $app _id = ' wxxxxxxxxxxxxx '; Public account AppID, the first to apply for matching with the public account
Private $app _secret = ' xxxxxxxxxxxxxxxxxxxxxxxx '//Public number secret, user access authorization token
Private $app _mchid = ' XXXXXXXX ';//merchant number ID
function __construct () {
Do sth here ....
}
/**
* Micro-Credit Payment
* @param string $openid user OpenID
*/
Public function Pay ($re _openid)
{
Include_once (' wxhongbaohelper.php ');
$commonUtil = new Commonutil ();
$wxHongBaoHelper = new Wxhongbaohelper ();
$wxHongBaoHelper->setparameter ("Nonce_str", $this->great_rand ());//random string, no longer than 32 bits
$wxHongBaoHelper->setparameter ("Mch_billno", $this->app_mchid.date (' Ymdhis '). Rand (1000, 9999));//Order number
$wxHongBaoHelper->setparameter ("mch_id", $this->app_mchid);/Merchant number
$wxHongBaoHelper->setparameter ("Wxappid", $this->app_id);
$wxHongBaoHelper->setparameter ("Nick_name", ' red envelope ');//Provider Name
$wxHongBaoHelper->setparameter ("Send_name", "red envelope");//Red Envelope sender Name
$wxHongBaoHelper->setparameter ("Re_openid", $re _openid);//OpenID relative to the medical pulse
$wxHongBaoHelper->setparameter ("Total_amount", 100);//payment amount, per cent
$wxHongBaoHelper->setparameter ("Min_value", 100);//minimum red envelope amount, Unit score
$wxHongBaoHelper->setparameter ("Max_value", 100);//maximum red envelope amount, Unit score
$wxHongBaoHelper->setparameter ("Total_num", 1);//Red envelopes, х. br/> $wxHongBaoHelper->setparameter ("Wishing", Thank you for your participation in the red envelope distribution activities, I wish you a happy New Year! ")//Red Envelopes Blessing astonished
$wxHongBaoHelper->setparameter ("Client_ip", ' 127.0.0.1 ');//Machine IP address of the calling interface
$wxHongBaoHelper->setparameter ("Act_name", ' Red envelope activity ');/Live Name
$wxHongBaoHelper->setparameter ("Remark", ' Come and rob! ');//Memo Information
$POSTXML = $wxHongBaoHelper->create_hongbao_xml ();
$url = ' Https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack ';
$RESPONSEXML = $wxHongBaoHelper->curl_post_ssl ($url, $postXml);
Use as result debug output
Echo htmlentities ($responseXml, Ent_compat, ' UTF-8 ');
$RESPONSEOBJ = simplexml_load_string ($responseXml, ' simplexmlelement ', libxml_nocdata);
return $RESPONSEOBJ->return_code;
}
To get a random string method:
/**
* Generate random numbers
*/
Public Function Great_rand () {
$str = ' 1234567890abcdefghijklmnopqrstuvwxyz ';
For ($i =0 $i <30; $i + +) {
$j =rand (0,35);
$t 1. = $str [$j];
}
return $t 1;
}
Signature algorithm:
/**
For example:
appid:wxd111665abv58f4f
mch_id:10000100
device_info:1000
Body:test
Nonce_str:ibuaivckdprxkhja
The first step is to follow the parameters in Key=value format and sort by the ASCII Dictionary of parameter names as follows:
Stringa= "Appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
D=10000100&nonce_str=ibuaivckdprxkhja ";
Step two: Splicing the payment key:
stringsigntemp= "Stringa&key=192006250b4c09247ec02edce69f6a2d"
SIGN=MD5 (stringsigntemp). toUpperCase () = "9a0a8659f005d6984697e2ca0a
9cf3b7 "
*/
protected function get_sign () {
Define (' Partnerkey ', "qsrxxxxxxxxxxxxxxxxxxxxx");
try {
if (null = = Partnerkey | | "" = = Partnerkey) {
throw new Sdkruntimeexception ("Key cannot be empty!") " . " <br> ");
}
if ($this->check_sign_parameters () = = False) {//Check generation signature Parameters
throw new Sdkruntimeexception ("Generate Signature parameter missing!") " . " <br> ");
}
$commonUtil = new Commonutil ();
Ksort ($this->parameters);
$unSignParaString = $commonUtil->formatqueryparamap ($this->parameters, false);
$MD 5SignUtil = new Md5signutil ();
Return $MD 5signutil->sign ($unSignParaString, $commonUtil->trimstring (Partnerkey));
}catch (sdkruntimeexception $e)
{
Die ($e->errormessage ());
}
}
Curl Request and send certificate:
function Curl_post_ssl ($url, $vars, $second =30, $aHeader =array ())
{
$ch = Curl_init ();
Timeout time
curl_setopt ($ch, Curlopt_timeout, $second);
curl_setopt ($ch, Curlopt_returntransfer, 1);
Set the agent here, 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 respectively
Make sure that your Libcurl version supports two-way authentication, and that the version is higher than 7.20.1
curl_setopt ($ch, Curlopt_sslcert,dirname (__file__). Directory_separator. ' Zhengshu '. Directory_separator. ' Apiclient_cert.pem ');
curl_setopt ($ch, Curlopt_sslkey,dirname (__file__). Directory_separator. ' Zhengshu '. Directory_separator. ' Apiclient_key.pem ');
curl_setopt ($ch, Curlopt_cainfo,dirname (__file__). Directory_separator. ' Zhengshu '. Directory_separator. ' Rootca.pem ');
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;
}
}
Entry file:
@require "pay.php";
Get user Information
$get = $_get[' param '];
$code = $_get[' code '];
Determine if the code exists
if ($get = = ' Access_token ' &&!empty ($code)) {
$param [' param '] = ' access_token ';
$param [' code '] = $code;
$packet = new Packet ();
Get user OpenID Information
$userinfo = $packet->_route (' userinfo ', $param);
if (Empty ($userinfo [' OpenID '])) {
Exit ("Noauth");
}
Method of the payment of the transfer
$packet->_route (' Wxpacket ', Array (' OpenID ' => $userinfo [' OpenID ']);
}else{
$packet->_route (' userinfo ');
}
Develop the required documentation:
1 Official Red Envelopes Advanced Interface Document
2 The official public platform to obtain user basic information interface document
3 Merchant Platform Development Document