Skip to content

Commit

Permalink
#19: Add concept of customers and recurring payments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mats Stijlaart committed Oct 27, 2016
1 parent d0d530e commit fda5107
Show file tree
Hide file tree
Showing 16 changed files with 611 additions and 22 deletions.
15 changes: 14 additions & 1 deletion src/main/java/nl/stil4m/mollie/Client.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package nl.stil4m.mollie;

import nl.stil4m.mollie.concepts.*;
import nl.stil4m.mollie.concepts.CustomerPayments;
import nl.stil4m.mollie.concepts.Customers;
import nl.stil4m.mollie.concepts.Issuers;
import nl.stil4m.mollie.concepts.Methods;
import nl.stil4m.mollie.concepts.Payments;
import nl.stil4m.mollie.concepts.Refunds;
import nl.stil4m.mollie.concepts.Status;

public class Client {

Expand Down Expand Up @@ -33,4 +39,11 @@ public Refunds refunds() {
return dynamicClient.refunds(apiKey);
}

public Customers customers() {
return dynamicClient.customers(apiKey);
}

public CustomerPayments customerPayments(String customerId) {
return dynamicClient.customerPayments(apiKey, customerId);
}
}
15 changes: 14 additions & 1 deletion src/main/java/nl/stil4m/mollie/DynamicClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package nl.stil4m.mollie;

import nl.stil4m.mollie.concepts.*;
import nl.stil4m.mollie.concepts.CustomerPayments;
import nl.stil4m.mollie.concepts.Customers;
import nl.stil4m.mollie.concepts.Issuers;
import nl.stil4m.mollie.concepts.Methods;
import nl.stil4m.mollie.concepts.Payments;
import nl.stil4m.mollie.concepts.Refunds;
import nl.stil4m.mollie.concepts.Status;

public class DynamicClient {

Expand Down Expand Up @@ -32,4 +38,11 @@ public Refunds refunds(String apiKey) {
return new Refunds(apiKey, endpoint, requestExecutor);
}

public Customers customers(String apiKey) {
return new Customers(apiKey, endpoint, requestExecutor);
}

public CustomerPayments customerPayments(String apiKey, String customerId) {
return new CustomerPayments(apiKey, endpoint, requestExecutor, customerId);
}
}
55 changes: 55 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package nl.stil4m.mollie.concepts;

import com.fasterxml.jackson.core.type.TypeReference;
import nl.stil4m.mollie.RequestExecutor;
import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.CustomerPayment;
import nl.stil4m.mollie.domain.Page;
import nl.stil4m.mollie.domain.Payment;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

public class CustomerPayments {

private static final TypeReference<Page<Payment>> PAGE_PAYMENT_TYPE = new TypeReference<Page<Payment>>() {
};

private static final TypeReference<Payment> PAYMENT_TYPE = new TypeReference<Payment>() {
};


private final String apiKey;
private final String endpoint;
private final RequestExecutor requestExecutor;
private final String customerId;

public CustomerPayments(String apiKey, String endpoint, RequestExecutor requestExecutor, String customerId) {
this.apiKey = apiKey;
this.endpoint = endpoint;
this.requestExecutor = requestExecutor;
this.customerId = customerId;
}


public ResponseOrError<Page<Payment>> all(Optional<Object> count, Optional<Object> offset) throws URISyntaxException, IOException {
URIBuilder builder = new URIBuilder(endpoint + "/customers/" + customerId + "/payments")
.setParameter("count", String.valueOf(count.orElse(10)))
.setParameter("offset", String.valueOf(offset.orElse(0)));

HttpGet httpGet = new HttpGet(builder.build());
return requestExecutor.execute(apiKey, httpGet, PAGE_PAYMENT_TYPE);
}

public ResponseOrError<Payment> create(CustomerPayment customerPayment) throws IOException {
HttpPost httpPost = new HttpPost(endpoint + "/customers/" + customerId + "/payments");
httpPost.setEntity(new StringEntity(requestExecutor.serialize(customerPayment), ContentType.APPLICATION_JSON));
return requestExecutor.execute(apiKey, httpPost, PAYMENT_TYPE);
}
}
71 changes: 71 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/Customers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package nl.stil4m.mollie.concepts;

import com.fasterxml.jackson.core.type.TypeReference;
import nl.stil4m.mollie.RequestExecutor;
import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.CreateCustomer;
import nl.stil4m.mollie.domain.Customer;
import nl.stil4m.mollie.domain.Page;
import nl.stil4m.mollie.domain.UpdateCustomer;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

import static nl.stil4m.mollie.Util.validatePaymentId;

public class Customers {


private static final TypeReference<Page<Customer>> PAGE_CUSTOMER_TYPE = new TypeReference<Page<Customer>>() {
};
private static final TypeReference<Customer> CUSTOMER_TYPE = new TypeReference<Customer>() {
};

private final String apiKey;
private final String endpoint;
private final RequestExecutor requestExecutor;

public Customers(String apiKey, String endpoint, RequestExecutor requestExecutor) {
this.apiKey = apiKey;
this.endpoint = endpoint;
this.requestExecutor = requestExecutor;
}

public ResponseOrError<Customer> create(CreateCustomer createCustomer) throws IOException {
HttpPost httpPost = new HttpPost(endpoint + "/customers");
httpPost.setEntity(new StringEntity(requestExecutor.serialize(createCustomer), ContentType.APPLICATION_JSON));
return requestExecutor.execute(apiKey, httpPost, new TypeReference<Customer>() {
});
}

public ResponseOrError<Customer> update(String customerId, UpdateCustomer updateCustomer) throws IOException {
HttpPut httpPut = new HttpPut(endpoint + "/customers/" + customerId);
System.out.println(requestExecutor.serialize(updateCustomer));
httpPut.setEntity(new StringEntity(requestExecutor.serialize(updateCustomer), ContentType.APPLICATION_JSON));
return requestExecutor.execute(apiKey, httpPut, new TypeReference<Customer>() {
});
}

public ResponseOrError<Page<Customer>> all(Optional<Integer> count, Optional<Integer> offset) throws IOException, URISyntaxException {
URIBuilder builder = new URIBuilder(endpoint + "/customers")
.setParameter("count", String.valueOf(count.orElse(10)))
.setParameter("offset", String.valueOf(offset.orElse(0)));

HttpGet httpGet = new HttpGet(builder.build());
return requestExecutor.execute(apiKey, httpGet, PAGE_CUSTOMER_TYPE);
}

public ResponseOrError<Customer> get(String id) throws IOException {
validatePaymentId(id);
HttpGet httpGet = new HttpGet(endpoint + "/customers/" + id);
return requestExecutor.execute(apiKey, httpGet, CUSTOMER_TYPE);
}

}
42 changes: 42 additions & 0 deletions src/main/java/nl/stil4m/mollie/domain/CreateCustomer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nl.stil4m.mollie.domain;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class CreateCustomer {

private final String name;
private final String email;
private final Optional<String> locale;
private final Map<String,Object> metadata;

public CreateCustomer(@JsonProperty("name") String name,
@JsonProperty("email") String email,
@JsonProperty("locale") Optional<String> locale,
@JsonProperty("metadata") Map<String, Object> metadata) {
this.name = name;
this.email = email;
this.locale = locale;
this.metadata = metadata;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public Optional<String> getLocale() {
return locale;
}

public Map<String, Object> getMetadata() {
return metadata;
}
}
79 changes: 79 additions & 0 deletions src/main/java/nl/stil4m/mollie/domain/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package nl.stil4m.mollie.domain;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Customer {

private final String resource;
private final String id;
private final String mode;
private final String name;
private final String email;
private final Optional<String> locale;
private final Map<String,Object> metadata;
private final List<String> recentlyUsedMethods;
private final Date createdDatetime;

public Customer(@JsonProperty("resource") String resource,
@JsonProperty("id") String id,
@JsonProperty("mode") String mode,
@JsonProperty("name") String name,
@JsonProperty("email") String email,
@JsonProperty("locale") Optional<String> locale,
@JsonProperty("metaData") Map<String, Object> metadata,
@JsonProperty("recentlyUsedMethods") List<String> recentlyUsedMethods,
@JsonProperty("createdDatetime") Date createdDatetime) {
this.resource = resource;
this.id = id;
this.mode = mode;
this.name= name;
this.email = email;
this.locale = locale;
this.metadata = metadata;
this.recentlyUsedMethods = recentlyUsedMethods;
this.createdDatetime = createdDatetime;
}

public String getResource() {
return resource;
}

public String getId() {
return id;
}

public String getMode() {
return mode;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public Optional<String> getLocale() {
return locale;
}

public Map<String, Object> getMetadata() {
return metadata;
}

public List<String> getRecentlyUsedMethods() {
return recentlyUsedMethods;
}

public Date getCreatedDatetime() {
return createdDatetime;
}


}
23 changes: 23 additions & 0 deletions src/main/java/nl/stil4m/mollie/domain/CustomerPayment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nl.stil4m.mollie.domain;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

public abstract class CustomerPayment {

@JsonUnwrapped
private final CreatePayment createPayment;
private final String recurringType;

public CustomerPayment(CreatePayment createPayment, String recurringType) {
this.createPayment = createPayment;
this.recurringType = recurringType;
}

public CreatePayment getCreatePayment() {
return createPayment;
}

public String getRecurringType() {
return recurringType;
}
}
Loading

0 comments on commit fda5107

Please sign in to comment.