| 
 | 
	
 
- @RestController
 
  
- @RequestMapping("/payment")
 
  
- public class XunhupayController {
 
  
 
 
-     @PostMapping("pay")
 
  
-     public String pay () {
 
  
-         // appid
 
  
-         String appid = "2019061250......;
 
  
-         // appsecret
 
  
-         String appsecret = "95c75affab606258fc.......";
 
  
-         // 请求路径
 
  
-         String url = "https://api.xunhupay.com/payment/do.html";
 
  
 
 
-         // 设置 传递参数的集合,方便 传递数据。
 
  
-         Map options = new HashMap<>();
 
  
-         // 必填 设置版本号
 
  
-         options.put("version","1.1");
 
  
-         // 必填 设置 appid
 
  
-         options.put("appid",appid);
 
  
 
 
-         // 密钥不需要直接传递
 
  
-         //options.put("appsecret",appsecret);
 
  
 
 
-         // 必填 订单号 具体内容自己控制 长度 32位 官网说 请确保在当前网站内是唯一订单号,具体含义 我测试了 在描述 此备注
 
  
-         options.put("trade_order_id","1");
 
  
 
 
-         // 必填 价格 精确到RMB分
 
  
-         options.put("total_fee","0.01");
 
  
 
 
-         // 必填 标题
 
  
-         options.put("title","测试使用的title");
 
  
 
 
-         // 必填 当前时间戳 调用 刚写的方法 getSecondTimestamp
 
  
-         options.put("time", getSecondTimestamp(new Date()));
 
  
 
 
-         // 必填 通知回调地址 url 什么含义 我们后台需要知道 用户支付了。
 
  
-         options.put("notify_url","https://zanglikun.cn1.utools.club/paycallback"); // 只有这个有用
 
  
 
 
-         // 非必填 使用 响应字段中 url 就直接跳到百度了,如果访问,url_qrcode ,不会直接跳转,只有当支付完成后,再次刷新 url_qrcode中的连接,才会跳转。
 
  
-         options.put("return_url","http://localhost:8080/pages/xunhupay/payok.html");
 
  
 
 
-         // 非必填 用户取消支付,跳转的页面   经过测试,没有触发机制,建议不传递
 
  
-         options.put("callback_url","https://www.sina.com.cn/");
 
  
 
 
-         //plugins 非必填 备注信息
 
  
-         options.put("plugins","我是备注信息");
 
  
 
 
-         // nonce_str  必填 随机值 32位内  作用: 1.避免服务器页面缓存,2.防止安全密钥被猜测出来(md5 密钥越复杂,就越难解密出来)
 
  
-         options.put("nonce_str","740969606");
 
  
 
 
-         // 定义 sb 为了获取 MD5 加密前的字符串
 
  
-         StringBuilder sb = new StringBuilder();
 
  
 
 
-         // 将HashMap 进行 键 的 Ascll 从小到大排序 并 将每个 hashmap元素 以 & 拼接起来
 
  
-         options.entrySet().stream().sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey())).forEach(a ->{
 
  
-             sb.append(a).append("&");});
 
  
 
 
-         // 去除 最后一位的 &
 
  
-         sb.deleteCharAt(sb.length()-1);
 
  
-         // 拼接上密钥
 
  
-         sb.append(appsecret);
 
  
 
 
-         // 调用 Hutool 的 加密工具 进行 MD5 加密
 
  
-         String s = SecureUtil.md5(sb.toString());
 
  
 
 
-         // 输出hash结果 postman 要用
 
  
-         System.out.println("我们生成的Hash 是:"+s);
 
  
-         // 输出time结果 postman 要用
 
  
-         System.out.println("我们生成的time 是: "+options.get("time"));
 
  
 
 
-         System.out.println();
 
  
-         // 必填 hash 签名
 
  
-         options.put("hash", s);
 
  
 
 
 
 
-         System.out.println("我们传递的参数有:"+options.toString());
 
  
-         System.out.println("开始调 虎皮椒支付 接口...");
 
  
 
 
-         // 调用 Hutool 的HttpUtil 发送 post 请求
 
  
-         String post = HttpUtil.post(url, options);
 
  
 
 
-         System.out.println("结束调 虎皮椒支付 接口...\n");
 
  
-         System.out.println("虎皮椒支付 接口 响应的结果是:"+post+"\n");
 
  
 
 
-         // 说明:这里 因为虎皮椒支付 响应结果 不统一,正确是Json;不正确 就是一行String 。没办法 判断是否请求是否有效。所以 只能通过 是由能够解析成json 有无异常判断 是否调用成功
 
  
-         try{
 
  
-             Map map = (Map) JSON.parse(post);
 
  
-             map.keySet().stream().forEach(k -> {
 
  
-                 if (k == "url") {
 
  
-                     System.out.println("url二维码链接是: "+map.get(k));
 
  
-                 }
 
  
-             });
 
  
-         }catch (Exception e){
 
  
-             e.printStackTrace();
 
  
-             System.out.println("调 虎皮椒支付 时 出现了问题");
 
  
-         }
 
  
 
 
-         return  post;
 
  
-     }
 
  
 
 
-     /**
 
  
-      * 获取精确到秒的时间戳   原理 获取毫秒时间戳,因为 1秒 = 100毫秒 去除后三位 就是秒的时间戳
 
  
-      * @return
 
  
-      */
 
  
-     public static int getSecondTimestamp(Date date){
 
  
-         if (null == date) {
 
  
-             return 0;
 
  
-         }
 
  
-         String timestamp = String.valueOf(date.getTime());
 
  
-         int length = timestamp.length();
 
  
-         if (length > 3) {
 
  
-             return Integer.valueOf(timestamp.substring(0,length-3));
 
  
-         } else {
 
  
-             return 0;
 
  
-         }
 
  
-     }
 
  
 
 
-     @RequestMapping("/query")
 
  
-     @ResponseBody
 
  
-     public String query(){
 
  
-         Map options = new HashMap<>();
 
  
 
 
-         // appid
 
  
-         String appid = "201906125050";
 
  
-         // appsecret
 
  
-         String appsecret = "95c75affab606258fca4942d89dc1b4e";
 
  
 
 
-        //https://api.xunhupay.com/payments/wechat/index?id=20213321543&
 
  
-         // nonce_str=0130460042&time=1642003004&appid=201906125050&hash=3eeea1c287cf9310b6ef395a9b04d313
 
  
 
 
-         // 必填 设置 appid
 
  
-         options.put("appid","201906125050");
 
  
 
 
-         // 商户网站订单号
 
  
-         options.put("out_trade_order","1");
 
  
 
 
-         // 虎皮椒内部订单号
 
  
-         options.put("open_order_id","20213321543");
 
  
 
 
-         // 当前时间戳
 
  
-         options.put("time",getSecondTimestamp(new Date()));
 
  
 
 
-         // 随机值
 
  
-         options.put("nonce_str","0130460042");
 
  
 
 
 
 
-         // 定义 sb 为了获取 MD5 加密前的字符串
 
  
-         StringBuilder sb = new StringBuilder();
 
  
 
 
-         // 将HashMap 进行 键 的 Ascll 从小到大排序 并 将每个 hashmap元素 以 & 拼接起来
 
  
-         options.entrySet().stream().sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey())).forEach(a ->{
 
  
-             sb.append(a).append("&");});
 
  
 
 
-         // 去除 最后一位的 &
 
  
-         sb.deleteCharAt(sb.length()-1);
 
  
-         // 拼接上密钥
 
  
-         sb.append(appsecret);
 
  
 
 
-         // 调用 Hutool 的 加密工具 进行 MD5 加密
 
  
-         String s = SecureUtil.md5(sb.toString());
 
  
 
 
-         // 输出hash结果 postman 要用
 
  
-         System.out.println("我们生成的Hash 是:"+s);
 
  
-         // 输出time结果 postman 要用
 
  
-         System.out.println("我们生成的time 是: "+options.get("time"));
 
  
 
 
-         System.out.println();
 
  
-         // 必填 hash 签名
 
  
-         options.put("hash", s);
 
  
 
 
-         // 调用 Hutool 的HttpUtil 发送 post 请求
 
  
-         String post = HttpUtil.post("https://api.xunhupay.com/payment/query.html", options);
 
  
 
 
-         return post;
 
  
-     }
 
  
 
 
 
 
- }
 
  复制代码 
 
 |   
 
上一篇: FileUtils文件处理操作下一篇: PPT导出-模板和自定义处理方案 
 
 |