Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

**/bin/
**/.env
target/
.mvn/
!**/src/main/**/target/
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/twikey/PaylinkGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.InputStreamReader;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;

import static com.twikey.TwikeyClient.HTTP_FORM_ENCODED;
Expand Down Expand Up @@ -110,12 +109,11 @@ public void feed(PaylinkCallback callback,String... sideloads) throws IOExceptio
JSONArray messagesArr = json.getJSONArray("Links");
isEmpty = messagesArr.isEmpty();
if (!isEmpty) {
for (int i = 0; i < messagesArr.length(); i++) {
JSONObject obj = messagesArr.getJSONObject(i);
callback.paylink(obj);
callback.paylink(PaylinkResponse.Paylink.fromJson(obj));
}

for (int i = 0; i < messagesArr.length(); i++) {
JSONObject obj = messagesArr.getJSONObject(i);
callback.paylink(obj);
callback.paylink(PaylinkResponse.Paylink.fromJson(obj));
}
}
}
} else {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/twikey/TwikeyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Map;
import java.util.Properties;

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

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/twikey/modal/DocumentRequests.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public InviteRequest setPrefix(String prefix) {
}

public InviteRequest setExpiry(long epoch) {
this.ed = ed;
this.ed = String.valueOf(epoch);
return this;
}

Expand Down
108 changes: 100 additions & 8 deletions src/main/java/com/twikey/modal/PaylinkResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,109 @@ public interface PaylinkResponse {

record Paylink(
long id,
long templateId,
double amount,
String message,
String url
String msg,
String remittance,
PaylinkState state,
String url,
Customer customer,
Meta meta
) {
public static Paylink fromJson(JSONObject json) {
return new Paylink(
json.getLong("id"),
json.getDouble("amount"),
json.optString("msg"),
json.getString("url")
);
long id = json.getLong("id");
long templateId = json.optLong("ct");
double amount = json.getDouble("amount");
String msg = json.optString("msg");
String remittance = json.optString("ref");
String stateStr = json.optString("state", null);
PaylinkState state = PaylinkState.parse(stateStr);
String url = json.optString("url");

Customer customer = null;
if (json.has("customer")) {
JSONObject customerJson = json.getJSONObject("customer");
customer = new Customer()
.setEmail(customerJson.optString("email"))
.setFirstname(customerJson.optString("firstname"))
.setLastname(customerJson.optString("lastname"))
.setStreet(customerJson.optString("address"))
.setCity(customerJson.optString("city"))
.setZip(customerJson.optString("zip"))
.setCountry(customerJson.optString("country"))
.setNumber(customerJson.optString("customerNumber"))
.setLang(customerJson.optString("l"))
.setMobile(customerJson.optString("mobile"))
.setCompanyName(customerJson.optString("companyName"))
.setCoc(customerJson.optString("coc"));
}

Meta meta = null;
if (json.has("meta")) {
meta = Meta.fromJson(json.getJSONObject("meta"));
}

return new Paylink(id, templateId, amount, msg, remittance, state, url, customer, meta);
}
}

record Meta(
boolean active,
String method,
String recurringId,
String expiry,
String type,
String invoice,
String sdd,
String tx,
String paymentMethod
) {
public static Meta fromJson(JSONObject json) {
boolean active = json.optBoolean("active");
String method = json.optString("method", null);
String recurringId = json.optString("recurringId", null);
String expiry = json.optString("expiry", null);
String type = json.optString("type", null);
String invoice = json.optString("invoice", null);
String sdd = json.optString("sdd", null);
String tx = json.optString("tx", null);
String paymentMethod = json.optString("paymentMethod", null);

return new Meta(active, method, recurringId, expiry, type, invoice, sdd, tx, paymentMethod);
}
}


enum PaylinkState {
CREATED("created"),
STARTED("started"),
PENDING("pending"),
DECLINED("declined"),
PAID("paid"),
EXPIRED("expired"),
REFUNDED("refunded");

private final String id;

PaylinkState(String id) {
this.id = id;
}

public String id() {
return id;
}

public static PaylinkState parse(String state) {
if (state == null) return PENDING;
return switch (state) {
case "created" -> CREATED;
case "started" -> STARTED;
case "declined" -> DECLINED;
case "paid" -> PAID;
case "expired" -> EXPIRED;
case "refunded" -> REFUNDED;
default -> PENDING;
};
}
}
}