From 0d547d16184a0775ec22a80d539dd93b4984e5dc Mon Sep 17 00:00:00 2001 From: Andre Date: Tue, 21 Mar 2023 10:53:41 -0300 Subject: [PATCH] Update PaymentPreview.java --- .../java/com/starkbank/PaymentPreview.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/starkbank/PaymentPreview.java b/src/main/java/com/starkbank/PaymentPreview.java index d26acaf..4cedb76 100644 --- a/src/main/java/com/starkbank/PaymentPreview.java +++ b/src/main/java/com/starkbank/PaymentPreview.java @@ -2,7 +2,13 @@ import com.starkbank.utils.Resource; import com.starkbank.utils.Rest; +import com.starkbank.utils.SubResource; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -40,19 +46,44 @@ public class PaymentPreview extends Resource { */ public PaymentPreview(String id, String scheduled, String type, String payment) { super(id); - this.scheduled = scheduled; + this.scheduled = formatDate(scheduled); this.type = type; this.payment = payment; - Map subResourceByType = new HashMap() {{ + Map subResourceByType = new HashMap() {{ put("brcode-payment", BrcodePreview.data); put("boleto-payment", BoletoPreview.data); put("utility-payment", UtilityPreview.data); put("tax-payment", TaxPreview.data); }}; - if (subResourceByType.containsKey(payment)) { - this.payment = subResourceByType.get(payment); + if (subResourceByType.containsKey(type)) { + try { + SubResource.ClassData classData = subResourceByType.get(type); + Class clazz = classData.cls; + Constructor ctor = clazz.getDeclaredConstructor(Map.class); + this.payment = ctor.newInstance(new HashMap() {{ + put("id", payment); + put("scheduled", scheduled); + }}); + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + + private String formatDate(String dateStr) { + if (dateStr == null) { + return null; + } + + try { + DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate date = LocalDate.parse(dateStr, inputFormatter); + return date.format(inputFormatter); + } catch (DateTimeParseException e) { + System.out.println("Invalid date format: " + dateStr); + return null; } }