当前位置: 首页 > news >正文

Java 微信商家打款到零钱(旧版本接口)

旧版微信支付接口要求请求时携带证书请求

构建请求参数

    /*** 付款到零钱** @param withdrawalDto dto撤军* @return {@link Map }<{@link String }, {@link Object }>* @throws Exception 异常* @Author chen 2023-07-27 15:04*/private Map<String, Object> payToUser(WithdrawalDto withdrawalDto) throws Exception {log.info("发起微信提现零钱请求");Map<String, String> paramMap = new HashMap<>();// 公众账号appidparamMap.put("mch_appid", wechatConfig.getAppId());// 微信支付分配的商户号paramMap.put("mchid", wechatConfig.getMchId());// 随机字符串,不长于32位。paramMap.put("nonce_str", RandomUtil.randomString(16));// 商户订单号,需保持唯一性paramMap.put("partner_trade_no", withdrawalDto.getSerialNo());// 商户appid下,某用户的openidparamMap.put("openid", withdrawalDto.getCardNo());//校验用户姓名选项   NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名paramMap.put("check_name", "FORCE_CHECK");//收款用户姓名,如果check_name设置为FORCE_CHECK,则必填用户真实姓名paramMap.put("re_user_name", withdrawalDto.getIdCardName());// 企业付款金额,金额必须为整数 单位为分//元转分BigDecimal applyMoney = withdrawalDto.getApplyMoney();BigDecimal amount = applyMoney.multiply(BigDecimal.valueOf(100));paramMap.put("amount", String.valueOf(amount.intValue()));// 企业付款描述信息paramMap.put("desc", "钱包提现");// 根据微信签名规则,生成签名paramMap.put("sign", WechatUtils.sign(paramMap, wechatConfig.getMchApiKey()));log.info("请求参数:{}", paramMap);// 把参数转换成XML数据格式String xmlParam = XmlUtil.mapToXmlStr(paramMap, "xml");String resXml = WechatUtils.httpsSSLPost(wechatConfig.getWithdrawalHost(), wechatConfig.getMchId(), xmlParam);Map<String, Object> resultMap = XmlUtil.xmlToMap(resXml);log.info("响应报文:{}", resultMap);return resultMap;}

签名

/*** 微信支付签名sign** @param param* @param key* @return {@link String }* @Author chen 2023-07-27 14:37*/public static String sign(Map<String, String> param, String key) {try {//签名步骤一:按字典排序参数List<Object> list = new ArrayList<>(param.keySet());Object[] ary = list.toArray();Arrays.sort(ary);list = Arrays.asList(ary);StringBuilder str = new StringBuilder();for (Object o : list) {str.append(o).append("=").append(param.get(String.valueOf(o))).append("&");}//签名步骤二:加上keystr.append("key=").append(key);//步骤三:加密并大写return DigestUtil.md5Hex(str.toString(), "utf-8").toUpperCase();} catch (Exception e) {log.error("微信签名失败:{}", e.getMessage());return null;}}

http 请求工具类--携带证书请求

证书路径为相对路径(放在 resources 目录下):youPath/cert.p12

/*** https ssl post** @param url    url* @param mchId* @param params 参数个数* @return {@link String }* @throws Exception 异常* @Author chen 2023-07-27 14:45*/public static String httpsSSLPost(String url, String mchId, String params) throws Exception {String text = "";// 指定读取证书格式为PKCS12KeyStore keyStore = KeyStore.getInstance(CERT_TYPE);// 读取本机存放的PKCS12证书文件try (InputStream stream = ResourceUtil.getStream(CERT_P12_PATH)) {// 指定PKCS12的密码(商户ID)keyStore.load(stream, mchId.toCharArray());}// Trust own CA and all self-signed certsSSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();// Allow TLSv1 protocol only// 指定TLS版本SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{TLS}, null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);// 设置httpclient的SSLSocketFactorytry (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {HttpPost post = new HttpPost(url);StringEntity s = new StringEntity(params, "utf-8");s.setContentType(CONTENT_TYPE);post.setEntity(s);HttpResponse res = httpclient.execute(post);HttpEntity entity = res.getEntity();text = EntityUtils.toString(entity, "utf-8");}return text;}

http://www.lryc.cn/news/105821.html

相关文章:

  • Vue+Element ui Study
  • JAVA基础-多线程入门(详解)
  • Cirno‘s Perfect Equation Class 2023牛客暑期多校训练营5 D
  • pytorch学习——如何构建一个神经网络——以手写数字识别为例
  • PySpark 数据操作
  • FPGA2-采集OV5640乒乓缓存后经USB3.0发送到上位机显示
  • 亚信科技AntDB数据库专家参加向量数据库首次技术标准研讨会
  • Windows中实现右键把电子书通过邮件发到kindle
  • Three.js之创建3D场景
  • 一个3年Android的找工作记录
  • CAS原理解析
  • SQL项目实战:银行客户分析
  • 【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群指令分析—实战篇)
  • ubuntu
  • 【芯片设计- RTL 数字逻辑设计入门 3- Verdi 常用使用命令】
  • python-pytorch基础之cifar10数据集使用图片分类
  • 华纳云:linux下磁盘管理与挂载硬盘方法是什么
  • ChatGPT + Stable Diffusion + 百度AI + MoviePy 实现文字生成视频,小说转视频,自媒体神器!(一)
  • linux strcpy/strncpy/sprintf内存溢出问题
  • Jmeter如何添加插件
  • flask---CBV使用和源码分析
  • Qt 实现压缩文件、文件夹和解压缩操作zip
  • 简单工厂模式VS策略模式
  • 【MySQL】触发器 (十二)
  • 听说 Spring Bean 的创建还有一条捷径?
  • 大数据课程E6——Flume的Processor
  • 实现邮箱管理之gmail邮箱、office365(Azure)邮箱之披荆斩棘问题一览
  • (AcWing)多重背包问题 I,II
  • 如何把几个视频合并在一起?视频合并方法分享
  • 【MyBatis】初学MyBatis