amzn / amazon-pay-api-sdk-java

Amazon Pay API SDK (Java)
Apache License 2.0
30 stars 17 forks source link

I am getting InvalidRequestSignature. #22

Closed connectSnipes closed 7 months ago

connectSnipes commented 8 months ago

Hello All,

I am getting InvalidRequestSignature even though my string to sign generated and the string to sign getting in the error message is the same.

Pic of signing string generated image

Pic of signing string got from error message image

`package Amazon;

import Amazon.AmazonPayResponse; import Amazon.PayConfiguration; import Amazon.WebstoreClient; import Amazon.AmazonPayClientException; import Amazon.Region; import Amazon.Environment;

import org.json.JSONObject;

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; // for generating an idempotency key import java.util.HashMap; import java.util.Map; import java.util.UUID;

public class sample {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    PayConfiguration payConfiguration = null;
    try {
        payConfiguration = new PayConfiguration()
            .setPublicKeyId("XXXXXXX")
            .setRegion(Region.NA)
            .setEnvironment(Environment.SANDBOX)
            .setPrivateKey(new String(Files.readAllBytes(Paths.get("C:\\Users\\OrCon\\Downloads\\AmazonPay_AF5CONNHKLCVRHOZE5Y6RVYF.pem"))).toCharArray())
            .setAlgorithm("AMZN-PAY-RSASSA-PSS-V2");

        WebstoreClient webstoreClient = new WebstoreClient(payConfiguration);
        AmazonPayResponse response = null;
        String chargeId = "S01-0803717-6760890-C071536";

        JSONObject payload = new JSONObject();
        JSONObject captureAmount = new JSONObject();
        captureAmount.put("amount", "78.73");
        captureAmount.put("currencyCode", "USD");
        payload.put("captureAmount", captureAmount);
        payload.put("softDescriptor", "Descriptor");

        Map<String, String> header = new HashMap<String, String>();
        header.put("x-amz-pay-idempotency-key", UUID.randomUUID().toString().replace("-", ""));
        System.out.println(payload.toString());
        response = webstoreClient.captureCharge(chargeId, payload, header);
        System.out.println("Response ==>" + response);
    } catch (AmazonPayClientException e) {
            e.printStackTrace();
    }

}

} `

connectSnipes commented 7 months ago

I was using wrong private key.