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
10 changes: 10 additions & 0 deletions src/main/java/co/omise/models/AuthorizationType.java
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 31 additions & 1 deletion src/main/java/co/omise/models/Charge.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ScopedList<Charge>> {
private ScopedList.Options options;

Expand Down Expand Up @@ -521,6 +530,8 @@ public static class CreateRequestBuilder extends RequestBuilder<Charge> {
private String returnUri;
@JsonProperty
private String source;
@JsonProperty("authorization_type")
private AuthorizationType authorizationType;

@Override
protected String method() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -765,10 +781,19 @@ public UpdateRequestBuilder metadata(String key, Object value) {

public static class CaptureRequestBuilder extends RequestBuilder<Charge> {
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;
Expand All @@ -783,6 +808,11 @@ protected HttpUrl path() {
protected ResponseType<Charge> type() {
return new ResponseType<>(Charge.class);
}

@Override
protected RequestBody payload() throws IOException {
return serialize();
}
}

public static class ExpireRequestBuilder extends RequestBuilder<Charge> {
Expand Down
43 changes: 42 additions & 1 deletion src/test/java/co/omise/live/LiveChargeRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Token> 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<Charge> 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<Charge> 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 {
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/co/omise/requests/ChargeRequestTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -65,6 +66,24 @@ public void testCreateChargeFromSource() throws IOException, OmiseException {
assertEquals(100000L, charge.getAmount());
}

@Test
public void testCreatePartialCaptureCharge() throws IOException, OmiseException {
Request<Charge> 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<Charge> updateChargeRequest =
Expand Down Expand Up @@ -92,6 +111,22 @@ public void testCapture() throws IOException, OmiseException {
assertTrue(charge.isPaid());
}

@Test
public void testPartialCapture() throws IOException, OmiseException {
Request<Charge> 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<Charge> reverseChargeRequest =
Expand Down
1 change: 1 addition & 0 deletions src/test/java/co/omise/requests/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected void assertRequestBody(String expectedRequestBody) {
assertEquals(expectedRequestBody, actualRequestBody);
} catch (final IOException e) {
e.printStackTrace();
fail("Error occurred.");
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/resources/testdata/objects/charge_object.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"location": "/charges/chrg_test_5086xlsx4lghk9bpb75",
"amount": 100000,
"currency": "thb",
"authorization_type": "pre_auth",
"description": null,
"capture": true,
"authorized": true,
Expand Down