支付成功页面添加
This commit is contained in:
1 parent
935307e9ca
commit
dcd8eadd20
2 files changed
+62
-42
No files matched your search
+47
-16
@@ -43,6 +43,8 @@ public class SysUsdtDemoController {
|
|||||||
private static final Logger log = LoggerFactory.getLogger(SysUsdtDemoController.class);
|
private static final Logger log = LoggerFactory.getLogger(SysUsdtDemoController.class);
|
||||||
private static final String DEFAULT_API_URL =
|
private static final String DEFAULT_API_URL =
|
||||||
"http://192.168.3.103:2020/api/v1/order/create-transaction";
|
"http://192.168.3.103:2020/api/v1/order/create-transaction";
|
||||||
|
private static final String DEFAULT_QUERY_API_URL =
|
||||||
|
"http://192.168.3.103:2020/api/v1/order/query";
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
public SysUsdtDemoController() {
|
public SysUsdtDemoController() {
|
||||||
@@ -74,12 +76,7 @@ public class SysUsdtDemoController {
|
|||||||
@RequestParam(value = "walletUserName", required = false) String walletUserName) {
|
@RequestParam(value = "walletUserName", required = false) String walletUserName) {
|
||||||
BigDecimal paymentAmount = parseAmount(amount);
|
BigDecimal paymentAmount = parseAmount(amount);
|
||||||
String apiUrl = optionalEnv("PAYMENT_API_URL", DEFAULT_API_URL);
|
String apiUrl = optionalEnv("PAYMENT_API_URL", DEFAULT_API_URL);
|
||||||
String apiKey = requiredEnvWithAlias("PAYMENT_API_KEY", "PAYMENT_API_KEK");
|
|
||||||
String apiSecret = requiredEnv("PAYMENT_API_SECRET");
|
|
||||||
String publicKeyText = requiredEnv("PAYMENT_RSA_PUBLIC_KEY").replace("\\n", "\n");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PublicKey publicKey = RsaKeyUtil.loadPublicKey(publicKeyText);
|
|
||||||
String contextPath = ServletUriComponentsBuilder.fromCurrentContextPath()
|
String contextPath = ServletUriComponentsBuilder.fromCurrentContextPath()
|
||||||
.build().toUriString();
|
.build().toUriString();
|
||||||
Map<String, Object> params = new HashMap<String, Object>();
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
@@ -92,17 +89,7 @@ public class SysUsdtDemoController {
|
|||||||
params.put("redirect_url", optionalEnv("PAYMENT_REDIRECT_URL",
|
params.put("redirect_url", optionalEnv("PAYMENT_REDIRECT_URL",
|
||||||
contextPath + "/system/demoUsdt/redirect"));
|
contextPath + "/system/demoUsdt/redirect"));
|
||||||
|
|
||||||
EncryptedRequest encrypted = SecureClient.encrypt(apiKey, apiSecret, publicKey, params);
|
return AjaxResult.success(sendEncryptedRequest(apiUrl, params, "创建支付订单失败"));
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
||||||
headers.setAccept(java.util.Collections.singletonList(MediaType.APPLICATION_JSON));
|
|
||||||
ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST,
|
|
||||||
new HttpEntity<EncryptedRequest>(encrypted, headers), String.class);
|
|
||||||
JSONObject result = JSON.parseObject(response.getBody());
|
|
||||||
if (result == null || result.getIntValue("code") != 200 || result.get("data") == null) {
|
|
||||||
throw new CustomException(result == null ? "创建支付订单失败" : result.getString("msg"));
|
|
||||||
}
|
|
||||||
return AjaxResult.success(result.get("data"));
|
|
||||||
} catch (RestClientResponseException ex) {
|
} catch (RestClientResponseException ex) {
|
||||||
log.warn("支付演示请求失败,HTTP状态={}", ex.getRawStatusCode());
|
log.warn("支付演示请求失败,HTTP状态={}", ex.getRawStatusCode());
|
||||||
throw new CustomException("支付服务暂时不可用,请稍后重试");
|
throw new CustomException("支付服务暂时不可用,请稍后重试");
|
||||||
@@ -114,6 +101,50 @@ public class SysUsdtDemoController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 查询支付状态;API 凭据和加密过程不会暴露给浏览器。 */
|
||||||
|
@PostMapping("/order-status")
|
||||||
|
public AjaxResult orderStatus(@RequestParam("orderId") String orderId) {
|
||||||
|
String normalized = orderId == null ? "" : orderId.trim();
|
||||||
|
if (normalized.isEmpty() || normalized.length() > 32
|
||||||
|
|| !normalized.matches("[A-Za-z0-9_-]+")) {
|
||||||
|
throw new CustomException("订单号格式不正确");
|
||||||
|
}
|
||||||
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
|
params.put("order_id", normalized);
|
||||||
|
String queryUrl = optionalEnv("PAYMENT_QUERY_API_URL", DEFAULT_QUERY_API_URL);
|
||||||
|
try {
|
||||||
|
return AjaxResult.success(sendEncryptedRequest(queryUrl, params, "查询支付订单失败"));
|
||||||
|
} catch (RestClientResponseException ex) {
|
||||||
|
log.warn("支付状态查询失败,HTTP状态={}", ex.getRawStatusCode());
|
||||||
|
throw new CustomException("支付状态查询暂时不可用");
|
||||||
|
} catch (CustomException ex) {
|
||||||
|
throw ex;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.warn("支付状态查询失败,异常类型={}", ex.getClass().getSimpleName());
|
||||||
|
throw new CustomException("支付状态查询暂时不可用");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object sendEncryptedRequest(String apiUrl, Map<String, Object> params,
|
||||||
|
String failureMessage) throws Exception {
|
||||||
|
String apiKey = requiredEnvWithAlias("PAYMENT_API_KEY", "PAYMENT_API_KEK");
|
||||||
|
String apiSecret = requiredEnv("PAYMENT_API_SECRET");
|
||||||
|
PublicKey publicKey = RsaKeyUtil.loadPublicKey(
|
||||||
|
requiredEnv("PAYMENT_RSA_PUBLIC_KEY").replace("\\n", "\n"));
|
||||||
|
EncryptedRequest encrypted = SecureClient.encrypt(apiKey, apiSecret, publicKey, params);
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
headers.setAccept(java.util.Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST,
|
||||||
|
new HttpEntity<EncryptedRequest>(encrypted, headers), String.class);
|
||||||
|
JSONObject result = JSON.parseObject(response.getBody());
|
||||||
|
if (result == null || result.getIntValue("code") != 200 || result.get("data") == null) {
|
||||||
|
throw new CustomException(result == null || StringUtils.isBlank(result.getString("msg"))
|
||||||
|
? failureMessage : result.getString("msg"));
|
||||||
|
}
|
||||||
|
return result.get("data");
|
||||||
|
}
|
||||||
|
|
||||||
private BigDecimal parseAmount(String amount) {
|
private BigDecimal parseAmount(String amount) {
|
||||||
try {
|
try {
|
||||||
BigDecimal value = new BigDecimal(amount);
|
BigDecimal value = new BigDecimal(amount);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.ruoyi.common.annotation.Log;
|
import com.ruoyi.common.annotation.Log;
|
||||||
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
import com.ruoyi.common.enums.BusinessType;
|
import com.ruoyi.common.enums.BusinessType;
|
||||||
import com.ruoyi.system.domain.AppApiKeysPlus;
|
|
||||||
import com.ruoyi.system.domain.UsdtOrdersPlus;
|
import com.ruoyi.system.domain.UsdtOrdersPlus;
|
||||||
import com.ruoyi.system.domain.dto.EncryptedRequest;
|
import com.ruoyi.system.domain.dto.EncryptedRequest;
|
||||||
import com.ruoyi.system.domain.vo.DecryptResult;
|
import com.ruoyi.system.domain.vo.DecryptResult;
|
||||||
@@ -15,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -25,13 +23,9 @@ import java.util.Map;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/order")
|
@RequestMapping("/api/v1/order")
|
||||||
public class PaymentApiController {
|
public class PaymentApiController {
|
||||||
@Autowired
|
|
||||||
private IAppApiKeysService keys;
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IUsdtOrdersService orders;
|
private IUsdtOrdersService orders;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppPrivateKeyCipher privateKeyCipher;
|
|
||||||
@Autowired
|
|
||||||
private ApiSecurityService apiSecurityService;
|
private ApiSecurityService apiSecurityService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private PaymentOrderOpenApiService paymentOrderOpenApiService;
|
private PaymentOrderOpenApiService paymentOrderOpenApiService;
|
||||||
@@ -45,13 +39,12 @@ public class PaymentApiController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/query")
|
@PostMapping("/query")
|
||||||
public AjaxResult query(@RequestBody Map<String, Object> body) {
|
public AjaxResult query(@RequestBody EncryptedRequest request) {
|
||||||
Long appId = parseLong(body.get("app_id"));
|
DecryptResult decrypted = apiSecurityService.decryptAndVerify(request);
|
||||||
AppApiKeysPlus key = activeKey(appId, body.get("api_key"));
|
String orderId = parseOrderId(decrypted.getPlainData());
|
||||||
String signature = String.valueOf(body.get("signature"));
|
UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper<UsdtOrdersPlus>()
|
||||||
if (key == null || !PaymentSignatureService.verify(body, privateKeyCipher.decryptCredentialSecret(key.getApiSecret()), signature))
|
.eq(UsdtOrdersPlus::getAppId, decrypted.getAppId())
|
||||||
return AjaxResult.error("签名无效");
|
.eq(UsdtOrdersPlus::getOrderId, orderId).last("limit 1"));
|
||||||
UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper<UsdtOrdersPlus>().eq(UsdtOrdersPlus::getAppId, appId).eq(UsdtOrdersPlus::getOrderId, String.valueOf(body.get("order_id"))));
|
|
||||||
if (order == null) return AjaxResult.error("订单不存在");
|
if (order == null) return AjaxResult.error("订单不存在");
|
||||||
Map<String, Object> result = new HashMap<String, Object>();
|
Map<String, Object> result = new HashMap<String, Object>();
|
||||||
result.put("order_id", order.getOrderId());
|
result.put("order_id", order.getOrderId());
|
||||||
@@ -61,19 +54,15 @@ public class PaymentApiController {
|
|||||||
return AjaxResult.success(result);
|
return AjaxResult.success(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private AppApiKeysPlus activeKey(Long appId, Object apiKey) {
|
private String parseOrderId(String plainData) {
|
||||||
if (appId == null || apiKey == null || String.valueOf(apiKey).trim().isEmpty()) return null;
|
final String prefix = "order_id=";
|
||||||
return keys.getOne(new LambdaQueryWrapper<AppApiKeysPlus>().eq(AppApiKeysPlus::getAppId, appId)
|
if (plainData == null || !plainData.startsWith(prefix) || !plainData.endsWith("&")) {
|
||||||
.eq(AppApiKeysPlus::getApiKey, String.valueOf(apiKey)).eq(AppApiKeysPlus::getStatus, 1)
|
throw new com.ruoyi.common.exception.CustomException("业务参数格式错误");
|
||||||
.isNull(AppApiKeysPlus::getDeletedAt)
|
|
||||||
.and(wrapper -> wrapper.isNull(AppApiKeysPlus::getExpiredAt).or().gt(AppApiKeysPlus::getExpiredAt, new Date())));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Long parseLong(Object value) {
|
|
||||||
try {
|
|
||||||
return value == null ? null : Long.valueOf(String.valueOf(value));
|
|
||||||
} catch (Exception ex) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
String orderId = plainData.substring(prefix.length(), plainData.length() - 1).trim();
|
||||||
|
if (orderId.isEmpty() || orderId.length() > 32 || orderId.contains("&")) {
|
||||||
|
throw new com.ruoyi.common.exception.CustomException("order_id 格式错误");
|
||||||
|
}
|
||||||
|
return orderId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user