Skip to content

Commit ac1cb06

Browse files
authored
Merge pull request #138 from Adyen/payments-dates
Added date serializers for payments request deliveryDate and dateofBirth
2 parents 2ee2e72 + 8a159a8 commit ac1cb06

File tree

9 files changed

+63
-19
lines changed

9 files changed

+63
-19
lines changed

src/main/java/com/adyen/Util/DateUtil.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.text.ParseException;
2424
import java.text.SimpleDateFormat;
2525
import java.util.Date;
26+
import java.util.Locale;
27+
import java.util.TimeZone;
2628

2729
public final class DateUtil {
2830
private DateUtil() {
@@ -33,15 +35,15 @@ public static Date parseDateToFormat(String dateString, String format) {
3335
return null;
3436
}
3537

36-
Date date;
37-
SimpleDateFormat fmt = new SimpleDateFormat(format);
38+
SimpleDateFormat fmt = new SimpleDateFormat(format, Locale.ENGLISH);
39+
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
40+
3841
try {
39-
date = fmt.parse(dateString);
40-
} catch (ParseException e) {
41-
return null;
42+
return fmt.parse(dateString);
43+
} catch (ParseException ignored) {
4244
}
4345

44-
return date;
46+
return null;
4547
}
4648

4749
public static Date parseYmdDate(String dateString) {

src/main/java/com/adyen/Util/Util.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import com.adyen.model.Amount;
2424
import java.math.BigDecimal;
2525
import java.math.RoundingMode;
26+
import java.text.DateFormat;
2627
import java.text.SimpleDateFormat;
2728
import java.util.Calendar;
2829
import java.util.Date;
2930
import java.util.List;
31+
import java.util.Locale;
32+
import java.util.TimeZone;
3033

3134
public final class Util {
3235
private Util() {
@@ -149,6 +152,9 @@ public static String calculateSessionValidity() {
149152
calendar.add(Calendar.DATE, 1);
150153
Date sessionDate = calendar.getTime();
151154

152-
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(sessionDate);
155+
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH);
156+
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
157+
158+
return fmt.format(sessionDate);
153159
}
154160
}

src/main/java/com/adyen/model/checkout/PaymentsRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.adyen.model.ForexQuote;
3636
import com.adyen.model.Installments;
3737
import com.adyen.model.Name;
38+
import com.adyen.serializer.DateSerializer;
39+
import com.adyen.serializer.DateTimeGMTSerializer;
3840
import com.google.gson.TypeAdapter;
3941
import com.google.gson.annotations.JsonAdapter;
4042
import com.google.gson.annotations.SerializedName;
@@ -65,12 +67,14 @@ public class PaymentsRequest {
6567
@SerializedName("countryCode")
6668
private String countryCode = null;
6769
@SerializedName("dateOfBirth")
70+
@JsonAdapter(DateSerializer.class)
6871
private Date dateOfBirth = null;
6972
@SerializedName("dccQuote")
7073
private ForexQuote dccQuote = null;
7174
@SerializedName("deliveryAddress")
7275
private Address deliveryAddress = null;
7376
@SerializedName("deliveryDate")
77+
@JsonAdapter(DateTimeGMTSerializer.class)
7478
private Date deliveryDate = null;
7579
@SerializedName("enableOneClick")
7680
private Boolean enableOneClick = null;

src/main/java/com/adyen/serializer/DateSerializer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* ######
33
* ######
44
* ############ ####( ###### #####. ###### ############ ############
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Type;
2424
import java.text.SimpleDateFormat;
2525
import java.util.Date;
26+
import java.util.Locale;
2627
import java.util.TimeZone;
2728
import com.google.gson.JsonElement;
2829
import com.google.gson.JsonPrimitive;
@@ -34,7 +35,7 @@ public class DateSerializer implements JsonSerializer<Date> {
3435

3536
@Override
3637
public JsonElement serialize(Date date, Type typeOfSrc, JsonSerializationContext context) {
37-
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
38+
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
3839
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
3940
return new JsonPrimitive(formatter.format(date));
4041
}

src/main/java/com/adyen/serializer/DateTimeGMTSerializer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* ######
33
* ######
44
* ############ ####( ###### #####. ###### ############ ############
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Type;
2424
import java.text.SimpleDateFormat;
2525
import java.util.Date;
26+
import java.util.Locale;
2627
import java.util.TimeZone;
2728
import com.google.gson.JsonElement;
2829
import com.google.gson.JsonPrimitive;
@@ -37,7 +38,7 @@ public class DateTimeGMTSerializer implements JsonSerializer<Date> {
3738
*/
3839
@Override
3940
public JsonElement serialize(Date date, Type typeOfSrc, JsonSerializationContext context) {
40-
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
41+
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
4142
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
4243
return new JsonPrimitive(formatter.format(date));
4344
}

src/main/java/com/adyen/service/Resource.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
package com.adyen.service;
2222

23+
import java.io.IOException;
24+
import java.util.List;
2325
import com.adyen.Config;
2426
import com.adyen.Service;
2527
import com.adyen.httpclient.ClientInterface;
@@ -31,9 +33,6 @@
3133
import com.google.gson.JsonSyntaxException;
3234
import com.google.gson.reflect.TypeToken;
3335

34-
import java.io.IOException;
35-
import java.util.List;
36-
3736
public class Resource {
3837

3938
protected static final Gson GSON = new Gson();

src/test/java/com/adyen/CheckoutTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
package com.adyen;
2222

23+
import java.text.ParseException;
24+
import java.text.SimpleDateFormat;
25+
import java.util.Date;
26+
import java.util.Locale;
27+
import java.util.TimeZone;
2328
import org.junit.Test;
2429
import com.adyen.model.Amount;
2530
import com.adyen.model.checkout.PaymentMethodDetails;
@@ -223,6 +228,20 @@ public void TestPaymentMethodDetails() {
223228
jsonRequest);
224229
}
225230

231+
@Test
232+
public void TestDateSerializers() throws ParseException {
233+
PaymentsRequest paymentsRequest = new PaymentsRequest();
234+
235+
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
236+
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
237+
238+
Date d = fmt.parse("2018-10-31");
239+
paymentsRequest.setDateOfBirth(d);
240+
paymentsRequest.setDeliveryDate(d);
241+
String jsonRequest = GSON.toJson(paymentsRequest);
242+
assertEquals("{\"dateOfBirth\":\"2018-10-31\",\"deliveryDate\":\"2018-10-31T00:00:00.000Z\"}", jsonRequest);
243+
}
244+
226245
/**
227246
* Returns a sample PaymentSessionRequest opbject with test data
228247
*/

src/test/java/com/adyen/MarketPayTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.text.SimpleDateFormat;
2424
import java.util.ArrayList;
2525
import java.util.List;
26+
import java.util.Locale;
27+
import java.util.TimeZone;
2628
import org.junit.Test;
2729
import com.adyen.model.Address;
2830
import com.adyen.model.Amount;
@@ -146,7 +148,9 @@ public void TestCreateSplitPayment() throws Exception {
146148

147149
assertTrue(paymentResult.isAuthorised());
148150

149-
SimpleDateFormat format = new SimpleDateFormat("M/yyyy");
151+
SimpleDateFormat format = new SimpleDateFormat("M/yyyy", Locale.ENGLISH);
152+
format.setTimeZone(TimeZone.getTimeZone("GMT"));
153+
150154
assertEquals("8/2018", format.format(paymentResult.getExpiryDate()));
151155

152156
assertEquals("411111", paymentResult.getCardBin());

src/test/java/com/adyen/PaymentTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
*/
2121
package com.adyen;
2222

23+
import java.text.DateFormat;
2324
import java.text.SimpleDateFormat;
2425
import java.util.HashMap;
2526
import java.util.List;
27+
import java.util.Locale;
28+
import java.util.TimeZone;
2629
import org.junit.Test;
2730
import com.adyen.constants.ApiConstants.AdditionalData;
2831
import com.adyen.constants.ApiConstants.RefusalReason;
@@ -70,7 +73,9 @@ public void TestAuthoriseSuccessMocked() throws Exception {
7073

7174
assertTrue(paymentResult.isAuthorised());
7275

73-
SimpleDateFormat format = new SimpleDateFormat("M/yyyy");
76+
SimpleDateFormat format = new SimpleDateFormat("M/yyyy", Locale.ENGLISH);
77+
format.setTimeZone(TimeZone.getTimeZone("GMT"));
78+
7479
assertEquals("8/2018", format.format(paymentResult.getExpiryDate()));
7580

7681
assertEquals("411111", paymentResult.getCardBin());
@@ -206,7 +211,7 @@ public void TestError401Mocked() throws Exception {
206211
HTTPClientException httpClientException = new HTTPClientException(401, "An error occured", new HashMap<String, List<String>>(), null);
207212

208213
when(httpURLConnectionClient.request(any(String.class), any(String.class), any(Config.class), anyBoolean(), any(RequestOptions.class))).thenThrow(httpClientException);
209-
when(httpURLConnectionClient.request(any(String.class), any(String.class), any(Config.class), anyBoolean(), (RequestOptions)isNull())).thenThrow(httpClientException);
214+
when(httpURLConnectionClient.request(any(String.class), any(String.class), any(Config.class), anyBoolean(), (RequestOptions) isNull())).thenThrow(httpClientException);
210215

211216
Client client = new Client();
212217
client.setHttpClient(httpURLConnectionClient);
@@ -272,11 +277,14 @@ public void TestBoletoSuccess() throws Exception {
272277
"BQABAQB8k7t5uD2wSpo185nNeQ9CU50Zf6z/z9EdC5yFH3+1o/DQH3v3dtTxqXD2DrEdVH0Ro3r/+G9bdUzrCUjfMFh7YB32VL2oPqye9Ly/MWzj7bOaRrpGH5PaB8gE9LkIgo8WKqHix1cwsFm3aHiLBECjItOpUR/CBuiJBGPvseN7yrSdG5vQAUM9AQixpPkyCNokbnDZoa1y3+qihZa7vvzV/XylTXdgirxboVKpk07Wfvpad8Owg/K/ofDqUfrZ3SUovkJzpZ5wP2NtOz84zBV8dJ+9vZs+aor/E//s+EjKgNJt2s2uX0OfdE3h1n41RW2MlfQBtXLbgbxKVVSH5qfPELsZhr10A9y9VpCd9DOP6lEAAFchf10tGLvIKj2j4ktIErp0uLCbLqa1/AvmfQ9a6e0TClmsbtwKoZ9LvAPpzHqRcmidgyUM1Igk5YsHBD7L8pzoJS5hL+DKXMeUav6oP20v9huLS3Ps6EiK4fyg5kgptZPhSQ5UN3GrGSoefja1Ylw32EBovEiaK9rdKkT/eVf+wncwLTLUiMD26R7qRxbvwAg4G8VIv6dxvOsKf2RutfOoCBNH6VhgwXfIoe0bHqmpx4dGwrjkVThspdsZYhHFrZK58grIb4OyKORibOYxvsmYmRdWMDX9Y1X8uva8OYs=",
273278
paymentResult.getBoletoData());
274279

275-
assertEquals("2017-05-22", new SimpleDateFormat("yyyy-MM-dd").format(paymentResult.getBoletoDueDate()));
280+
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
281+
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
282+
283+
assertEquals("2017-05-22", fmt.format(paymentResult.getBoletoDueDate()));
276284
assertEquals(
277285
"https://test.adyen.com/hpp/generationBoleto.shtml?data=BQABAQB8k7t5uD2wSpo185nNeQ9CU50Zf6z%2Fz9EdC5yFH3%2B1o%2FDQH3v3dtTxqXD2DrEdVH0Ro3r%2F%2BG9bdUzrCUjfMFh7YB32VL2oPqye9Ly%2FMWzj7bOaRrpGH5PaB8gE9LkIgo8WKqHix1cwsFm3aHiLBECjItOpUR%2FCBuiJBGPvseN7yrSdG5vQAUM9AQixpPkyCNokbnDZoa1y3%2BqihZa7vvzV%2FXylTXdgirxboVKpk07Wfvpad8Owg%2FK%2FofDqUfrZ3SUovkJzpZ5wP2NtOz84zBV8dJ%2B9vZs%2Baor%2FE%2F%2Fs%2BEjKgNJt2s2uX0OfdE3h1n41RW2MlfQBtXLbgbxKVVSH5qfPELsZhr10A9y9VpCd9DOP6lEAAFchf10tGLvIKj2j4ktIErp0uLCbLqa1%2FAvmfQ9a6e0TClmsbtwKoZ9LvAPpzHqRcmidgyUM1Igk5YsHBD7L8pzoJS5hL%2BDKXMeUav6oP20v9huLS3Ps6EiK4fyg5kgptZPhSQ5UN3GrGSoefja1Ylw32EBovEiaK9rdKkT%2FeVf%2BwncwLTLUiMD26R7qRxbvwAg4G8VIv6dxvOsKf2RutfOoCBNH6VhgwXfIoe0bHqmpx4dGwrjkVThspdsZYhHFrZK58grIb4OyKORibOYxvsmYmRdWMDX9Y1X8uva8OYs%3D",
278286
paymentResult.getBoletoUrl());
279-
assertEquals("2017-06-06", new SimpleDateFormat("yyyy-MM-dd").format(paymentResult.getBoletoExpirationDate()));
287+
assertEquals("2017-06-06", fmt.format(paymentResult.getBoletoExpirationDate()));
280288
assertEquals(RECEIVED, paymentResult.getResultCode());
281289
assertEquals("8814950120218231", paymentResult.getPspReference());
282290
}

0 commit comments

Comments
 (0)