Skip to content
Merged
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
23 changes: 19 additions & 4 deletions src/main/java/com/unzer/payment/Unzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
import com.unzer.payment.marketplace.MarketplaceCancel;
import com.unzer.payment.marketplace.MarketplaceCharge;
import com.unzer.payment.marketplace.MarketplacePayment;
import com.unzer.payment.models.*;
import com.unzer.payment.models.AdditionalTransactionData;
import com.unzer.payment.models.CardTransactionData;
import com.unzer.payment.models.CustomerType;
import com.unzer.payment.models.PaylaterInvoiceConfig;
import com.unzer.payment.models.PaylaterInvoiceConfigRequest;
import com.unzer.payment.models.paylater.InstallmentPlansRequest;
import com.unzer.payment.paymenttypes.PaylaterInstallment;
import com.unzer.payment.paymenttypes.PaymentType;
import com.unzer.payment.resources.PaypageV2;
import com.unzer.payment.service.*;
import com.unzer.payment.service.LinkpayService;
import com.unzer.payment.service.PaymentService;
import com.unzer.payment.service.PaypageService;
import com.unzer.payment.service.TokenService;
import com.unzer.payment.service.WebhookService;
import com.unzer.payment.service.marketplace.MarketplacePaymentService;
import com.unzer.payment.util.JwtHelper;
import com.unzer.payment.webhook.Webhook;
Expand All @@ -23,7 +31,11 @@

import java.math.BigDecimal;
import java.net.URL;
import java.util.*;
import java.util.Currency;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
* {@code Unzer} is a facade to the Unzer REST Api. The facade is
Expand Down Expand Up @@ -368,7 +380,10 @@ public Authorization authorize(BigDecimal amount, Currency currency, PaymentType
URL returnUrl,
Customer customer, Boolean card3ds)
throws HttpCommunicationException {
return authorize(amount, currency, createPaymentType(paymentType).getId(), returnUrl,
if (paymentType.getId() == null) {
paymentType = createPaymentType(paymentType);
}
return authorize(amount, currency, paymentType.getId(), returnUrl,
getCustomerId(createCustomerIfPresent(customer)), card3ds);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.unzer.payment.communication;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
* Gson converter for dates in format yyyyMMdd.
*/
public class RiskRegistrationDateConverter implements JsonDeserializer<Date>, JsonSerializer<Date> {

public static final String DATE_FORMAT = "yyyyMMdd";

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
String value = json.getAsString();
if (value == null || value.isEmpty()) {
return null;
}
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setLenient(false);
// Interpret as UTC start of day when only date is given
df.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
return df.parse(value);
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date in format '" + DATE_FORMAT + "': " + value, e);
}
}

@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
df.setLenient(false);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return new JsonPrimitive(df.format(src));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;

import java.util.Objects;

@Data
@JsonTypeName("additionalTransactionDataModel")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class AdditionalTransactionData {
private CardTransactionData card;
private ShippingTransactionData shipping;
private RiskData riskData;
private PaypalData paypal;
private WeroTransactionData wero;

/**
* URL to the merchant's Terms and Conditions Page
Expand All @@ -23,32 +26,7 @@ public class AdditionalTransactionData {
*/
private String privacyPolicyUrl;

public CardTransactionData getCard() {
return card;
}

public AdditionalTransactionData setCard(CardTransactionData card) {
this.card = card;
return this;
}

public ShippingTransactionData getShipping() {
return shipping;
}

public AdditionalTransactionData setShipping(ShippingTransactionData shipping) {
this.shipping = shipping;
return this;
}

public RiskData getRiskData() {
return riskData;
}

public AdditionalTransactionData setRiskData(RiskData riskData) {
this.riskData = riskData;
return this;
}

/**
* URL to the merchant's Terms and Conditions Page
Expand Down Expand Up @@ -80,15 +58,6 @@ public AdditionalTransactionData setPrivacyPolicyUrl(String privacyPolicyUrl) {
return this;
}

public PaypalData getPaypal() {
return paypal;
}

public AdditionalTransactionData setPaypal(PaypalData paypal) {
this.paypal = paypal;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -112,6 +81,9 @@ public boolean equals(Object o) {
if (!Objects.equals(paypal, that.paypal)) {
return false;
}
if (!Objects.equals(wero, that.wero)) {
return false;
}
if (!Objects.equals(termsAndConditionUrl, that.termsAndConditionUrl)) {
return false;
}
Expand All @@ -124,6 +96,7 @@ public int hashCode() {
result = 31 * result + (shipping != null ? shipping.hashCode() : 0);
result = 31 * result + (riskData != null ? riskData.hashCode() : 0);
result = 31 * result + (paypal != null ? paypal.hashCode() : 0);
result = 31 * result + (wero != null ? wero.hashCode() : 0);
result = 31 * result + (termsAndConditionUrl != null ? termsAndConditionUrl.hashCode() : 0);
result = 31 * result + (privacyPolicyUrl != null ? privacyPolicyUrl.hashCode() : 0);
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.unzer.payment.models;

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

/**
* Event dependent payment configuration for Wero payments.
*/
@Data
public class WeroEventDependentPayment {
private CaptureTrigger captureTrigger;
private AmountPaymentType amountPaymentType;
/**
* Maximum time from authorization to capture in seconds.
*/
private Integer maxAuthToCaptureTime;
/**
* Whether multiple captures are allowed.
*/
private Boolean multiCapturesAllowed;

public enum CaptureTrigger {
@SerializedName("shipping")
SHIPPING,
@SerializedName("delivery")
DELIVERY,
@SerializedName("availability")
AVAILABILITY,
@SerializedName("servicefulfilment")
SERVICEFULFILMENT,
@SerializedName("other")
OTHER
}

public enum AmountPaymentType {
@SerializedName("pay")
PAY,
@SerializedName("payupto")
PAYUPTO
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/unzer/payment/models/WeroTransactionData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.unzer.payment.models;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;

/**
* Additional transaction data for Wero payments.
* Wrapped under the key "wero" inside additionalTransactionData.
*/
@Data
@JsonTypeName("wero")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class WeroTransactionData {
private WeroEventDependentPayment eventDependentPayment;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unzer.payment.models.paypage;

import com.unzer.payment.models.WeroEventDependentPayment;
import lombok.Data;

@Data
Expand All @@ -13,6 +14,8 @@ public class PaymentMethodConfig {
private Boolean credentialOnFile = null; // card only.
private String exemption; // card only.

private WeroEventDependentPayment eventDependentPayment; // wero only

public PaymentMethodConfig(boolean enabled, Integer order) {
this.enabled = enabled;
this.order = order;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/unzer/payment/models/paypage/Risk.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.unzer.payment.models.paypage;

import com.google.gson.annotations.JsonAdapter;
import com.unzer.payment.communication.RiskRegistrationDateConverter;
import lombok.Data;

import java.util.Date;

@Data
public class Risk {
private Integer registrationLevel;
@JsonAdapter(value = RiskRegistrationDateConverter.class)
private Date registrationDate;
private String customerGroup;
private Integer confirmedOrders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public enum PaymentTypeEnum {
PAYLATER_DIRECT_DEBIT("pdd"),
TWINT("twt"),
OPEN_BANKING("obp"),
WERO("wro"),
UNKNOWN("unknown");

private final String shortName;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/unzer/payment/paymenttypes/Wero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.unzer.payment.paymenttypes;

import com.unzer.payment.GeoLocation;
import com.unzer.payment.communication.json.ApiIdObject;
import com.unzer.payment.communication.json.ApiObject;

public class Wero extends BasePaymentType {

@Override
public String getResourceUrl() {
return "/v1/types/wero/<resourceId>";
}

@Override
public PaymentType map(PaymentType wero, ApiObject jsonId) {
((Wero) wero).setId(jsonId.getId());
((Wero) wero).setRecurring(((ApiIdObject) jsonId).getRecurring());
GeoLocation tempGeoLocation =
new GeoLocation(((ApiIdObject) jsonId).getGeoLocation().getClientIp(),
((ApiIdObject) jsonId).getGeoLocation().getCountryIsoA2());
((Wero) wero).setGeoLocation(tempGeoLocation);
return wero;
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/unzer/payment/resources/PaypageV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import com.unzer.payment.BaseResource;
import com.unzer.payment.communication.JsonDateTimeIso8601Converter;
import com.unzer.payment.communication.JsonFieldIgnore;
import com.unzer.payment.models.paypage.*;
import com.unzer.payment.models.paypage.AmountSettings;
import com.unzer.payment.models.paypage.PaymentMethodConfig;
import com.unzer.payment.models.paypage.PaypagePayment;
import com.unzer.payment.models.paypage.Resources;
import com.unzer.payment.models.paypage.Risk;
import com.unzer.payment.models.paypage.Style;
import com.unzer.payment.models.paypage.Urls;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -122,7 +128,8 @@ public enum MethodName {
BANCONTACT("bancontact"),
PFCARD("pfcard"),
PFEFINANCE("pfefinance"),
TWINT("twint");
TWINT("twint"),
WERO("wero");

private final String name;

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/unzer/payment/service/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import com.unzer.payment.paymenttypes.Sofort;
import com.unzer.payment.paymenttypes.Twint;
import com.unzer.payment.paymenttypes.Wechatpay;
import com.unzer.payment.paymenttypes.Wero;

import java.math.BigDecimal;
import java.net.URL;
Expand Down Expand Up @@ -254,6 +255,8 @@ private PaymentType getPaymentTypeFromTypeId(String typeId) {
return new Twint();
case OPEN_BANKING:
return new OpenBanking();
case WERO:
return new Wero();
default:
throw new PaymentException("Type '" + typeId + "' is currently not supported by the SDK");
}
Expand Down Expand Up @@ -281,6 +284,7 @@ private ApiIdObject getJsonObjectFromTypeId(String typeId) {
case KLARNA:
case CLICK_TO_PAY:
case TWINT:
case WERO:
return new ApiIdObject();
case PAYPAL:
return new ApiPaypal();
Expand Down Expand Up @@ -953,7 +957,7 @@ protected enum TransactionType {
AUTHORIZE, PREAUTHORIZE, CHARGE, CHARGEBACK, PAYOUT, CANCEL_AUTHORIZE, CANCEL_CHARGE;

public String apiName() {
return this.name().toLowerCase().replaceAll("_", "-");
return this.name().toLowerCase().replace("_", "-");
}
}
}
Loading
Loading