Java微信企业付款

语言:Java(基于 SpringBoot 实现方案)、XML(微信在 HTTP 协议中的数据传输方案)
工具包:XML 解析包(JDOM),HTTP 请求工具包(ApacheHttp),MD5 签名包(JDK 自带)

一:账号申请
1、需要到微信商户平台进行账号申请,获取商户号(mch_id)和密钥(key)
2、需要在微信开放平台申请注册一个 APP(移动支付、公众号、小程序需要到微信公众平台获取对应的服务 ID),获取 APP ID(app_id)

二:实现代码

package com.fxkj.photo.app.pay.component;

import com.alibaba.fastjson.JSONObject;
import com.fxkj.common.result.Result;
import com.fxkj.common.util.wachat.*;
import com.fxkj.photo.app.pay.constants.WeChatPayConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
 import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.KeyStore;
import java.util.Map;
import java.util.TreeMap;

 /**
 * @Author admin
 * @Description 微信接口
 */
@Slf4j
@Component public class WeChatPayComponent {
    @Autowired
    private WeChatPayConstants weChatPayConstants; 

     /** * 微信提现(企业付款)
     * @param request
     * @param response
     * @param params
     * @return
     */
     public Result weChatWithdrawal(HttpServletRequest request, HttpServletResponse response , Map params) {
            String nonce_str = WXUtil.getNonceStr();
            TreeMap parameters = new TreeMap();             parameters.put("mch_appid",weChatPayConstants.WX_WITHDRAWAL_APP_ID);
            parameters.put("mchid", weChatPayConstants.WX_MCH_ID);
            parameters.put("nonce_str", nonce_str);    
            parameters.put("partner_trade_no", params.get("partner_trade_no"));
            parameters.put("openid", params.get("openId"));
            parameters.put("check_name", weChatPayConstants.WX_WITHDRAWAL_CHECK_NAME);            
            parameters.put("amount", "100");
            parameters.put("desc", weChatPayConstants.WX_WITHDRAWAL_DESC); parameters.put("spbill_create_ip",  request.getRemoteAddr());
            parameters.put("sign", WeChatUtils.createSign("UTF-8", parameters,weChatPayConstants.WX_PARTNER_KEY));
            String resContent = "";
            String tosend = WeChatUtils.getRequestXml(parameters);
            try {
                resContent = httpsRefundRequest(weChatPayConstants.WX_WITHDRAWAL_GATE_URL, "POST", tosend,weChatPayConstants.WX_MCH_ID);
                Map map = XMLUtil.doXMLParse(resContent);
                if (map.get("return_code").equalsIgnoreCase("SUCCESS") && map.get("result_code").equalsIgnoreCase("SUCCESS")) {
                    return Result.error(0 ,map.get("return_msg"));
                } else {
                    return Result.error(1 ,map.get("return_msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return Result.error(1 ,"FAIL");
        }

    /**
    * 企业付款请求方法
    * @param requestUrl
    * @param requestMethod
    * @param outputStr
    * @param mch_id
    * @return
    * @throws Exception
    */
    @SuppressWarnings("deprecation")
    private String httpsRefundRequest(String requestUrl, String requestMethod, String outputStr, String mch_id) throws Exception { 
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        StringBuilder res = new StringBuilder("");
        ClassPathResource classPathResource = new ClassPathResource("apiclient_cert.p12");
        InputStream instream = classPathResource.getInputStream();
        try {
            keyStore.load(instream, mch_id.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mch_id.toCharArray()).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null,         SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            HttpPost httpost = new HttpPost(requestUrl);
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            StringEntity entity2 = new StringEntity(outputStr, Consts.UTF_8);
            httpost.setEntity(entity2);  
            CloseableHttpResponse response = httpclient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(entity.getContent()));
                    String text = "";
                    res.append(text);
                    while ((text = bufferedReader.readLine()) != null) {
                        res.append(text);
                    }  
                }
                EntityUtils.consume(entity);
            } finally { response.close();
        }
     } finally {
         httpclient.close();
     }
     return res.toString();
 }

package com.fxkj.photo.app.pay.constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Author admin
* @Description 微信 常量配置
*/
@Component
public class WeChatPayConstants {

     /** * 微信支付商户号 */
     @Value("${wx.pay.app.mchId}")
     public String WX_MCH_ID;

     /** * 商户号对应的密钥 */
     @Value("${wx.pay.app.privateKey}")
     public String WX_PARTNER_KEY;

    /** * 校验用户姓名选项 * 1、NO_CHECK:不校验真实姓名 * 2、FORCE_CHECK:强校验真实姓名 */     @Value("${wx.withdrawal.app.check_name}")
     public String WX_WITHDRAWAL_CHECK_NAME;

     /** * 公众号APPID */
     @Value("${wx.withdrawal.app.appId}")
     public String WX_WITHDRAWAL_APP_ID;

     /** * 请求Url */
     @Value("${wx.withdrawal.app.gateUrl}")
     public String WX_WITHDRAWAL_GATE_URL;

    /** * 企业付款备注 */
    @Value("${wx.withdrawal.app.desc}")
    public String WX_WITHDRAWAL_DESC;
}


你可能感兴趣的:(Java微信企业付款)