-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#19: Add concept of customers and recurring payments
- Loading branch information
Mats Stijlaart
committed
Oct 27, 2016
1 parent
d0d530e
commit fda5107
Showing
16 changed files
with
611 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/main/java/nl/stil4m/mollie/domain/CustomerPayment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.