Skip to content

Commit

Permalink
Update Klarna, Payment properties and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed Sep 2, 2024
1 parent a98e2bb commit 46795ea
Show file tree
Hide file tree
Showing 19 changed files with 577 additions and 24 deletions.
32 changes: 32 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,9 @@
import com.checkout.issuing.controls.responses.create.VelocityCardControlResponse;
import com.checkout.payments.PaymentDestinationType;
import com.checkout.payments.previous.PaymentAction;
import com.checkout.payments.AbstractPaymentPlanType;
import com.checkout.payments.InstallmentPlan;
import com.checkout.payments.RecurringPlan;
import com.checkout.payments.sender.Sender;
import com.checkout.payments.sender.SenderType;
import com.checkout.webhooks.previous.WebhookResponse;
Expand All @@ -32,6 +35,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 +45,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 @@ -94,6 +99,8 @@ public class GsonSerializer implements Serializer {
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(com.checkout.payments.response.source.ResponseSource.class, CheckoutUtils.TYPE, true, com.checkout.payments.response.source.AlternativePaymentSourceResponse.class)
.registerSubtype(com.checkout.payments.response.source.CardResponseSource.class, identifier(PaymentSourceType.CARD))
.registerSubtype(com.checkout.payments.response.source.CurrencyAccountResponseSource.class, identifier(PaymentSourceType.CURRENCY_ACCOUNT)))
// Payments - Payment Plan
.registerTypeAdapter(AbstractPaymentPlanType.class, getPaymentPlanResponseDeserializer())
// Payment Contexts
.registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(com.checkout.payments.response.source.contexts.ResponseSource.class, CheckoutUtils.TYPE, true, com.checkout.payments.response.source.contexts.AlternativePaymentSourceResponse.class)
.registerSubtype(com.checkout.payments.response.source.contexts.PaymentContextsPayPalResponseSource.class, identifier(PaymentSourceType.PAYPAL))
Expand Down Expand Up @@ -268,12 +275,37 @@ private static JsonDeserializer<GetScheduleResponse> getScheduleResponseDeserial
};
}

private static JsonDeserializer<AbstractPaymentPlanType> getPaymentPlanResponseDeserializer(){
return (json, typeOfT, context) -> {
JsonObject jsonObject = json.getAsJsonObject();

if (jsonObject.has("amount_variability")) {
return context.deserialize(json, RecurringPlan.class);
}

if (jsonObject.has("financing") && jsonObject.has("amount")) {
return context.deserialize(json, InstallmentPlan.class);
}

throw new JsonParseException("Payment plan type could not be determined");
};
}

private static JsonDeserializer<Instant> getInstantJsonDeserializer() {
return (json, typeOfT, context) -> {
final String dateString = json.getAsString();
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
34 changes: 34 additions & 0 deletions src/main/java/com/checkout/payments/AbstractPaymentPlanType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.checkout.payments;

import com.google.gson.annotations.SerializedName;
import lombok.Data;

import java.time.Instant;

@Data
public abstract class AbstractPaymentPlanType {

@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;

protected AbstractPaymentPlanType(
final Integer daysBetweenPayments,
final Integer totalNumberOfPayments,
final Integer currentPaymentNumber,
final Instant expiry) {
this.daysBetweenPayments = daysBetweenPayments;
this.totalNumberOfPayments = totalNumberOfPayments;
this.currentPaymentNumber = currentPaymentNumber;
this.expiry = expiry;
}

}
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

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

import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.Instant;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class InstallmentPlan extends AbstractPaymentPlanType {

@SerializedName("financing")
private Boolean financing;

@SerializedName("amount")
private String amount;

@Builder
private InstallmentPlan(
final Boolean financing,
final String amount,
final Integer daysBetweenPayments,
final Integer totalNumberOfPayments,
final Integer currentPaymentNumber,
final Instant expiry) {
super(daysBetweenPayments, totalNumberOfPayments, currentPaymentNumber, expiry);
this.financing = financing;
this.amount = amount;
}

}
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

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

import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.Instant;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class RecurringPlan extends AbstractPaymentPlanType {

@SerializedName("amount_variability")
private AmountVariability amountVariability;

@Builder
private RecurringPlan(
final AmountVariability amountVariability,
final Integer daysBetweenPayments,
final Integer totalNumberOfPayments,
final Integer currentPaymentNumber,
final Instant expiry) {
super(daysBetweenPayments, totalNumberOfPayments, currentPaymentNumber, expiry);
this.amountVariability = amountVariability;
}

}
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.AbstractPaymentPlanType;
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 AbstractPaymentPlanType 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.AbstractPaymentPlanType;
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 AbstractPaymentPlanType 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
Loading

0 comments on commit 46795ea

Please sign in to comment.