diff --git a/src/main/java/co/omise/models/AuthorizationType.java b/src/main/java/co/omise/models/AuthorizationType.java new file mode 100644 index 00000000..5ed5177e --- /dev/null +++ b/src/main/java/co/omise/models/AuthorizationType.java @@ -0,0 +1,10 @@ +package co.omise.models; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public enum AuthorizationType { + @JsonProperty("pre_auth") + PreAuth, + @JsonProperty("final_auth") + FinalAuth; +} diff --git a/src/main/java/co/omise/models/Charge.java b/src/main/java/co/omise/models/Charge.java index 9b5920ee..4e24f7ba 100644 --- a/src/main/java/co/omise/models/Charge.java +++ b/src/main/java/co/omise/models/Charge.java @@ -1,7 +1,6 @@ package co.omise.models; import co.omise.Endpoint; -import co.omise.models.schedules.ChargeSchedule; import co.omise.models.schedules.Schedule; import co.omise.requests.RequestBuilder; import co.omise.requests.ResponseType; @@ -84,6 +83,8 @@ public class Charge extends Model { private boolean voided; @JsonProperty("zero_interest_installments") private boolean zeroInterestInstallments; + @JsonProperty("authorization_type") + private AuthorizationType authorizationType; public long getAmount() { return this.amount; @@ -461,6 +462,14 @@ public void setTransactionFees(TransactionFee transactionFees) { this.transactionFees = transactionFees; } + public AuthorizationType getAuthorizationType() { + return authorizationType; + } + + public void setAuthorizationType(AuthorizationType authorizationType) { + this.authorizationType = authorizationType; + } + public static class ListRequestBuilder extends RequestBuilder> { private ScopedList.Options options; @@ -521,6 +530,8 @@ public static class CreateRequestBuilder extends RequestBuilder { private String returnUri; @JsonProperty private String source; + @JsonProperty("authorization_type") + private AuthorizationType authorizationType; @Override protected String method() { @@ -607,6 +618,11 @@ public CreateRequestBuilder zeroInterestInstallments(boolean zeroInterestInstall return this; } + public CreateRequestBuilder authorizationType(AuthorizationType authorizationType) { + this.authorizationType = authorizationType; + return this; + } + @Override protected RequestBody payload() throws IOException { return serialize(); @@ -765,10 +781,19 @@ public UpdateRequestBuilder metadata(String key, Object value) { public static class CaptureRequestBuilder extends RequestBuilder { private String chargeId; + + @JsonProperty("capture_amount") + private long captureAmount; + public CaptureRequestBuilder(String chargeId) { this.chargeId = chargeId; } + public CaptureRequestBuilder captureAmount(long captureAmount) { + this.captureAmount = captureAmount; + return this; + } + @Override protected String method() { return POST; @@ -783,6 +808,11 @@ protected HttpUrl path() { protected ResponseType type() { return new ResponseType<>(Charge.class); } + + @Override + protected RequestBody payload() throws IOException { + return serialize(); + } } public static class ExpireRequestBuilder extends RequestBuilder { diff --git a/src/test/java/co/omise/live/LiveChargeRequestTest.java b/src/test/java/co/omise/live/LiveChargeRequestTest.java index 21f59cfd..42eba0b6 100644 --- a/src/test/java/co/omise/live/LiveChargeRequestTest.java +++ b/src/test/java/co/omise/live/LiveChargeRequestTest.java @@ -287,7 +287,7 @@ public void testLiveChargeCapture() throws IOException, OmiseException { .name("Omise Co., Ltd. - testLiveCharge") .number("4242424242424242") .securityCode("123") - .expiration(10, 2020)) + .expiration(10, 2030)) .build(); Token token = client.sendRequest(tokenRequest); @@ -317,6 +317,47 @@ public void testLiveChargeCapture() throws IOException, OmiseException { assertTrue(capturedCharge.isPaid()); } + + @Test + @Ignore("only hit the network when we need to.") + public void testLiveChargeSinglePartialCapture() throws IOException, OmiseException { + Request tokenRequest = new Token.CreateRequestBuilder() + .card(new Card.Create() + .name("Omise Co., Ltd. - testLiveCharge") + .number("4242424242424242") + .securityCode("123") + .expiration(10, 2030)) + .build(); + + Token token = client.sendRequest(tokenRequest); + + Request createChargeRequest = + new Charge.CreateRequestBuilder() + .amount(100000) + .currency("thb") + .description("omise-java test") + .card(token.getId()) + .capture(false) + .authorizationType(AuthorizationType.PreAuth) + .build(); + Charge unCapturedCharge = client.sendRequest(createChargeRequest); + + System.out.println("created charge: " + unCapturedCharge.getId()); + + assertNotNull(unCapturedCharge.getId()); + assertFalse(unCapturedCharge.isCapture()); + assertEquals(AuthorizationType.PreAuth, unCapturedCharge.getAuthorizationType()); + + Request captureChargeRequest = + new Charge.CaptureRequestBuilder(unCapturedCharge.getId()).captureAmount(50000).build(); + + Charge capturedCharge = client.sendRequest(captureChargeRequest); + + assertNotNull(capturedCharge); + assertEquals(unCapturedCharge.getId(), capturedCharge.getId()); + assertTrue(capturedCharge.isPaid()); + } + @Test @Ignore("only hit the network when we need to.") public void testLiveChargeReverse() throws IOException, OmiseException { diff --git a/src/test/java/co/omise/requests/ChargeRequestTest.java b/src/test/java/co/omise/requests/ChargeRequestTest.java index 7946b5e5..9e42c0e7 100644 --- a/src/test/java/co/omise/requests/ChargeRequestTest.java +++ b/src/test/java/co/omise/requests/ChargeRequestTest.java @@ -1,5 +1,6 @@ package co.omise.requests; +import co.omise.models.AuthorizationType; import co.omise.models.Barcode; import co.omise.models.Charge; import co.omise.models.OmiseException; @@ -65,6 +66,24 @@ public void testCreateChargeFromSource() throws IOException, OmiseException { assertEquals(100000L, charge.getAmount()); } + @Test + public void testCreatePartialCaptureCharge() throws IOException, OmiseException { + Request createChargeRequest = + new Charge.CreateRequestBuilder() + .amount(100000) + .currency("thb") + .capture(false) + .authorizationType(AuthorizationType.PreAuth) + .returnUri("http://example.com/orders/345678/complete") + .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\":\"http://example.com/orders/345678/complete\",\"authorization_type\":\"pre_auth\"}"); + assertNotNull(charge); + } + @Test public void testUpdate() throws IOException, OmiseException { Request updateChargeRequest = @@ -92,6 +111,22 @@ public void testCapture() throws IOException, OmiseException { assertTrue(charge.isPaid()); } + @Test + public void testPartialCapture() throws IOException, OmiseException { + Request captureChargeRequest = + new Charge.CaptureRequestBuilder(CHARGE_ID) + .captureAmount(100000) + .build(); + + Charge charge = getTestRequester().sendRequest(captureChargeRequest); + + assertRequested("POST", "/charges/" + CHARGE_ID + "/capture", 200); + assertRequestBody("{\"capture_amount\":100000}"); + assertEquals(CHARGE_ID, charge.getId()); + assertFalse(charge.isCapture()); + assertTrue(charge.isPaid()); + } + @Test public void testReverse() throws IOException, OmiseException { Request reverseChargeRequest = diff --git a/src/test/java/co/omise/requests/RequestTest.java b/src/test/java/co/omise/requests/RequestTest.java index bb985a6a..84a32dde 100644 --- a/src/test/java/co/omise/requests/RequestTest.java +++ b/src/test/java/co/omise/requests/RequestTest.java @@ -46,6 +46,7 @@ protected void assertRequestBody(String expectedRequestBody) { assertEquals(expectedRequestBody, actualRequestBody); } catch (final IOException e) { e.printStackTrace(); + fail("Error occurred."); } } diff --git a/src/test/resources/testdata/objects/charge_object.json b/src/test/resources/testdata/objects/charge_object.json index ffe02707..648ffe18 100644 --- a/src/test/resources/testdata/objects/charge_object.json +++ b/src/test/resources/testdata/objects/charge_object.json @@ -5,6 +5,7 @@ "location": "/charges/chrg_test_5086xlsx4lghk9bpb75", "amount": 100000, "currency": "thb", + "authorization_type": "pre_auth", "description": null, "capture": true, "authorized": true,