package com.github.wxpay.sdk;
import com.alibaba.fastjson.JSON;
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 javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName: AsingleCollecting
* @description: 分账相关代码
* @date: 2021/11/6/0006 星期六 下午 14:50
* @Company: 鑫开拓电子商务有限公司
* @author: lwc
* @version: V1.0
* @Copyright: Copyright (c) 2021
*/
public class AsingleCollecting {
public static void main(String[] args) {
//1.参数封装
Map param=new HashMap();
//公众账号ID
param.put("appid", "wx83343e1a6fe356b4");
//商户号
param.put("mch_id", "123");
//微信支付分配的子商户号,即分账的出资商户号。
param.put("sub_mch_id", "1234");
//随机字符串
param.put("nonce_str", WXPayUtil.generateNonceStr());
//签名类型,目前只支持HMAC-SHA256
param.put("sign_type", "HMAC-SHA256");
//微信支付订单号
param.put("transaction_id", "42000011802021111061727606018");
/**
* 服务商系统内部的分账单号,在服务商系统内部唯一
* (单次分账、多次分账、完结分账应使用不同的商户分账单号),
* 同一分账单号多次请求等同一次。只能是数字、大小写字母_-|*@
*/
param.put("out_order_no", "9065994835145096287");
/**
* 分账接收方列表,不超过50个json对象,不能设置出资子商户作为分账接受方
点击行前的+展开字段详情
*/
List<Map> receivers=new ArrayList<Map>();
Map receiver=new HashMap();
/**
* -分账接收方类型
* MERCHANT_ID:商户号(mch_id或者sub_mch_id)
PERSONAL_OPENID:个人openid(由父商户APPID转换得到)
PERSONAL_SUB_OPENID: 个人sub_openid(由子商户APPID转换得到)
*/
receiver.put("type","PERSONAL_OPENID");
/**
* 类型是MERCHANT_ID时,是商户号(mch_id或者sub_mch_id)
类型是PERSONAL_OPENID时,是个人openid
类型是PERSONAL_SUB_OPENID时,是个人sub_openid
*/
receiver.put("account","openid");
/**
* -分账金额
* 分账金额,单位为分,只能为整数,不能超过原订单支付金额及最大分账比例金额
*/
receiver.put("amount",100);
/**
* -分账描述
*
* 分账的原因描述,分账账单中需要体现
*/
receiver.put("description","旺旺拿到钱了");
/**
* -分账个人接收方姓名
* 可选项,在接收方类型为个人的时可选填,若有值,会检查与 name 是否实名匹配,不匹配会拒绝分账请求
1、分账接收方类型是PERSONAL_OPENID时,是个人姓名(选传,传则校验)
*/
//receiver.put("name","这人很强");
Map receiver1=new HashMap();
/**
* -分账接收方类型
* MERCHANT_ID:商户号(mch_id或者sub_mch_id)
PERSONAL_OPENID:个人openid(由父商户APPID转换得到)
PERSONAL_SUB_OPENID: 个人sub_openid(由子商户APPID转换得到)
*/
receiver1.put("type","PERSONAL_OPENID");
/**
* 类型是MERCHANT_ID时,是商户号(mch_id或者sub_mch_id)
类型是PERSONAL_OPENID时,是个人openid
类型是PERSONAL_SUB_OPENID时,是个人sub_openid
*/
receiver1.put("account","
openid
");
/**
* -分账金额
* 分账金额,单位为分,只能为整数,不能超过原订单支付金额及最大分账比例金额
*/
receiver1.put("amount",100);
/**
* -分账描述
*
* 分账的原因描述,分账账单中需要体现
*/
receiver1.put("description","雷哥拿到钱了");
receivers.add(receiver1);
receivers.add(receiver);
param.put("receivers", JSON.toJSONString(receivers));
try {
String xmlParam = WXPayUtil.generateSignedXml(param, "24F9997A3EBC2161AE1C61141720C6DA", WXPayConstants.SignType.HMACSHA256);
xmlParam = new String(xmlParam.getBytes("UTF-8"),"ISO8859-1");
//2.发送请求
// String xmlResult = HttpClientUtil.PostXml(url, xmlParam);
// String s = WxUtils.httpPostRequest(url, xmlParam);
// System.out.println(xmlResult);
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
KeyStore keyStore = getCertificate("1608677221");
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, "1608677221".toCharArray()).build();
SSLConnectionSocketFactory sslf = new SSLConnectionSocketFactory(sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslf).build();
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/profitsharing");
StringEntity reqEntity = new StringEntity(xmlParam);
// 设置类型
reqEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(reqEntity);
// String result = null;
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String resultStr = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println("微信响应报文:"+resultStr);
EntityUtils.consume(httpEntity);
// m.put("xmlResult",xmlResult);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @description: 获取微信证书
* @param mch_id
* @auther: lijinquan
* @date: 2019-11-11
*/
public static KeyStore getCertificate(String mch_id){
//try-with-resources 关流
try{
FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\test\\apiclient_cert.p12"));
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(inputStream, mch_id.toCharArray());
return keyStore;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}