I need to tweak an API https://sandbox.api.visa.com/cybersource/payments/flex/v1/keys?apikey= /echatspin /echatrandom {apikey} I am imitating the official document X-Pay Token,but it fail with "Token validation failed" error.
{ "responseStatus": { "status": 401, "code": "9159", "severity": "ERROR", "message": "Token validation failed", "info": "" } }
Below is my x-pay-token generation code.
import java.math.BigInteger; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.SignatureException; public class T { private static String resoucePath = "payments/flex/v1/keys"; private static String queryString = "apikey=6DC0NMXO53QQFE6NFOLE213HXA-pvG6xE-1NtuCd5oOQr-O-s"; private static String requestBody = "{encryptionType:RsaOaep256}"; private static String sharedSecret = "gAynzAGf89+V}3{Q4Jx5cp-/R@Y#PEv#1XvxnjQC"; public static void main(String[] args) throws SignatureException { System.out.println(T.generateXpaytoken(resoucePath, queryString, requestBody, sharedSecret)); } public static String generateXpaytoken(String resourcePath, String queryString, String requestBody, String sharedSecret) throws SignatureException { String timestamp = timeStamp(); String beforeHash = timestamp + resourcePath + queryString + requestBody; String hash = hmacSha256Digest(beforeHash, sharedSecret); String token = "xv2:" + timestamp + ":" + hash; return token; } private static String timeStamp() { return String.valueOf(System.currentTimeMillis() / 1000L); } private static String hmacSha256Digest(String data, String sharedSecret) throws SignatureException { return getDigest("HmacSHA256", sharedSecret, data, true); } private static String getDigest(String algorithm, String sharedSecret, String data, boolean toLower) throws SignatureException { try { Mac sha256HMAC = Mac.getInstance(algorithm); SecretKeySpec secretKey = new SecretKeySpec(sharedSecret.getBytes(StandardCharsets.UTF_8), algorithm); sha256HMAC.init(secretKey); byte[] hashByte = sha256HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8)); String hashString = toHex(hashByte); return toLower ? hashString.toLowerCase() : hashString; } catch (Exception e) { throw new SignatureException(e); } } private static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); return String.format("%0" + (bytes.length << 1) + "X", bi); } }
somebody can help me please?
09-19-2022 09:59 PM