From b062a5c7b4893dcad98094f674f7b77161ddadcf Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Tue, 10 Oct 2023 17:42:37 +0700 Subject: [PATCH 1/2] Add support for dynamic webhooks --- src/main/java/co/omise/models/Charge.java | 18 ++++++++++++++++++ .../co/omise/requests/ChargeRequestTest.java | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/co/omise/models/Charge.java b/src/main/java/co/omise/models/Charge.java index 73399632..819387f5 100644 --- a/src/main/java/co/omise/models/Charge.java +++ b/src/main/java/co/omise/models/Charge.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -89,6 +90,8 @@ public class Charge extends Model { private long authorizedAmount; @JsonProperty("captured_amount") private long capturedAmount; + @JsonProperty("webhook_endpoints") + private String[] webhookEndpoints; public long getAmount() { return this.amount; @@ -490,6 +493,14 @@ public void setCapturedAmount(long capturedAmount) { this.capturedAmount = capturedAmount; } + public String[] getWebhookEndpoints() { + return webhookEndpoints; + } + + public void setWebhookEndpoints(String[] webhookEndpoints) { + this.webhookEndpoints = webhookEndpoints; + } + public static class ListRequestBuilder extends RequestBuilder> { private ScopedList.Options options; @@ -552,6 +563,8 @@ public static class CreateRequestBuilder extends RequestBuilder { private String source; @JsonProperty("authorization_type") private AuthorizationType authorizationType; + @JsonProperty("webhook_endpoints") + private String[] webhookEndpoints; @Override protected String method() { @@ -643,6 +656,11 @@ public CreateRequestBuilder authorizationType(AuthorizationType authorizationTyp return this; } + public CreateRequestBuilder webhookEndpoints(List webhookEndpoints) { + this.webhookEndpoints = webhookEndpoints.toArray(new String[0]); + return this; + } + @Override protected RequestBody payload() throws IOException { return serialize(); diff --git a/src/test/java/co/omise/requests/ChargeRequestTest.java b/src/test/java/co/omise/requests/ChargeRequestTest.java index 9e42c0e7..6ae54e70 100644 --- a/src/test/java/co/omise/requests/ChargeRequestTest.java +++ b/src/test/java/co/omise/requests/ChargeRequestTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import java.io.IOException; +import java.util.Arrays; public class ChargeRequestTest extends RequestTest { private final String CHARGE_ID = "chrg_test_4yq7duw15p9hdrjp8oq"; @@ -45,6 +46,22 @@ public void testCreate() throws IOException, OmiseException { assertEquals("trxn_test_4yq7duwb9jts1vxgqua", charge.getTransaction()); } + @Test + public void testCreateWithWebhooks() throws IOException, OmiseException { + Request createChargeRequest = + new Charge.CreateRequestBuilder() + .amount(100000) + .currency("thb") + .webhookEndpoints(Arrays.asList("https://webhook.site/123")) + .build(); + + Charge charge = getTestRequester().sendRequest(createChargeRequest); + + assertRequested("POST", "/charges", 200); + assertRequestBody("{\"amount\":100000,\"capture\":false,\"card\":null,\"currency\":\"thb\",\"customer\":null,\"description\":null,\"ip\":null,\"metadata\":null,\"reference\":null,\"source\":null,\"zero_interest_installments\":false,\"expires_at\":null,\"platform_fee\":null,\"return_uri\":null,\"authorization_type\":null,\"webhook_endpoints\":[\"https://webhook.site/123\"]}"); + assertNotNull(charge); + } + @Test public void testCreateChargeFromSource() throws IOException, OmiseException { Request createChargeRequest = @@ -80,7 +97,7 @@ public void testCreatePartialCaptureCharge() throws IOException, OmiseException Charge charge = getTestRequester().sendRequest(createChargeRequest); assertRequested("POST", "/charges", 200); - assertRequestBody("{\"amount\":100000,\"capture\":false,\"card\":null,\"currency\":\"thb\",\"customer\":null,\"description\":null,\"ip\":null,\"metadata\":null,\"reference\":null,\"source\":null,\"zero_interest_installments\":false,\"expires_at\":null,\"platform_fee\":null,\"return_uri\":\"http://example.com/orders/345678/complete\",\"authorization_type\":\"pre_auth\"}"); + assertRequestBody("{\"amount\":100000,\"capture\":false,\"card\":null,\"currency\":\"thb\",\"customer\":null,\"description\":null,\"ip\":null,\"metadata\":null,\"reference\":null,\"source\":null,\"zero_interest_installments\":false,\"expires_at\":null,\"platform_fee\":null,\"return_uri\":\"http://example.com/orders/345678/complete\",\"authorization_type\":\"pre_auth\",\"webhook_endpoints\":null}"); assertNotNull(charge); } From 404baa14c0400b13d484a586c8f8ae42618b1d8b Mon Sep 17 00:00:00 2001 From: AnasNaouchi Date: Wed, 11 Oct 2023 14:25:36 +0700 Subject: [PATCH 2/2] Switched to List --- src/main/java/co/omise/models/Charge.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/co/omise/models/Charge.java b/src/main/java/co/omise/models/Charge.java index 819387f5..ad82c9ec 100644 --- a/src/main/java/co/omise/models/Charge.java +++ b/src/main/java/co/omise/models/Charge.java @@ -91,7 +91,7 @@ public class Charge extends Model { @JsonProperty("captured_amount") private long capturedAmount; @JsonProperty("webhook_endpoints") - private String[] webhookEndpoints; + private List webhookEndpoints; public long getAmount() { return this.amount; @@ -493,11 +493,11 @@ public void setCapturedAmount(long capturedAmount) { this.capturedAmount = capturedAmount; } - public String[] getWebhookEndpoints() { + public List getWebhookEndpoints() { return webhookEndpoints; } - public void setWebhookEndpoints(String[] webhookEndpoints) { + public void setWebhookEndpoints(List webhookEndpoints) { this.webhookEndpoints = webhookEndpoints; } @@ -564,7 +564,7 @@ public static class CreateRequestBuilder extends RequestBuilder { @JsonProperty("authorization_type") private AuthorizationType authorizationType; @JsonProperty("webhook_endpoints") - private String[] webhookEndpoints; + private List webhookEndpoints; @Override protected String method() { @@ -657,7 +657,7 @@ public CreateRequestBuilder authorizationType(AuthorizationType authorizationTyp } public CreateRequestBuilder webhookEndpoints(List webhookEndpoints) { - this.webhookEndpoints = webhookEndpoints.toArray(new String[0]); + this.webhookEndpoints = webhookEndpoints; return this; }