diff --git a/.github/workflows/maven-build.yml b/.github/workflows/maven-build.yml index 71064c9..71969a9 100644 --- a/.github/workflows/maven-build.yml +++ b/.github/workflows/maven-build.yml @@ -22,11 +22,6 @@ jobs: with: distribution: 'adopt' java-version: ${{ matrix.java-version }} - server-id: ossrh - server-username: MAVEN_USERNAME # env variable for username in deploy - server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy - gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - name: Cache Maven packages uses: actions/cache@v1 diff --git a/pom.xml b/pom.xml index d2bcd1a..1bb1426 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.twikey twikey-api-java jar - 0.1.1 + 0.1.2 Twikey Api http://maven.apache.org Official wrapper around the Twikey.com rest api diff --git a/src/main/java/com/twikey/InvoiceGateway.java b/src/main/java/com/twikey/InvoiceGateway.java index c6b6542..7f2c2aa 100644 --- a/src/main/java/com/twikey/InvoiceGateway.java +++ b/src/main/java/com/twikey/InvoiceGateway.java @@ -63,7 +63,7 @@ public JSONObject create(long ct, Customer customer, Map invoice JSONObject invoice = new JSONObject() .put("customer", customerAsJson) .put("date", invoiceDetails.getOrDefault("date", LocalDate.now().toString())) - .put("duedate", invoiceDetails.getOrDefault("date", LocalDate.now().plusMonths(1).toString())) + .put("duedate", invoiceDetails.getOrDefault("duedate", LocalDate.now().plusMonths(1).toString())) .put("ct", ct); for (Map.Entry entry : invoiceDetails.entrySet()) { diff --git a/src/main/java/com/twikey/PaylinkGateway.java b/src/main/java/com/twikey/PaylinkGateway.java index a7c8378..ba23236 100644 --- a/src/main/java/com/twikey/PaylinkGateway.java +++ b/src/main/java/com/twikey/PaylinkGateway.java @@ -116,7 +116,7 @@ public void feed(PaylinkCallback callback) throws IOException, TwikeyClient.User try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) { JSONObject json = new JSONObject(new JSONTokener(br)); - JSONArray messagesArr = json.getJSONArray("Messages"); + JSONArray messagesArr = json.getJSONArray("Links"); isEmpty = messagesArr.isEmpty(); if (!isEmpty) { for (int i = 0; i < messagesArr.length(); i++) { diff --git a/src/test/java/com/twikey/DocumentGatewayTest.java b/src/test/java/com/twikey/DocumentGatewayTest.java new file mode 100644 index 0000000..dfe8d41 --- /dev/null +++ b/src/test/java/com/twikey/DocumentGatewayTest.java @@ -0,0 +1,79 @@ +package com.twikey; + +import com.twikey.callback.DocumentCallback; +import com.twikey.modal.Customer; +import org.json.JSONObject; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.HashMap; + +import static org.junit.Assert.assertNotNull; + +public class DocumentGatewayTest { + + private final String apiKey = System.getenv("TWIKEY_API_KEY"); // found in https://www.twikey.com/r/admin#/c/settings/api + + private final String ct = System.getenv("CT"); // found @ https://www.twikey.com/r/admin#/c/template + + private Customer customer; + + private TwikeyClient api; + + @Before + public void createCustomer(){ + customer = new Customer() + .setNumber("customerNum123") + .setEmail("no-reply@example.com") + .setFirstname("Twikey") + .setLastname("Support") + .setStreet("Derbystraat 43") + .setCity("Gent") + .setZip("9000") + .setCountry("BE") + .setLang("nl") + .setMobile("32498665995"); + + api = new TwikeyClient(apiKey,true) + .withUserAgent("twikey-api-java/junit"); + } + + @Test + public void testInviteMandateWithoutCustomerDetails() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey); + JSONObject response = api.document().create(Long.parseLong(ct), null, new HashMap<>()); + assertNotNull("Invite URL",response.getString("url")); + assertNotNull("Document Reference",response.getString("mndtId")); + } + + @Test + public void testInviteMandateCustomerDetails() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey); + JSONObject response = api.document().create(Long.parseLong(ct), customer, new HashMap<>()); + assertNotNull("Invite URL",response.getString("url")); + assertNotNull("Document Reference",response.getString("mndtId")); + } + + @Test + public void getMandatesAndDetails() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey); + api.document().feed(new DocumentCallback() { + @Override + public void newDocument(JSONObject newMandate) { + System.out.println("New mandate: "+newMandate); + } + + @Override + public void updatedDocument(JSONObject updatedMandate) { + System.out.println("Updated mandate: "+updatedMandate); + } + + @Override + public void cancelledDocument(JSONObject cancelledMandate) { + System.out.println("Cancelled mandate: "+cancelledMandate); + } + }); + } +} diff --git a/src/test/java/com/twikey/InvoiceGatewayTest.java b/src/test/java/com/twikey/InvoiceGatewayTest.java new file mode 100644 index 0000000..83a5b66 --- /dev/null +++ b/src/test/java/com/twikey/InvoiceGatewayTest.java @@ -0,0 +1,66 @@ +package com.twikey; + +import com.twikey.modal.Customer; +import junit.framework.TestCase; +import org.json.JSONObject; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertNotNull; + +public class InvoiceGatewayTest { + + private final String apiKey = System.getenv("TWIKEY_API_KEY"); // found in https://www.twikey.com/r/admin#/c/settings/api + + private final String ct = System.getenv("CT"); // found @ https://www.twikey.com/r/admin#/c/template + + private Customer customer; + + private TwikeyClient api; + + @Before + public void createCustomer() { + customer = new Customer() + .setNumber("customerNum123") + .setEmail("no-reply@example.com") + .setFirstname("Twikey") + .setLastname("Support") + .setStreet("Derbystraat 43") + .setCity("Gent") + .setZip("9000") + .setCountry("BE") + .setLang("nl") + .setMobile("32498665995"); + + api = new TwikeyClient(apiKey, true) + .withUserAgent("twikey-api-java/junit"); + } + + @Test + public void testCreateInvoice() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey, ct); + Map invoiceDetails = new HashMap<>(); + invoiceDetails.put("number", "Invss123"); + invoiceDetails.put("title", "Invoice April"); + invoiceDetails.put("remittance", "123456789123"); + invoiceDetails.put("amount", "10.90"); + invoiceDetails.put("date", "2020-03-20"); + invoiceDetails.put("duedate", "2020-04-28"); + JSONObject invoiceResponse = api.invoice().create(Long.parseLong(ct), customer, invoiceDetails); + assertNotNull("Payment URL", invoiceResponse.getString("url")); + assertNotNull("Invoice Id", invoiceResponse.getString("id")); + } + + + @Test + public void getInvoicesAndDetails() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey); + api.invoice().feed(updatedInvoice -> System.out.println("Updated invoice: " + updatedInvoice)); + } + +} diff --git a/src/test/java/com/twikey/PaylinkGatewayTest.java b/src/test/java/com/twikey/PaylinkGatewayTest.java new file mode 100644 index 0000000..706db08 --- /dev/null +++ b/src/test/java/com/twikey/PaylinkGatewayTest.java @@ -0,0 +1,61 @@ +package com.twikey; + +import com.twikey.modal.Customer; +import org.json.JSONObject; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; + +public class PaylinkGatewayTest { + + private final String apiKey = System.getenv("TWIKEY_API_KEY"); // found in https://www.twikey.com/r/admin#/c/settings/api + + private final String ct = System.getenv("CT"); // found @ https://www.twikey.com/r/admin#/c/template + + private Customer customer; + + private TwikeyClient api; + + @Before + public void createCustomer() { + customer = new Customer() + .setNumber("customerNum123") + .setEmail("no-reply@example.com") + .setFirstname("Twikey") + .setLastname("Support") + .setStreet("Derbystraat 43") + .setCity("Gent") + .setZip("9000") + .setCountry("BE") + .setLang("nl") + .setMobile("32498665995"); + + api = new TwikeyClient(apiKey, true) + .withUserAgent("twikey-api-java/junit"); + } + + @Test + public void testCreate() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey, ct); + Map extra = new HashMap<>(); + extra.put("message", "Test Link"); + extra.put("amount", "10.00"); + JSONObject linkResponse = api.paylink().create(Long.parseLong(ct), customer, extra); + assertNotNull("Payment URL", linkResponse.getString("url")); + assertNotEquals(0, linkResponse.getLong("id")); + + } + + @Test + public void testFeed() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey); + api.paylink().feed(updatedLink -> System.out.println("Updated link: " + updatedLink)); + } +} diff --git a/src/test/java/com/twikey/TransactionGatewayTest.java b/src/test/java/com/twikey/TransactionGatewayTest.java new file mode 100644 index 0000000..001471e --- /dev/null +++ b/src/test/java/com/twikey/TransactionGatewayTest.java @@ -0,0 +1,46 @@ +package com.twikey; + +import org.json.JSONObject; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertNotNull; + +public class TransactionGatewayTest { + + private final String apiKey = System.getenv("TWIKEY_API_KEY"); // found in https://www.twikey.com/r/admin#/c/settings/api + + private final String mandateNumber = System.getenv("mndtNumber"); + + private TwikeyClient api; + + @Before + public void prepClient() { + api = new TwikeyClient(apiKey, true) + .withUserAgent("twikey-api-java/junit"); + } + + @Test + public void testCreate() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey,mandateNumber); + Map extra = new HashMap<>(); + extra.put("message", "Test Message"); + extra.put("amount", "10.00"); + JSONObject linkResponse = api.transaction().create(mandateNumber, extra); + System.out.println(linkResponse); + assertNotNull("Payment URL", linkResponse.getString("url")); + assertNotNull("Invoice Id", linkResponse.getString("id")); + } + + @Test + public void testFeed() throws IOException, TwikeyClient.UserException { + Assume.assumeNotNull(apiKey); + api.transaction().feed(updatedTransaction -> System.out.println("Updated transaction: " + updatedTransaction)); + } + +} diff --git a/src/test/java/com/twikey/TwikeyAPITest.java b/src/test/java/com/twikey/TwikeyAPITest.java deleted file mode 100644 index 6d9fe50..0000000 --- a/src/test/java/com/twikey/TwikeyAPITest.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.twikey; - -import com.twikey.callback.DocumentCallback; -import com.twikey.modal.Customer; -import org.json.JSONObject; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class TwikeyAPITest { - - private String apiKey = System.getenv("TWIKEY_API_KEY"); // found in https://www.twikey.com/r/admin#/c/settings/api - - private long ct = Long.getLong("CT",0L); // found @ https://www.twikey.com/r/admin#/c/template - - private Customer customer; - - private TwikeyClient api; - - @Before - public void createCustomer(){ - customer = new Customer() - .setNumber("customerNum123") - .setEmail("no-reply@example.com") - .setFirstname("Twikey") - .setLastname("Support") - .setStreet("Derbystraat 43") - .setCity("Gent") - .setZip("9000") - .setCountry("BE") - .setLang("nl") - .setMobile("32498665995"); - - api = new TwikeyClient(apiKey,true) - .withUserAgent("twikey-api-java/junit"); - } - - @Test - public void testInviteMandateWithoutCustomerDetails() throws IOException, TwikeyClient.UserException { - Assume.assumeNotNull(apiKey); - System.out.println(api.document().create(ct,null,new HashMap<>())); - } - - @Test - public void testInviteMandateCustomerDetails() throws IOException, TwikeyClient.UserException { - Assume.assumeNotNull(apiKey); - System.out.println(api.document().create(ct,customer,new HashMap<>())); - } - - @Test - public void testCreateInvoice() throws IOException, TwikeyClient.UserException { - Assume.assumeNotNull(apiKey); - Map invoiceDetails = new HashMap<>(); - invoiceDetails.put("number", "Invss123"); - invoiceDetails.put("title", "Invoice April"); - invoiceDetails.put("remittance", "123456789123"); - invoiceDetails.put("amount", "10.90"); - invoiceDetails.put("date", "2020-03-20"); - invoiceDetails.put("duedate","2020-04-28"); - System.out.println(api.invoice().create(ct,customer,invoiceDetails)); - } - - @Test - public void getMandatesAndDetails() throws IOException, TwikeyClient.UserException { - Assume.assumeNotNull(apiKey); - api.document().feed(new DocumentCallback() { - @Override - public void newDocument(JSONObject newMandate) { - System.out.println("New mandate: "+newMandate); - } - - @Override - public void updatedDocument(JSONObject updatedMandate) { - System.out.println("Updated mandate: "+updatedMandate); - } - - @Override - public void cancelledDocument(JSONObject cancelledMandate) { - System.out.println("Cancelled mandate: "+cancelledMandate); - } - }); - } - - @Test - public void getInvoicesAndDetails() throws IOException, TwikeyClient.UserException { - Assume.assumeNotNull(apiKey); - api.invoice().feed(updatedInvoice -> System.out.println("Updated invoice: "+updatedInvoice)); - } - - @Test - public void verifySignatureAndDecryptAccountInfo() { - // exiturl defined in template http://example.com?mandatenumber={{mandateNumber}}&status={{status}}&signature={{s}}&account={{account}} - // outcome http://example.com?mandatenumber=MYDOC&status=ok&signature=8C56F94905BBC9E091CB6C4CEF4182F7E87BD94312D1DD16A61BF7C27C18F569&account=2D4727E936B5353CA89B908309686D74863521CAB32D76E8C2BDD338D3D44BBA - - String outcome = "http://example.com?mandatenumber=MYDOC&status=ok&" + - "signature=8C56F94905BBC9E091CB6C4CEF4182F7E87BD94312D1DD16A61BF7C27C18F569&" + - "account=2D4727E936B5353CA89B908309686D74863521CAB32D76E8C2BDD338D3D44BBA"; - - String websiteKey = "BE04823F732EDB2B7F82252DDAF6DE787D647B43A66AE97B32773F77CCF12765"; - String doc = "MYDOC"; - String status = "ok"; - - String signatureInOutcome = "8C56F94905BBC9E091CB6C4CEF4182F7E87BD94312D1DD16A61BF7C27C18F569"; - String encryptedAccountInOutcome = "2D4727E936B5353CA89B908309686D74863521CAB32D76E8C2BDD338D3D44BBA"; - assertTrue("Valid Signature",TwikeyClient.verifyExiturlSignature(websiteKey,doc,status,null,signatureInOutcome)); - String[] ibanAndBic = TwikeyClient.decryptAccountInformation(websiteKey, doc, encryptedAccountInOutcome); - assertEquals("BE08001166979213",ibanAndBic[0]); - assertEquals("GEBABEBB",ibanAndBic[1]); - } -} diff --git a/src/test/java/com/twikey/TwikeyClientTest.java b/src/test/java/com/twikey/TwikeyClientTest.java new file mode 100644 index 0000000..9bf9603 --- /dev/null +++ b/src/test/java/com/twikey/TwikeyClientTest.java @@ -0,0 +1,31 @@ +package com.twikey; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TwikeyClientTest { + + @Test + public void verifySignatureAndDecryptAccountInfo() { + // exiturl defined in template http://example.com?mandatenumber={{mandateNumber}}&status={{status}}&signature={{s}}&account={{account}} + // outcome http://example.com?mandatenumber=MYDOC&status=ok&signature=8C56F94905BBC9E091CB6C4CEF4182F7E87BD94312D1DD16A61BF7C27C18F569&account=2D4727E936B5353CA89B908309686D74863521CAB32D76E8C2BDD338D3D44BBA + + String outcome = "http://example.com?mandatenumber=MYDOC&status=ok&" + + "signature=8C56F94905BBC9E091CB6C4CEF4182F7E87BD94312D1DD16A61BF7C27C18F569&" + + "account=2D4727E936B5353CA89B908309686D74863521CAB32D76E8C2BDD338D3D44BBA"; + + String websiteKey = "BE04823F732EDB2B7F82252DDAF6DE787D647B43A66AE97B32773F77CCF12765"; + String doc = "MYDOC"; + String status = "ok"; + + String signatureInOutcome = "8C56F94905BBC9E091CB6C4CEF4182F7E87BD94312D1DD16A61BF7C27C18F569"; + String encryptedAccountInOutcome = "2D4727E936B5353CA89B908309686D74863521CAB32D76E8C2BDD338D3D44BBA"; + assertTrue("Valid Signature",TwikeyClient.verifyExiturlSignature(websiteKey,doc,status,null,signatureInOutcome)); + String[] ibanAndBic = TwikeyClient.decryptAccountInformation(websiteKey, doc, encryptedAccountInOutcome); + assertEquals("BE08001166979213",ibanAndBic[0]); + assertEquals("GEBABEBB",ibanAndBic[1]); + } + +}