Skip to content

Commit

Permalink
Amazon Pay Java SDK 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bjguillot committed Jul 11, 2017
1 parent 5e2d53e commit 77c3c6c
Show file tree
Hide file tree
Showing 29 changed files with 955 additions and 385 deletions.
7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 3.1.0 - July 2017
- EventType enum from BillingAgreementNotification IPN metadata now available.
- Fix BillingAgreementStatus class to handle mis-named "lastUpdate" attribute from IPNs.
- Modified IPN XML parsing so that extra jaxb jar files no longer required.
- Add AMAZON_A_Z_CANCEL enum to RefundType.
- Multicurrency attributes [ConvertedAmount, ConversionRate] now available on GetCaptureDetails and GetRefundDetails responses (and IPN's) when allowed and applicable. Initially only supported for select UK and European merchants.

Version 3.0.2 - March 2017
- Add a new element "FullDescriptor" in PaymentDescriptor object in GetOrderReferenceDetailsResponse API call.
- Deprecate "AccountNumberTail" and "Name" elements in the favor of "FullDescriptor" element.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ CaptureResponseData response = client.capture(request);

// Close the order reference once your one time
// transaction is complete.
client.closeOrderReference("AMAZON_ORDER_REFERENCE_ID");
client.closeOrderReference(new CloseOrderReferenceRequest("AMAZON_ORDER_REFERENCE_ID"));


```
Expand Down Expand Up @@ -400,4 +400,4 @@ PaymentDescriptor paymentDescriptor = response.getDetails().getPaymentDescriptor
if (paymentDescriptor != null) {
log.info("Full Descriptor = " + paymentDescriptor.getFullDescriptor());
log.info("Use Amazon Balance First = " + paymentDescriptor.isUseAmazonBalanceFirst());
}
}
Binary file not shown.
13 changes: 13 additions & 0 deletions logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# If you want to see all requests and repsonses logged, uncomment the following line,
# otherwise, only XML unmarshalling issues with AmazonValidationEventHandler will be
# logged to the console when the brazil-build tests are running.
#
# com.amazon.pay.level = FINE

com.amazon.pay.response.parser.AmazonValidationEventHandler.level = FINE

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINE

java.util.logging.SimpleFormatter.format = ---- %1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s %4$s: %5$s%n
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
14 changes: 2 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@
<groupId>com.amazon.pay</groupId>
<artifactId>amazon-pay</artifactId>
<packaging>jar</packaging>
<version>3.0.2</version>
<version>3.1.0</version>
<dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down Expand Up @@ -67,4 +57,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
28 changes: 20 additions & 8 deletions src/com/amazon/pay/impl/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.amazon.pay.response.parser.ResponseData;
import com.amazon.pay.types.Region;
import com.amazon.pay.types.ServiceConstants;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
Expand All @@ -30,7 +32,12 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

Expand All @@ -39,6 +46,8 @@

public class Util {

private static PayLogUtil payLogUtil = new PayLogUtil();

public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String JAVA_VERSION = System.getProperty("java.version");
public static final String OS_NAME = System.getProperty("os.name");
Expand Down Expand Up @@ -76,7 +85,10 @@ public static String getTimestamp() {
* @return ResponseData
* @throws IOException
*/
public static ResponseData httpSendRequest(String method, String url, String urlParameters, Map<String,String> headers) throws IOException{
public static ResponseData httpSendRequest(String method, String url, String urlParameters, Map<String,String> headers) throws IOException {

payLogUtil.logMessage("Request:\nURL=" + url + "\nPOST Data=" + urlParameters);

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
if (headers != null && !headers.isEmpty()) {
Expand All @@ -86,7 +98,7 @@ public static ResponseData httpSendRequest(String method, String url, String url
}
con.setDoOutput(true);
con.setRequestMethod(method);
if(urlParameters != null) {
if (urlParameters != null) {
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
Expand Down Expand Up @@ -121,11 +133,11 @@ public static ResponseData httpSendRequest(String method, String url, String url
* @return ResponseData
* @throws IOException
*/
public static ResponseData httpSendRequest(String method, String url, String urlParameters, Map<String,String> headers , PayConfig config) throws IOException {
public static ResponseData httpSendRequest(String method, String url, String urlParameters, Map<String,String> headers, PayConfig config) throws IOException {

Map<String,String> headerMap = new HashMap<String,String>();

if(config!= null) {
if (config != null) {

final String applicationName = config.getApplicationName();
final String applicationVersion = config.getApplicationVersion();
Expand Down Expand Up @@ -220,14 +232,14 @@ public static String convertParameterMapToString(Map<String, String> params) {
* Helper method to get Service URL endpoint including service version name
* Ex:
*/
public static String getServiceURLEndpoint(Region region , Environment environment) {
return ServiceConstants.mwsEndpointMappings.get(region) + "/" + getServiceVersionName(environment);
public static String getServiceURLEndpoint(Region region, Environment environment) {
return ServiceConstants.mwsEndpointMappings.get(region) + getServiceVersionName(environment);
}


public static String getServiceVersionName(Environment environment) {
String mwsServiceAPIVersionName;
if(environment == Environment.SANDBOX) {
if (environment == Environment.SANDBOX) {
mwsServiceAPIVersionName = "/" + "OffAmazonPayments_Sandbox" + "/" + ServiceConstants.AMAZON_PAY_API_VERSION;
}
else {
Expand Down
34 changes: 16 additions & 18 deletions src/com/amazon/pay/impl/ipn/NotificationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.amazon.pay.response.ipn.model.ProviderCreditReversalNotification;
import com.amazon.pay.response.ipn.model.RefundNotification;
import com.amazon.pay.response.ipn.model.SolutionProviderMerchantNotification;
import com.amazon.pay.response.parser.AmazonValidationEventHandler;

import com.google.gson.Gson;

import java.io.StringReader;
Expand Down Expand Up @@ -63,7 +65,7 @@ public class NotificationFactory {
*/
public static Notification parseNotification(Map<String,String> headers, final String body) throws AmazonClientException{

NotificationVerification verifier = new NotificationVerification();
final NotificationVerification verifier = new NotificationVerification();

//verify notification header values
verifier.verifyHeaders(headers);
Expand All @@ -76,7 +78,7 @@ public static Notification parseNotification(Map<String,String> headers, final S
}

//parse notification
Notification notification = NotificationFactory.getNotification(body);
final Notification notification = NotificationFactory.getNotification(body);

//log notification body contents to the console.
try {
Expand Down Expand Up @@ -106,19 +108,19 @@ public static Notification parseNotification(Map<String,String> headers, final S


private static Notification getNotification(String payLoad) {
com.amazon.pay.response.ipn.model.Notification notifData = null;
Notification notifData = null;
if (payLoad == null || payLoad.isEmpty()) {
throw new AmazonClientException("Aborting, empty payload");
}
String notificationDataAsJSON = payLoad;
Map<String,String> notificationDataAsMap = new Gson().fromJson(payLoad, Map.class);
final String notificationDataAsJSON = payLoad;
final Map<String,String> notificationDataAsMap = new Gson().fromJson(payLoad, Map.class);

String message = notificationDataAsMap.get("Message");
final String message = notificationDataAsMap.get("Message");
Map<String,String> messageDataMap = null;
if (message != null) {
messageDataMap = new Gson().fromJson(message, Map.class);
if (messageDataMap != null) {
String notificationType = messageDataMap.get("NotificationType");
final String notificationType = messageDataMap.get("NotificationType");
JAXBContext jaxbContext = null;
try {
if ("OrderReferenceNotification".equalsIgnoreCase(notificationType)) {
Expand All @@ -143,20 +145,16 @@ private static Notification getNotification(String payLoad) {

// Modify namespace declaration so it is ignored
if (jaxbContext != null) {
String notificationData = messageDataMap.get("NotificationData");
//ignore the namespace only for marshalling purpose
notificationData = notificationData.replaceAll("xmlns(?:.*?)?=\"https://mws.amazonservices.com/ipn/OffAmazonPayments/2013-01-01\"", "");
StringReader reader = new StringReader(notificationData.trim());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

// If you need to do some deep dive troubleshooting to trace hard to find
// XML parsing/unmarshalling issues, consider uncommenting the next line:
// unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
final String notificationData = messageDataMap.get("NotificationData").replaceAll(
"xmlns(?:.*?)?=\"https://mws.amazonservices.com/ipn/OffAmazonPayments/2013-01-01\"", "");
final StringReader reader = new StringReader(notificationData.trim());
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setEventHandler(new AmazonValidationEventHandler());

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
notifData = (Notification) unmarshaller.unmarshal(xmlStreamReader);
notifData.setNotificationMetadata(new NotificationMetaData(notificationDataAsMap));
notifData.setMessageMetaData(new IPNMessageMetaData(messageDataMap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ipn", propOrder = {
"authorizationDetails"
}, namespace="https://mws.amazonservices.com/ipn/OffAmazonPayments/2013-01-01")
@XmlRootElement(name="AuthorizationNotification")
public class AuthorizationNotification extends Notification {
})
@XmlRootElement(name = "AuthorizationNotification")

public class AuthorizationNotification extends Notification {

/**
* Authorization details field.
Expand Down Expand Up @@ -65,9 +66,8 @@ public AuthorizationDetails getAuthorizationDetails() {
*/
@Override
public String toString() {
return "AuthorizationNotification{" + "authorizationDetails=" + authorizationDetails.toString() + '}';
return "AuthorizationNotification{"
+ "authorizationDetails=" + authorizationDetails.toString() + '}';
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
@javax.xml.bind.annotation.XmlSchema(namespace = "http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.amazon.pay.response.model;

package com.amazon.pay.response.ipn.model;

/**
* Event types generated by the Amazon Pay
* Billing Agreement IPNs
*/
public enum EventType {
BillingAgreementStatusUpdated,
PaymentMethodUpdated,
AddressUpdated,
AddressAndPaymentMethodUpdated
}
33 changes: 32 additions & 1 deletion src/com/amazon/pay/response/ipn/model/IPNMessageMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ public final class IPNMessageMetaData {
private final String notificationReferenceId;
private final String sellerId;
private final String version;
private final String notificationType;
private final EventType eventType;

public IPNMessageMetaData(Map<String,String> messageMetaDataMap) {
releaseEnvironment = messageMetaDataMap.get("ReleaseEnvironment");
timeStamp = messageMetaDataMap.get("Timestamp");
notificationReferenceId = messageMetaDataMap.get("NotificationReferenceId");
sellerId = messageMetaDataMap.get("SellerId");
version = messageMetaDataMap.get("Version");
notificationType = messageMetaDataMap.get("NotificationType");

final String eventTypeString = messageMetaDataMap.get("EventType");
if (eventTypeString != null) {
eventType = EventType.valueOf(eventTypeString);
} else {
eventType = null;
}
}

/**
Expand Down Expand Up @@ -77,6 +87,24 @@ public String getVersion() {
return version;
}

/**
* Returns the Notification Type string specified in IPN message
*
* @return Returns the NotificationType specified in IPN message
*/
public String getNotificationType() {
return notificationType;
}

/**
* Returns the Event Type string specified in IPN message
*
* @return Returns the EventType specified in IPN message
*/
public EventType getEventType() {
return eventType;
}

/**
* String representation of IPN Message Metadata
*
Expand All @@ -88,7 +116,10 @@ public String toString() {
+ ", timeStamp=" + timeStamp
+ ", notificationReferenceId=" + notificationReferenceId
+ ", sellerId=" + sellerId
+ ", version=" + version + '}';
+ ", version=" + version
+ ", notificationType=" + notificationType
+ ", eventType=" + eventType
+ '}';
}

}
Loading

0 comments on commit 77c3c6c

Please sign in to comment.