This article is the thirteenth article in the series of wechat Payment. It mainly explains how to open the red envelope function of merchant platform and issue red envelope for users, as well as query the record of sending red envelope.


Analysis of wechat payment series has been updated 13 yo ~, have not seen the friends can have a look at oh.

Analysis of wechat payment :(balance withdrawal) enterprises pay to wechat users change or bank account

Analysis of wechat Pay: Payment acceptance Examples and acceptance guidelines

Wechat Pay: How to use sandbox environment test

Brief analysis of wechat Payment: refund application, refund callback interface, refund inquiry

In the last article, we said that if there is demand for balance withdrawal, rebate and welfare, merchants will use the operation of payment to the user. Based on wechat payment, we talked about the payment to the user’s balance and bank card. This article explains how to use cash red envelopes to send cash red envelopes to users, first of all, let’s understand what is wechat cash red envelopes.

Cash bonus

Cash red envelopes, one of the marketing tools provided by the wechat payment merchant platform, have been loved by the majority of merchants and users since its launch. Merchants can issue red envelopes in cash to wechat Pay users through the platform. After the user receives the red envelope, the funds will arrive at the user’s wechat Pay account, which has the same usage exit as other funds of the zero wallet.

Note: If the user does not claim the funds, the funds will be returned to the merchant’s wechat Pay account after 24 hours.

Official Document Address:

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_1
Copy the code

Meaning of cash red envelope

Wechat Pay cash red envelopes have always been favored by users because of the way of cash. In recent years, cash red envelopes have played an important role in the Spring Festival Gala. In the daily operation of the merchant’s marketing activities to bring a warm response. In general, red envelopes of cash play an important role in including but not limited to the following scenarios:

Mixer pull new users for the enterprise, consolidate old customers relationship, enhance the user active mixer combined with clever ideas, with a red envelope to adorn, make popular activities, improve enterprise and brand awareness in pieces combined with business activities, with a red envelope as a prize, and make your lottery, full marketing activities such as more convenient pieces are at the same time, in addition to marketing, Cash red envelopes also play an important role in the daily operation of enterprises. For example: benefits for employees, rebates for suppliers, member points/virtual level cash and so on.

What do you mean? To put it simply, is the cash red envelope has special marketing properties, take the public number, we can establish activities, through the way of activities for users to send cash red envelope, and the red envelope reached the message is in the public number chat window page, so can also guide users to pay attention to the public number, improve activity and so on.

Open a cash red envelope

The official documents are as follows:

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_3&index=2
Copy the code

Before using the cash red envelope, please go to open the cash red envelope function. Operation path: [Login wechat Pay merchant platform — > Product Center — > Cash red envelope — > Open].

You can open a cash red envelope according to the official statement. Here are some important points:

  1. Stay longer than 90 days;
  2. Continuous trading normal trading time of 30 days;

Be sure to note: the above two points are necessary conditions, many newly registered companies are very easy to the road, check-in time is not enough, trading time is not enough, did not understand, waiting alive for three months; If you encounter such a situation, you can change to a host company that satisfies the requirements. My Github code also supports different host service numbers to use wechat Payment functions. You can check out the source code WXPayConstants and WXPay. Extend WXPayConfigImpl when calling the interface.

Note: Please select your usage scenario truthfully when opening, and report your scenario truthfully during the issuance of red envelopes. If there is any fraud, wechat Pay will have the right to deal with your merchant number according to the “wechat Pay Merchant Platform Use Agreement”.

Preparation before development

The specific operation steps are not described here, friends can check the official document:

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_3&index=2
Copy the code

As detailed in the above documentation, I would like to briefly describe the key items for attention:

  1. Downloading API Certificates
  2. Recharge to ensure that the balance of merchants has enough money (must pay attention to the difference between the basic account and the operation account, generally, when there is an operation account, the payment will be deducted from the operation account) Operation path: [Login merchant platform — > Trading center — > Fund Management — > Recharge]
  3. Get openID, specify the user to send red packets must first know the user id openID, can be obtained according to the web page authorization interface
  4. To set red envelope parameters, the operation path is: [Login merchant platform — > Product Center — > Cash red envelope — > Product Settings]

For the fourth point, you can set and change the following parameters:

  1. Call IP address: After setting, only the configured IP address can be called. Other IP addresses will report errors.
  2. Upper limit of user claim: limit the number of claim for the same OpenID on the same day;
  3. Anti-brush level: Anti-brush refers to the wechat risk control for the interception of wechat trumpet, zombie number, machine number, etc. You can control the intensity of anti-brush by changing the anti-brush level.
  4. At the same time, you can also apply to change the amount of red envelope. However, it will not take effect until it is approved by wechat Pay.

Knock on the blackboard!! The key comes, the above first point OF IP address, is that we call the cash red envelope to issue the server IP address; Second, we should also pay attention to the limit of the number of red packets each user can receive.

The most important thing to note is that when invoking the interface, the application scenario of sending red packets must be carefully selected. Check the corresponding restrictions of each scenario. For example, when the amount of red packets is greater than 200 yuan or less than 1 yuan, the scenario parameters must be transmitted, and then we need to configure the threshold.

Provisioning Mode (Interface Provisioning)

Mode 1: The interface issuing merchant develops according to the development document. One call can send a specified amount of red envelope to a specified user to meet diversified operation needs.

Method 2: Send the red packets by uploading the OpenID file. Collect the OpenID of the red packets to be sent, edit the OpenID into a TXT file, log in to the wechat Pay merchant platform, and use the file uploading function to send the red packets. One file corresponds to one red envelope template for easy management.

Distribution merchants can configure self-service rules on the merchant platform: users who meet certain conditions for transactions using wechat Pay can immediately send cash red envelopes

This article focuses on the way of interface provisioning.

Interface link

https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
Copy the code

Whether certificate is required

is

Call interface

Official Document Address:

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
Copy the code

The first is the distribution rule:

  1. Send frequency limit —— The default value is 1800/min
  2. Maximum number of packets to be sent —— The default value is 1800/min
  3. Amount limit —— The default red envelope amount is 1-200 yuan, if necessary, you can go to the merchant platform to set up and apply for
  4. Any other restrictions? —— a single user can receive red envelopes for 10 times per day. If necessary, you can go to the merchant platform to set up and apply for them
  5. How do we raise the caps if the volume doesn’t meet our needs? The upper limit of —— amount and the upper limit of the number of times a user receives it on the same day can be set on the merchant platform

Notice 1-If the red envelope value is greater than 200 yuan or less than 1 yuan, request parameter scene_id is mandatory. Note 2- According to regulatory requirements, the newly applied merchant account should meet two conditions for the use of cash red envelopes: 1. The settlement time is more than 90 days; 2. Note 3- The mobile app appID cannot use the red envelope interface.

PS: above is the official introduction, not difficult to understand, underline!! (Note 3: only openID of public account can be used. Openid of small program cannot be used.)

Message reach rules refer to official documentation:

Let’s start with the dry goods, post the source code, application code:

/** @author yclimb * @date 2018/9/18 */ private void sendRedPack() throws Exception {WXPay WXPay = new WXPay(AsydWXPayConfigImpl.getInstance()); Map<String, String> resultMap = wxPay.sendRedPack(WXPayUtil.getPayNo(),"obX_c0YRpT47zKcvq-ZYpjU6GFuA"."1"."Activity Name"."Red envelope greetings"."Note"."127.0.0.1");
    System.out.println("wxPay.sendRedPack:" + resultMap);
}
Copy the code

Call the interface for sending cash red packets:

/** * Effect: Enterprises can issue red envelopes of specified amount to openID of designated wechat users. After the user receives the red envelope, the funds will arrive at the user's wechat Pay account, which has the same usage exit as other funds of the zero wallet. If the user does not claim the money, the funds will be returned to the merchant's wechat Pay account after 24 hours. * Interface document address: https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3 * * @ param McH_billno merchant order number * @ param Openid User OpenID * @param Amount Amount paid by the enterprise * @param act_name Event name * @param Wishing on red packets * @param remark Remarks * @param Spbill_create_ip This IP address can be transmitted to the client or server * @returnAPI Returned data * @throws Exception e */ public Map<String, String> sendRedPack(String McH_billno, String openID, String amount, String act_name, String wishing, String remark, String spbill_create_IP) throws Exception {/** Construct request parameter data **/ Map<String, String> data = new HashMap<>(); / / merchant order number McH_billno is 10000098201411111234567890 String (28) merchant order number (each order number must be unique. Value range: 0 to 9, A to Z, a to Z) The interface supports reentrance based on the merchant order number. If a timeout occurs, the interface can be invoked again. data.put("mch_billno", mch_billno); // Merchant name send_name is rainbow department store String(32) Red envelope sender name data.put("send_name"."Yue shop"); / / users openid is re_openid oxTWIuGaIt6gTKsQRLau2M0yL16E String (32) accept red envelopes users openid Openid is the unique identifier of the user under wxappID (to obtain openID, refer to the developer document of wechat Public Platform: Web Page Authorization to Obtain user basic Information) data.put("re_openid", openid); // total_amount is 1000 int amount, data.put("total_amount", String.valueOf(new BigDecimal(amount).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue())); // total_num=1 int total_num=1 data.put("total_num"."1"); Wishing is to thank you for participating in lantern riddles guessing activities, WISH you a happy Lantern Festival! String(128) Data.put ("wishing", wishing); // Ip address client_IP is 192.168.0.1 String(15) Ip address of the calling interface data.put("client_ip", spbill_create_ip); String(32) activity name data.put("act_name", act_name); // The more you guess, the more you get. String(256) Remarks Data.put ("remark", remark); /** The following parameters are optional parameters **/ /** Scenario ID: Scene_id No PRODUCT_8 String(32) Application scenario of sending red packets, When the red envelope amount is greater than 200 or less than 1 yuan, it will be passed * PRODUCT_1: product promotion * PRODUCT_2: lucky draw * PRODUCT_3: virtual goods redemption * PRODUCT_4: enterprise internal welfare * PRODUCT_5: channel distribution * PRODUCT_6: insurance reward * PRODUCT_7: lottery prize * PRODUCT_8: tax scratch prize */ //data.put("scene_id"."PRODUCT_1"); /* * Active information Risk_info No postTime % 3D123123412%26ClientVersion % 3D234134%26Mobile % 3D122344545%26DevicEID %3dIOS String(128) * Posttime: indicates the time stamp of a user operation. * Mobile: indicates the mobile phone number of a service system account. The country code is - Mobile phone number. No + number * deviceid: MAC address or unique device identifier * clientversion: clientversion operated by the user concatenates non-empty information with key=value. Urlencode UrlenCode (postTime =xx& Mobile = xx&Deviceid = XX) */ // Merchant number authorized by funds Consume_mch_id No 1222000096 String(32) Merchant number authorized by funds The service provider uses the following four parameters for the specially contracted merchant when issuing, In the this.redPackRequestData method the merchant number McH_id is 10000098 String(32) The merchant number assigned by wechat Pay // the random String nonce_str is 5 k8264iltkch16cq2502si8znmtm67vs String (32) random String, No longer than a 32-bit / / signature sign is C380BEC2BFD727A4B6845133519F3AD6 String (32) can be found in the signature generation algorithm / / public accounts appid wxappid wx8888888888888888 String(32) Public account ID assigned by wechat (enterprise ID corpid is appId). Appid, a mobile application applied for on wechat's open platform (open.weixin.qq.com), cannot use this interface. ResultMap = this.sendRedPack(data); // resultMap <String, String> resultMap = this.sendRedPack(data); WXPayUtil.getLogger().info("wxPay.sendRedPack:" + resultMap);

    return resultMap;
}
Copy the code

The above is the interface call code, for the interface call of the input parameter and the output parameter, small partners look at the official website document oh, interface notes to give you a reference.

The official also provides an interface for distributing fission red packets. If you need it, you can check the document address:

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4
Copy the code

Split Red Packets: You can distribute one group of red packets at a time. Seed users receive one of a set of red envelopes first and can give the rest to other users through social sharing. Fission red envelope makes full use of the advantages of interpersonal communication.

Query red envelope Records

Used for merchants to query the specific information of the red envelope, which can support ordinary red envelope and fission packet.

This interface is very simple, is to query the record of the red envelope has been sent, according to the merchant order number query merchant red envelope.

Interface link

https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo
Copy the code

Whether certificate is required

is

Call interface

Application code:

** @author yclimb * @date 2018/9/18 */ private void getHbInfo() throws Exception {WXPay WXPay = new WXPay(AsydWXPayConfigImpl.getInstance()); Map<String, String> resultMap = wxPay.getHbInfo("1502348237482342342");
    System.out.println("wxPay.getHbInfo:" + resultMap);
}
Copy the code

Query interface code:

/** * Function: query red envelope records <br> * Scenario: Used for merchants to query the specific information of the red envelope, which can support ordinary red envelope and fission packet. * Interface document address: https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_6&index=5 * * @ param McH_billno merchant order number * @return* @throws Exception e */ public Map<String, String> getHbInfo(String McH_billno) throws Exception {/** Construct request parameter data **/ Map<String, String> data = new HashMap<>(); / / merchant order number McH_billno is 10000098201411111234567890 String (28) merchant order number (each order number must be unique. Value range: 0 to 9, A to Z, a to Z) The interface supports reentrance based on the merchant order number. If a timeout occurs, the interface can be invoked again. data.put("mch_billno", mch_billno); // Order type bill_type is MCHT String(32) MCHT: obtains the red envelope information by the merchant order number. data.put("bill_type"."MCHT"); /** Set the following parameters: In this. FillRequestData the merchant number McH_id is 10000098 String(32) The merchant number assigned by wechat Pay // the random String nonce_str is 5 k8264iltkch16cq2502si8znmtm67vs String (32) random String, No longer than a 32-bit / / signature sign is C380BEC2BFD727A4B6845133519F3AD6 String (32) can be found in the signature generation algorithm / / public accounts appid appid is wx8888888888888888 String(32) Public account ID assigned by wechat (enterprise ID corpid is appId). Appid, a mobile application applied for on wechat's open platform (open.weixin.qq.com), cannot use this interface. ResultMap = this.gethbinfo (data); // resultMap <String, String> resultMap = this.gethbinfo (data); WXPayUtil.getLogger().info("wxPay.getHbInfo:" + resultMap);

    return resultMap;
}
Copy the code

This interface obtains the red envelope information through the merchant order number, very simple, for the return parameter friends can view the official document, pay attention to the error code.

conclusion

Above for the merchant platform to open cash red envelope, designated user release, red envelope record query related explanation and source, partners must pay attention to look at the official document oh, the specific source can see the author’s Github, in the face of each method has a detailed note.

If you have a problem that you can’t solve, you can follow the author’s wechat official account and join a discussion group to ask questions and solve them with your friends

Notice: the next article will talk about another way to distribute rewards merchant platform voucher or instant discount opening, designated user release, query, please look forward to!!

If you want to see the friend of the source in advance, can first take a look at my lot, address is as follows: https://github.com/YClimb/wxpay-sdk/blob/master/README.md

Follow the author’s wechat official account, click on the discussion group below, scan the code to join the wechat Pay discussion group with your friends

To this article is over, pay attention to the public account to view more push!!