Skip to content

Commit

Permalink
Add support for dynamic webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
AnasNaouchi committed Oct 10, 2023
1 parent 2904d43 commit b062a5c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/co/omise/models/Charge.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ScopedList<Charge>> {
private ScopedList.Options options;

Expand Down Expand Up @@ -552,6 +563,8 @@ public static class CreateRequestBuilder extends RequestBuilder<Charge> {
private String source;
@JsonProperty("authorization_type")
private AuthorizationType authorizationType;
@JsonProperty("webhook_endpoints")
private String[] webhookEndpoints;

@Override
protected String method() {
Expand Down Expand Up @@ -643,6 +656,11 @@ public CreateRequestBuilder authorizationType(AuthorizationType authorizationTyp
return this;
}

public CreateRequestBuilder webhookEndpoints(List<String> webhookEndpoints) {
this.webhookEndpoints = webhookEndpoints.toArray(new String[0]);
return this;
}

@Override
protected RequestBody payload() throws IOException {
return serialize();
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/co/omise/requests/ChargeRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -45,6 +46,22 @@ public void testCreate() throws IOException, OmiseException {
assertEquals("trxn_test_4yq7duwb9jts1vxgqua", charge.getTransaction());
}

@Test
public void testCreateWithWebhooks() throws IOException, OmiseException {
Request<Charge> 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<Charge> createChargeRequest =
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit b062a5c

Please sign in to comment.