Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. The four payment series tutorials are over. If you think it is over, it will be wrong, some people told me that there was a red envelope feature. I didn't pay attention to it at first, so I read about it on the merchant's platform, and the payment platform had been there for a long time, after research, we will continue to issue tutorials on red packets. Next, let's take a look at the cash red packet payment tutorial!
Now, merchants can send red packets to the specified openid. Currently, there are two types of Red Packets: Cash red packets and fission red packets. This tutorial is about cash red packets.
Before you paste the code, pay attention to the following points: 1. Go to the merchant platform and charge your merchant money. If you have no money, you will not be able to make a red envelope! 2. Red Packets require certificate support, so please download the certificate from the merchant's platform and put it in the security folder. You must specify the certificate path in the configuration file!
Step 1: Let's introduce the configuration file WxPayConf_pub.php. After reading the previous payment tutorial, we should be very clear about this part. Here I will release the code and configure it before proceeding to the next step!
Step 2: Download your certificate and put it in a directory, corresponding to the configuration file. Remember that this is the absolute path!
Step 3: The previous demo has helped us write WxPayHelper. php class library, we can easily call it, and there is no official demo for the red packet, so here we have to go to WxPayHelper. write your own red envelope payment method under the PHP file:
/**
* Cash red envelope Interface
* @ Author gaoyl101
*/
Class Redpack_pub extends Wxpay_client_pub
{
Var $ code; // code to obtain the openid
Var $ openid; // the user's openid
Function _ construct ()
{
// Set the interface link
$ This-> url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack ";
// Set curl timeout
$ This-> curl_timeout = WxPayConf_pub: CURL_TIMEOUT;
}
/**
* Generate interface parameter xml
*/
Function createXml ()
{
Try
{
// Check required parameters
If ($ this-> parameters ["mch_billno"] = null)
{
Throw new SDKRuntimeException ("The mch_billno parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["nick_name"] = null ){
Throw new SDKRuntimeException ("the interface for sending red packets is missing. The parameter nick_name is required! "."
");
} Elseif ($ this-> parameters ["send_name"] = null ){
Throw new SDKRuntimeException ("The send_name parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["total_amount"] = null ){
Throw new SDKRuntimeException ("The total_amount parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["min_value"] = null ){
Throw new SDKRuntimeException ("The min_value parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["max_value"] = null ){
Throw new SDKRuntimeException ("The max_value parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["total_num"] = null ){
Throw new SDKRuntimeException ("The total_num parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["wishing"] = null ){
Throw new SDKRuntimeException ("the wishing parameter is required when the red packet sending interface is missing! "."
");
} Elseif ($ this-> parameters ["act_name"] = null ){
Throw new SDKRuntimeException ("the required parameter act_name is missing for the red packet sending interface! "."
");
} Elseif ($ this-> parameters ["remark"] = null ){
Throw new SDKRuntimeException ("the remark parameter is required when the red packet sending interface is missing! "."
");
}
$ This-> parameters ["wxappid"] = WxPayConf_pub: APPID; // public account ID
$ This-> parameters ["mch_id"] = WxPayConf_pub: MCHID; // merchant ID
$ This-> parameters ["client_ip"] = $ _ SERVER ['remote _ ADDR ']; // Terminal ip
$ This-> parameters ["nonce_str"] = $ this-> createNoncestr (); // random string
$ This-> parameters ["re_openid"] = $ this-> openid; // user openid
$ This-> parameters ["sign"] = $ this-> getSign ($ this-> parameters); // Signature
Return $ this-> arrayToXml ($ this-> parameters );
} Catch (SDKRuntimeException $ e)
{
Die ($ e-> errorMessage ());
}
}
Function sendRedpack ()
{
$ This-> postXmlSSL ();
$ This-> result = $ this-> xmlToArray ($ this-> response );
Return $ this-> result;
}
/**
* Function: generate a url that can obtain code
*/
Function createOauthUrlForCode ($ redirectUrl)
{
$ UrlObj ["appid"] = WxPayConf_pub: APPID;
$ UrlObj ["redirect_uri"] = "$ redirectUrl ";
$ UrlObj ["response_type"] = "code ";
$ UrlObj ["scope"] = "snsapi_base ";
$ UrlObj ["state"] = "STATE". "# wechat_redirect ";
$ BizString = $ this-> formatBizQueryParaMap ($ urlObj, false );
Return "https://open.weixin.qq.com/connect/oauth2/authorize? ". $ BizString;
}
/**
* Function: generate a url that can obtain the openid.
*/
Function createOauthUrlForOpenid ()
{
$ UrlObj ["appid"] = WxPayConf_pub: APPID;
$ UrlObj ["secret"] = WxPayConf_pub: APPSECRET;
$ UrlObj ["code"] = $ this-> code;
$ UrlObj ["grant_type"] = "authorization_code ";
$ BizString = $ this-> formatBizQueryParaMap ($ urlObj, false );
Return "https://api.weixin.qq.com/sns/oauth2/access_token? ". $ BizString;
}
/**
* Function: Use curl to submit code to obtain the openid.
*/
Function getOpenid ()
{
$ Url = $ this-> createOauthUrlForOpenid ();
// Initialize curl
$ Ch = curl_init ();
// Set timeout
Curl_setopt ($ ch, CURLOP_TIMEOUT, $ this-> curl_timeout );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
Curl_setopt ($ ch, CURLOPT_HEADER, FALSE );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );
// Run curl and return the result in jason format
$ Res = curl_exec ($ ch );
Curl_close ($ ch );
// Retrieve the openid
$ Data = json_decode ($ res, true );
$ This-> openid = $ data ['openid'];
Return $ this-> openid;
}
/**
* Function: Set the code.
*/
Function setCode ($ code _)
{
$ This-> code = $ code _;
}
}
In fact, the code here is not very good, and I have not encapsulated it, because similar code will be used for fission red packets. I will not change it if I do a demo here, if you are interested, you can continue the evening on this basis! The above code is the tool class we will use. put it at the bottom of WxPayHelper. php!
Step 4: Create a controller WxCashRedPackController
Code in the controller:
1. Introduce the WxPayHelper. php class library
/**
* Initialization
*/
Public function _ initialize ()
{
// Introduce WxPayPubHelper
Vendor ('wxpaypubhelper. WxPayPubHelper ');
}
2. Create a red envelope sending method: sendRedpack. This method is the specific function code for sending the red envelope!
/**
* Send a red envelope
*/
Public function sendRedpack ()
{
// Call the request interface base class
$ Redpack = new \ Redpack_pub ();
// =========== Step 1: Obtain the user's openid through web page authorization ==============
// Obtain the openid through code
If (! Isset ($ _ GET ['code'])
{
// Trigger return code
$ Export ct_uri = WEB_HOST. "/index. php/Home/WxCashRedPack/sendRedpack ";
$ Url = $ Redpack-> createOauthUrlForCode ($ export ct_uri );
Header ("Location: $ url ");
} Else
{
// Obtain the code to obtain the openid
$ Code = $ _ GET ['code'];
$ Redpack-> setCode ($ code );
$ Openid = $ Redpack-> getOpenId ();
}
// Merchant Order Number
$ Redpack-> setParameter ('mch _ billno', C ('wxpayconf _ pub. APPID '). "static ");
// Provider name
$ Redpack-> setParameter ('Nick _ name', "gaoyl101 ");
// Merchant name
$ Redpack-> setParameter ('send _ name', "gaoyl101 ");
// User openid
// $ Redpack-> setParameter ('re _ openid', $ parameterValue );
// Payment amount
$ Redpack-> setParameter ('total _ amount', 100 );
// Minimum red packet amount
$ Redpack-> setParameter ('min _ value', 100 );
// Maximum red envelope amount
$ Redpack-> setParameter ('max _ value', 100 );
// Total number of red packets issued
$ Redpack-> setParameter ('total _ num', 1 );
// Red envelope greetings
$ Redpack-> setParameter ('wishing ', "Happy code writing in the cash red packet tutorial ");
// Activity name
$ Redpack-> setParameter ('act _ name', "Cash red packet tutorial ");
// Remarks
$ Redpack-> setParameter ('remark', "Happy code writing in the cash red packet tutorial ");
// The following are optional items
// Sub-merchant ID
// $ Redpack-> setParameter ('sub _ mch_id ', $ parameterValue );
//// Merchant logo url
// $ Redpack-> setParameter ('logo _ imgurl', $ parameterValue );
//// Share the text
// $ Redpack-> setParameter ('share _ content', $ parameterValue );
//// Share Link
// $ Redpack-> setParameter ('share _ url', $ parameterValue );
//// Shared image
// $ Redpack-> setParameter ('share _ imgurl', $ parameterValue );
$ Result = $ Redpack-> sendRedpack ();
Dump ($ result );
}
When you access this method, you will receive a red packet.
Here, I dump the result returned after sending the red packet, and the following business logic can be written based on my own needs. The description of the return value can be seen in the description of the red packet interface, on the payment platform.
Here, the red envelope cash red envelope code has all been completed, and the function has been tested!
The red packet after success is shown below:
After reading the previous payment tutorials, many of you will encounter problems and find my solution. I personally think my article has gained the value it deserves, I hope this article will also help those who are worried about sending red packets!
If you have any questions, please leave a message. Next we will introduce the split red envelope!
Jsapi for payment:
Http://www.thinkphp1.cn/code/1321.html
Payment tutorial Scan Mode 1:
Http://www.thinkphp1.cn/code/1322.html
Payment tutorial scan mode 2:
Http://www.thinkphp1.cn/code/1323.html
Payment tutorial card payment:
Http://www.thinkphp1.cn/code/1324.html
Fission red packet Tutorial:
Http://www.thinkphp1.cn/code/1330.html
Thank you for your comments. For more information, please describe the source. Please support originality. Thank you!
Our development group: 422579975 (full) 105195188 (full), the code is already in the group file
Welcome to join the discussion
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB