ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

微信服务商分账代码

2021-11-08 10:34:13  阅读:179  来源: 互联网

标签:mch 微信 param 分账 服务商 put import id


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);
}
}

}

标签:mch,微信,param,分账,服务商,put,import,id
来源: https://www.cnblogs.com/lwukong/p/15523056.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有