|
|
|
@ -3,9 +3,7 @@ package com.example.demo.serviceImpl.cash; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.example.demo.domain.DTO.BankDTO; |
|
|
|
import com.example.demo.domain.DTO.PaymentDTO; |
|
|
|
import com.example.demo.domain.DTO.StripeDTO; |
|
|
|
import com.example.demo.domain.DTO.*; |
|
|
|
import com.example.demo.domain.vo.cash.BankVO; |
|
|
|
import com.example.demo.domain.vo.cash.CashCollection; |
|
|
|
import com.example.demo.domain.vo.coin.Result; |
|
|
|
@ -25,7 +23,10 @@ import org.springframework.util.LinkedMultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import javax.crypto.Mac; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.net.http.HttpResponse; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.security.MessageDigest; |
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
@ -516,6 +517,154 @@ public class BankServiceImpl implements BankService { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//firstdata银行接口(批量) |
|
|
|
@Override |
|
|
|
public Result firstdataAuto(BankDTO bankDTO) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
//firstdata银行接口(单个) |
|
|
|
@Override |
|
|
|
public Result getFirstdata(BankDTO bankDTO) { |
|
|
|
try { |
|
|
|
// 获取签名参数 |
|
|
|
FirstdataRequestDTO firstdataRequestDTO = generatePaymentAsiaSignature(); |
|
|
|
|
|
|
|
// 构建请求URL,使用bankDTO中的orderNo |
|
|
|
String orderNo = bankDTO.getOrderNo(); |
|
|
|
String url = "https://prod.api.firstdata.com/gateway/v2/payments/" + orderNo + "?storeId=4530056594"; |
|
|
|
|
|
|
|
// 使用RestTemplate发送GET请求 |
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
|
headers.set("accept", "application/json"); |
|
|
|
headers.set("Client-Request-Id", String.valueOf(firstdataRequestDTO.getClientRequestId())); |
|
|
|
headers.set("Api-Key", firstdataRequestDTO.getKey()); |
|
|
|
headers.set("Timestamp", String.valueOf(firstdataRequestDTO.getTime())); |
|
|
|
headers.set("Message-Signature", firstdataRequestDTO.getHmacBase64()); |
|
|
|
headers.set("User-Agent", "Apifox/1.0.0 (https://apifox.com)"); |
|
|
|
headers.set("Content-Type", "application/json"); |
|
|
|
headers.set("Host", "prod.api.firstdata.com"); |
|
|
|
headers.set("Connection", "keep-alive"); |
|
|
|
|
|
|
|
HttpEntity<String> entity = new HttpEntity<>(headers); |
|
|
|
|
|
|
|
ResponseEntity<String> response = restTemplate.exchange( |
|
|
|
url, |
|
|
|
HttpMethod.GET, |
|
|
|
entity, |
|
|
|
String.class |
|
|
|
); |
|
|
|
|
|
|
|
// 解析响应数据 |
|
|
|
// API返回的是对象格式,需要先解析为JSONObject |
|
|
|
JSONObject jsonObject = JSON.parseObject(response.getBody()); |
|
|
|
|
|
|
|
// 创建BankVO对象并设置数据 |
|
|
|
BankVO bankVO = new BankVO(); |
|
|
|
List<String> message = new ArrayList<>(); |
|
|
|
|
|
|
|
// 检查jsonObject是否为空或者是否包含错误信息 |
|
|
|
if (jsonObject != null && !jsonObject.isEmpty()) { |
|
|
|
// 提取需要的字段 |
|
|
|
String country = jsonObject.getString("country"); |
|
|
|
String orderId = jsonObject.getString("orderId"); |
|
|
|
|
|
|
|
// 提取currency和total |
|
|
|
JSONObject transactionAmount = jsonObject.getJSONObject("transactionAmount"); |
|
|
|
String currency = transactionAmount != null ? transactionAmount.getString("currency") : null; |
|
|
|
Integer total = transactionAmount != null ? transactionAmount.getInteger("total") : null; |
|
|
|
|
|
|
|
// 创建FirstdataDTO对象并存储数据 |
|
|
|
FirstdataDTO firstdataDTO = new FirstdataDTO(); |
|
|
|
firstdataDTO.setCountry(country); |
|
|
|
firstdataDTO.setOrderId(orderId); |
|
|
|
firstdataDTO.setCurrency(currency); |
|
|
|
firstdataDTO.setTotal(total); |
|
|
|
|
|
|
|
// 将firstdataDTO存入bankVO |
|
|
|
bankVO.setFirstdataDTO(firstdataDTO); |
|
|
|
|
|
|
|
// 创建一个简单的Map来存储提取的数据 |
|
|
|
Map<String, Object> extractedData = new HashMap<>(); |
|
|
|
extractedData.put("country", country); |
|
|
|
extractedData.put("orderId", orderId); |
|
|
|
extractedData.put("currency", currency); |
|
|
|
extractedData.put("total", total); |
|
|
|
extractedData.put("success", true); // 添加成功标识 |
|
|
|
|
|
|
|
// 将提取的数据转换为JSON字符串并添加到message列表 |
|
|
|
message.add(JSON.toJSONString(extractedData, true)); |
|
|
|
|
|
|
|
// 将message存入bankVO |
|
|
|
bankVO.setMessage(message); |
|
|
|
|
|
|
|
return Result.success(bankVO); |
|
|
|
} else { |
|
|
|
// 没有数据时也添加失败标识 |
|
|
|
Map<String, Object> emptyData = new HashMap<>(); |
|
|
|
emptyData.put("success", false); |
|
|
|
emptyData.put("message", "No data found"); |
|
|
|
message.add(JSON.toJSONString(emptyData, true)); |
|
|
|
|
|
|
|
// 将message存入bankVO |
|
|
|
bankVO.setMessage(message); |
|
|
|
|
|
|
|
return Result.error("No data found"); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Firstdata银行接口调用失败", e); |
|
|
|
|
|
|
|
// 在异常情况下也构建包含错误信息的message |
|
|
|
BankVO bankVO = new BankVO(); |
|
|
|
List<String> message = new ArrayList<>(); |
|
|
|
Map<String, Object> errorData = new HashMap<>(); |
|
|
|
errorData.put("success", false); |
|
|
|
errorData.put("error", e.getMessage()); |
|
|
|
message.add(JSON.toJSONString(errorData, true)); |
|
|
|
bankVO.setMessage(message); |
|
|
|
|
|
|
|
return Result.error("Firstdata银行接口调用失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
|
|
* 生成PaymentAsia API所需的签名 |
|
|
|
* @return 签名字符串 |
|
|
|
*/ |
|
|
|
public FirstdataRequestDTO generatePaymentAsiaSignature() { |
|
|
|
try { |
|
|
|
String key = "3E04ZUCKFmQKrW0uoBa89QKIJWYoU9OX"; |
|
|
|
String secret = "ZLtBPgfMIT4HXg25SoVuCyUQZ6GtSv9UFmDmYaoVSKS"; |
|
|
|
|
|
|
|
// 生成ClientRequestId(1-10000000的随机数) |
|
|
|
long clientRequestId = new Random().nextInt(10000000) + 1; |
|
|
|
|
|
|
|
// 获取当前时间戳(毫秒) |
|
|
|
long time = System.currentTimeMillis(); |
|
|
|
|
|
|
|
// 构造原始签名数据 |
|
|
|
String rawSignature = key + clientRequestId + time; |
|
|
|
|
|
|
|
// 计算HMAC-SHA256 |
|
|
|
Mac sha256Hmac = Mac.getInstance("HmacSHA256"); |
|
|
|
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); |
|
|
|
sha256Hmac.init(secretKey); |
|
|
|
byte[] hmacBytes = sha256Hmac.doFinal(rawSignature.getBytes(StandardCharsets.UTF_8)); |
|
|
|
// 转换为Base64编码 |
|
|
|
String hmacBase64 = Base64.getEncoder().encodeToString(hmacBytes); |
|
|
|
// 赋值给DTO |
|
|
|
FirstdataRequestDTO firstdataRequestDTO = new FirstdataRequestDTO(); |
|
|
|
firstdataRequestDTO.setKey(key); |
|
|
|
firstdataRequestDTO.setSecret(secret); |
|
|
|
firstdataRequestDTO.setClientRequestId(clientRequestId); |
|
|
|
firstdataRequestDTO.setTime(time); |
|
|
|
firstdataRequestDTO.setHmacBase64(hmacBase64); |
|
|
|
|
|
|
|
// Base64编码并返回签名 |
|
|
|
return firstdataRequestDTO; |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException("生成PaymentAsia签名失败", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* http_build_query的查询字符串(key=value&key=value) |
|
|
|
*/ |
|
|
|
|