微信商户批量转账到零钱
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、微信商户批量转账到零钱
- 1.引入库
- 2.获取商户序列号密钥
- 2.获取签名
- 2.RFC3339日期转换
- 总结
前言
微信商户批量转账
一、微信商户批量转账到零钱
1.引入库
代码如下(示例):
/*** 微信支付专用类 请求操作方法** @author Administrator*/
public class WeChatNewUtil {private static Logger log = Logger.getLogger(WeChatNewUtil.class);public static String transferBatch="/v3/transfer/batches";public static String domain="https://api.mch.weixin.qq.com";public static int connectionTimeout=10000;public static int readTimeout=10000;public static String privatekeypath = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"/weixin/apiclient_key.pem";//从微信商户平台下载的密钥存放的路径/*** 发起批量转账API 批量转账到零钱* @param url 全路径地址* @param requestJson 请求body* @param mchID4M 商户id* @param method 请求方法(get,post)* @param requestUrl (域名后面的请求url)* @return*/public static String postTransBatRequest(String url,String requestJson,String mchID,String method,String requestUrl) {CloseableHttpClient httpclient = HttpClients.createDefault();CloseableHttpResponse response = null;HttpEntity entity = null;try {String payserialNo= PemUtil.loadCertificate().getSerialNumber().toString(16).toUpperCase();//商户私钥证书HttpPost httpPost = new HttpPost(url);httpPost.addHeader("Content-Type", "application/json");httpPost.addHeader("Accept", "application/json");httpPost.addHeader("Wechatpay-Serial", payserialNo);//-------------------------核心认证 start-----------------------------------------------------------------String strToken = V3Util.getToken(method,requestUrl,requestJson,mchID,payserialNo, privatekeypath);// 添加认证信息httpPost.addHeader("Authorization","WECHATPAY2-SHA256-RSA2048" + " "+ strToken);//---------------------------核心认证 end---------------------------------------------------------------RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout).setConnectTimeout(connectionTimeout).build();httpPost.setConfig(requestConfig);if(requestJson!=null)httpPost.setEntity(new StringEntity(requestJson, "UTF-8"));//发起转账请求response = httpclient.execute(httpPost);entity = response.getEntity();//获取返回的数据return EntityUtils.toString(entity);} catch (Exception e) {e.printStackTrace();} finally {// 关闭流}return null;}/*** get查询订单状态* @param url 全路径地址* @param requestJson 请求body* @param mchID4M 商户id* @param method 请求方法(get,post)* @param requestUrl (域名后面的请求url)* @return*/public static String getTransBatRequest(String url,String requestJson,String mchID,String method,String requestUrl) {CloseableHttpClient httpclient = HttpClients.createDefault();CloseableHttpResponse response = null;HttpEntity entity = null;try {String payserialNo= PemUtil.loadCertificate().getSerialNumber().toString(16).toUpperCase();//商户私钥证书HttpGet httpGet = new HttpGet(url);httpGet.addHeader("Content-Type", "application/json");httpGet.addHeader("Accept", "application/json");httpGet.addHeader("Wechatpay-Serial", payserialNo);//-------------------------核心认证 start-----------------------------------------------------------------String strToken = V3Util.getToken(method,requestUrl,requestJson,mchID,payserialNo, privatekeypath);// 添加认证信息httpGet.addHeader("Authorization","WECHATPAY2-SHA256-RSA2048" + " "+ strToken);//---------------------------核心认证 end---------------------------------------------------------------RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout).setConnectTimeout(connectionTimeout).build();httpGet.setConfig(requestConfig);//发起转账请求response = httpclient.execute(httpGet);entity = response.getEntity();//获取返回的数据return EntityUtils.toString(entity);} catch (Exception e) {e.printStackTrace();} finally {// 关闭流}return null;}}
2.获取商户序列号密钥
代码如下(示例):
public class PemUtil {public static PrivateKey loadPrivateKey(String privateKey) {privateKey = privateKey.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "").replaceAll("\\s+", "");try {KeyFactory kf = KeyFactory.getInstance("RSA");return kf.generatePrivate(new PKCS8EncodedKeySpec(new Base64().decode(privateKey)));} catch (NoSuchAlgorithmException e) {throw new RuntimeException("当前Java环境不支持RSA", e);} catch (InvalidKeySpecException e) {throw new RuntimeException("无效的密钥格式");}}public static PrivateKey loadPrivateKey() throws Exception {String certPath1 = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"/weixin/apiclient_key.pem";//从微信商户平台下载的密钥存放的路径File file1 = new File(certPath1);InputStream inputStream = new FileInputStream(file1);ByteArrayOutputStream os = new ByteArrayOutputStream(2048);byte[] buffer = new byte[1024];String privateKey;try {for (int length; (length = inputStream.read(buffer)) != -1; ) {os.write(buffer, 0, length);}privateKey = os.toString("UTF-8");} catch (IOException e) {throw new IllegalArgumentException("无效的密钥", e);}return loadPrivateKey(privateKey);}public static X509Certificate loadCertificate() throws Exception {try {String certPath1 = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"/weixin/apiclient_cert.pem";//从微信商户平台下载的序列号存放的路径File file1 = new File(certPath1);InputStream inputStream = new FileInputStream(file1);CertificateFactory cf = CertificateFactory.getInstance("X509");X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);cert.checkValidity();return cert;} catch (CertificateExpiredException e) {throw new RuntimeException("证书已过期", e);} catch (CertificateNotYetValidException e) {throw new RuntimeException("证书尚未生效", e);} catch (CertificateException e) {throw new RuntimeException("无效的证书", e);}}private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";public static String encryptOAEP(String message, X509Certificate certificate) throws Exception {return encrypt(message, certificate, TRANSFORMATION);}/*** 敏感信息加密*/public static String encrypt(String message, X509Certificate certificate, String transformation) throws Exception {try {Cipher cipher = Cipher.getInstance(transformation);cipher.init(Cipher.ENCRYPT_MODE, certificate.getPublicKey());byte[] data = message.getBytes(StandardCharsets.UTF_8);byte[] ciphertext = cipher.doFinal(data);return new Base64().encodeToString(ciphertext);} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);} catch (InvalidKeyException e) {throw new IllegalArgumentException("无效的证书", e);} catch (IllegalBlockSizeException | BadPaddingException e) {throw new IllegalBlockSizeException("加密原串的长度不能超过214字节");}}}
2.获取签名
代码如下(示例):
public class V3Util {private static Logger log = Logger.getLogger(V3Util.class);/*** * @param method 请求方法 post* @param canonicalUrl 请求地址* @param body 请求参数* @param merchantId 这里用的商户号* @param certSerialNo 商户证书序列号* @param keyPath 商户证书地址* @return* @throws Exception*/public static String getToken(String method,String canonicalUrl,String body,String merchantId,String certSerialNo,String keyPath) throws Exception {String signStr = "";//获取32位随机字符串String nonceStr = getRandomString(32);//当前系统运行时间long timestamp = System.currentTimeMillis() / 1000;if (StringUtils.isEmpty(body)) {body = "";}//签名操作String message = buildMessage(method, canonicalUrl, timestamp, nonceStr, body);//签名操作String signature = sign(message.getBytes("utf-8"), keyPath);//组装参数signStr = "mchid=\"" + merchantId + "\",timestamp=\"" + timestamp+ "\",nonce_str=\"" + nonceStr+ "\",serial_no=\"" + certSerialNo + "\",signature=\"" + signature + "\"";return signStr;}public static String buildMessage(String method, String canonicalUrl, long timestamp, String nonceStr, String body) {return method + "\n" + canonicalUrl + "\n" + timestamp + "\n" + nonceStr + "\n" + body + "\n";}public static String sign(byte[] message, String keyPath) throws Exception {Signature sign = Signature.getInstance("SHA256withRSA");sign.initSign(getPrivateKey(keyPath));sign.update(message);return Base64.encodeBase64String(sign.sign());}/*** 微信支付-前端唤起支付参数-获取商户私钥** @param filename 私钥文件路径 (required)* @return 私钥对象* @throws Exception */public static PrivateKey getPrivateKey(String filename) throws Exception {return PemUtil.loadPrivateKey();}/*** 获取随机位数的字符串* @param length* @return*/public static String getRandomString(int length) {String base = "abcdefghijklmnopqrstuvwxyz0123456789";Random random = new Random();StringBuffer sb = new StringBuffer();for (int i = 0; i < length; i++) {int number = random.nextInt(base.length());sb.append(base.charAt(number));}return sb.toString();}
}
2.RFC3339日期转换
代码如下(示例):
public static String getDateByRFC3339(String time){DateTime dateTime = new DateTime(time);long timeInMillis = dateTime.toCalendar(Locale.getDefault()).getTimeInMillis();Date date = new Date(timeInMillis);SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmsss");String time = format.format(date);return time;}
总结
这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了微信商户批量转账到零钱,需要注意的是敏感信息需要进行加密处理,请求时需要获取序列号和签名
微信商户批量转账到零钱相关推荐
- Java微信企业(批量)转账到零钱(超详细)
文章目录 一.引入jar包 二.yml里面配置支付 三.java 实现支付的步骤 (1)在controller里面写一个支付接口 (2) EntPayReq 的实体类对象 (3)业务层 WxEntPa ...
- 微信商户平台转账到零钱功能接入实战
1.背景说明 2.实现过程 2.1 接入之前的准备工作 2.2 代码实现 3.注意事项以及相关说明 3.1 参数组装说明 ...
- PHP微信支付 “商家转账到零钱”一文概述
PHP微信支付 商家转账到零钱 这里有个坑 1:转账低于5毛会失败 2:转账金额需要自己取整一下,微信官方金额是 分 为单位,换算成 元 时可能会除不尽 { "code":&quo ...
- 微信支付 商家转账到零钱 Api 过程
企业付款到零钱,已经限额500以内,完全不够用 商家转账到零钱,限额20000单笔,需要申请要按不通过原因修改多试几次 准备前提: 1,平台证书,商家证书,一共2个: 2,设置APIv3密钥: 正文: ...
- 支付宝批量转账到零钱
1. 服务集成灵活:可集成到商户自身业务系统,无需登录支付宝. 2. 资金实时到账:付款资金实时到账,方便安全快捷. 3. 支持大额支付:日.月限额高,支持动态额度提升. 4. 实时到账通知:收款方可 ...
- php微信提现(商家转账到零钱)
/*** 微信提现**/public function ty_commission(){ //这里是自己的逻辑代码,查询这个提现订单的信息等.....$batch_name = '余额提现';//转账 ...
- 微信支付:商家转账到零钱的开发
主要所需:1.微信商户平台的证书apiclient_cert.pem 2.微信商户平台证书的密钥apiclient_key.pem 3.微信商户平台的证书的序列号 一.转账所需字段 public cl ...
- 平台资金提现解决方案之实现微信商家转账到零钱功能
大家好,我是小悟 使用场景 不管是做APP.电脑网站.手机网站还是小程序,为了推广基本上都离不开用户分佣的场景. 换句话说就是在其平台内为每个用户设置"电子钱包"功能,而电子钱包在 ...
- 基于微信云开发的商家转账至零钱
基于微信云开发的商家转账至零钱 相关简介 开通功能 阅读文档 技术框架 配置环境 编写代码 注意事项 相关参考 相关简介 本篇文章主要介绍如何通过微信云开发的云函数实现商家转账到零钱.(让那些没有服务 ...
- cms小猪o2o企业付款配置中微信提现配置实现商家转账到零钱(企业付款到零钱)解决:“操作失败!产品权限验证失败,请查看您当前是否具有该产品的权限“的错误提示
先说下概念什么是商家转账到零钱?其实这个功能是由企业付款到零钱功能演变过来的,微信支付里面在2022年5月之前这个功能一直叫"企业付款到零钱"后来因为业务需求改成了"商家 ...
最新文章
- SQL Server查询某个字段存在哪些表中
- poj 1144 割点和桥
- 电脑温度检测软件哪个好_实时检测Mac电脑的温度
- 我不是在吓你,但是区块链,可能真的是普通人弯道超车的最后一个机会了。
- 用Redis存储Tomcat集群的Session
- 【推荐实践】微博多尺度序列推荐算法实践.pdf(附下载链接)
- Java操作Json工具——Jackson
- JAVA-常量、运算符、类型转换、Scanner
- 1,3-丁二烯(BD)行业调研报告 - 市场现状分析与发展前景预测
- 网关信息认证服务器不可达,网关消息认证服务器不可达
- LG 源代码或被盗,如何才能毫发无损地要回来?
- 转 海量数据处理(2)
- 剑指offer——面试题17:合并两个排序的链表
- 广州爱立信java笔试题_爱立信笔试经历
- 基于Scrapy+MySQL爬取国家药监局100w+数据踩坑记录
- 电商产品设计:如何设计产品分销体系
- oracle系统优化
- 全力配合金融改革,尝试期货投资基金
- 【LaTeX】子图和图片并排
- 【转】RFire系列免杀教程
热门文章
- android sqlite多条件查询,sqlite 多条件查询
- Arduino 操作BT008蓝牙串口模块
- JDK源码阅读之Long
- Java求 a+aa+aaa+aaaa+a....a 的值
- 智能驾驶的狂想与现实落地
- 期货交易常用术语中英文对照表
- 日语---之百度百科
- win11右键菜单缺少“新建“选项解决办法
- matlab 空间向量的夹角,空间两向量之间的旋转角如何求?角度范围在0-360°
- 备课手记:把Ken Olsen换成姚明