All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wallee.sdk.trid.JSON Maven / Gradle / Ivy

/*
 * MDES for Merchants
 * The MDES APIs are designed as RPC style stateless web services where each API endpoint represents an operation to be performed.  All request and response payloads are sent in the JSON (JavaScript Object Notation) data-interchange format. Each endpoint in the API specifies the HTTP Method used to access it. All strings in request and response objects are to be UTF-8 encoded.  Each API URI includes the major and minor version of API that it conforms to.  This will allow multiple concurrent versions of the API to be deployed simultaneously. 
__Authentication__ Mastercard uses OAuth 1.0a with body hash extension for authenticating the API clients. This requires every request that you send to Mastercard to be signed with an RSA private key. A private-public RSA key pair must be generated consisting of:
1 . A private key for the OAuth signature for API requests. It is recommended to keep the private key in a password-protected or hardware keystore.
2. A public key is shared with Mastercard during the project setup process through either a certificate signing request (CSR) or the API Key Generator. Mastercard will use the public key to verify the OAuth signature that is provided on every API call.
An OAUTH1.0a signer library is available on [GitHub](https://github.com/Mastercard/oauth1-signer-java)
__Encryption__
All communications between Issuer web service and the Mastercard gateway is encrypted using TLS.
__Additional Encryption of Sensitive Data__ In addition to the OAuth authentication, when using MDES Digital Enablement Service, any PCI sensitive and all account holder Personally Identifiable Information (PII) data must be encrypted. This requirement applies to the API fields containing encryptedData. Sensitive data is encrypted using a symmetric session (one-time-use) key. The symmetric session key is then wrapped with an RSA Public Key supplied by Mastercard during API setup phase (the Customer Encryption Key).
Java Client Encryption Library available on [GitHub](https://github.com/Mastercard/client-encryption-java) * * OpenAPI spec version: 1.2.10 * * * NOTE: This class is auto generated by the swagger code generator program. * https://github.com/swagger-api/swagger-codegen.git * Do not edit the class manually. */ package com.wallee.sdk.trid; import java.io.IOException; import java.lang.reflect.Type; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.TimeZone; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public class JSON { public static final double JAVA_VERSION; public static final boolean IS_ANDROID; public static final int ANDROID_SDK_VERSION; static { JAVA_VERSION = Double.parseDouble(System.getProperty("java.specification.version")); boolean isAndroid; try { Class.forName("android.app.Activity"); isAndroid = true; } catch (ClassNotFoundException e) { isAndroid = false; } IS_ANDROID = isAndroid; int sdkVersion = 0; if (IS_ANDROID) { try { sdkVersion = Class.forName("android.os.Build$VERSION").getField("SDK_INT").getInt(null); } catch (Exception e) { try { sdkVersion = Integer.parseInt((String) Class.forName("android.os.Build$VERSION").getField("SDK").get(null)); } catch (Exception e2) { } } } ANDROID_SDK_VERSION = sdkVersion; } /** * The datetime format to be used when lenientDatetimeFormat is enabled. */ public static final String LENIENT_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; private DateFormat dateFormat; private DateFormat datetimeFormat; private boolean lenientDatetimeFormat; private int dateLength; private Gson gson; /** * JSON constructor. */ public JSON() { /* * Use RFC3339 format for date and datetime. * See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 */ this.dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // Always use UTC as the default time zone when dealing with date (without time). this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); initDatetimeFormat(); // Be lenient on datetime formats when parsing datetime from string. // See parseDatetime. this.lenientDatetimeFormat = true; gson = new GsonBuilder() .registerTypeAdapter(Date.class, new DateAdapter(this)) .registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter()) .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter()) .create(); } /** * Initialize datetime format according to the current environment, e.g. Java 1.7 and Android. */ private void initDatetimeFormat() { String formatWithTimeZone = null; if (IS_ANDROID) { if (ANDROID_SDK_VERSION >= 18) { // The time zone format "ZZZZZ" is available since Android 4.3 (SDK version 18) formatWithTimeZone = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"; } } else if (JAVA_VERSION >= 1.7) { // The time zone format "XXX" is available since Java 1.7 formatWithTimeZone = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; } if (formatWithTimeZone != null) { this.datetimeFormat = new SimpleDateFormat(formatWithTimeZone); // NOTE: Use the system's default time zone (mainly for datetime formatting). } else { // Use a common format that works across all systems. this.datetimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // Always use the UTC time zone as we are using a constant trailing "Z" here. this.datetimeFormat.setTimeZone(TimeZone.getTimeZone("UTC")); } } /** * Get Gson. * * @return Gson */ public Gson getGson() { return gson; } /** * Set Gson. * * @param gson Gson */ public void setGson(Gson gson) { this.gson = gson; } /** * Serialize the given Java object into JSON string. * * @param obj Object * @return String representation of the JSON */ public String serialize(Object obj) { return gson.toJson(obj); } /** * Deserialize the given JSON string to Java object. * * @param Type * @param body The JSON string * @param returnType The type to deserialize into * @return The deserialized Java object */ @SuppressWarnings("unchecked") public T deserialize(String body, Type returnType) { try { return gson.fromJson(body, returnType); } catch (JsonParseException e) { // Fallback processing when failed to parse JSON form response body: // return the response body string directly for the String return type; // parse response body into date or datetime for the Date return type. if (returnType.equals(String.class)) return (T) body; else if (returnType.equals(Date.class)) return (T) parseDateOrDatetime(body); else throw(e); } } public DateFormat getDateFormat() { return dateFormat; } public JSON setDateFormat(DateFormat dateFormat) { this.dateFormat = dateFormat; this.dateLength = this.dateFormat.format(new Date()).length(); return this; } public DateFormat getDatetimeFormat() { return datetimeFormat; } public JSON setDatetimeFormat(DateFormat datetimeFormat) { this.datetimeFormat = datetimeFormat; return this; } /** * Parse the given date string into Date object. * The default dateFormat supports these ISO 8601 date formats: * 2015-08-16 * 2015-8-16 * @param str String to be parsed * @return Date */ public Date parseDate(String str) { if (str == null) return null; try { return dateFormat.parse(str); } catch (ParseException e) { throw new RuntimeException(e); } } /** * Parse the given datetime string into Date object. * When lenientDatetimeFormat is enabled, the following ISO 8601 datetime formats are supported: * 2015-08-16T08:20:05Z * 2015-8-16T8:20:05Z * 2015-08-16T08:20:05+00:00 * 2015-08-16T08:20:05+0000 * 2015-08-16T08:20:05.376Z * 2015-08-16T08:20:05.376+00:00 * 2015-08-16T08:20:05.376+00 * Note: The 3-digit milli-seconds is optional. Time zone is required and can be in one of * these formats: * Z (same with +0000) * +08:00 (same with +0800) * -02 (same with -0200) * -0200 * @see ISO 8601 * @param str Date time string to be parsed * @return Date representation of the string */ public Date parseDatetime(String str) { if (str == null) return null; DateFormat format; if (lenientDatetimeFormat) { /* * When lenientDatetimeFormat is enabled, normalize the date string * into LENIENT_DATETIME_FORMAT to support various formats * defined by ISO 8601. */ // normalize time zone // trailing "Z": 2015-08-16T08:20:05Z => 2015-08-16T08:20:05+0000 str = str.replaceAll("[zZ]\\z", "+0000"); // remove colon in time zone: 2015-08-16T08:20:05+00:00 => 2015-08-16T08:20:05+0000 str = str.replaceAll("([+-]\\d{2}):(\\d{2})\\z", "$1$2"); // expand time zone: 2015-08-16T08:20:05+00 => 2015-08-16T08:20:05+0000 str = str.replaceAll("([+-]\\d{2})\\z", "$100"); // add milliseconds when missing // 2015-08-16T08:20:05+0000 => 2015-08-16T08:20:05.000+0000 str = str.replaceAll("(:\\d{1,2})([+-]\\d{4})\\z", "$1.000$2"); format = new SimpleDateFormat(LENIENT_DATETIME_FORMAT); } else { format = this.datetimeFormat; } try { return format.parse(str); } catch (ParseException e) { throw new RuntimeException(e); } } /* * Parse date or date time in string format into Date object. * * @param str Date time string to be parsed * @return Date representation of the string */ public Date parseDateOrDatetime(String str) { if (str == null) return null; else if (str.length() <= dateLength) return parseDate(str); else return parseDatetime(str); } /** * Format the given Date object into string (Date format). * * @param date Date object * @return Formatted date in string representation */ public String formatDate(Date date) { return dateFormat.format(date); } /** * Format the given Date object into string (Datetime format). * * @param date Date object * @return Formatted datetime in string representation */ public String formatDatetime(Date date) { return datetimeFormat.format(date); } } class DateAdapter implements JsonSerializer, JsonDeserializer { private JSON json; /** * Constructor for DateAdapter * * @param apiClient Api client */ public DateAdapter(JSON json) { super(); this.json = json; } /** * Serialize * * @param src Date * @param typeOfSrc Type * @param context Json Serialization Context * @return Json Element */ @Override public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { if (src == null) { return JsonNull.INSTANCE; } else { return new JsonPrimitive(json.formatDatetime(src)); } } /** * Deserialize * * @param json Json element * @param date Type * @param context Json Serialization Context * @return Date * @throws JsonParseException if fail to parse */ @Override public Date deserialize(JsonElement json, Type date, JsonDeserializationContext context) throws JsonParseException { String str = json.getAsJsonPrimitive().getAsString(); try { return this.json.parseDateOrDatetime(str); } catch (RuntimeException e) { throw new JsonParseException(e); } } } /** * Gson TypeAdapter for jsr310 OffsetDateTime type */ class OffsetDateTimeTypeAdapter extends TypeAdapter { private final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; @Override public void write(JsonWriter out, OffsetDateTime date) throws IOException { if (date == null) { out.nullValue(); } else { out.value(formatter.format(date)); } } @Override public OffsetDateTime read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); if (date.endsWith("+0000")) { date = date.substring(0, date.length()-5) + "Z"; } return OffsetDateTime.parse(date, formatter); } } } /** * Gson TypeAdapter for jsr310 LocalDate type */ class LocalDateTypeAdapter extends TypeAdapter { private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE; @Override public void write(JsonWriter out, LocalDate date) throws IOException { if (date == null) { out.nullValue(); } else { out.value(formatter.format(date)); } } @Override public LocalDate read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); return LocalDate.parse(date, formatter); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy