java - 结帐芬兰 PSP API Java 实现
问题描述
我正在尝试将芬兰的结帐 API 集成到我的项目中。它总是给我 401(即,根据 API,我的 Hmac 计算失败)。她是他们使用 nodejs 和 php 计算 Hmac 的 api 的链接。 https://checkoutfinland.github.io/psp-api/#/examples
任何人都可以帮助我在我的 java 代码中出错的地方。这是我的代码:
// Make sure all check-out headers are in alphabetical order and
// all header keys must be in lower case
// if request contains body - it should be valid JSON and content-type header should be included
Map<String, String> headersMap = new TreeMap<>();
headersMap.put("checkout-account", "375917");
headersMap.put("checkout-algorithm", "sha256");
headersMap.put("checkout-method", "POST");
headersMap.put("checkout-nonce", String.valueOf(UUID.randomUUID())); // unique identifier for this request
headersMap.put("checkout-timestamp", ZonedDateTime.now( ZoneOffset.UTC ).format( DateTimeFormatter.ISO_INSTANT )); // ISO 8601 format date time
/**
* All API calls need to be signed using HMAC and SHA-256 or SHA-512
*/
String headers[] = new String[headersMap.size()];
int index = 0;
for(Map.Entry<String, String> headersEntry: headersMap.entrySet()){
headers[index++] = headersEntry.getKey()+":"+headersEntry.getValue();
}
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("stamp", "29858472952");
requestBody.put("reference", "9187445");
requestBody.put("amount", 1500);
requestBody.put("currency", "EUR");
requestBody.put("language", "FI");
List<List<Map<String, Object>>> itemsList = new ArrayList<>();
List<Map<String, Object>> item = new ArrayList<>();
Map<String, Object> itemObj = new HashMap<>();
itemObj.put("unitPrice", 1500);
itemObj.put("units",1);
itemObj.put("vatPercentage", 5);
itemObj.put("productCode", "#1234");
itemObj.put("deliveryDate", "2018-09-01");
item.add(itemObj);
itemsList.add(item);
requestBody.put("items", itemsList);
Map<String, Object> customer = new HashMap<>();
customer.put("email", "test.customer@example.com");
requestBody.put("customer", customer);
Map<String, Object> redirectUrls = new HashMap<>();
redirectUrls.put("success","example.com");
redirectUrls.put("cancel","example.com");
requestBody.put("redirectUrls",redirectUrls);
String body = new ObjectMapper().writeValueAsString(requestBody);
String data = String.join("\n", ArrayUtils.addAll(headers, body));
String hmacSha256Hash = calculateHmacSha256("SAIPPUAKAUPPIAS", data);
if(hmacSha256Hash != null){
headersMap.put("content-type", "application/json; charset=utf-8");
headersMap.put("signature", hmacSha256Hash);
// Make network call checkout api
}
Hmac计算方法:
private String calculateHmacSha256(String key, String data){
String resultHash = null;
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"),"HmacSHA256");
sha256_HMAC.init(secret_key);
resultHash = Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes()));
} catch (Exception e) {
System.out.println("Error calculating HmacSHA256 hash for data data"+ e);
}
return resultHash;
}
我实现它与他们在示例中的相同。
解决方案
推荐阅读
- python-3.x - 使用 DEVNULL 等抑制输出/CMD 后,Ping 在 Windows 上不起作用
- r - 我在 R 中加载库土壤纹理时遇到问题
- reactjs - useSelector 和映射函数 React Redux
- python - numpy.bool_' 对象没有属性 'parameters'
- amazon-web-services - 气流:aws-mwaa-local-runner:无法从 docker 中获取 SecretManager 连接
- sql - 当它显然应该生成 2 个表时,为什么会生成 1 个表?
- python - 如何让我的杀戮开关更有效?
- haskell - 如何在 Haskell 中“翻译”?
- ios - DateFormatter 不转换特定日期 (Swift)
- azure - azurerm_vpn_gateway_connection 不支持的属性 vpn_site_link_id