After the last essay, after improving and reconstructing the entire WeChat framework, WeChat payment, corporate payment, cash red envelopes, vouchers and various card coupons have been packaged, and WeChat payment has been included and the shake red envelope part are common parts of official accounts and enterprise accounts. These payment-related interfaces can be called in official accounts and enterprise accounts. After a series of optimization and sorting, these contents will be added one by one. Introducing, hope everyone likes and supports.
1) Usage scenarios
WeChat Pay cash red envelopes are developed for WeChat Pay merchants. The specific capabilities are as follows:
◆ When merchants call the interface, they issue red envelopes by specifying the sending object and the sending amount. This method allows merchants to flexibly apply it to a variety of rich activity scenarios
◆ Receive After receiving the red envelope, the user's funds are directly transferred to WeChat change, avoiding the complicated award process and giving users the smooth experience of WeChat payment.
2) WeChat red envelope sending rules
Sending frequency rules
◆ The number of red envelopes sent per minute shall not exceed 1,800;
◆ The same merchant number, the maximum number of red envelopes sent per minute The same user sends a red envelope;
Red envelope rules
◆ The amount of a single red envelope is between [1.00 yuan, 200.00 yuan];
◆ The same red envelope can only be sent to one user; (If the above rules do not meet your needs, please send an email to wxhongbao@tencent.com for upgrade instructions)
◆ If the red envelope is not claimed 72 hours after it is issued, it will be processed Refund
3) WeChat red envelope interface calling process
◆Backend API call: communicate with the development team in detail when entering the joint debugging process;
◆ Notify the server: Inform the server of the openID of the user who received the WeChat red envelope, and inform the server of the amount received by the user;
◆ Deduct money from the business account: After the server obtains the information, it will deduct the money from the corresponding business account Get the corresponding amount;
◆ Call failure: The call fails due to non-compliance with the sending rules, insufficient balance of the business account, etc., and feedback to the caller;
◆ Successful sending: public with WeChat red envelope The account sends the corresponding red envelope to the corresponding user;
is used by enterprises to send red envelopes to individual WeChat users. It currently supports issuing red envelopes of specified amounts to the openid of designated WeChat users.
Although cash red envelopes can be issued through the merchant backend of WeChat, we can also use the interface API provided by WeChat to send cash red envelopes.
Request Url | https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack |
---|---|
Is it required? The certificate | is (see the merchant certificate for certificate and usage instructions) |
Request method | POST |
Data example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The above is a description of the interface and input parameters, generally In this case, we need to decide how to implement C# code encapsulation based on these. First, we define the interfaces and classes we need, as shown below.
By analyzing the above interface description, we can find that some of the interfaces are fixed conventional parameters, that is, the identity information of general public accounts or enterprise accounts, and some are business parameters, so we separate them separately, which is conducive to our encapsulation and use of the interface. We can obtain those regular parameters through the official account identity. For business information, we can define an entity class to carry out data Just store and exchange.
corresponds to the fixed conventional parameters in the above figure, as shown below in the interface description.
Therefore, we can set and obtain this information from the account. We can configure them in the management background, and then take them out and use them in the code logic.
According to the above introduction, we can define the red packet interface code as follows.
1 2 3 4 5 6 7 8 9 10 11 |
|
SendRedPackJson is the business parameter we changed. We defined a class to carry information to facilitate the transfer of information through the interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
According to the definition of the above parameters, we implement the interface of the cash red envelope. The specific code is as follows. The logical content inside is mainly to pass in the regular Parameters and business parameters, then call the interface address to submit data (POST), obtain the returned results and parse them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
The operation of sending red envelopes requires a certificate, so the corresponding certificate needs to be added. The certificate is downloaded from the WeChat merchant platform.
Download the certificate from the [API Security] project in the merchant backend for use in our development environment.
After downloading the certificate, in a Windows environment, we generally need to double-click to install it and enter the required merchant number as the password.
In the code, we can use the certificate class to add
1 2 3 4 5 6 |
|
For example, our calling code in the test example is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The hbApi is the structure of the above interface, as shown in the following code.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
After successful call, we can see the red envelope information results in the conversation of the official account. The following is the entire process of sending and opening the red envelope.
For more C# development of WeChat portal and application of WeChat cash red envelope packaging and use, please pay attention to the PHP Chinese website for related articles!
nonce_str | is | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | String(32) | Random string, no longer than 32 characters | |
sign | is | C380BEC2BFD727A4B6845133519F3AD6 | String(32) | See signature generation algorithm for details | |
mch_billno | is | 10000098201411111234567890 | String(28) |
Composition: mch_id+yyyymmdd+10 digits cannot be repeated within one day number. The interface supports re-entry based on the merchant's order number. If a timeout occurs, it can be called again. |
|
mch_id | is | 10000098 | String(32) | The merchant number assigned by WeChat payment | |
wxappid | is | wx8888888888888888 | String(32) | The public account ID assigned by WeChat (corporate number corpid is this appId). All appids passed in through the interface should be the appid of the public account (applied at mp.weixin.qq.com), not the appid of the APP (applied at open.weixin.qq.com). | |
send_name | is | Tianhong Department Store | String(32) | Red envelope sender name | |
re_openid | is | oxTWIuGaIt6gTKsQRLau2M0yL16E | String(32 ) |
The user’s openid under wxappid |
|
total_amount | is | 1000 | int | Payment amount, unit points | |
total_num | is | 1 | int |
total_num=1 |
|
wishing | Yes | Thank you for participating in the lantern riddle guessing activity, and I wish you a happy Lantern Festival! | String(128) | Red envelope blessings | |
client_ip | is | 192.168.0.1 | String(15) | Machine IP address calling the interface | |
act_name | is | Guessing lantern riddles and grabbing red envelope activities | String(32) | Activity name | |
remark | Yes | The more you guess, the more you get, come and grab it! | String(256) | Remarks |