搜索
热搜: 活动 交友 discuz
查看: 6674|回复: 0

[JAVA] JAVA接入讯虎支付接口调用

[复制链接]

57

主题

58

帖子

1232

积分

超级版主

Rank: 8Rank: 8

积分
1232
发表于 2024-6-21 17:35:11 | 显示全部楼层 |阅读模式
  1. @RestController

  2. @RequestMapping("/payment")

  3. public class XunhupayController {



  4.     @PostMapping("pay")

  5.     public String pay () {

  6.         // appid

  7.         String appid = "2019061250......;

  8.         // appsecret

  9.         String appsecret = "95c75affab606258fc.......";

  10.         // 请求路径

  11.         String url = "https://api.xunhupay.com/payment/do.html";



  12.         // 设置 传递参数的集合,方便 传递数据。

  13.         Map options = new HashMap<>();

  14.         // 必填 设置版本号

  15.         options.put("version","1.1");

  16.         // 必填 设置 appid

  17.         options.put("appid",appid);



  18.         // 密钥不需要直接传递

  19.         //options.put("appsecret",appsecret);



  20.         // 必填 订单号 具体内容自己控制 长度 32位 官网说 请确保在当前网站内是唯一订单号,具体含义 我测试了 在描述 此备注

  21.         options.put("trade_order_id","1");



  22.         // 必填 价格 精确到RMB分

  23.         options.put("total_fee","0.01");



  24.         // 必填 标题

  25.         options.put("title","测试使用的title");



  26.         // 必填 当前时间戳 调用 刚写的方法 getSecondTimestamp

  27.         options.put("time", getSecondTimestamp(new Date()));



  28.         // 必填 通知回调地址 url 什么含义 我们后台需要知道 用户支付了。

  29.         options.put("notify_url","https://zanglikun.cn1.utools.club/paycallback"); // 只有这个有用



  30.         // 非必填 使用 响应字段中 url 就直接跳到百度了,如果访问,url_qrcode ,不会直接跳转,只有当支付完成后,再次刷新 url_qrcode中的连接,才会跳转。

  31.         options.put("return_url","http://localhost:8080/pages/xunhupay/payok.html");



  32.         // 非必填 用户取消支付,跳转的页面   经过测试,没有触发机制,建议不传递

  33.         options.put("callback_url","https://www.sina.com.cn/");



  34.         //plugins 非必填 备注信息

  35.         options.put("plugins","我是备注信息");



  36.         // nonce_str  必填 随机值 32位内  作用: 1.避免服务器页面缓存,2.防止安全密钥被猜测出来(md5 密钥越复杂,就越难解密出来)

  37.         options.put("nonce_str","740969606");



  38.         // 定义 sb 为了获取 MD5 加密前的字符串

  39.         StringBuilder sb = new StringBuilder();



  40.         // 将HashMap 进行 键 的 Ascll 从小到大排序 并 将每个 hashmap元素 以 & 拼接起来

  41.         options.entrySet().stream().sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey())).forEach(a ->{

  42.             sb.append(a).append("&");});



  43.         // 去除 最后一位的 &

  44.         sb.deleteCharAt(sb.length()-1);

  45.         // 拼接上密钥

  46.         sb.append(appsecret);



  47.         // 调用 Hutool 的 加密工具 进行 MD5 加密

  48.         String s = SecureUtil.md5(sb.toString());



  49.         // 输出hash结果 postman 要用

  50.         System.out.println("我们生成的Hash 是:"+s);

  51.         // 输出time结果 postman 要用

  52.         System.out.println("我们生成的time 是: "+options.get("time"));



  53.         System.out.println();

  54.         // 必填 hash 签名

  55.         options.put("hash", s);





  56.         System.out.println("我们传递的参数有:"+options.toString());

  57.         System.out.println("开始调 虎皮椒支付 接口...");



  58.         // 调用 Hutool 的HttpUtil 发送 post 请求

  59.         String post = HttpUtil.post(url, options);



  60.         System.out.println("结束调 虎皮椒支付 接口...\n");

  61.         System.out.println("虎皮椒支付 接口 响应的结果是:"+post+"\n");



  62.         // 说明:这里 因为虎皮椒支付 响应结果 不统一,正确是Json;不正确 就是一行String 。没办法 判断是否请求是否有效。所以 只能通过 是由能够解析成json 有无异常判断 是否调用成功

  63.         try{

  64.             Map map = (Map) JSON.parse(post);

  65.             map.keySet().stream().forEach(k -> {

  66.                 if (k == "url") {

  67.                     System.out.println("url二维码链接是: "+map.get(k));

  68.                 }

  69.             });

  70.         }catch (Exception e){

  71.             e.printStackTrace();

  72.             System.out.println("调 虎皮椒支付 时 出现了问题");

  73.         }



  74.         return  post;

  75.     }



  76.     /**

  77.      * 获取精确到秒的时间戳   原理 获取毫秒时间戳,因为 1秒 = 100毫秒 去除后三位 就是秒的时间戳

  78.      * @return

  79.      */

  80.     public static int getSecondTimestamp(Date date){

  81.         if (null == date) {

  82.             return 0;

  83.         }

  84.         String timestamp = String.valueOf(date.getTime());

  85.         int length = timestamp.length();

  86.         if (length > 3) {

  87.             return Integer.valueOf(timestamp.substring(0,length-3));

  88.         } else {

  89.             return 0;

  90.         }

  91.     }



  92.     @RequestMapping("/query")

  93.     @ResponseBody

  94.     public String query(){

  95.         Map options = new HashMap<>();



  96.         // appid

  97.         String appid = "201906125050";

  98.         // appsecret

  99.         String appsecret = "95c75affab606258fca4942d89dc1b4e";



  100.        //https://api.xunhupay.com/payments/wechat/index?id=20213321543&

  101.         // nonce_str=0130460042&time=1642003004&appid=201906125050&hash=3eeea1c287cf9310b6ef395a9b04d313



  102.         // 必填 设置 appid

  103.         options.put("appid","201906125050");



  104.         // 商户网站订单号

  105.         options.put("out_trade_order","1");



  106.         // 虎皮椒内部订单号

  107.         options.put("open_order_id","20213321543");



  108.         // 当前时间戳

  109.         options.put("time",getSecondTimestamp(new Date()));



  110.         // 随机值

  111.         options.put("nonce_str","0130460042");





  112.         // 定义 sb 为了获取 MD5 加密前的字符串

  113.         StringBuilder sb = new StringBuilder();



  114.         // 将HashMap 进行 键 的 Ascll 从小到大排序 并 将每个 hashmap元素 以 & 拼接起来

  115.         options.entrySet().stream().sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey())).forEach(a ->{

  116.             sb.append(a).append("&");});



  117.         // 去除 最后一位的 &

  118.         sb.deleteCharAt(sb.length()-1);

  119.         // 拼接上密钥

  120.         sb.append(appsecret);



  121.         // 调用 Hutool 的 加密工具 进行 MD5 加密

  122.         String s = SecureUtil.md5(sb.toString());



  123.         // 输出hash结果 postman 要用

  124.         System.out.println("我们生成的Hash 是:"+s);

  125.         // 输出time结果 postman 要用

  126.         System.out.println("我们生成的time 是: "+options.get("time"));



  127.         System.out.println();

  128.         // 必填 hash 签名

  129.         options.put("hash", s);



  130.         // 调用 Hutool 的HttpUtil 发送 post 请求

  131.         String post = HttpUtil.post("https://api.xunhupay.com/payment/query.html", options);



  132.         return post;

  133.     }





  134. }
复制代码






上一篇:FileUtils文件处理操作
下一篇:PPT导出-模板和自定义处理方案
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

抖音账号
关注抖音
加入粉丝群,抽取论坛金币


Archiver|手机版|小黑屋|IT趣-GEEK社区

GMT+8, 2024-9-8 09:19 , Processed in 0.073932 second(s), 22 queries .

Powered by Discuz! X3.4. 技术支持 by 巅峰设计

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表