Skip to content
Open
Show file tree
Hide file tree
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
88 changes: 21 additions & 67 deletions src/main/java/com/unzer/payment/PaylaterInstallmentPlans.java
Original file line number Diff line number Diff line change
@@ -1,104 +1,58 @@
package com.unzer.payment;

import com.unzer.payment.communication.json.paylater.ApiInstallmentPlan;
import com.unzer.payment.communication.json.paylater.ApiInstallmentPlanRate;
import com.unzer.payment.communication.json.paylater.ApiInstallmentPlans;
import com.unzer.payment.models.paylater.InstallmentPlan;
import com.unzer.payment.models.paylater.InstallmentPlanRate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

public class PaylaterInstallmentPlans {
/**
* @deprecated Use {@link PaylaterPlans} instead. Kept as a backward-compatible alias returned by
* {@link Unzer#fetchPaylaterInstallmentPlans(com.unzer.payment.models.paylater.InstallmentPlansRequest)}.
* The covariant setter overrides and the {@code map} overload are retained to preserve source and
* binary compatibility for existing callers.
*/
@Deprecated
public class PaylaterInstallmentPlans extends PaylaterPlans {
Comment thread
Copilot marked this conversation as resolved.
Resolved

private String id;

private BigDecimal amount;
private Currency currency;
private List<InstallmentPlan> plans = new ArrayList<>();
private BaseTransaction.Status status;

public BigDecimal getAmount() {
return amount;
public String getTypeUrl() {
return "types/paylater-installment/plans";
}

@Override
public PaylaterInstallmentPlans setAmount(BigDecimal amount) {
this.amount = amount;
super.setAmount(amount);
return this;
}

public Currency getCurrency() {
return currency;
}

@Override
public PaylaterInstallmentPlans setCurrency(Currency currency) {
this.currency = currency;
super.setCurrency(currency);
return this;
}

public List<InstallmentPlan> getPlans() {
return plans;
}

@Override
public PaylaterInstallmentPlans setPlans(List<InstallmentPlan> plans) {
this.plans = plans;
super.setPlans(plans);
return this;
}

public String getTypeUrl() {
return "types/paylater-installment/plans";
}

public String getId() {
return id;
}

@Override
public PaylaterInstallmentPlans setId(String id) {
this.id = id;
super.setId(id);
return this;
}

public BaseTransaction.Status getStatus() {
return status;
}

@Override
public PaylaterInstallmentPlans setStatus(BaseTransaction.Status status) {
this.status = status;
super.setStatus(status);
return this;
}

public PaylaterInstallmentPlans map(PaylaterInstallmentPlans installmentPlans,
ApiInstallmentPlans jsonInstallmentPlans) {
if (installmentPlans != null && jsonInstallmentPlans != null) {
installmentPlans.setId((jsonInstallmentPlans).getId())
.setAmount(jsonInstallmentPlans.getAmount())
.setCurrency(jsonInstallmentPlans.getCurrency());

map(installmentPlans.getPlans(), jsonInstallmentPlans.getPlans());
}
super.map(installmentPlans, jsonInstallmentPlans);
return installmentPlans;
}

private void map(List<InstallmentPlan> installmentPlan,
List<ApiInstallmentPlan> jsonInstallmentPlan) {
jsonInstallmentPlan.forEach((plan) -> {
List<InstallmentPlanRate> planRates = new ArrayList<InstallmentPlanRate>();
mapRates(planRates, plan.getInstallmentRates());
installmentPlan.add(new InstallmentPlan().setTotalAmount(plan.getTotalAmount())
.setNumberOfRates(plan.getNumberOfRates())
.setNominalInterestRate(plan.getNominalInterestRate())
.setEffectiveInterestRate(plan.getEffectiveInterestRate()).setInstallmentRates(planRates)
.setSecciUrl(plan.getSecciUrl()));
});
}

private void mapRates(List<InstallmentPlanRate> installmentPlanRates,
List<ApiInstallmentPlanRate> json) {
json.forEach((rate) -> {
installmentPlanRates.add(
new InstallmentPlanRate().setRate(rate.getRate()).setDate(rate.getDate()));
});
}
}
107 changes: 107 additions & 0 deletions src/main/java/com/unzer/payment/PaylaterPlans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.unzer.payment;

import com.unzer.payment.communication.json.paylater.ApiInstallmentPlan;
import com.unzer.payment.communication.json.paylater.ApiInstallmentPlanRate;
import com.unzer.payment.communication.json.paylater.ApiInstallmentPlans;
import com.unzer.payment.models.paylater.InstallmentPlan;
import com.unzer.payment.models.paylater.InstallmentPlanRate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

/**
* Available plans returned by the Paylater {@code /plans} endpoints
* (installment, invoice or direct debit). Each plan carries its SECCI document url.
*/
public class PaylaterPlans {

private String id;

private BigDecimal amount;
private Currency currency;
private List<InstallmentPlan> plans = new ArrayList<>();
private BaseTransaction.Status status;

public BigDecimal getAmount() {
return amount;
}

public PaylaterPlans setAmount(BigDecimal amount) {
this.amount = amount;
return this;
}

public Currency getCurrency() {
return currency;
}

public PaylaterPlans setCurrency(Currency currency) {
this.currency = currency;
return this;
}

public List<InstallmentPlan> getPlans() {
return plans;
}

public PaylaterPlans setPlans(List<InstallmentPlan> plans) {
this.plans = plans;
return this;
}

public String getId() {
return id;
}

public PaylaterPlans setId(String id) {
this.id = id;
return this;
}

public BaseTransaction.Status getStatus() {
return status;
}

public PaylaterPlans setStatus(BaseTransaction.Status status) {
this.status = status;
return this;
}

public PaylaterPlans map(PaylaterPlans paylaterPlans, ApiInstallmentPlans jsonPlans) {
if (paylaterPlans != null && jsonPlans != null) {
paylaterPlans.setId(jsonPlans.getId())
.setAmount(jsonPlans.getAmount())
.setCurrency(jsonPlans.getCurrency());

map(paylaterPlans.getPlans(), jsonPlans.getPlans());
}
return paylaterPlans;
}

private void map(List<InstallmentPlan> plans, List<ApiInstallmentPlan> jsonPlans) {
if (jsonPlans == null) {
return;
}
jsonPlans.forEach((plan) -> {
List<InstallmentPlanRate> planRates = new ArrayList<>();
mapRates(planRates, plan.getInstallmentRates());
plans.add(new InstallmentPlan().setTotalAmount(plan.getTotalAmount())
.setNumberOfRates(plan.getNumberOfRates())
.setNominalInterestRate(plan.getNominalInterestRate())
.setEffectiveInterestRate(plan.getEffectiveInterestRate())
.setInstallmentRates(planRates)
.setSecciUrl(plan.getSecciUrl())
.setMinimumInstallmentFee(plan.getMinimumInstallmentFee()));
});
}

private void mapRates(List<InstallmentPlanRate> planRates, List<ApiInstallmentPlanRate> json) {
if (json == null) {
return;
}
json.forEach((rate) ->
planRates.add(new InstallmentPlanRate().setRate(rate.getRate()).setDate(rate.getDate())));
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/unzer/payment/Unzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.unzer.payment.models.PaylaterInvoiceConfig;
import com.unzer.payment.models.PaylaterInvoiceConfigRequest;
import com.unzer.payment.models.paylater.InstallmentPlansRequest;
import com.unzer.payment.models.paylater.PaylaterPlanType;
import com.unzer.payment.models.paylater.PaylaterPlansRequest;
import com.unzer.payment.paymenttypes.PaylaterInstallment;
import com.unzer.payment.paymenttypes.PaymentType;
import com.unzer.payment.resources.PaypageV2;
Expand Down Expand Up @@ -1579,11 +1581,29 @@ private Recurring getRecurring(String typeId, String customerId, String metadata
return recurring;
}

/**
* Fetch the available Paylater plans (installment, invoice or direct debit) for the given
* request. The SECCI PDF locale follows the configured {@code Locale} via the
* {@code Accept-Language} header.
*
* @param plansRequest Contains information for the requested plans.
* @param type Selects which Paylater plans endpoint to query.
* @return Response contains a list of available plans the customer can choose from.
* @throws HttpCommunicationException generic Payment API communication error
*/
public PaylaterPlans fetchPaylaterPlans(
PaylaterPlansRequest plansRequest, PaylaterPlanType type) throws HttpCommunicationException {
return paymentService.fetchPaylaterPlans(plansRequest, type);
}

/**
* @param installmentPlansRequest Contains information for requested plan.
* @return Response contains a list of available installment plans the customer can choose from.
* @throws HttpCommunicationException generic Payment API communication error
* @deprecated Use {@link #fetchPaylaterPlans(PaylaterPlansRequest, PaylaterPlanType)} with
* {@link PaylaterPlanType#INSTALLMENT} instead.
*/
@Deprecated
public PaylaterInstallmentPlans fetchPaylaterInstallmentPlans(
InstallmentPlansRequest installmentPlansRequest) throws HttpCommunicationException {
return paymentService.fetchPaylaterInstallmentPlans(installmentPlansRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.unzer.payment.Customer;
import com.unzer.payment.Linkpay;
import com.unzer.payment.PaylaterInstallmentPlans;
import com.unzer.payment.PaylaterPlans;
import com.unzer.payment.Payout;
import com.unzer.payment.Paypage;
import com.unzer.payment.Processing;
Expand Down Expand Up @@ -520,9 +521,21 @@ public PaymentType mapToBusinessObject(PaymentType out, ApiIdObject src) {
return out.map(out, src);
}

public <T extends PaylaterPlans> T mapToBusinessObject(T paylaterPlans,
ApiInstallmentPlans jsonPlans) {
Comment thread
Copilot marked this conversation as resolved.
Resolved
paylaterPlans.setStatus(extractStatus(jsonPlans));
paylaterPlans.map(paylaterPlans, jsonPlans);
return paylaterPlans;
}

/**
* @deprecated Use {@link #mapToBusinessObject(PaylaterPlans, ApiInstallmentPlans)} instead.
* Kept as a binary-compatible overload for callers of the previous installment-specific method.
*/
@Deprecated
public PaylaterInstallmentPlans mapToBusinessObject(PaylaterInstallmentPlans installmentPlans,
ApiInstallmentPlans jsonInstallmentPlans) {
installmentPlans.setStatus(extractStatus(jsonInstallmentPlans));
return installmentPlans.map(installmentPlans, jsonInstallmentPlans);
mapToBusinessObject((PaylaterPlans) installmentPlans, jsonInstallmentPlans);
return installmentPlans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ public class InstallmentPlan {
private BigDecimal effectiveInterestRate;
private List<InstallmentPlanRate> installmentRates;
private URL secciUrl;
private BigDecimal minimumInstallmentFee;

/**
* Backward-compatible constructor preserving the original signature from before
* {@code minimumInstallmentFee} was added (leaves that field {@code null}).
*/
public InstallmentPlan(BigDecimal totalAmount, int numberOfRates, BigDecimal nominalInterestRate,
BigDecimal effectiveInterestRate, List<InstallmentPlanRate> installmentRates,
URL secciUrl) {
this.totalAmount = totalAmount;
this.numberOfRates = numberOfRates;
this.nominalInterestRate = nominalInterestRate;
this.effectiveInterestRate = effectiveInterestRate;
this.installmentRates = installmentRates;
this.secciUrl = secciUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.unzer.payment.models.paylater;

import com.unzer.payment.Resource;
import com.unzer.payment.models.CustomerType;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;

@AllArgsConstructor
public class InstallmentPlansRequest implements Resource {
private final BigDecimal amount;
private final String currency;
private final String country;
private final CustomerType customerType;
/**
* @deprecated Use {@link PaylaterPlansRequest} together with
* {@link PaylaterPlanType#INSTALLMENT} instead. Kept for backward compatibility; it defaults
* to the installment plans endpoint.
*/
@Deprecated
public class InstallmentPlansRequest extends PaylaterPlansRequest {
public InstallmentPlansRequest(BigDecimal amount, String currency, String country,
CustomerType customerType) {
super(amount, currency, country, customerType);
}

@Override
public String getId() {
Expand All @@ -20,22 +23,6 @@ public String getId() {

@Override
public String getUrl() {
StringBuilder base = new StringBuilder("/v1/types/paylater-installment/plans?amount=");
base.append(amount.toString());

if (currency != null) {
base.append("&currency=");
base.append(currency);
}
if (country != null) {
base.append("&country=");
base.append(country);
}
if (customerType != null) {
base.append("&customerType=");
base.append(customerType);
}

return base.toString();
return getUrl(PaylaterPlanType.INSTALLMENT);
}
}
Loading
Loading