Table of Contents
Preparation
Cash red envelopes are divided into two categories:
com.javen.weixin.api.RedPackApi.java
Home WeChat Applet WeChat Development Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

May 19, 2017 pm 04:18 PM

Welcome to leave messages and forwards

WeChat speed development series of articles: Click here

The previous articles introduced WeChat payment. Official account payment, WeChat scan code payment, card payment, WeChat payment

This article will talk about the payment tool in WeChat merchants - cash red envelope

Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

Cash red envelope.png

Preparation

[Official Document]

1. Activate cash red envelope permissions

Before using cash red envelopes, please go to activate the cash red envelope function. Operation path: [Log in to the WeChat Payment Merchant Platform——>Product Center——>Cash Red Envelope——>Activate].

2. Download the APIcertificate

When a merchant calls the WeChat red envelopeinterface, the server will perform certificate verification. Please download the certificate on the merchant platform

Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

Cash Red Envelope - Download Certificate.png

Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

Cash Red Packet-File after downloading.png

Someone wants to ask which one to use with so many certificates? Does it have anything to do with development language?

Which language uses which certificate, and how to use it? Please refer to the Certificate Description.txt in the screenshot

The content is as follows:

欢迎使用微信支付!
微信支付API共四份(证书pkcs12格式、证书pem格式、证书密钥pem格式、CA证书),为接口中强制要求时需携带的证书文件。
证书属于敏感信息,请妥善保管不要泄露和被他人复制。
不同开发语言下的证书格式不同,以下为说明指引:
    证书pkcs12格式(apiclient_cert.p12)
        包含了私钥信息的证书文件,为p12(pfx)格式,由微信支付签发给您用来标识和界定您的身份
        部分安全性要求较高的API需要使用该证书来确认您的调用身份
        windows上可以直接双击导入系统,导入过程中会提示输入证书密码,证书密码默认为您的商户ID(如:10010000)
    证书pem格式(apiclient_cert.pem)
        从apiclient_cert.p12中导出证书部分的文件,为pem格式,请妥善保管不要泄漏和被他人复制
        部分开发语言和环境,不能直接使用p12文件,而需要使用pem,所以为了方便您使用,已为您直接提供
        您也可以使用openssl命令来自己导出:openssl pkcs12 -clcerts -nokeys -in apiclient_cert.p12 -out apiclient_cert.pem
    证书密钥pem格式(apiclient_key.pem)
        从apiclient_cert.p12中导出密钥部分的文件,为pem格式
        部分开发语言和环境,不能直接使用p12文件,而需要使用pem,所以为了方便您使用,已为您直接提供
        您也可以使用openssl命令来自己导出:openssl pkcs12 -nocerts -in apiclient_cert.p12 -out apiclient_key.pem
    CA证书(rootca.pem)
        微信支付api服务器上也部署了证明微信支付身份的服务器证书,您在使用api进行调用时也需要验证所调用服务器及域名的真实性
        该文件为签署微信支付证书的权威机构的根证书,可以用来验证微信支付服务器证书的真实性
        某些环境和工具已经内置了若干权威机构的根证书,无需引用该证书也可以正常进行验证,这里提供给您在未内置所必须根证书的环境中载入使用
Copy after login

3. Recharge

Before issuing cash red envelopes, please make sure you have sufficient funds. If it is insufficient, please top up. Operation path: [Log in to the merchant platform——>Account Center——>Fund Management——>Recharge]

The average amount of each red envelope must be between 1.00 yuan and 200.00 yuan

Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

Cash red envelope-recharge.png

4. Obtain openid

Currently, it is supported to issue red envelopes of specified amounts to the openid of designated WeChat users. (To obtain openid, please refer to the previously written article: Quickly develop WeChat public account authorization to obtain user information

5. Related parameter settings

and parameters related to red envelopes [mainly security aspects: binding IP, preventing users from stealing], you You can set and change it independently on the page.
The operation path is as follows: [Log in to the merchant platform->Product Center->Cash Red Packet->Product Settings] (Note: The "Product Settings" button is only available when It will only appear after you activate the cash red envelope function). You can set and change the following parameters:

Share an example tutorial on how to develop cash red envelopes on WeChat public accounts

Cash red envelope-setting parameters.png

Instructions:

1. Calling IP address: After setting, only the The set IP address can be called, and other IP calls will report an error;
2. User collection limit: limit the number of the same openid received on the same day
3. Anti-brushing level: Anti-brushing refers to WeChat risk control targeting WeChat accounts and zombies To intercept the number, machine number, etc., you can control the intensity of the anti-swipe by changing the anti-swipe level.
4. At the same time, you can also apply to change the red envelope amount, but it will only take effect after being approved by WeChat Pay. .
Cash red envelope categories

Cash red envelopes are divided into two categories:

1. Ordinary red envelopes [official documents]
2. Fission red envelopes [official documents]


Some parameters are different between the two

Send cash red envelopes

1 , send ordinary red envelope interface

com.javen.weixin.api.RedPackApi.java

private static String sendRedPackUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";

    /**
     * 发送红包
     * @param params 请求参数
     * @param certPath 证书文件目录
     * @param partner 证书密码
     * @return {String}
     */
    public static String sendRedPack(Map<String, String> params, String certPath, String partner) {
        return HttpUtils.postSSL(sendRedPackUrl, PaymentKit.toXml(params), certPath, partner);
    }
Copy after login
2. Send fission red envelope interface com.javen.weixin.api.RedPackApi.java

private static String sendGroupRedPackUrl = "api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack";

    /**
     * 发送裂变红包
     * @param params 请求参数
     * @param certPath 证书文件目录
     * @param partner 证书密码
     * @return {String}
     */
    public static String sendGroupRedPack(Map<String, String> params, String certPath, String partner) {
        return HttpUtils.postSSL(sendGroupRedPackUrl, PaymentKit.toXml(params), certPath, partner);
    }
Copy after login
Specific implementation package com.javen .utils.ReadPackUtils.java

1. Ordinary red packet<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>/** * 发送普通红包 * @param request 获取IP * @param total_amount 付款现金(单位分) * @param total_num 红包发放总人数 * @param wishing 红包祝福语 * @param act_name 活动名称 * @param remark 备注 * @param reOpenid 用户openid * @param partner 商户号 * @param wxappid 公众账号appid * @param sendName 商户名称 * @param paternerKey 商户签名key * @param certPath 证书路径 * @return */ public static boolean sendredpack(HttpServletRequest request,String total_amount,String total_num,String wishing,String act_name,String remark,String reOpenid,String partner,String wxappid,String sendName,String paternerKey,String certPath) { // 商户订单号 String mchBillno = System.currentTimeMillis() + &quot;&quot;; String ip = IpKit.getRealIp(request); Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); // 随机字符串 params.put(&quot;nonce_str&quot;, System.currentTimeMillis() / 1000 + &quot;&quot;); // 商户订单号 params.put(&quot;mch_billno&quot;, mchBillno); // 商户号 params.put(&quot;mch_id&quot;, partner); // 公众账号ID params.put(&quot;wxappid&quot;, wxappid); // 商户名称 params.put(&quot;send_name&quot;, sendName); // 用户OPENID params.put(&quot;re_openid&quot;, reOpenid); // 付款现金(单位分) params.put(&quot;total_amount&quot;, total_amount); // 红包发放总人数 params.put(&quot;total_num&quot;, total_num); // 红包祝福语 params.put(&quot;wishing&quot;, wishing); // 终端IP params.put(&quot;client_ip&quot;, ip); // 活动名称 params.put(&quot;act_name&quot;, act_name ); // 备注 params.put(&quot;remark&quot;, remark); //创建签名 String sign = PaymentKit.createSign(params, paternerKey); params.put(&quot;sign&quot;, sign); String xmlResult = RedPackApi.sendRedPack(params, certPath, partner); Map&lt;String, String&gt; result = PaymentKit.xmlToMap(xmlResult); log.warn(JsonKit.toJson(result)); //此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断 String return_code = result.get(&quot;return_code&quot;); //业务结果 String result_code = result.get(&quot;result_code&quot;); if (StrKit.isBlank(return_code) || !&quot;SUCCESS&quot;.equals(return_code)) { return false; } if (StrKit.notBlank(result_code) &amp;&amp; &quot;SUCCESS&quot;.equals(result_code)) { return true; } return false; }</pre><div class="contentsignin">Copy after login</div></div>2. Fission red packet

/**
     * 发送裂变红包
     * @param partner
     * @param wxappid
     * @param sendName
     * @param reOpenid
     * @param total_amount
     * @param total_num
     * @param wishing
     * @param act_name
     * @param remark
     * @param paternerKey
     * @param certPath
     * @return
     */
    public static boolean sendGroupRedPack(String partner, String wxappid, String sendName, String reOpenid, String total_amount, String total_num, String wishing, String act_name, String remark, String paternerKey, String certPath){
        // 商户订单号
        String mchBillno = System.currentTimeMillis() + "";

        Map<String, String> params = new HashMap<String, String>();
        // 随机字符串
        params.put("nonce_str", System.currentTimeMillis() / 1000 + "");
        // 商户订单号
        params.put("mch_billno", mchBillno);
        // 商户号
        params.put("mch_id", partner);
        // 公众账号ID
        params.put("wxappid", wxappid);
        // 商户名称
        params.put("send_name", sendName);
        // 用户OPENID
        params.put("re_openid", reOpenid);
        // 付款现金(单位分)
        params.put("total_amount", total_amount);
        // 红包发放总人数
        params.put("total_num", total_num);
        //红包金额设置方式
        params.put("amt_type", "ALL_RAND");
        // 红包祝福语
        params.put("wishing", wishing);
        // 活动名称
        params.put("act_name", act_name    );
        // 备注
        params.put("remark", remark);

        //创建签名
        String sign = PaymentKit.createSign(params, paternerKey);
        params.put("sign", sign);

        String xmlResult = RedPackApi.sendGroupRedPack(params, certPath, partner);
        Map<String, String> result = PaymentKit.xmlToMap(xmlResult);
        log.warn(JsonKit.toJson(result));
        //此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断
        String return_code = result.get("return_code");
        //业务结果
        String result_code = result.get("result_code");

        if (StrKit.isBlank(return_code) || !"SUCCESS".equals(return_code)) {
            return false;
        }
        if (StrKit.notBlank(result_code) && "SUCCESS".equals(result_code)) {

            return true;
        }
        return false;

    }
Copy after login

Send red packet Demo

/**
 * 微信红包demo
 * @author Javen
 * 2016年5月28日
 */
public class RedPackApiController extends Controller {
    private static String sendName = "Javen205";
    //微信证书路径
    private static String certPath = "/Users/Javen/Downloads/cert/apiclient_cert.p12";
    //商户相关资料
    String wxappid = PropKit.get("appId");
    // 微信支付分配的商户号
    String partner = PropKit.get("mch_id");
    //API密钥
    String paternerKey = PropKit.get("paternerKey");

    /**
     * 发送普通红包
     */
    public void sendredpack() {

        boolean isSend = ReadPackUtils.sendredpack(getRequest(), "100", "1", "感谢您参加猜灯谜活动,祝您元宵节快乐!",
                "猜灯谜抢红包活动", "猜越多得越多,快来抢!", "o_pncsidC-pRRfCP4zj98h6slREw",
                partner, wxappid, sendName, paternerKey, certPath);

        renderJson(isSend);
    }
    /**
     * 发送裂变红包
     */
    public void sendGroupRedPack() {

        boolean isSend = ReadPackUtils.sendGroupRedPack(partner, wxappid, "天虹百货", "o_pncsidC-pRRfCP4zj98h6slREw", 
                "100", "10", "感谢您参加猜灯谜活动,祝您元宵节快乐!", "猜灯谜抢红包活动",
                "猜越多得越多,快来抢", paternerKey, certPath);


        renderJson(isSend);
    }


    public void query() {
        String query = ReadPackUtils.query("10000098201411111234567890", partner, wxappid, paternerKey, certPath);
        renderJson(query);
    }

}
Copy after login

Query red packet record

【Query red packet record official document】

Interface encapsulation

com.javen.weixin .api.RedPackApi.java

private static String getHBInfo = "api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo ";

    /**
     * 根据商户订单号查询信息
     * @param params 请求参数
     * @param certPath 证书文件目录
     * @param partner 证书密码
     * @return {String}
     */
    public static String getHbInfo(Map<String, String> params, String certPath, String partner) {
        return HttpUtils.postSSL(getHBInfo, PaymentKit.toXml(params), certPath, partner);
    }
Copy after login
Specific implementation package com.javen.utils.ReadPackUtils.java

/**
     * 根据商户订单号查询红包
     * @param mch_billno 商户订单号
     * @param partner 商户号
     * @param wxappid 公众账号ID
     * @param paternerKey 商户签名Key
     * @param certPath 证书路径
     * @return
     */
    public static String  query(String mch_billno,String partner,String wxappid,String paternerKey,String certPath) {
        Map<String, String> params = new HashMap<String, String>();
        // 随机字符串
        params.put("nonce_str", System.currentTimeMillis() / 1000 + "");
        // 商户订单号
        params.put("mch_billno", mch_billno);
        // 商户号
        params.put("mch_id", partner);
        // 公众账号ID
        params.put("appid", wxappid);
        params.put("bill_type", "MCHT");
        //创建签名
        String sign = PaymentKit.createSign(params, paternerKey);
        params.put("sign", sign);

        String xmlResult = RedPackApi.getHbInfo(params, certPath, partner);
        Map<String, String> result = PaymentKit.xmlToMap(xmlResult);
        System.out.println(result);
        return JsonKit.toJson(result);
    }
Copy after login
【Related recommendations】

1.

WeChat public account platform source code download

2. WeChat voting Source code

3. WeChat People Network v3.4.5 Advanced Business Edition WeChat Rubik’s Cube source code

The above is the detailed content of Share an example tutorial on how to develop cash red envelopes on WeChat public accounts. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
PHP WeChat development: How to implement message encryption and decryption PHP WeChat development: How to implement message encryption and decryption May 13, 2023 am 11:40 AM

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

Using PHP to develop WeChat mass messaging tools Using PHP to develop WeChat mass messaging tools May 13, 2023 pm 05:00 PM

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

PHP WeChat development: How to implement user tag management PHP WeChat development: How to implement user tag management May 13, 2023 pm 04:31 PM

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

PHP WeChat development: How to implement group message sending records PHP WeChat development: How to implement group message sending records May 13, 2023 pm 04:31 PM

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

Steps to implement WeChat public account development using PHP Steps to implement WeChat public account development using PHP Jun 27, 2023 pm 12:26 PM

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform

PHP WeChat development: How to implement voting function PHP WeChat development: How to implement voting function May 14, 2023 am 11:21 AM

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

PHP WeChat development: How to implement customer service chat window management PHP WeChat development: How to implement customer service chat window management May 13, 2023 pm 05:51 PM

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

How to use PHP for WeChat development? How to use PHP for WeChat development? May 21, 2023 am 08:37 AM

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform

See all articles