热销系统

如何开通微信商户打款到用户微信零钱功能

发表时间:2020-05-18 18:11

开通条件:

  1、商户号已入驻90日

  2、商户号有30天连续正常交易

两条红色的条件至关重要。

3、 登录微信支付商户平台-产品中心,开通企业付款。

登录微信支付商户平台,通过网页充值功能充值(商户平台-交易中心)。

温馨提示:


商户平台登录帐号及密码,会通过开户邮件发放给企业;

商户平台可支持企业设置不同的操作员帐号(商户平台-账户中心-员工帐号管理),分配不同的操作权限;

涉及资金操作的功能,安全性要求较高,需要操作员安装证书(商户平台-账户中心-操作证书);

充值的资金,企业可自助提现至结算账户(商户平台-交易中心-提现)。

付款

通过API或网页操作,付款至目标用户(企业可根据APPID+OpenID锁定目标用户)。针对已实名认证的用户,微信支付可提供校验真实姓名一致性的可选功能。

温馨提示:


◆ 给同一个实名用户付款,单笔单日限额2W/2W


◆ 不支持给非实名用户打款


◆ 一个商户同一日付款总额限额100W


◆ 仅支持商户号已绑定的APPID;◆ 针对付款的目标用户,已微信支付实名认证的用户可提供校验真实姓名的功能,未实名认证的用户无法校验,企业可根据自身业务的安全级别选择验证类型;◆ 付款金额必须小于或等于商户当前可用余额的金额;◆ 已付款的记录,企业可通过企业付款查询查看相应数据。

到账

付款资金将进入目标用户的零钱(微信-我-钱包-零钱)。微信支付将做零钱入账消息通知,零钱收支明细会展示相应记录。

温馨提示:

针对无零钱账户的历史客户端版本,资金将进入用户的红包账户,微信支付无消息通知用户,企业可选择自行触达用户。


以下是技术具体实现细节


2、这第一步就是获取微信用户的openid,这个就不赘述了,只要配置对了,经过那几步请求基本上就能获取到openid。

3、获取证书:微信支付接口中,涉及资金回滚的接口会使用到商户证书,包括退款、撤销接口。商家在申请微信支付成功后,收到的相应邮件后,可以按照指引下载API证书,也可以按照以下路径下载:微信商户平台(pay.weixin.qq.com)–>账户设置–>API安全–>证书下载 。证书文件有四个,详情可点击此处安全规范到最下面的商户证书查看。


4、使用https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers接口地址付款,就是加上一系列的请求参数,把一些参数以字典序排序加上商户key签名得到sign字段,最后再请求接口这样。

5、比较麻烦的地方就是这个证书问题就是这个要使用证书去发起https请求的问题。我请求的使用就一直报 证书问题:Certificate for <api.mch.weixin.qq.com> doesn't match any of the subject alternative names,后面我在微信支付那边例子代码找到了加上证书发起请求的例子就成功了。地址 : 微信支付例子地址

6、下面我就贴完整微信支付企业付款到零钱代码

WeChatWithdrawUtils.java 微信支付企业付款到零钱主文件

import com.rw.common.Constants;

import com.rw.common.utils.MD5Utils;

import com.rw.common.utils.OtherUtils;

import com.rw.common.utils.http.HttpClient;

import com.rw.common.utils.http.HttpUtils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.config.RegistryBuilder;

import org.apache.http.conn.socket.ConnectionSocketFactory;

import org.apache.http.conn.socket.PlainConnectionSocketFactory;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.impl.conn.BasicHttpClientConnectionManager;

import org.apache.http.util.EntityUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import sun.net.www.protocol.https.DefaultHostnameVerifier;


import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.security.KeyStore;

import java.security.SecureRandom;

import java.util.*;


/**

*

* 所需lib

*   <dependency>

            <groupId>org.apache.httpcomponents</groupId>

            <artifactId>httpclient</artifactId>

            <version>4.5.3</version>

        </dependency>

* **/



/**

* @author Zhou Zhong Qing

* @Title: ${file_name}

* @Package ${package_name}

* @Description: 微信提现

* @date 2018/6/25 16:57

*/

public class WeChatWithdrawUtils {


    private static final Logger log = LoggerFactory.getLogger(WeChatWithdrawUtils.class);

    private byte[] certData;


    public   InputStream getCertStream() {

        ByteArrayInputStream certBis;

        certBis = new ByteArrayInputStream(this.certData);

        return certBis;

    }


    public WeChatWithdrawUtils() throws Exception{

        String certPath = Constants.CERT_PATH;

        File file = new File(certPath);

        InputStream certStream = new FileInputStream(file);

        this.certData = new byte[(int) file.length()];

        certStream.read(this.certData);

        certStream.close();

    }




    public static void main(String[] args) {

        try {


           String result = withdrawRequestOnce(new HashMap<>(),3000,3000,true);

           /*<xml>

<return_code><![CDATA[SUCCESS]]></return_code>

<return_msg><![CDATA[支付失败]]></return_msg>

<mch_appid><![CDATA[wx317e16f8818682cf]]></mch_appid>

<mchid><![CDATA[1507675211]]></mchid>

<result_code><![CDATA[FAIL]]></result_code>

<err_code><![CDATA[NOTENOUGH]]></err_code>

<err_code_des><![CDATA[请到商户平台充值后再重试.]]></err_code_des>

</xml>*/

          Map<String,String> resultMap = OtherUtils.readStringXmlOut(result);

          log.info(resultMap.toString());


          if(resultMap.containsKey("result_code") && "SUCCESS".equals(resultMap.getOrDefault("result_code",""))){

                //TODO 成功

          }else{

                //TODO 失败

          }

        }catch (Exception e){

            e.printStackTrace();

        }

    }


    /**

     *

     * 提现

     * 请求,只请求一次,不做重试

     * @param connectTimeoutMs

     * @param readTimeoutMs

     * @return

     * @throws Exception

     */

    public static String withdrawRequestOnce(   Map<String,String> params, int connectTimeoutMs, int readTimeoutMs,boolean useCert) throws Exception {


        Map<String, String> paraMap = new HashMap<>();

        paraMap.put("mch_appid", Constants.OPEN_ANDROID_APP_ID);

        paraMap.put("mchid",Constants.MCH_ID);

        paraMap.put("nonce_str", OtherUtils.getNonceStr());

        paraMap.put("partner_trade_no","qianchen"+System.currentTimeMillis());

        paraMap.put("openid",params.getOrDefault("openId",""));//"o5mZ40yBjIqco2NzKc19k9oIBI9o");

        // 校验用户姓名选项 NO_CHECK:不校验真实姓名   FORCE_CHECK:强校验真实姓名

        paraMap.put("check_name","NO_CHECK");

        paraMap.put("amount",params.getOrDefault("money",""));//"100");

        //企业付款操作说明信息。必填。

        paraMap.put("desc",params.getOrDefault("clientId","")+"用户提现");

        paraMap.put("spbill_create_ip",params.getOrDefault("spbillCreateIp",""));


        String url = OtherUtils.formatUrlMap(paraMap, false, false);

        url = url + "&key=" + Constants.MCH_SECRET;


        String sign = MD5Utils.MD5Encoding(url).toUpperCase();


        StringBuffer xml = new StringBuffer();

        xml.append("<xml>");

        for (Map.Entry<String, String> entry : paraMap.entrySet()) {

            xml.append("<" + entry.getKey() + ">");

            xml.append(entry.getValue());

            xml.append("</" + entry.getKey() + ">" + "\n");

        }

        xml.append("<sign>");

        xml.append(sign);

        xml.append("</sign>");

        xml.append("</xml>");


        log.info("xml {} ", xml.toString());


        BasicHttpClientConnectionManager connManager;

        if (useCert) {

            // 证书

            char[] password = Constants.MCH_ID.toCharArray();

            InputStream certStream =   new WeChatWithdrawUtils().getCertStream();

            KeyStore ks = KeyStore.getInstance("PKCS12");

            ks.load(certStream, password);


            // 实例化密钥库 & 初始化密钥工厂

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

            kmf.init(ks, password);


            // 创建 SSLContext

            SSLContext sslContext = SSLContext.getInstance("TLS");

            sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());


            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(

                    sslContext,

                    new String[]{"TLSv1"},

                    null,

                    new org.apache.http.conn.ssl.DefaultHostnameVerifier());


            connManager = new BasicHttpClientConnectionManager(

                    RegistryBuilder.<ConnectionSocketFactory>create()

                            .register("http", PlainConnectionSocketFactory.getSocketFactory())

                            .register("https", sslConnectionSocketFactory)

                            .build(),

                    null,

                    null,

                    null

            );

        }

        else {

            connManager = new BasicHttpClientConnectionManager(

                    RegistryBuilder.<ConnectionSocketFactory>create()

                            .register("http", PlainConnectionSocketFactory.getSocketFactory())

                            .register("https", SSLConnectionSocketFactory.getSocketFactory())

                            .build(),

                    null,

                    null,

                    null

            );

        }


        org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create()

                .setConnectionManager(connManager)

                .build();



        HttpPost httpPost = new HttpPost(Constants.WITHDRAW_URL);


        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();

        httpPost.setConfig(requestConfig);


        StringEntity postEntity = new StringEntity(xml.toString(), "UTF-8");

        httpPost.addHeader("Content-Type", "text/xml");

        httpPost.addHeader("User-Agent", "wxpay sdk java v1.0 " + Constants.MCH_ID);   // TODO: 很重要,用来检测 sdk 的使用情况,要不要加上商户信息?

        httpPost.setEntity(postEntity);


        HttpResponse httpResponse = httpClient.execute(httpPost);

        HttpEntity httpEntity = httpResponse.getEntity();

        return EntityUtils.toString(httpEntity, "UTF-8");

    }



}


分享到:
广州码达品牌管理有限公司     
官方公众号
首页
防伪溯源  
二维码营销
马 上 咨 询
姓名
*
手机
*
提交
申请免费试用
180-2632-1315
隐形暗码防窜
代理商管理系统
分销电商系统
留言