PHP version of the micro-credit random Red Envelopes Interface API interface

Source: Internet
Author: User
Tags openid rand vars

Manual Hand Release

In the case where the merchant is already aware of the OpenID for the user to send, OpenID can be edited into TXT file, direct login micro-letter payment merchant platform, the same in the "Fund management-Cash Management" in the recharge, create the red envelopes to be issued, and fill in the amount of relevant information, and then operate according to the guidelines.

Automatic system distribution

Businesses need to send mail to wxhongbao@tencent.com to obtain process guidance, and then login to the merchant platform (pay.weixin.qq.com), in the "money Management-Cash Management" recharge; then you can develop according to the development document, and issue cash red envelopes, We focus on system release.

First, when the merchant invokes the micro-letter red envelope, the server authenticates the certificate, so it is necessary to download the certificate on the merchant platform. In the admin background of "account information" found in "Security settings" can download the certificate.

The next is to recharge the account, because the cash bonus will be deducted from the merchant's available balance, so need to recharge, to ensure sufficient balance available. Enter the "Money Management" menu to find "Cash management", where to recharge, to ensure that the account has the balance can be issued red envelopes.

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

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.