Pu Zhibing
2025-01-04 a015e7c916474bb51b33c228f690aa17f30c746c
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/util/payment/PaymentUtil.java
@@ -1,12 +1,11 @@
package com.ruoyi.order.util.payment;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.*;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.order.util.payment.model.*;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
/**
@@ -48,6 +47,7 @@
   public static UniPayResult uniPay(String orderNo, Double amount, String productName, String productDesc, String mp, String notifyUrl, String openId, String tradeMerchantNo){
      String url = "https://trade.joinpay.com/tradeRt/uniPay";
      HttpRequest post = HttpUtil.createPost(url);
      post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
      JSONObject body = new JSONObject();
      //版本号
      body.put("p0_Version", "2.5");
@@ -82,7 +82,7 @@
         throw new RuntimeException(e);
      }
      body.put("hmac", sign);
      post.body(body.toString());
      post.form(body);
      log.info("支付接口请求参数:" + body);
      HttpResponse execute = post.execute();
      log.info("支付接口请求响应:" + execute.body());
@@ -103,6 +103,7 @@
   public static QueryOrderResult queryOrder(String orderNo){
      String url = "https://trade.joinpay.com/tradeRt/queryOrder";
      HttpRequest post = HttpUtil.createPost(url);
      post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
      JSONObject body = new JSONObject();
      //版本号
      body.put("p0_Version", "2.5");
@@ -117,7 +118,7 @@
         throw new RuntimeException(e);
      }
      body.put("hmac", sign);
      post.body(body.toString());
      post.form(body);
      log.info("查询支付接口请求参数:" + body);
      HttpResponse execute = post.execute();
      log.info("查询支付接口请求响应:" + execute.body());
@@ -141,6 +142,7 @@
   public static RefundResult refund(String orderNo, String refundOrderNo, Double refundAmount, String notifyUrl){
      String url = "https://trade.joinpay.com/tradeRt/refund";
      HttpRequest post = HttpUtil.createPost(url);
      post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
      JSONObject body = new JSONObject();
      //版本号
      body.put("p0_Version", "2.3");
@@ -161,7 +163,7 @@
         throw new RuntimeException(e);
      }
      body.put("hmac", sign);
      post.body(body.toString());
      post.form(body);
      log.info("退款接口请求参数:" + body);
      HttpResponse execute = post.execute();
      log.info("退款接口请求响应:" + execute.body());
@@ -182,6 +184,7 @@
   public static QueryRefundResult queryRefund(String refundOrderNo){
      String url = "https://trade.joinpay.com/tradeRt/refund";
      HttpRequest post = HttpUtil.createPost(url);
      post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
      JSONObject body = new JSONObject();
      //版本号
      body.put("p0_Version", "2.3");
@@ -196,7 +199,7 @@
         throw new RuntimeException(e);
      }
      body.put("hmac", sign);
      post.body(body.toString());
      post.form(body);
      log.info("退款接口请求参数:" + body);
      HttpResponse execute = post.execute();
      log.info("退款接口请求响应:" + execute.body());
@@ -217,6 +220,7 @@
   public static CloseOrderResult closeOrder(String orderNo){
      String url = "https://www.joinpay.com/trade/closeOrder.action";
      HttpRequest post = HttpUtil.createPost(url);
      post.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded");
      JSONObject body = new JSONObject();
      //商户编号
      body.put("p1_MerchantNo", merchantNo);
@@ -231,7 +235,7 @@
         throw new RuntimeException(e);
      }
      body.put("hmac", sign);
      post.body(body.toString());
      post.form(body);
      log.info("关闭订单接口请求参数:" + body);
      HttpResponse execute = post.execute();
      log.info("关闭订单接口请求响应:" + execute.body());
@@ -265,12 +269,16 @@
         }
      }
      sb.append(key);
      log.info("待签名串:{}", sb.toString());
      return MD5AndKL.MD5(sb.toString());
   }
   
   
   public static void main(String[] args) {
      UniPayResult uniPayResult = PaymentUtil.uniPay("123456", 0.01D, "测试商品", "这是用于对接支付测试的商品描述", "", "", "", "");
      System.err.println(JSON.toJSONString(uniPayResult));
//      UniPayResult uniPayResult = PaymentUtil.uniPay("852963742", 0.01D, "测试商品", "这是用于对接支付测试的商品描述",
//            "", "/order/shopping-cart/shoppingCartPaymentCallback", "ooOrs64zHLuInkZ_GF0LpIN9_Rxc", "777168500885852");
//      PaymentUtil.queryOrder("852963742");
//      PaymentUtil.closeOrder("852963742");
   }
}