Skip to content

Commit

Permalink
Add params to list endpoints (#34)
Browse files Browse the repository at this point in the history
Add params to list endpoints: 

- buyers
- transactions
- payment-methods
  • Loading branch information
steve-gr4vy authored Nov 6, 2023
1 parent 2d463ee commit 84534f7
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Add the `gr4vy-java` dependency to your pom.xml:
<dependency>
<groupId>com.github.gr4vy</groupId>
<artifactId>gr4vy-java</artifactId>
<version>0.17.1</version>
<version>0.18.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>gr4vy</artifactId>
<packaging>jar</packaging>
<name>gr4vy</name>
<version>0.17.1</version>
<version>0.18.0</version>
<url>https://gr4vy.com</url>
<description>Gr4vy Java SDK</description>

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/gr4vy/sdk/Gr4vyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ public Buyers listBuyers() {
String response = this.get("/buyers");
return this.gson.fromJson(response,Buyers.class);
}
public Buyers listBuyers(Map<String, Object> params) {
String endpoint = "/buyers";
if (params.size() > 0) {
String encoded = Helper.urlEncodeUTF8(params);
endpoint = endpoint + "?" + encoded;
}
String response = this.get(endpoint);
return this.gson.fromJson(response,Buyers.class);
}
public boolean deleteBuyer(String buyerId) {
return this.delete("/buyers/" + buyerId);
}
Expand All @@ -434,6 +443,15 @@ public PaymentMethods listPaymentMethods() {
String response = this.get("/payment-methods");
return this.gson.fromJson(response,PaymentMethods.class);
}
public PaymentMethods listPaymentMethods(Map<String, Object> params) {
String endpoint = "/payment-methods";
if (params.size() > 0) {
String encoded = Helper.urlEncodeUTF8(params);
endpoint = endpoint + "?" + encoded;
}
String response = this.get(endpoint);
return this.gson.fromJson(response,PaymentMethods.class);
}
public PaymentMethods listBuyerPaymentMethods(String buyerId) {
String response = this.get("/buyers/payment-methods?buyer_id=" + buyerId);
return this.gson.fromJson(response,PaymentMethods.class);
Expand Down Expand Up @@ -462,6 +480,15 @@ public Transactions listTransactions() {
String response = this.get("/transactions");
return this.gson.fromJson(response,Transactions.class);
}
public Transactions listTransactions(Map<String, Object> params) {
String endpoint = "/transactions";
if (params.size() > 0) {
String encoded = Helper.urlEncodeUTF8(params);
endpoint = endpoint + "?" + encoded;
}
String response = this.get(endpoint);
return this.gson.fromJson(response,Transactions.class);
}
public TransactionHistoryEvents listEventsForTransaction(String transactionId) {
String response = this.get("/transactions/" + transactionId + "/events");
return this.gson.fromJson(response,TransactionHistoryEvents.class);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/gr4vy/sdk/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gr4vy.sdk;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class Helper {
static String urlEncodeUTF8(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UnsupportedOperationException(e);
}
}
static String urlEncodeUTF8(Map<?,?> map) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<?,?> entry : map.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(String.format("%s=%s",
urlEncodeUTF8(entry.getKey().toString()),
urlEncodeUTF8(entry.getValue().toString())
));
}
return sb.toString();
}
}
40 changes: 33 additions & 7 deletions src/test/java/com/gr4vy/sdk/Gr4vyClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,24 @@ public void listBuyersTest() throws Gr4vyException {
assert response != null;
}

@Test
public void listBuyersWithParamsTest() throws Gr4vyException {
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");

Map<String, Object> params = new HashMap<String, Object>();
params.put("limit", 2);
try {
Buyers response = client.listBuyers(params);
assert response != null;
}
catch (Gr4vyException e) {
System.out.println(e.getError());
assert false;
}
}

@Test
public void newCheckoutSessionTransactionTest() throws Gr4vyException {
/*Test cannot be run because checkout session is empty*/
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");

CheckoutSession checkoutSession = client.newCheckoutSession(null);
Expand Down Expand Up @@ -134,16 +149,13 @@ public void newCheckoutSessionTransactionTest() throws Gr4vyException {

@Test
public void getCheckoutSessionTest() throws Gr4vyException {
/*Test cannot be run because checkout session is empty*/
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");

CheckoutSession checkoutSession = client.newCheckoutSession(null);


CheckoutSession retrieved = client.getCheckoutSession(checkoutSession.getId().toString());
assert retrieved != null;
System.out.println(retrieved);

}

@Test
Expand Down Expand Up @@ -280,8 +292,6 @@ public void refundTransactionTest() throws Gr4vyException {
Transaction response = client.newTransaction(request);
assert response != null;

System.out.println(response);

TransactionRefundRequest refund = new TransactionRefundRequest()
.amount(100);
Refund refundResponse = client.refundTransaction(response.getId().toString(), refund);
Expand Down Expand Up @@ -341,14 +351,30 @@ public void listTransactionsTest() throws Gr4vyException {
assert response != null;
}

@Test
public void listTransactionsWithParamsTest() throws Gr4vyException {
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");

Map<String, Object> params = new HashMap<String, Object>();
params.put("limit", 2);
params.put("amount_gte", "100");
try {
Transactions response = client.listTransactions(params);
assert response != null;
}
catch (Gr4vyException e) {
System.out.println(e.getError());
assert false;
}
}

@Test
public void listTransactionHistoryEventsTest() throws Gr4vyException {
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");

TransactionHistoryEvents response = client.listEventsForTransaction("0cb94b92-aeb9-4f67-96c5-084cbaf5b66c");

assert response != null;
System.out.println(response);
}

@Test
Expand Down

0 comments on commit 84534f7

Please sign in to comment.