Skip to content

Commit 17817b7

Browse files
author
Aman Aalam
authored
RC 2.0.0 (#44)
# Fixes # Release Client for MAJOR v2.0.0 update ### Checklist - [x] I acknowledge that all my contributions will be made under the project's license - [x] I have made a material change to the repo (functionality, testing, spelling, grammar) - [x] I have titled the PR appropriately - [x] I have updated my branch with the main branch - [x] I have added tests that prove my fix is effective or that my feature works - [x] I have added the necessary documentation about the functionality in the appropriate .md file - [x] I have added inline documentation to the code I modified If you have questions, create a GitHub Issue in this repository.
1 parent e392438 commit 17817b7

80 files changed

Lines changed: 3322 additions & 1441 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ACCESS_KEY=
2-
SECRET_KEY=
2+
SECRET_KEY=
3+
BASE_URL=

README.md

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
# Trolley Java SDK
22

3-
A native Java SDK for Trolley
3+
Native [Java](https://www.oracle.com/java/index.html) SDK for Trolley
44

55
## Installation
66

7-
#
8-
9-
#### For [Java](https://www.oracle.com/java/index.html)
10-
11-
#
12-
13-
#### To install the reference:
14-
15-
### Maven
7+
#### Maven
168

179
Add this dependency to your project's POM:
1810

1911
```xml
2012
<dependency>
2113
<groupId>com.trolley</groupId>
2214
<artifactId>java-sdk</artifactId>
23-
<version>1.1.3</version>
15+
<version>2.0.0</version>
2416
</dependency>
2517
```
2618

@@ -36,8 +28,12 @@ import com.trolley.Exceptions.*;
3628

3729
public class TrolleyExample {
3830
public static void main(String[] args) {
31+
Configuration config = new Configuration("<YOUR_ACCESS_KEY>","<YOUR_SECRET_KEY>");
32+
33+
// Provide your custom HttpClient
34+
// Configuration config = new Configuration("<YOUR_ACCESS_KEY>","<YOUR_SECRET_KEY>", customHttpClient);
3935

40-
Gateway client = new Gateway(new Configuration("YOUR_PUBLIC_KEY","YOUR_PRIVATE_KEY","production"));
36+
Gateway client = new Gateway(config);
4137

4238
try {
4339
Recipient recipient = client.recipient.find("R-1a2B3c4D5e6F7g8H9i0J1k");
@@ -49,22 +45,31 @@ public class TrolleyExample {
4945
}
5046
```
5147

52-
### Usage
5348

54-
Methods should all have Java Doc comments to help you understand their usage. As mentioned the [full API documentation](http://docs.trolley.com)
55-
is the best source of information about the API.
5649

57-
For more information please read the [Java API docs](https://github.com/PaymentRails/java-sdk/tree/master/docs) is available. The best starting point is:
50+
## Providing custom API base url
51+
52+
If you are running from source and want to provide custom base url for the SDK to use (e.g. using a mock server temporarily), you will have to do the following:
53+
54+
1. Create a file named `.env` in the SDK source's root
55+
2. Add a parameter in the env file:
56+
```java
57+
BASE_URL=https://localhost:3000
58+
```
59+
3. While creating the `Configuration` object, pass a third parameter `"development"` so the constructor can look for the `BASE_URL` in the `.env` file:
60+
61+
```java
62+
Configuration config = new Configuration("ACCESS_KEY","SECRET_KEY", "development")
63+
Gateway client = new Gateway(config);
64+
```
65+
66+
A sample `.env` file is provided in the project rood named `.env.example`.
67+
68+
69+
## Documentation
5870

59-
| Data Type | SDK Documentation |
60-
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------ |
61-
| Batch | [API Docs for Batch](https://github.com/PaymentRails/java-sdk/tree/master/docs/classes/batchgateway.md) |
62-
| Payment | [API Docs for Payment](https://github.com/PaymentRails/java-sdk/tree/master/docs/classes/paymentgateway.md) |
63-
| Recipient | [API Docs for Recipient](https://github.com/PaymentRails/java-sdk/tree/master/docs/classes/recipientgateway.md) |
64-
| Recipient Account | [API Docs for Recipient Account](https://github.com/PaymentRails/java-sdk/tree/master/docs/classes/recipientaccountgateway.md) |
71+
Methods should all have Java Doc comments to help you understand their usage.
6572

66-
### merchantKey
73+
For more code samples, refer to our documentation on [https://docs.trolley.com](https://docs.trolley.com)
6774

68-
- **Type**: Authorization
69-
- **Authorization parts**: Access code, Secret code
70-
- **Location**: HTTP header
75+
Limited [Javadocs](https://github.com/PaymentRails/java-sdk/tree/master/docs) are available too.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.trolley</groupId>
55
<artifactId>java-sdk</artifactId>
6-
<version>1.1.4</version>
6+
<version>2.0.0</version>
77
<description>Java SDK for Trolley API</description>
88
<packaging>jar</packaging>
99
<name>Trolley Java SDK</name>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.trolley;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.trolley.Exceptions.InvalidFieldException;
10+
import com.trolley.types.Balances;
11+
12+
public class BalancesGateway
13+
{
14+
Client client;
15+
16+
public BalancesGateway(final Configuration config) {
17+
this.client = new Client(config);
18+
}
19+
20+
/**
21+
* Get balances of all paypal accounts
22+
* @return List<Balance>
23+
* @throws Exception
24+
*/
25+
public List<Balances> getPaypalAccountBalances() throws Exception{
26+
return fetchBalances("paypal");
27+
}
28+
29+
/**
30+
* Get balances of all paymentrails/Trolley accounts
31+
* @return List<Balance>
32+
* @throws Exception
33+
*/
34+
public List<Balances> getTrolleyAccountBalances() throws Exception{
35+
return fetchBalances("paymentrails");
36+
}
37+
38+
/**
39+
* Get balances of all accounts you hold with Trolley
40+
* @return List<Balance>
41+
* @throws Exception
42+
*/
43+
public List<Balances> getAllBalances() throws Exception{
44+
return fetchBalances("");
45+
}
46+
47+
private List<Balances> fetchBalances(final String accountType) throws Exception {
48+
if (accountType == null) {
49+
throw new InvalidFieldException("Account type cannot be null");
50+
}
51+
final String endPoint = "/v1/balances/" + accountType;
52+
final String response = this.client.get(endPoint);
53+
return balancesListFactory(response);
54+
}
55+
56+
private List<Balances> balancesListFactory(final String data) throws Exception{
57+
final ObjectMapper mapper = new ObjectMapper();
58+
final JsonNode node = mapper.readTree(data);
59+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
60+
61+
final List<Balances> balancesFromResponse = (List<Balances>)mapper.readValue(node.get("balances").traverse(), (Class)Object.class);
62+
63+
ArrayList<Balances> balances = new ArrayList<Balances>();
64+
for (int i = 0; i < balancesFromResponse.size(); ++i) {
65+
final Balances pojo = (Balances)mapper.convertValue(balancesFromResponse.get(i), (Class)Balances.class);
66+
balances.add(pojo);
67+
}
68+
69+
return balances;
70+
}
71+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.trolley;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import java.util.List;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.trolley.Exceptions.InvalidFieldException;
10+
import com.trolley.types.Batch;
11+
import com.trolley.types.Payment;
12+
import com.trolley.types.supporting.BatchSummary;
13+
import com.trolley.types.supporting.Batches;
14+
import com.trolley.types.supporting.BatchesIterator;
15+
import com.trolley.types.supporting.Meta;
16+
17+
public class BatchGateway
18+
{
19+
Client client;
20+
21+
public BatchGateway(final Configuration config) {
22+
this.client = new Client(config);
23+
}
24+
25+
public Batch find(final String batchId) throws Exception {
26+
if (batchId == null || batchId.isEmpty()) {
27+
throw new InvalidFieldException("Batch id cannot be null or empty.");
28+
}
29+
final String endPoint = "/v1/batches/" + batchId;
30+
final String response = this.client.get(endPoint);
31+
return this.batchFactory(response);
32+
}
33+
34+
public boolean update(final String batchId, final Batch batch) throws Exception {
35+
if (batchId == null || batchId.isEmpty()) {
36+
throw new InvalidFieldException("Batch id cannot be null or empty.");
37+
}
38+
if (batch == null) {
39+
throw new InvalidFieldException("Batch object cannot be null or empty.");
40+
}
41+
final String jsonBatch = new ObjectMapper().writeValueAsString((Object)batch);
42+
final String endPoint = "/v1/batches/" + batchId;
43+
this.client.patch(endPoint, jsonBatch);
44+
return true;
45+
}
46+
47+
public boolean delete(final String batchId) throws Exception {
48+
if (batchId == null || batchId.isEmpty()) {
49+
throw new InvalidFieldException("Batch id cannot be null or empty.");
50+
}
51+
final String endPoint = "/v1/batches/" + batchId;
52+
this.client.delete(endPoint);
53+
return true;
54+
}
55+
56+
/**
57+
* Delete multiple batches.
58+
* <p>You should pass a {@code List<Batch>} object to this method, with each item of the list filled with the ID of the batch you want to delete.
59+
* <p>This method will serialize only the IDs.
60+
* @param batches a List<Batch> representing the batches that need to be deleted.
61+
* @return True if delete operation was successful
62+
* @throws Exception Thrown if the delete operation wasn't successful or if any other exception occurs.
63+
*/
64+
public boolean delete(final List<Batch> batches) throws Exception {
65+
if (batches == null || batches.isEmpty()) {
66+
throw new InvalidFieldException("batches cannot be null or empty.");
67+
}
68+
final String endPoint = "/v1/batches";
69+
70+
String body = "{\"ids\" : [";
71+
72+
for (int i = 0; i < batches.size(); i++) {
73+
body+=new ObjectMapper().writeValueAsString(batches.get(i).getId());
74+
if(i<(batches.size()-1)){
75+
body+=",";
76+
}
77+
}
78+
79+
body+="]}";
80+
this.client.delete(endPoint, body);
81+
82+
return true;
83+
}
84+
85+
public Batch create(final Batch batch) throws Exception {
86+
if (batch == null) {
87+
throw new InvalidFieldException("Batch cannot be null.");
88+
}
89+
final String body = new ObjectMapper().writeValueAsString((Object)batch);
90+
final String endPoint = "/v1/batches/";
91+
final String response = this.client.post(endPoint, body);
92+
return this.batchFactory(response);
93+
}
94+
95+
public String generateQuote(final String batchId) throws Exception {
96+
if (batchId == null || batchId.isEmpty()) {
97+
throw new InvalidFieldException("Batch id cannot be null or empty.");
98+
}
99+
final String endPoint = "/v1/batches/" + batchId + "/generate-quote";
100+
final String response = this.client.post(endPoint);
101+
return response;
102+
}
103+
104+
public String processBatch(final String batchId) throws Exception {
105+
if (batchId == null || batchId.isEmpty()) {
106+
throw new InvalidFieldException("Batch id cannot be null or empty.");
107+
}
108+
final String endPoint = "/v1/batches/" + batchId + "/start-processing";
109+
final String response = this.client.post(endPoint);
110+
return response;
111+
}
112+
113+
/**
114+
* Search for batches.
115+
* This method returns an iterator which auto-paginate with 10 items per page.
116+
* If you want to paginate manually, please use the {@code search(page, pageSize, searchTerm)} method
117+
* @param searchTerm the search keyword to be searched for
118+
* @return BatchIterator which auto paginates through all available payments 10 items per page
119+
* @throws Exception
120+
*/
121+
public BatchesIterator search(final String searchTerm) throws Exception {
122+
if (searchTerm == null) {
123+
throw new InvalidFieldException("searchTerm cannot be null. If you don't wish to provide a searchTerm, pass a blank String.");
124+
}
125+
int pageSize = 10;
126+
Batches b = search(1, pageSize, searchTerm);
127+
return new BatchesIterator(this, b, searchTerm);
128+
}
129+
130+
/**
131+
* Search for Batches with manual pagination.
132+
* @param page which page number you want to access
133+
* @param pageSize number of items you want per page
134+
* @param searchTerm keyword to search for
135+
* @return {@code Batches} object, containing a {@code List<Batch>} object and a {@code Meta} object to access pagination information
136+
* @throws Exception
137+
*/
138+
public Batches search(final int page, final int pageSize, final String searchTerm) throws Exception {
139+
if (page < 0) {
140+
throw new InvalidFieldException("page cannot be less than 0");
141+
}
142+
if (pageSize < 0) {
143+
throw new InvalidFieldException("pageSize cannot be less than 0");
144+
}
145+
if (searchTerm == null) {
146+
throw new InvalidFieldException("searchTerm cannot be null. If you don't wish to provide a searchTerm, pass a blank String.");
147+
}
148+
final String endPoint = "/v1/batches/?&search=" + searchTerm + "&page=" + page + "&pageSize=" + pageSize;
149+
final String response = this.client.get(endPoint);
150+
return this.batchListFactory(response);
151+
}
152+
153+
public BatchSummary summary(final String batchId) throws Exception {
154+
if (batchId == null || batchId.isEmpty()) {
155+
throw new InvalidFieldException("Batch id cannot be null os empty");
156+
}
157+
final String endPoint = "/v1/batches/" + batchId + "/summary";
158+
final String response = this.client.get(endPoint);
159+
final ObjectMapper mapper = new ObjectMapper();
160+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
161+
final JsonNode node = mapper.readTree(response);
162+
final BatchSummary batchSummary = (BatchSummary)mapper.readValue(node.get("batchSummary").traverse(), (Class)BatchSummary.class);
163+
return batchSummary;
164+
}
165+
166+
private Batch batchFactory(final String data) throws IOException {
167+
final ObjectMapper mapper = new ObjectMapper();
168+
final JsonNode node = mapper.readTree(data);
169+
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
170+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
171+
final Object payments = mapper.readValue(node.get("batch").get("payments").get("payments").traverse(), (Class)Object.class);
172+
final List<Payment> castPayments = (List<Payment>)payments;
173+
final List<Payment> paymentList = new ArrayList<Payment>();
174+
for (int i = 0; i < castPayments.size(); ++i) {
175+
final Payment payment = (Payment)mapper.convertValue((Object)castPayments.get(i), (Class)Payment.class);
176+
paymentList.add(payment);
177+
}
178+
final Batch batch = (Batch)mapper.readValue(node.get("batch").traverse(), (Class)Batch.class);
179+
batch.setPayments(paymentList);
180+
return batch;
181+
}
182+
183+
private Batches batchListFactory(final String data) throws IOException {
184+
final ObjectMapper mapper = new ObjectMapper();
185+
final JsonNode node = mapper.readTree(data);
186+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
187+
188+
final List<Batch> batchesFromResponse = (List<Batch>)mapper.readValue(node.get("batches").traverse(), (Class)Object.class);
189+
final Meta meta = (Meta)mapper.readValue(node.get("meta").traverse(), (Class)Meta.class);
190+
191+
final List<Batch> batches = new ArrayList<Batch>();
192+
for (int i = 0; i < batchesFromResponse.size(); ++i) {
193+
final Batch pojo = (Batch)mapper.convertValue((Object)batchesFromResponse.get(i), (Class)Batch.class);
194+
batches.add(pojo);
195+
}
196+
197+
return new Batches(batches, meta);
198+
}
199+
}

0 commit comments

Comments
 (0)