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

零下一度
Release: 2017-05-19 16:18:03
Original
4658 people have browsed it

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:html;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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!