Skip to content

Commit 3ce0ce2

Browse files
authored
Merge pull request #6 from chaixdev/improve-java-sdk-consistency
paylink response dto implementation aligned with docs
2 parents 6768af1 + df23cb3 commit 3ce0ce2

5 files changed

Lines changed: 108 additions & 18 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
1+
**/bin/
2+
**/.env
23
target/
34
.mvn/
45
!**/src/main/**/target/

src/main/java/com/twikey/PaylinkGateway.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.InputStreamReader;
1515
import java.net.http.HttpRequest;
1616
import java.net.http.HttpResponse;
17-
import java.util.HashMap;
1817
import java.util.Map;
1918

2019
import static com.twikey.TwikeyClient.HTTP_FORM_ENCODED;
@@ -110,12 +109,11 @@ public void feed(PaylinkCallback callback,String... sideloads) throws IOExceptio
110109
JSONArray messagesArr = json.getJSONArray("Links");
111110
isEmpty = messagesArr.isEmpty();
112111
if (!isEmpty) {
113-
for (int i = 0; i < messagesArr.length(); i++) {
114-
JSONObject obj = messagesArr.getJSONObject(i);
115-
callback.paylink(obj);
116-
callback.paylink(PaylinkResponse.Paylink.fromJson(obj));
117-
}
118-
112+
for (int i = 0; i < messagesArr.length(); i++) {
113+
JSONObject obj = messagesArr.getJSONObject(i);
114+
callback.paylink(obj);
115+
callback.paylink(PaylinkResponse.Paylink.fromJson(obj));
116+
}
119117
}
120118
}
121119
} else {

src/main/java/com/twikey/TwikeyClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.security.GeneralSecurityException;
1313
import java.security.MessageDigest;
1414
import java.util.Map;
15-
import java.util.Properties;
1615

1716
import static java.nio.charset.StandardCharsets.UTF_8;
1817

src/main/java/com/twikey/modal/DocumentRequests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public InviteRequest setPrefix(String prefix) {
318318
}
319319

320320
public InviteRequest setExpiry(long epoch) {
321-
this.ed = ed;
321+
this.ed = String.valueOf(epoch);
322322
return this;
323323
}
324324

src/main/java/com/twikey/modal/PaylinkResponse.java

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,109 @@ public interface PaylinkResponse {
66

77
record Paylink(
88
long id,
9+
long templateId,
910
double amount,
10-
String message,
11-
String url
11+
String msg,
12+
String remittance,
13+
PaylinkState state,
14+
String url,
15+
Customer customer,
16+
Meta meta
1217
) {
1318
public static Paylink fromJson(JSONObject json) {
14-
return new Paylink(
15-
json.getLong("id"),
16-
json.getDouble("amount"),
17-
json.optString("msg"),
18-
json.getString("url")
19-
);
19+
long id = json.getLong("id");
20+
long templateId = json.optLong("ct");
21+
double amount = json.getDouble("amount");
22+
String msg = json.optString("msg");
23+
String remittance = json.optString("ref");
24+
String stateStr = json.optString("state", null);
25+
PaylinkState state = PaylinkState.parse(stateStr);
26+
String url = json.optString("url");
27+
28+
Customer customer = null;
29+
if (json.has("customer")) {
30+
JSONObject customerJson = json.getJSONObject("customer");
31+
customer = new Customer()
32+
.setEmail(customerJson.optString("email"))
33+
.setFirstname(customerJson.optString("firstname"))
34+
.setLastname(customerJson.optString("lastname"))
35+
.setStreet(customerJson.optString("address"))
36+
.setCity(customerJson.optString("city"))
37+
.setZip(customerJson.optString("zip"))
38+
.setCountry(customerJson.optString("country"))
39+
.setNumber(customerJson.optString("customerNumber"))
40+
.setLang(customerJson.optString("l"))
41+
.setMobile(customerJson.optString("mobile"))
42+
.setCompanyName(customerJson.optString("companyName"))
43+
.setCoc(customerJson.optString("coc"));
44+
}
45+
46+
Meta meta = null;
47+
if (json.has("meta")) {
48+
meta = Meta.fromJson(json.getJSONObject("meta"));
49+
}
50+
51+
return new Paylink(id, templateId, amount, msg, remittance, state, url, customer, meta);
52+
}
53+
}
54+
55+
record Meta(
56+
boolean active,
57+
String method,
58+
String recurringId,
59+
String expiry,
60+
String type,
61+
String invoice,
62+
String sdd,
63+
String tx,
64+
String paymentMethod
65+
) {
66+
public static Meta fromJson(JSONObject json) {
67+
boolean active = json.optBoolean("active");
68+
String method = json.optString("method", null);
69+
String recurringId = json.optString("recurringId", null);
70+
String expiry = json.optString("expiry", null);
71+
String type = json.optString("type", null);
72+
String invoice = json.optString("invoice", null);
73+
String sdd = json.optString("sdd", null);
74+
String tx = json.optString("tx", null);
75+
String paymentMethod = json.optString("paymentMethod", null);
76+
77+
return new Meta(active, method, recurringId, expiry, type, invoice, sdd, tx, paymentMethod);
78+
}
79+
}
80+
81+
82+
enum PaylinkState {
83+
CREATED("created"),
84+
STARTED("started"),
85+
PENDING("pending"),
86+
DECLINED("declined"),
87+
PAID("paid"),
88+
EXPIRED("expired"),
89+
REFUNDED("refunded");
90+
91+
private final String id;
92+
93+
PaylinkState(String id) {
94+
this.id = id;
95+
}
96+
97+
public String id() {
98+
return id;
99+
}
100+
101+
public static PaylinkState parse(String state) {
102+
if (state == null) return PENDING;
103+
return switch (state) {
104+
case "created" -> CREATED;
105+
case "started" -> STARTED;
106+
case "declined" -> DECLINED;
107+
case "paid" -> PAID;
108+
case "expired" -> EXPIRED;
109+
case "refunded" -> REFUNDED;
110+
default -> PENDING;
111+
};
20112
}
21113
}
22114
}

0 commit comments

Comments
 (0)