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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

public abstract class BasePaymentType extends BaseResource implements PaymentType {
private String id;
/**
* Indicates whether the payment type is registered for recurring collection.
*
* <p>For most payment types this is a read-only value returned by the API.
* {@code sepa-direct-debit} is the type where it is also accepted on creation
* (see {@link SepaDirectDebit#recurring(boolean)}).</p>
*/
private Boolean recurring;
private GeoLocation geoLocation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import java.util.Currency;

/**
* Sepa direct debit business object. Iban is mandatory, bic and holder are optional fields
* Sepa direct debit business object. Iban is mandatory, bic and holder are optional fields.
*
* <p>For {@code sepa-direct-debit} the recurrence intent is captured once, at type registration,
* via the {@code recurring} flag (see {@link #recurring(boolean)}). Unlike most payment types,
* where {@code recurring} is a read-only value returned by the API, this type also accepts the
* flag when the type is created.</p>
*
* @author Unzer E-Com GmbH
*/
Expand All @@ -32,6 +37,27 @@ public SepaDirectDebit(String iban) {
this.iban = iban;
}

/**
* Registers this {@code sepa-direct-debit} type for recurring (mandate-based) collection.
*
* <p>This flag must be set <b>before</b> the type is registered via
* {@link com.unzer.payment.Unzer#createPaymentType(PaymentType)}; it cannot be changed
* afterwards. It is not needed for one-off payments.</p>
*
* <p>This is unrelated to {@code recurrenceType} on charges/authorizations, which is
* card-specific.</p>
*
* <p>The fluent return value is a convenience over the
* {@link #setRecurring(Boolean)} setter, which keeps working.</p>
*
* @param recurring {@code true} to register the type for recurring collection
* @return this instance, for chaining
*/
public SepaDirectDebit recurring(boolean recurring) {
setRecurring(recurring);
return this;
}

@Override
public String getResourceUrl() {
return "/v1/types/sepa-direct-debit/<resourceId>";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.unzer.payment.business.paymenttypes;

import com.unzer.payment.communication.JsonParser;
import com.unzer.payment.communication.json.ApiSepaDirectDebit;
import com.unzer.payment.paymenttypes.SepaDirectDebit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Objects;
import java.util.Scanner;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SepaDirectDebitTest {

private static final String IBAN = "DE89370400440532013000";

private static String compactJson(SepaDirectDebit sdd) {
return new JsonParser().toJson(sdd).replaceAll("\\s", "");
}

private static String getResponse(String response) {
return new Scanner(Objects.requireNonNull(
SepaDirectDebitTest.class.getResourceAsStream("/api-response/sepa-direct-debit/" + response)))
.useDelimiter("\\A").next();
}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void recurring_flag_is_serialized_when_set(boolean recurring) {
SepaDirectDebit sdd = new SepaDirectDebit(IBAN).recurring(recurring);

assertTrue(compactJson(sdd).contains("\"recurring\":" + recurring));
}

@Test
void recurring_fluent_method_returns_same_instance_and_sets_lombok_value() {
SepaDirectDebit sdd = new SepaDirectDebit(IBAN);

assertEquals(sdd, sdd.recurring(true));
assertEquals(Boolean.TRUE, sdd.getRecurring());

sdd.setRecurring(false);
assertTrue(compactJson(sdd).contains("\"recurring\":false"));
}

@Test
void recurring_key_absent_when_not_set() {
SepaDirectDebit sdd = new SepaDirectDebit(IBAN);

assertFalse(compactJson(sdd).contains("recurring"));
}

@Test
void recurring_is_mapped_from_response() {
ApiSepaDirectDebit apiObject =
new JsonParser().fromJson(getResponse("type.json"), ApiSepaDirectDebit.class);

SepaDirectDebit sdd = new SepaDirectDebit(IBAN);
sdd.map(sdd, apiObject);

assertEquals(Boolean.TRUE, sdd.getRecurring());
assertEquals("s-sdd-giqxlgfvwjb7", sdd.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ void testCreateSepaDirectDebitFullType() {
assertSddEquals(sddOriginal, sddCreated);
}

@Test
void testCreateRecurringSepaDirectDebitType() {
Unzer unzer = getUnzer(Keys.LEGACY_PRIVATE_KEY);
SepaDirectDebit sdd = unzer.createPaymentType(getSepaDirectDebit().recurring(true));
assertNotNull(sdd.getId());

SepaDirectDebit fetchedSdd = (SepaDirectDebit) unzer.fetchPaymentType(sdd.getId());
assertEquals(Boolean.TRUE, fetchedSdd.getRecurring());
}

@Test
void testChargeSepaDirectDebitType() {
Unzer unzer = getUnzer(Keys.LEGACY_PRIVATE_KEY);
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/api-response/sepa-direct-debit/type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "s-sdd-giqxlgfvwjb7",
"method": "sepa-direct-debit",
"recurring": true,
"iban": "DE89370400440532013000",
"bic": "COBADEFFXXX",
"holder": "Max Mustermann",
"geoLocation": {
"clientIp": "0:0:0:0:0:0:0:1",
"countryIsoA2": ""
}
}
Loading