diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java index 63ed4af..a94dd3d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUsdtDemoController.java @@ -12,21 +12,9 @@ import com.ruoyi.system.service.PaymentSignatureService; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.http.HttpStatus; +import org.springframework.http.*; import org.springframework.http.client.SimpleClientHttpRequestFactory; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestClientResponseException; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; @@ -40,7 +28,9 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -/** 公开支付演示入口;加密凭据仅从服务端环境变量读取。 */ +/** + * 公开支付演示入口;加密凭据仅从服务端环境变量读取。 + */ @RestController @RequestMapping("/system/demoUsdt") public class SysUsdtDemoController { @@ -103,12 +93,14 @@ public class SysUsdtDemoController { return ResponseEntity.ok("redirect SUCCESS"); } - /** 使用 RSA-AES-HMAC 协议创建支付订单。 */ + /** + * 使用 RSA-AES-HMAC 协议创建支付订单。 + */ @PostMapping("/pay") public AjaxResult pay(@RequestParam("amount") String amount, - @RequestParam(value = "walletUserName", required = false) String walletUserName, - @RequestParam(value = "redirectUrl", required = false) String redirectUrl, - @RequestParam(value = "notifyUrl", required = false) String notifyUrl) { + @RequestParam(value = "walletUserName", required = false) String walletUserName, + @RequestParam(value = "redirectUrl", required = false) String redirectUrl, + @RequestParam(value = "notifyUrl", required = false) String notifyUrl) { BigDecimal paymentAmount = parseAmount(amount); String apiUrl = optionalEnv("PAYMENT_API_URL", DEFAULT_API_URL); try { @@ -136,7 +128,9 @@ public class SysUsdtDemoController { } } - /** 查询支付状态;API 凭据和加密过程不会暴露给浏览器。 */ + /** + * 查询支付状态;API 凭据和加密过程不会暴露给浏览器。 + */ @PostMapping("/order-status") public AjaxResult orderStatus(@RequestParam("orderId") String orderId) { String normalized = orderId == null ? "" : orderId.trim(); @@ -160,36 +154,64 @@ public class SysUsdtDemoController { } } - /** RSA-AES-HMAC代付创建客户端示例,不依赖浏览器上下文。 */ + /** + * RSA-AES-HMAC代付创建客户端示例,不依赖浏览器上下文。 + */ @PostMapping("/payout") - public AjaxResult payout(@RequestParam String orderId,@RequestParam String currency, - @RequestParam String amount,@RequestParam String toAddress, - @RequestParam String notifyUrl,@RequestParam(required=false) String redirectUrl) { - Map p=new HashMap();p.put("order_id",orderId);p.put("currency",currency); - p.put("amount",amount);p.put("to_address",toAddress);p.put("notify_url",notifyUrl);p.put("redirect_url",redirectUrl); - try{return AjaxResult.success(sendEncryptedRequest(optionalEnv("PAYOUT_API_URL",DEFAULT_PAYOUT_API_URL),p,"创建代付订单失败"));} - catch(Exception e){throw e instanceof CustomException?(CustomException)e:new CustomException("创建代付订单失败");} + public AjaxResult payout(@RequestParam String orderId, @RequestParam String currency, + @RequestParam String amount, @RequestParam String toAddress, + @RequestParam String notifyUrl, @RequestParam(required = false) String redirectUrl) { + Map p = new HashMap(); + p.put("order_id", orderId); + p.put("currency", currency); + p.put("amount", amount); + p.put("to_address", toAddress); + p.put("notify_url", notifyUrl); + p.put("redirect_url", redirectUrl); + try { + return AjaxResult.success(sendEncryptedRequest(optionalEnv("PAYOUT_API_URL", DEFAULT_PAYOUT_API_URL), p, "创建代付订单失败")); + } catch (Exception e) { + throw e instanceof CustomException ? (CustomException) e : new CustomException("创建代付订单失败"); + } } - /** 代付查询示例;完成后响应中的redirect_url由商户前端自行跳转。 */ + /** + * 代付查询示例;完成后响应中的redirect_url由商户前端自行跳转。 + */ @PostMapping("/payout-status") public AjaxResult payoutStatus(@RequestParam String orderId) { - Map p=new HashMap();p.put("order_id",orderId); - try{return AjaxResult.success(sendEncryptedRequest(optionalEnv("PAYOUT_QUERY_API_URL",DEFAULT_PAYOUT_QUERY_API_URL),p,"查询代付订单失败"));} - catch(Exception e){throw e instanceof CustomException?(CustomException)e:new CustomException("查询代付订单失败");} + Map p = new HashMap(); + p.put("order_id", orderId); + try { + return AjaxResult.success(sendEncryptedRequest(optionalEnv("PAYOUT_QUERY_API_URL", DEFAULT_PAYOUT_QUERY_API_URL), p, "查询代付订单失败")); + } catch (Exception e) { + throw e instanceof CustomException ? (CustomException) e : new CustomException("查询代付订单失败"); + } } - @PostMapping("/payout-notify") @ResponseBody - public ResponseEntity payoutNotify(@RequestHeader(value="X-Payment-Api-Key",required=false)String apiKey, - @RequestHeader(value="X-Payment-Rsa-Signature",required=false)String signature,@RequestBody(required=false)String body){ - try{JSONObject p=JSON.parseObject(body);if(p==null||!"3".equals(p.getString("status"))||!verifyCallback(p,apiKey,signature))return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("invalid signature");return ResponseEntity.ok("ok");} - catch(Exception e){return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("invalid signature");}} + @PostMapping("/payout-notify") + @ResponseBody + public ResponseEntity payoutNotify(@RequestHeader(value = "X-Payment-Api-Key", required = false) String apiKey, + @RequestHeader(value = "X-Payment-Rsa-Signature", required = false) String signature, @RequestBody(required = false) String body) { + try { + JSONObject p = JSON.parseObject(body); + if (p == null || !"3".equals(p.getString("status")) || !verifyCallback(p, apiKey, signature)) + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("invalid signature"); + return ResponseEntity.ok("ok"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("invalid signature"); + } + } - @GetMapping("/payout-redirect") @ResponseBody - public ResponseEntity payoutRedirect(@RequestParam Map parameters){ - String signature=parameters.remove("signature");parameters.remove("api_key"); - if(!"3".equals(parameters.get("status"))||!verifyCallback(parameters,null,signature))return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("invalid signature"); - return ResponseEntity.ok("payout redirect SUCCESS");} + @GetMapping("/payout-redirect") + @ResponseBody + public ResponseEntity payoutRedirect(@RequestParam Map parameters) { + String signature = parameters.remove("signature"); + parameters.remove("api_key"); + if (!"3".equals(parameters.get("status")) || !verifyCallback(parameters, null, signature)) + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("invalid signature"); + return ResponseEntity.ok("payout redirect SUCCESS"); + } private Object sendEncryptedRequest(String apiUrl, Map params, String failureMessage) throws Exception { @@ -223,7 +245,9 @@ public class SysUsdtDemoController { } } - /** 将异步通知JSON规范化为签名字段,拒绝嵌套值和缺失的核心订单字段。 */ + /** + * 将异步通知JSON规范化为签名字段,拒绝嵌套值和缺失的核心订单字段。 + */ private JSONObject parseCallbackPayload(String body) { if (StringUtils.isBlank(body)) { throw new CustomException("回调正文为空"); @@ -241,7 +265,7 @@ public class SysUsdtDemoController { } private Map callbackValues(String orderId, String tradeId, - Integer status, String transactionId) { + Integer status, String transactionId) { Map values = new HashMap(); values.put("order_id", orderId); values.put("trade_id", tradeId);