租赁资金账户资金充值功能,对接项目的支付接口,application-rental.yml添加配置NILE和主网的对接配置
This commit is contained in:
1 parent
911a3634ee
commit
e73f099734
20 files changed
+878
-9
No files matched your search
@@ -0,0 +1,80 @@
|
||||
package com.ruoyi.system.rental;
|
||||
|
||||
import static com.ruoyi.system.rental.RentalValues.*;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.system.security.RsaKeyUtil;
|
||||
import com.ruoyi.system.security.SecureClient;
|
||||
import okhttp3.*;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Signature;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import com.ruoyi.system.service.PaymentSignatureService;
|
||||
|
||||
/** 支付HTTP适配器:复用项目混合加密SDK;不允许重定向携带支付认证材料。 */
|
||||
@Component
|
||||
public class RentalPaymentClient {
|
||||
private final ObjectMapper json;
|
||||
private final RentalPaymentProperties properties;
|
||||
private final ResourceLoader resources;
|
||||
public RentalPaymentClient(ObjectMapper json, RentalPaymentProperties properties, ResourceLoader resources) {
|
||||
this.json = json.copy().enable(com.fasterxml.jackson.databind.DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
|
||||
this.properties = properties;
|
||||
this.resources = resources;
|
||||
}
|
||||
public String publicKey(RentalPaymentProperties.Channel channel) throws Exception {
|
||||
try (InputStream input = resources.getResource(channel.getRsaPublicKeyLocation()).getInputStream()) {
|
||||
java.io.ByteArrayOutputStream output = new java.io.ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int size;
|
||||
while ((size = input.read(buffer)) != -1) {
|
||||
require(output.size() + size <= 16384, "支付公钥文件过大");
|
||||
output.write(buffer, 0, size);
|
||||
}
|
||||
String pem = new String(output.toByteArray(), StandardCharsets.UTF_8);
|
||||
RsaKeyUtil.loadPublicKey(pem);
|
||||
return pem;
|
||||
}
|
||||
}
|
||||
public Map<String, Object> call(Map<String, Object> order, String operation) throws Exception {
|
||||
RentalPaymentProperties.Channel channel = properties.channel(text(order, "network"));
|
||||
require(properties.isEnabled() && channel.isEnabled(), "支付充值尚未启用");
|
||||
require(channel.getApiKey().equals(text(order, "payment_api_key"))
|
||||
&& !channel.getApiSecret().isEmpty(), "在途充值凭证已变化,请保留旧通道配置后核对");
|
||||
Map<String, Object> parameters = map("order_id", text(order, "topup_no"));
|
||||
if ("create-transaction".equals(operation)) {
|
||||
parameters.putAll(map("amount", amount(order, "requested_amount").setScale(2).toPlainString(),
|
||||
"amount_currency", "USDT", "network", text(order, "network"),
|
||||
"walletUserName", "rent:" + text(order, "topup_no"), "notify_url", text(order, "notify_url")));
|
||||
}
|
||||
Object encrypted = SecureClient.encrypt(text(order, "payment_api_key"), channel.getApiSecret(),
|
||||
RsaKeyUtil.loadPublicKey(text(order, "payment_public_key")), parameters);
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.connectTimeout(properties.getConnectTimeoutMs(), TimeUnit.MILLISECONDS)
|
||||
.readTimeout(properties.getReadTimeoutMs(), TimeUnit.MILLISECONDS)
|
||||
.callTimeout(30, TimeUnit.SECONDS).followRedirects(false).followSslRedirects(false)
|
||||
.retryOnConnectionFailure(false).build();
|
||||
Request request = new Request.Builder().url(text(order, "payment_base_url") + "/api/v1/order/" + operation)
|
||||
.post(RequestBody.create(MediaType.parse("application/json"), json.writeValueAsString(encrypted))).build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
Map<String, Object> result = json.readValue(RentalHttp.body(response), new TypeReference<Map<String, Object>>() { });
|
||||
require(number(result, "code") == 200 && result.get("data") instanceof Map, "支付请求未成功,请稍后核对");
|
||||
return (Map<String, Object>) result.get("data");
|
||||
}
|
||||
}
|
||||
/** RSA验签使用项目既有参数规范化规则,不使用租赁HMAC或浏览器跳转结果。 */
|
||||
public boolean verify(Map<String, Object> order, Map<String, Object> payload, String key, String signature) {
|
||||
try {
|
||||
if (!text(order, "payment_api_key").equals(key) || signature == null) return false;
|
||||
Signature verifier = Signature.getInstance("SHA256withRSA");
|
||||
verifier.initVerify(RsaKeyUtil.loadPublicKey(text(order, "payment_public_key")));
|
||||
verifier.update(PaymentSignatureService.canonicalText(payload).getBytes(StandardCharsets.UTF_8));
|
||||
return verifier.verify(Base64.getDecoder().decode(signature));
|
||||
} catch (Exception exception) { return false; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user