Skip to content

Commit

Permalink
Update Klarna, Payment properties and enums (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko authored Sep 3, 2024
1 parent a98e2bb commit 7064069
Show file tree
Hide file tree
Showing 19 changed files with 488 additions and 36 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/checkout/GsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.checkout.issuing.controls.responses.create.VelocityCardControlResponse;
import com.checkout.payments.PaymentDestinationType;
import com.checkout.payments.previous.PaymentAction;
import com.checkout.payments.PaymentPlanType;
import com.checkout.payments.sender.Sender;
import com.checkout.payments.sender.SenderType;
import com.checkout.webhooks.previous.WebhookResponse;
Expand All @@ -32,6 +33,7 @@
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
Expand All @@ -41,6 +43,7 @@

import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -274,6 +277,15 @@ private static JsonDeserializer<Instant> getInstantJsonDeserializer() {
try {
return Instant.parse(dateString);
} catch (final DateTimeParseException ex) {
if (dateString.length() == 8) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dateTime = LocalDate.parse(dateString, formatter).atStartOfDay();
return dateTime.toInstant(ZoneOffset.UTC);
} catch (final DateTimeParseException e) {
throw new JsonParseException("Failed to parse date in format yyyyMMdd: " + dateString, e);
}
}
for (final DateTimeFormatter formatter : DEFAULT_FORMATTERS) {
try {
final LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/checkout/common/CheckoutUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public final class CheckoutUtils {
public static final String WEEKLY = "Weekly";
public static final String MONTHLY = "Monthly";
public static final String ACCEPT_JSON = "application/json;charset=UTF-8";
private static final String CKO_REQUEST_ID = "Cko-Request-Id";
public static final String CONTROL_TYPE = "control_type";
private static final String CKO_REQUEST_ID = "Cko-Request-Id";

private CheckoutUtils() {
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/checkout/payments/AmountVariability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.checkout.payments;

import com.google.gson.annotations.SerializedName;

public enum AmountVariability {

@SerializedName("Fixed")
FIXED,

@SerializedName("Variable")
VARIABLE

}
37 changes: 37 additions & 0 deletions src/main/java/com/checkout/payments/PaymentPlanType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.checkout.payments;

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;

@Data
@Builder
public final class PaymentPlanType {

// Recurring
@SerializedName("amount_variability")
private AmountVariability amountVariability;

// Installment
private Boolean financing;

private String amount;

// Common properties
@SerializedName("days_between_payments")
private Integer daysBetweenPayments;

@SerializedName("total_number_of_payments")
private Integer totalNumberOfPayments;

@SerializedName("current_payment_number")
private Integer currentPaymentNumber;

@SerializedName("expiry")
private Instant expiry;

}
20 changes: 14 additions & 6 deletions src/main/java/com/checkout/payments/PaymentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@

public enum PaymentType {

@SerializedName("Regular")
REGULAR,
@SerializedName("Recurring")
RECURRING,
@SerializedName("Moto")
MOTO,
@SerializedName("Installment")
INSTALLMENT,

@SerializedName("Moto")
MOTO,

@SerializedName("PayLater")
PAYLATER,

@SerializedName("Recurring")
RECURRING,

@SerializedName("Regular")
REGULAR,

@SerializedName("Unscheduled")
UNSCHEDULED

}
3 changes: 3 additions & 0 deletions src/main/java/com/checkout/payments/ProcessingSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public final class ProcessingSettings {
@SerializedName("shipping_info")
private List<ShippingInfo> shippingInfo;

@SerializedName("pan_preference")
private PanProcessedType panPreference;

//Previous
private DLocalProcessingSettings dlocal;

Expand Down
29 changes: 28 additions & 1 deletion src/main/java/com/checkout/payments/ProductType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,38 @@ public enum ProductType {

@SerializedName("QR Code")
QR_CODE,

@SerializedName("In-App")
IN_APP,

@SerializedName("Official Account")
OFFICIAL_ACCOUNT,

@SerializedName("Mini Program")
MINI_PROGRAM
MINI_PROGRAM,

@SerializedName("pay_in_full")
PAY_IN_FULL,

@SerializedName("pay_by_instalment")
PAY_BY_INSTALMENT,

@SerializedName("pay_by_instalment_2")
PAY_BY_INSTALMENT_2,

@SerializedName("pay_by_instalment_3")
PAY_BY_INSTALMENT_3,

@SerializedName("pay_by_instalment_4")
PAY_BY_INSTALMENT_4,

@SerializedName("pay_by_instalment_6")
PAY_BY_INSTALMENT_6,

@SerializedName("invoice")
INVOICE,

@SerializedName("pay_later")
PAY_LATER

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@AllArgsConstructor
public final class PaymentContextsItems {

private String type;

private String name;

private Integer quantity;
Expand All @@ -24,14 +26,24 @@ public final class PaymentContextsItems {
@SerializedName("total_amount")
private Integer totalAmount;

@SerializedName("tax_rate")
private Integer taxRate;

@SerializedName("tax_amount")
private Integer taxAmount;

@SerializedName("discount_amount")
private Integer discountAmount;

@SerializedName("wxpay_goods_id")
private String wxpayGoodsId;

private String url;

@SerializedName("image_url")
private String imageUrl;

@SerializedName("service_ends_on")
private String serviceEndsOn;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public final class PaymentContextsProcessing {

private BillingPlan plan;

@SerializedName("discount_amount")
private Integer discountAmount;

@SerializedName("shipping_amount")
private Integer shippingAmount;

Expand All @@ -41,4 +44,5 @@ public final class PaymentContextsProcessing {

@SerializedName("airline_data")
private List<PaymentContextsAirlineData> airlineData;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.checkout.payments.ShippingDetails;
import com.checkout.payments.ThreeDSRequest;
import com.checkout.payments.request.source.AbstractRequestSource;
import com.checkout.payments.PaymentPlanType;
import com.checkout.payments.sender.PaymentSender;
import com.google.gson.annotations.SerializedName;
import lombok.Builder;
Expand All @@ -38,7 +39,10 @@ public final class PaymentRequest {
private Currency currency;

@SerializedName("payment_type")
private PaymentType paymentType;
private PaymentType paymentType = PaymentType.REGULAR;

@SerializedName("payment_plan")
private PaymentPlanType paymentPlan;

@SerializedName("merchant_initiated")
private Boolean merchantInitiated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.checkout.payments.ThreeDSData;
import com.checkout.payments.request.PaymentInstruction;
import com.checkout.payments.response.destination.PaymentResponseDestination;
import com.checkout.payments.PaymentPlanType;
import com.checkout.payments.response.source.ResponseSource;
import com.checkout.payments.sender.Sender;
import com.google.gson.annotations.SerializedName;
Expand Down Expand Up @@ -51,6 +52,9 @@ public final class GetPaymentResponse extends Resource {
@SerializedName("payment_type")
private PaymentType type;

@SerializedName("payment_plan")
private PaymentPlanType paymentPlan;

private String reference;

private String description;
Expand Down Expand Up @@ -103,12 +107,12 @@ public final class GetPaymentResponse extends Resource {
private String schemeId;

private List<PaymentActionSummary> actions;

private PaymentRetryResponse retry;

@SerializedName("pan_type_processed")
private PanProcessedType panTypeProcessed;

@SerializedName("cko_network_token_available")
private Boolean ckoNetworkTokenAvailable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import com.checkout.payments.PaymentProcessing;
import com.checkout.payments.PaymentRecipient;
import com.checkout.payments.PaymentStatus;
import com.checkout.payments.PaymentType;
import com.checkout.payments.RiskAssessment;
import com.checkout.payments.ShippingDetails;
import com.checkout.payments.ThreeDSEnrollment;
import com.checkout.payments.PaymentPlanType;
import com.checkout.payments.response.source.ResponseSource;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
Expand All @@ -29,6 +31,12 @@ public final class PaymentResponse extends Resource implements Serializable {

private String id;

@SerializedName("payment_type")
private PaymentType paymentType;

@SerializedName("payment_plan")
private PaymentPlanType paymentPlan;

@SerializedName("action_id")
private String actionId;

Expand Down
45 changes: 34 additions & 11 deletions src/test/java/com/checkout/GsonSerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,39 +131,62 @@ void shouldSerializePaymentContextsPayPalDetailsResponseFromJson() {
assertEquals(PaymentSourceType.PAYPAL, paymentContextsPayPalResponseSource.getPaymentRequest().getSource().getType());
}

@Test
void shouldSerializePaymentContextsResponseFromJson() {

final com.checkout.payments.response.PaymentResponse paymentContextsResponse = serializer.fromJson(getMock("/mocks/payments/response/plan/get_payment_context_response.json"), com.checkout.payments.response.PaymentResponse.class);

assertNotNull(paymentContextsResponse);
assertNotNull(paymentContextsResponse.getPaymentPlan());
}

@Test
void shouldSerializePaymentDetailsResponseFromJson() {

final com.checkout.payments.response.GetPaymentResponse paymentDetailsResponse = serializer.fromJson(getMock("/mocks/payments/response/plan/get_payment_details_response.json"), com.checkout.payments.response.GetPaymentResponse.class);

assertNotNull(paymentDetailsResponse);
assertNotNull(paymentDetailsResponse.getPaymentPlan());
}

@Test
void shouldDeserializeMultipleDateFormats() {
Instant instant = Instant.parse("2021-06-08T12:25:01Z");
PaymentResponse paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01.000Z\"}", PaymentResponse.class);
Instant instant = Instant.parse("2021-06-08T00:00:00Z");
PaymentResponse paymentResponse;

// Test format yyyyMMdd
paymentResponse = serializer.fromJson("{\"processed_on\":\"20210608\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
assertEquals(instant, paymentResponse.getProcessedOn());

paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01Z\"}", PaymentResponse.class);
// Test other valid formats
paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01.000Z\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
assertEquals(instant, paymentResponse.getProcessedOn());
assertEquals(Instant.parse("2021-06-08T12:25:01Z"), paymentResponse.getProcessedOn());

paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01+00:00\"}", PaymentResponse.class);
paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01Z\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
assertEquals(instant, paymentResponse.getProcessedOn());
assertEquals(Instant.parse("2021-06-08T12:25:01Z"), paymentResponse.getProcessedOn());

paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01+0000\"}", PaymentResponse.class);
paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01+00:00\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
assertEquals(instant, paymentResponse.getProcessedOn());
assertEquals(Instant.parse("2021-06-08T12:25:01Z"), paymentResponse.getProcessedOn());

paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01+00\"}", PaymentResponse.class);
paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01+0000\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
assertEquals(instant, paymentResponse.getProcessedOn());
assertEquals(Instant.parse("2021-06-08T12:25:01Z"), paymentResponse.getProcessedOn());

paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
assertEquals(instant, paymentResponse.getProcessedOn());
assertEquals(Instant.parse("2021-06-08T12:25:01Z"), paymentResponse.getProcessedOn());

// Additional cases for different milliseconds precision
paymentResponse = serializer.fromJson("{\"processed_on\":\"2021-06-08T12:25:01.4698039\"}", PaymentResponse.class);
assertNotNull(paymentResponse);
assertNotNull(paymentResponse.getProcessedOn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.checkout.payments.request.source.apm.RequestMbwaySource;
import com.checkout.payments.request.source.apm.RequestMultiBancoSource;
import com.checkout.payments.request.source.apm.RequestP24Source;
import com.checkout.payments.request.source.apm.RequestPayPalSource;
import com.checkout.payments.request.source.apm.RequestPostFinanceSource;
import com.checkout.payments.request.source.apm.RequestQPaySource;
import com.checkout.payments.request.source.apm.RequestSepaSource;
Expand Down
Loading

0 comments on commit 7064069

Please sign in to comment.