Skip to content

Commit

Permalink
Amazon Pay API SDK (Java) 2.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
akshitaWaldia committed Mar 16, 2023
1 parent ba42d88 commit 24f1fdf
Show file tree
Hide file tree
Showing 18 changed files with 1,073 additions and 138 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### Version 2.6.0 - March 2023
* Introducing new v2 Reporting APIs. Reports allow you to retrieve consolidated data about Amazon Pay transactions and settlements. In addition to managing and downloading reports using Seller Central, Amazon Pay offers APIs to manage and retrieve your reports.
* Introducing new signature generation algorithm AMZN-PAY-RSASSA-PSS-V2 & increasing salt length from 20 to 32.
* Added support for handling new parameter 'shippingAddressList' in Checkout Session response. Change is fully backwards compatible.
* Enabled Connection Pooling Support.
* Adding Error code 408 to API retry logic
* Note : To use new algorithm AMZN-PAY-RSASSA-PSS-V2, "algorithm" needs to be provided as an additional field in "payConfiguration" and also while rendering Amazon Pay button in "createCheckoutSessionConfig". The changes are backwards-compatible, SDK will use AMZN-PAY-RSASSA-PSS by default.

#### Version 2.5.1 - January 2022
* Applied patch to address issues occurred in Version 2.5.0.
* **Please dont use Version 2.5.0**
Expand Down
148 changes: 142 additions & 6 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ To use the SDK in a Maven project, add a <dependency> reference in your pom.xml
<dependency>
<groupId>software.amazon.pay</groupId>
<artifactId>amazon-pay-api-sdk-java</artifactId>
<version>2.5.1</version>
<version>2.6.0</version>
</dependency>
</dependencies>
```

To use the SDK in a Gradle project, add the following line to your build.gradle file::

```
implementation 'software.amazon.pay:amazon-pay-api-sdk-java:2.5.1'
implementation 'software.amazon.pay:amazon-pay-api-sdk-java:2.6.0'
```

For legacy projects, you can just grab the binary [jar file](https://github.com/amzn/amazon-pay-api-sdk-java/releases) from the GitHub Releases page.
Expand Down Expand Up @@ -106,7 +106,8 @@ try {
.setPublicKeyId("YOUR_PUBLIC_KEY_ID")
.setRegion(Region.YOUR_REGION_CODE)
.setPrivateKey("YOUR_PRIVATE_KEY_STRING".toCharArray())
.setEnvironment(Environment.SANDBOX);
.setEnvironment(Environment.SANDBOX)
.setAlgorithm("AMZN-PAY-RSASSA-PSS-V2"); // Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
Expand All @@ -117,7 +118,8 @@ try {
payConfiguration = new PayConfiguration()
.setPublicKeyId("YOUR_PUBLIC_KEY_ID") // LIVE-XXXXX or SANDBOX-XXXXX
.setRegion(Region.YOUR_REGION_CODE)
.setPrivateKey("YOUR_PRIVATE_KEY_STRING".toCharArray());
.setPrivateKey("YOUR_PRIVATE_KEY_STRING".toCharArray())
.setAlgorithm('AMZN-PAY-RSASSA-PSS-V2'); // Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
Expand All @@ -129,7 +131,8 @@ try {
.setPublicKeyId("YOUR_PUBLIC_KEY_ID")
.setRegion(Region.YOUR_REGION_CODE)
.setPrivateKey(new String(Files.readAllBytes(Paths.get("private.pem"))).toCharArray())
.setEnvironment(Environment.SANDBOX);
.setEnvironment(Environment.SANDBOX)
.setAlgorithm('AMZN-PAY-RSASSA-PSS-V2'); // Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
Expand All @@ -143,7 +146,8 @@ try {
.setPublicKeyId("YOUR_PUBLIC_KEY_ID")
.setRegion(Region.YOUR_REGION_CODE)
.setPrivateKey(privateKey)
.setEnvironment(Environment.SANDBOX);
.setEnvironment(Environment.SANDBOX)
.setAlgorithm('AMZN-PAY-RSASSA-PSS-V2'); // Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
Expand All @@ -162,10 +166,25 @@ try {
.setRegion(Region.YOUR_REGION_CODE)
.setPrivateKey("YOUR_PRIVATE_KEY_STRING".toCharArray())
.setEnvironment(Environment.SANDBOX)
.setAlgorithm('AMZN-PAY-RSASSA-PSS-V2'); // Amazon Signing Algorithm, Optional: uses AMZN-PAY-RSASSA-PSS if not specified
.setProxySettings(proxySettings);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}

// If you want to enable the Custom Connection Pool, you can set it in the payConfiguration in the following way:

try {
int MAX_CLIENT_CONNECTIONS = 30; // This value should be decided according to your requirement
payConfiguration = new PayConfiguration()
.setPublicKeyId("YOUR_PUBLIC_KEY_ID")
.setRegion(Region.YOUR_REGION_CODE)
.setPrivateKey("YOUR_PRIVATE_KEY_STRING".toCharArray())
.setEnvironment(Environment.SANDBOX)
.setClientConnections(MAX_CLIENT_CONNECTIONS); // Default is set to 20
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

# Convenience Functions (Overview)
Expand Down Expand Up @@ -767,3 +786,120 @@ while ((inputLine = in.readLine()) != null) {

String chargePermissionId = JSONObject.fromObject(response.toString()).getString("chargePermissionId");
```

# Reporting APIs code samples

## Amazon Checkout v2 Reporting APIs - GetReports API
```java
AmazonPayResponse response = null;

Map<String, List<String>> queryParameters = new HashMap<>();
List<String> reportTypes = new ArrayList<>();
reportTypes.add("_GET_FLAT_FILE_OFFAMAZONPAYMENTS_SETTLEMENT_DATA_");
List<String> processingStatuses = new ArrayList<>();
processingStatuses.add("COMPLETED");

queryParameters.put("reportTypes", reportTypes);
queryParameters.put("reportTypes", processingStatuses);

try {
response = webstoreClient.getReports(queryParameters);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - GetReportById API
```java
AmazonPayResponse response = null;
String reportId = "1234567890";

try {
response = webstoreClient.getReportById(reportId);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - GetReportDocument API
```java
AmazonPayResponse response = null;
String reportDocumentId = "1234567890";

try {
response = webstoreClient.getReportDocument(reportDocumentId);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - GetReportSchedules API
```java
AmazonPayResponse response = null;
String reportTypes = "_GET_FLAT_FILE_OFFAMAZONPAYMENTS_ORDER_REFERENCE_DATA_,_GET_FLAT_FILE_OFFAMAZONPAYMENTS_BILLING_AGREEMENT_DATA_";

try {
response = webstoreClient.getReportSchedules(reportTypes);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - GetReportScheduleById API
```java
AmazonPayResponse response = null;
String reportScheduleId = "1234567890";

try {
response = webstoreClient.getReportScheduleById(reportScheduleId);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - CreateReport API
```java
AmazonPayResponse response = null;
JSONObject requestPayload = new JSONObject();
requestPayload.put("reportType", "_GET_FLAT_FILE_OFFAMAZONPAYMENTS_ORDER_REFERENCE_DATA_");
requestPayload.put("startTime", "20221114T074550Z");
requestPayload.put("endTime", "20221202T150350Z");
Map<String, String> header = new HashMap<String, String>();
header.put("x-amz-pay-idempotency-key", UUID.randomUUID().toString().replace("-", ""));

try {
response = webstoreClient.createReport(requestPayload, header);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - CreateReportSchedule API
```java
AmazonPayResponse response = null;
JSONObject requestPayload = new JSONObject();
requestPayload.put("reportType", "_GET_FLAT_FILE_OFFAMAZONPAYMENTS_ORDER_REFERENCE_DATA_");
requestPayload.put("scheduleFrequency", "P14D");
requestPayload.put("nextReportCreationTime", "20221202T150350Z");
requestPayload.put("deleteExistingSchedule", "false");
Map<String, String> header = new HashMap<String, String>();
header.put("x-amz-pay-idempotency-key", UUID.randomUUID().toString().replace("-", ""));

try {
response = webstoreClient.createReportSchedule(requestPayload, header);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```

## Amazon Checkout v2 Reporting APIs - CancelReportSchedule API
```java
AmazonPayResponse response = null;
String reportScheduleId = "1234567890";

try {
response = webstoreClient.cancelReportSchedule(reportScheduleId);
} catch (AmazonPayClientException e) {
e.printStackTrace();
}
```
2 changes: 1 addition & 1 deletion pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>software.amazon.pay</groupId>
<artifactId>amazon-pay-api-sdk-java</artifactId>
<packaging>jar</packaging>
<version>2.5.1</version>
<version>2.6.0</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
Expand Down
15 changes: 10 additions & 5 deletions src/com/amazon/pay/api/AmazonPayClient.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.commons.lang.StringUtils;

import com.amazon.pay.api.exceptions.AmazonPayClientException;
import com.amazon.pay.api.types.AmazonSignatureAlgorithm;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -129,9 +131,10 @@ public String generateButtonSignature(final JSONObject payload) throws AmazonPay
public String generateButtonSignature(final String payload) throws AmazonPayClientException {
String signature = null;
final SignatureHelper signatureHelper = new SignatureHelper(payConfiguration);
final AmazonSignatureAlgorithm algorithm = payConfiguration.getAlgorithm();
try {
final String stringToSign = signatureHelper.createStringToSign(payload);
signature = signatureHelper.generateSignature(stringToSign, payConfiguration.getPrivateKey());
final String stringToSign = signatureHelper.createStringToSign(payload, algorithm.getName());
signature = signatureHelper.generateSignature(stringToSign, payConfiguration.getPrivateKey(), algorithm);
} catch (NoSuchAlgorithmException
| NoSuchProviderException
| InvalidAlgorithmParameterException
Expand Down Expand Up @@ -210,7 +213,9 @@ private AmazonPayResponse processRequest(final URI uri,
if (response.get(ServiceConstants.RESPONSE_STRING) != null) {
// Converting the response string into a JSONObject
rawResponseObject = response.get(ServiceConstants.RESPONSE_STRING);
jsonResponse = new JSONObject(response.get(ServiceConstants.RESPONSE_STRING));
if(!StringUtils.isEmpty(response.get(ServiceConstants.RESPONSE_STRING))) {
jsonResponse = new JSONObject(response.get(ServiceConstants.RESPONSE_STRING));
}
}
} catch (InterruptedException | JSONException e) {
throw new AmazonPayClientException(e.getMessage(), e);
Expand Down Expand Up @@ -241,8 +246,8 @@ private List<String> sendRequest(final URI uri,
String requestId = null;
int responseCode = 0;
try (final CloseableHttpClient client = Optional.ofNullable(payConfiguration.getProxySettings()).isPresent()
? Util.getCloseableHttpClientWithProxy(payConfiguration.getProxySettings())
: HttpClients.createDefault()) {
? Util.getCloseableHttpClientWithProxy(payConfiguration.getProxySettings(), payConfiguration)
: Util.getHttpClientWithConnectionPool(payConfiguration)) {
final HttpUriRequest httpUriRequest = Util.getHttpUriRequest(uri, httpMethodName, payload);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpUriRequest.addHeader(entry.getKey(), entry.getValue());
Expand Down
43 changes: 43 additions & 0 deletions src/com/amazon/pay/api/PayConfiguration.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.amazon.pay.api.exceptions.AmazonPayClientException;
import com.amazon.pay.api.types.Environment;
import com.amazon.pay.api.types.Region;
import com.amazon.pay.api.types.AmazonSignatureAlgorithm;

import java.security.PrivateKey;

Expand All @@ -25,10 +26,12 @@ public class PayConfiguration {
private String publicKeyId;
private PrivateKey privateKey;
private Environment environment;
private AmazonSignatureAlgorithm algorithm;
private int maxRetries = 3;
private boolean userAgentRedaction = false;
private ProxySettings proxySettings;
protected String overrideServiceURL;
private Integer clientConnections;

/**
* @return Returns region code from PayConfiguration
Expand Down Expand Up @@ -98,6 +101,26 @@ public PayConfiguration setPrivateKey(PrivateKey privateKey) {
return this;
}

/**
* @return returns Algorithm from PayConfiguration, default algorithm is AMZN-PAY-RSASSA-PSS if not set
*/
public AmazonSignatureAlgorithm getAlgorithm() {
if(algorithm != null) {
return algorithm;
} else {
return AmazonSignatureAlgorithm.DEFAULT;
}
}

/**
* @param algorithm the Amazon Signature Algorithm
* @return the PayConfiguration object
*/
public PayConfiguration setAlgorithm(final String algorithm) {
this.algorithm = AmazonSignatureAlgorithm.returnIfValidAlgorithm(algorithm);
return this;
}

/**
* @return returns the environment from the PayConfiguration
*/
Expand Down Expand Up @@ -179,4 +202,24 @@ public String getOverrideServiceURL() {
return overrideServiceURL;
}

/**
* @return Returns clientConnections from PayConfiguration
*/
public Integer getClientConnections() {
if(clientConnections != null && clientConnections != 0) {
return clientConnections;
} else {
return ServiceConstants.MAX_CLIENT_CONNECTIONS;
}
}

/**
* @param clientConnections Sets the maximum number of Client Connections to be made
* @return the PayConfiguration object
*/
public PayConfiguration setClientConnections(int clientConnections) {
this.clientConnections = clientConnections;
return this;
}

}
Loading

0 comments on commit 24f1fdf

Please sign in to comment.