Complete micro-letter Red Envelope API implementation (PHP version)

Source: Internet
Author: User
Tags cdata openid vars

A, micro-letter Red Envelopes Document description

At present, the micro-credit red envelopes are divided into cash red envelopes and two kinds of fission.
1. Cash Red envelopes: Https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
2. Fission Red envelopes: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=16_5
More please check: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php

Second, PHP interface implementation

This article explains the call of cash red envelopes, the other is roughly the same, do not try.
Parameter description:

Code implementation:

Fragment One,

/**
* Micro-Credit Payment
* @param string $openid user OpenID
*/
Public function Pay ($re _openid)
{
Include_once (' wxpacketclass.php ');
$wxHongBaoHelper = new Wxpacketclass ($this->app_sign);
$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 (28 bits)
$wxHongBaoHelper->setparameter ("mch_id", $this->app_mchid);/Merchant number
$wxHongBaoHelper->setparameter ("Wxappid", $this->app_id);
$wxHongBaoHelper->setparameter ("Send_name", "Yang and Macro Technology");//Red Envelope sender Name
$wxHongBaoHelper->setparameter ("Re_openid", $re _openid);//openid
$wxHongBaoHelper->setparameter ("Total_amount", 100);//payment amount, per cent
$wxHongBaoHelper->setparameter ("Total_num", 1);//Red envelopes, х. br/> $wxHongBaoHelper->setparameter ("Wishing", ' To your old age, I wish you happiness in old age! ")//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", "Everybody 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);
$RESPONSEOBJ = simplexml_load_string ($responseXml, ' simplexmlelement ', libxml_nocdata);
return $RESPONSEOBJ->return_code;
}

Fragment two,
 
//Generate Red Envelopes interface XML Information
/*
<xml>
 <sign>![ Cdata[e1ee61a9]]</sign>
 <mch_billno>![ Cdata[00100]]</mch_billno>
 <mch_id>![ Cdata[888]]</mch_id>
 <wxappid>![ Cdata[wxcbda96de0b165486]]</wxappid>
 <send_name>![ Cdata[send_name]]</send_name>
 <re_openid>![ Cdata[onqojjxxxxxxxxx]]</re_openid>
 <total_amount>![ Cdata[100]]</total_amount>
 <total_num>![ Cdata[1]]</total_num>
 <wishing>![ Cdata[congratulate the rich]]</wishing>
 <client_ip>![ Cdata[127.0.0.1]]</client_ip>
 <act_name>![ cdata[New Year red envelopes]]</act_name>
 <act_id>![ Cdata[act_id]]</act_id>
 <remark>![ cdata[New Year red envelopes]]</remark>
</xml>
*/

function Create_hongbao_xml ($retcode = 0, $reterrmsg = "OK") {
try {
$this->setparameter (' sign ', $this->get_sign ());
$commonUtil = new Commonutil ();
Return $commonUtil->arraytoxml ($this->parameters);
}catch (Sdkexception $e) {
Die ($e->errormessage ());
}
}

Fragment Three,

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
curl_setopt ($ch, Curlopt_sslcert,dirname (__file__). Directory_separator. ' Cert '. Directory_separator. ' Apiclient_cert.pem ');
curl_setopt ($ch, Curlopt_sslkey,dirname (__file__). Directory_separator. ' Cert '. Directory_separator. ' Apiclient_key.pem ');
curl_setopt ($ch, Curlopt_cainfo,dirname (__file__). Directory_separator. ' Cert '. 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);
Curl_close ($ch);
return false;
}
}

Code structure:

|~action/
| '-packetclass.php
|~lib/
| |~cert/
| | | |-apiclient_cert.pem
| | | |-apiclient_key.pem
| | '-rootca.pem
| |-sdkextraclass.php
| |-wxapi.php
| '-wxpacketclass.php
'-index.php

Each file is described in detail.

Third, the effect shows

  

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.