Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/main/java/com/starkbank/PaymentPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, ClassData> subResourceByType = new HashMap<String, ClassData>() {{
Map<String, SubResource.ClassData> subResourceByType = new HashMap<String, SubResource.ClassData>() {{
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<String, Object>() {{
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;
}
}

Expand Down