Skip to content

Commit a98d345

Browse files
author
Aman Aalam
authored
RC-2.0.1 Add support for InvoicePaymet API updates (#45)
# Update # Adding support for `InvoicePayment.Create`, `InvoicePayment.Update` API updates ### 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 17817b7 commit a98d345

6 files changed

Lines changed: 220 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Add this dependency to your project's POM:
1212
<dependency>
1313
<groupId>com.trolley</groupId>
1414
<artifactId>java-sdk</artifactId>
15-
<version>2.0.0</version>
15+
<version>2.0.1</version>
1616
</dependency>
1717
```
1818

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>2.0.0</version>
6+
<version>2.0.1</version>
77
<description>Java SDK for Trolley API</description>
88
<packaging>jar</packaging>
99
<name>Trolley Java SDK</name>

src/main/java/com/trolley/InvoicePaymentGateway.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import com.trolley.Exceptions.InvalidFieldException;
1010
import com.trolley.types.InvoicePayment;
11+
import com.trolley.types.supporting.InvoicePaymentRequest;
1112
import com.trolley.types.supporting.InvoicePaymentPart;
1213
import com.trolley.types.supporting.InvoicePayments;
1314
import com.trolley.types.supporting.InvoicePaymentsIterator;
@@ -26,12 +27,14 @@ public InvoicePaymentGateway(final Configuration config) {
2627
* NOTE: If you provide a batch id, this method will try to add the payment to it. If you provide a
2728
* {@code null} or blank ({@code ""}) batch id, a new batch will be created.
2829
*
30+
* @deprecated As of v2.0.1, this method is deprecated to support improvements to Create InvoicePayment API. Use {@link InvoicePaymentGateway#create(InvoicePaymentRequest) create(InvoicePaymentRequest)} instead.
2931
*
3032
* @param batchId (Optional) Id of the batch you want to add these payments too.
3133
* @param payment InvoicePaymentPart object representing the Invoice payment that needs to be created
3234
* @return InvoicePayment object of representing the created payment for the Invoice
3335
* @throws Exception
3436
*/
37+
@Deprecated
3538
public InvoicePayment create(final String batchId, final InvoicePaymentPart payment) throws Exception {
3639
final String endPoint = "/v1/invoices/payment/create/";
3740

@@ -62,11 +65,14 @@ public InvoicePayment create(final String batchId, final InvoicePaymentPart paym
6265
* NOTE: If you provide a batch id, this method will try to add the payment to it. If you provide a
6366
* {@code null} or blank ({@code ""}) batch id, a new batch will be created.
6467
*
68+
* @deprecated As of v2.0.1, this method is deprecated to support improvements to Create InvoicePayment API. Use {@link InvoicePaymentGateway#create(InvoicePaymentRequest) create(InvoicePaymentRequest)} instead.
69+
*
6570
* @param batchId (Optional) Id of the batch you want to add these payments too.
6671
* @param invoicePaymentParts A List of InvoicePaymentPart objects representing the Invoice payments that need to be created.
6772
* @return InvoicePayment object of representing the created payment for the Invoice
6873
* @throws Exception
6974
*/
75+
@Deprecated
7076
public InvoicePayment create(final String batchId, final List<InvoicePaymentPart> invoicePaymentParts) throws Exception {
7177
final String endPoint = "/v1/invoices/payment/create/";
7278

@@ -86,6 +92,27 @@ public InvoicePayment create(final String batchId, final List<InvoicePaymentPart
8692
return InvoicePayment.invoicePaymentFactory(response);
8793
}
8894

95+
/**
96+
* Create a payment against an invoice, with payment fields.
97+
* <p>
98+
* NOTE: If you provide a batch id, this method will try to add the payment to it. If you provide a
99+
* {@code null} or blank ({@code ""}) batch id, a new batch will be created.
100+
*
101+
* @param invoicePaymentRequest {@code InvoicePaymentRequest} object representing the InvoicePayment request, with payment fields that needs to be added to the created payment.
102+
* @return InvoicePayment object of representing the created payment for the Invoice
103+
* @throws Exception
104+
*/
105+
public InvoicePayment create(final InvoicePaymentRequest invoicePaymentRequest) throws Exception {
106+
final String endPoint = "/v1/invoices/payment/create/";
107+
108+
String body= new ObjectMapper()
109+
.setSerializationInclusion(Include.NON_EMPTY)
110+
.writeValueAsString(invoicePaymentRequest);
111+
112+
final String response = this.client.post(endPoint, body);
113+
return InvoicePayment.invoicePaymentFactory(response);
114+
}
115+
89116
/**
90117
* Update an Invoice Payment whose details are provided in invoicePaymentPart object.
91118
* <p>

src/main/java/com/trolley/types/supporting/InvoicePaymentPart.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.trolley.types.supporting;
22

3+
import java.util.ArrayList;
4+
35
public class InvoicePaymentPart{
46
private String invoiceId;
57
private String invoiceLineId;
68
private String paymentId;
79
private Amount amount;
10+
11+
//These fields are to hold values from parsed JSON, not for request body
12+
private boolean coverFees;
13+
private String memo;
14+
private String externalId;
15+
private String[] tags;
816

917
public InvoicePaymentPart() {
1018
}
@@ -16,6 +24,21 @@ public InvoicePaymentPart(String invoiceId, String invoiceLineId, String payment
1624
this.amount = amount;
1725
}
1826

27+
/**
28+
* IMPORTANT: Use as request only while updating an InvoicePayment. For "Create Invoice Payment" request, use {@link InvoicePaymentRequest#InvoicePaymentRequest(String, boolean, String, String, String[], InvoicePaymentPart) InvoicePaymentRequest}.
29+
*/
30+
public InvoicePaymentPart(String invoiceId, String invoiceLineId, String paymentId, Amount amount,
31+
boolean coverFees, String memo, String externalId, String[] tags) {
32+
this.invoiceId = invoiceId;
33+
this.invoiceLineId = invoiceLineId;
34+
this.paymentId = paymentId;
35+
this.amount = amount;
36+
this.coverFees = coverFees;
37+
this.memo = memo;
38+
this.externalId = externalId;
39+
this.tags = tags;
40+
}
41+
1942
public String getInvoiceId() {
2043
return invoiceId;
2144
}
@@ -46,5 +69,53 @@ public Amount getAmount() {
4669

4770
public void setAmount(Amount amount) {
4871
this.amount = amount;
49-
}
72+
}
73+
74+
public boolean shouldCoverFees() {
75+
return coverFees;
76+
}
77+
78+
/**
79+
* IMPORTANT: Use as request only while updating an InvoicePayment
80+
* @param coverFees
81+
*/
82+
public void setCoverFees(boolean coverFees) {
83+
this.coverFees = coverFees;
84+
}
85+
86+
public String getMemo() {
87+
return memo;
88+
}
89+
90+
/**
91+
* IMPORTANT: Use as request only while updating an InvoicePayment
92+
* @param memo
93+
*/
94+
public void setMemo(String memo) {
95+
this.memo = memo;
96+
}
97+
98+
public String getExternalId() {
99+
return externalId;
100+
}
101+
102+
/**
103+
* IMPORTANT: Use as request only while updating an InvoicePayment
104+
* @param externalId
105+
*/
106+
public void setExternalId(String externalId) {
107+
this.externalId = externalId;
108+
}
109+
110+
public String[] getTags() {
111+
return tags;
112+
}
113+
114+
/**
115+
* IMPORTANT: Use as request only while updating an InvoicePayment
116+
* @param tags
117+
*/
118+
public void setTags(String[] tags) {
119+
this.tags = tags;
120+
}
50121
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.trolley.types.supporting;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Used while creating a new InvoicePayment request
7+
*/
8+
public class InvoicePaymentRequest{
9+
private String batchId;
10+
private boolean coverFees;
11+
private String memo;
12+
private String externalId;
13+
private String[] tags;
14+
private InvoicePaymentPart[] ids;
15+
16+
public InvoicePaymentRequest() {
17+
}
18+
19+
/**
20+
* Constructor to create a new request when you have more than one Invoices or InvoiceLines to create payment for.
21+
* @param batchId ID of the batch you'd want to add this payment to. If it's {@code null} or {@code ""}, a new payment will be created.
22+
* @param coverFees Denotes whether you want to cover fees with this payment.
23+
* @param memo A recipient viewable Memo that you'd want the created payment to have.
24+
* @param externalId A unique External ID that you'd want to assign to the created payment.
25+
* @param tags A {@code String[]} array where each element is a merchant-viewable tag that you want to assign to the created payment.
26+
* @param ids An {@code InvoicePaymentPart[]} array representing InvoicePaymentPart objects containing IDs and Amounts of Invoices and InvoiceLines you want to create payments for.
27+
*/
28+
public InvoicePaymentRequest(String batchId, boolean coverFees, String memo, String externalId,
29+
String[] tags, InvoicePaymentPart[] ids) {
30+
this.batchId = batchId;
31+
this.coverFees = coverFees;
32+
this.memo = memo;
33+
this.externalId = externalId;
34+
this.tags = tags;
35+
this.ids = ids;
36+
}
37+
38+
/**
39+
* Constructor to create a new request when you have only one Invoice/InvoiceLine to create a payment for.
40+
* @param batchId ID of the batch you'd want to add this payment to. If it's {@code null} or {@code ""}, a new payment will be created.
41+
* @param coverFees Denotes whether you want to cover fees with this payment.
42+
* @param memo A recipient viewable Memo that you'd want the created payment to have.
43+
* @param externalId A unique External ID that you'd want to assign to the created payment.
44+
* @param tags A {@code String[]} array where each element is a merchant-viewable tag that you want to assign to the created payment.
45+
* @param ids An {@code InvoicePaymentPart[]} array representing InvoicePaymentPart objects containing IDs and Amounts of Invoices and InvoiceLines you want to create payments for.
46+
*/
47+
public InvoicePaymentRequest(String batchId, boolean coverFees, String memo, String externalId,
48+
String[] tags, InvoicePaymentPart paymentPart) {
49+
this.batchId = batchId;
50+
this.coverFees = coverFees;
51+
this.memo = memo;
52+
this.externalId = externalId;
53+
this.tags = tags;
54+
this.ids = new InvoicePaymentPart[]{paymentPart};
55+
}
56+
57+
public String getBatchId() {
58+
return batchId;
59+
}
60+
61+
public void setBatchId(String batchId) {
62+
this.batchId = batchId;
63+
}
64+
65+
public boolean shouldCoverFees() {
66+
return coverFees;
67+
}
68+
69+
public void setCoverFees(boolean coverFees) {
70+
this.coverFees = coverFees;
71+
}
72+
73+
public String getMemo() {
74+
return memo;
75+
}
76+
77+
public void setMemo(String memo) {
78+
this.memo = memo;
79+
}
80+
81+
public String getExternalId() {
82+
return externalId;
83+
}
84+
85+
public void setExternalId(String externalId) {
86+
this.externalId = externalId;
87+
}
88+
89+
public String[] getTags() {
90+
return tags;
91+
}
92+
93+
public void setTags(String[] tags) {
94+
this.tags = tags;
95+
}
96+
97+
public InvoicePaymentPart[] getIds() {
98+
return ids;
99+
}
100+
101+
public void setIds(InvoicePaymentPart[] ids) {
102+
this.ids = ids;
103+
}
104+
}

src/test/java/com/trolley/integration/InvoiceTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.trolley.types.Invoices;
1717
import com.trolley.types.Recipient;
1818
import com.trolley.types.supporting.Amount;
19+
import com.trolley.types.supporting.InvoicePaymentRequest;
1920
import com.trolley.types.supporting.InvoicePaymentPart;
2021
import com.trolley.types.supporting.InvoicePaymentsIterator;
2122
import com.trolley.types.supporting.InvoicesIterator;
@@ -247,6 +248,7 @@ public void testInvoicePayments() throws Exception {
247248
null,
248249
null,
249250
invoiceLines));
251+
250252
assertEquals(invoice.getRecipientId(), recipient.getId());
251253

252254
//Create a new Invoice Payment
@@ -255,7 +257,17 @@ public void testInvoicePayments() throws Exception {
255257
paymentPart.setInvoiceLineId(invoice.getLines().get(0).getId());
256258
paymentPart.setAmount(new Amount("50", "USD"));
257259

258-
InvoicePayment invoicePayment = client.invoicePayment.create(null, paymentPart);
260+
String[] tags = {"tag1", "tag2"};
261+
262+
InvoicePaymentRequest invoicePaymentRequest = new InvoicePaymentRequest(
263+
null,
264+
false,
265+
"Integration Test Payment",
266+
"ext-id-"+System.currentTimeMillis(),
267+
tags,
268+
paymentPart);
269+
270+
InvoicePayment invoicePayment = client.invoicePayment.create(invoicePaymentRequest);
259271

260272
assertTrue(invoicePayment.getInvoicePayments().size()>0);
261273

@@ -265,6 +277,8 @@ public void testInvoicePayments() throws Exception {
265277
paymentPart.setInvoiceLineId(invoice.getLines().get(0).getId());
266278
paymentPart.setPaymentId(invoicePayment.getPaymentId());
267279
paymentPart.setAmount(new Amount("10","USD"));
280+
paymentPart.setExternalId("ext-id-"+System.currentTimeMillis());
281+
paymentPart.setCoverFees(true);
268282

269283
boolean paymentUpdateResult = client.invoicePayment.update(paymentPart);
270284
assertTrue(paymentUpdateResult);

0 commit comments

Comments
 (0)