From e76a8e5cccec866d2fa988026a6b5e5b0e21e3c6 Mon Sep 17 00:00:00 2001 From: tyler-techcombine Date: Thu, 2 Oct 2025 15:38:51 +0700 Subject: [PATCH 1/5] tas passkey support --- src/main/java/co/omise/Example.java | 38 ++++++++++++- .../co/omise/models/AuthenticationType.java | 10 ++++ src/main/java/co/omise/models/Charge.java | 29 +++++++++- .../co/omise/requests/ChargeRequestTest.java | 34 +++++++++++- .../fixtures/api.omise.co/charges-get.json | 2 + .../fixtures/api.omise.co/charges-post.json | 2 + .../charges/chrg_test_3dsu8b8xyl0d3s-get.json | 53 +++++++++++++++++++ .../chrg_test_4yq7duw15p9hdrjp8oq-get.json | 2 + .../chrg_test_4yq7duw15p9hdrjp8oq-patch.json | 2 + .../capture-post.json | 2 + .../reverse-post.json | 2 + .../charges/chrg_test_noauth-get.json | 36 +++++++++++++ .../testdata/objects/charge_object.json | 2 + 13 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 src/main/java/co/omise/models/AuthenticationType.java create mode 100644 src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_3dsu8b8xyl0d3s-get.json create mode 100644 src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_noauth-get.json diff --git a/src/main/java/co/omise/Example.java b/src/main/java/co/omise/Example.java index 8e93c572..12c450f3 100644 --- a/src/main/java/co/omise/Example.java +++ b/src/main/java/co/omise/Example.java @@ -9,6 +9,7 @@ final class Example { private static final String OMISE_SKEY = "skey_test_123"; + private static final String OMISE_PKEY = "pkey_test_123"; void retrieveAccount() throws IOException, OmiseException, ClientException { Request getAccountRequest = new Account.GetRequestBuilder().build(); @@ -90,6 +91,40 @@ void chargeWithCard() throws IOException, OmiseException, ClientException { System.out.printf("created charge: %s", charge.getId()); } + void chargeWithAuthentication(AuthenticationType authenticationType) + throws IOException, OmiseException, ClientException { + Request request = new Token.CreateRequestBuilder() + .card(new Card.Create() + .name("Somchai Prasert") + .number("5185600630000142") + .expirationMonth(10) + .expirationYear(2030) + .city("Bangkok") + .email("demo@example.co") + .phoneNumber("09434343444") + .postalCode("10320") + .securityCode("123")) + .build(); + Token token = client().sendRequest(request); + System.out.println("created token: " + token.getId()); + + Request createChargeRequest = + new Charge.CreateRequestBuilder() + .amount(100000) // 1,000 THB + .currency("thb") + .card(token.getId()) + .authentication(authenticationType) + .returnUri("https://example.com/orders/345678/complete") + .build(); + Charge charge = client().sendRequest(createChargeRequest); + + System.out.printf( + "created charge with %s authentication: %s and auth url: %s ", + authenticationType.name(), + charge.getId(), + charge.getAuthorizeUri()); + } + void chargeWithCustomer() throws IOException, OmiseException, ClientException { Request createChargeRequest = new Charge.CreateRequestBuilder() @@ -599,7 +634,8 @@ void getCapapabilities() throws ClientException, IOException, OmiseException { private Client client() throws ClientException { return new Client.Builder() + .publicKey(OMISE_PKEY) .secretKey(OMISE_SKEY) .build(); } -} \ No newline at end of file +} diff --git a/src/main/java/co/omise/models/AuthenticationType.java b/src/main/java/co/omise/models/AuthenticationType.java new file mode 100644 index 00000000..00c96432 --- /dev/null +++ b/src/main/java/co/omise/models/AuthenticationType.java @@ -0,0 +1,10 @@ +package co.omise.models; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public enum AuthenticationType { + @JsonProperty("3DS") + ThreeDS, + @JsonProperty("PASSKEY") + Passkey +} diff --git a/src/main/java/co/omise/models/Charge.java b/src/main/java/co/omise/models/Charge.java index 31e3db55..451be26c 100644 --- a/src/main/java/co/omise/models/Charge.java +++ b/src/main/java/co/omise/models/Charge.java @@ -24,6 +24,10 @@ public class Charge extends Model { @JsonProperty("authorize_uri") private String authorizeUri; private boolean authorized; + @JsonProperty("authentication") + private AuthenticationType authentication; + @JsonProperty("authenticated_by") + private String authenticatedBy; private String branch; private boolean capturable; private boolean capture; @@ -122,6 +126,22 @@ public void setAuthorized(boolean authorized) { this.authorized = authorized; } + public AuthenticationType getAuthentication() { + return authentication; + } + + public void setAuthentication(AuthenticationType authentication) { + this.authentication = authentication; + } + + public String getAuthenticatedBy() { + return authenticatedBy; + } + + public void setAuthenticatedBy(String authenticatedBy) { + this.authenticatedBy = authenticatedBy; + } + public String getBranch() { return this.branch; } @@ -579,6 +599,8 @@ public static class CreateRequestBuilder extends RequestBuilder { private String returnUri; @JsonProperty private String source; + @JsonProperty("authentication") + private AuthenticationType authentication; @JsonProperty("authorization_type") private AuthorizationType authorizationType; @JsonProperty("webhook_endpoints") @@ -677,6 +699,11 @@ public CreateRequestBuilder zeroInterestInstallments(boolean zeroInterestInstall return this; } + public CreateRequestBuilder authentication(AuthenticationType authentication) { + this.authentication = authentication; + return this; + } + public CreateRequestBuilder authorizationType(AuthorizationType authorizationType) { this.authorizationType = authorizationType; return this; @@ -971,4 +998,4 @@ protected ResponseType type() { return new ResponseType<>(Charge.class); } } -} \ No newline at end of file +} diff --git a/src/test/java/co/omise/requests/ChargeRequestTest.java b/src/test/java/co/omise/requests/ChargeRequestTest.java index 734285b0..13f26d8e 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.AuthenticationType; import co.omise.models.AuthorizationType; import co.omise.models.Barcode; import co.omise.models.Charge; @@ -13,6 +14,8 @@ public class ChargeRequestTest extends RequestTest { private final String CHARGE_ID = "chrg_test_4yq7duw15p9hdrjp8oq"; + private final String THREE_DS_CHARGE_ID = "chrg_test_3dsu8b8xyl0d3s"; + private final String NO_AUTH_CHARGE_ID = "chrg_test_noauth"; @Test public void testGet() throws IOException, OmiseException { @@ -27,9 +30,33 @@ public void testGet() throws IOException, OmiseException { assertEquals("trxn_test_4yq7duwb9jts1vxgqua", charge.getTransaction()); assertEquals("Test advice", charge.getMerchantAdvice()); assertEquals("Test advice code", charge.getMerchantAdviceCode()); + assertEquals(AuthenticationType.Passkey, charge.getAuthentication()); + assertEquals("PASSKEY", charge.getAuthenticatedBy()); assertEquals(Collections.singletonList("email"), charge.getMissing3DSFields()); } + @Test + public void testGetThreeDSCharge() throws IOException, OmiseException { + Request getChargeRequest = new Charge.GetRequestBuilder(THREE_DS_CHARGE_ID).build(); + + Charge charge = getTestRequester().sendRequest(getChargeRequest); + + assertRequested("GET", "/charges/" + THREE_DS_CHARGE_ID, 200); + assertEquals(AuthenticationType.ThreeDS, charge.getAuthentication()); + assertEquals("3DS", charge.getAuthenticatedBy()); + } + + @Test + public void testGetChargeWithoutAuthenticatedBy() throws IOException, OmiseException { + Request getChargeRequest = new Charge.GetRequestBuilder(NO_AUTH_CHARGE_ID).build(); + + Charge charge = getTestRequester().sendRequest(getChargeRequest); + + assertRequested("GET", "/charges/" + NO_AUTH_CHARGE_ID, 200); + assertEquals(AuthenticationType.Passkey, charge.getAuthentication()); + assertNull(charge.getAuthenticatedBy()); + } + @Test public void testCreate() throws IOException, OmiseException { Request createChargeRequest = @@ -47,6 +74,8 @@ public void testCreate() throws IOException, OmiseException { assertEquals(100000L, charge.getAmount()); assertEquals("thb", charge.getCurrency()); assertEquals("trxn_test_4yq7duwb9jts1vxgqua", charge.getTransaction()); + assertEquals(AuthenticationType.Passkey, charge.getAuthentication()); + assertEquals("PASSKEY", charge.getAuthenticatedBy()); } @Test @@ -61,7 +90,7 @@ public void testCreateWithWebhooks() 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\":null,\"authorization_type\":null,\"webhook_endpoints\":[\"https://webhook.site/123\"],\"first_charge\":null,\"linked_account\":null,\"recurring_reason\":null,\"transaction_indicator\":null}"); + 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,\"authentication\":null,\"authorization_type\":null,\"webhook_endpoints\":[\"https://webhook.site/123\"],\"first_charge\":null,\"linked_account\":null,\"recurring_reason\":null,\"transaction_indicator\":null}"); assertNotNull(charge); } @@ -93,6 +122,7 @@ public void testCreatePartialCaptureCharge() throws IOException, OmiseException .amount(100000) .currency("thb") .capture(false) + .authentication(AuthenticationType.Passkey) .authorizationType(AuthorizationType.PreAuth) .returnUri("http://example.com/orders/345678/complete") .firstCharge(CHARGE_ID) @@ -104,7 +134,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\",\"webhook_endpoints\":null,\"first_charge\":\"chrg_test_4yq7duw15p9hdrjp8oq\",\"linked_account\":\"acc_id\",\"recurring_reason\":\"reason\",\"transaction_indicator\":\"trans_id\"}"); + 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\",\"authentication\":\"PASSKEY\",\"authorization_type\":\"pre_auth\",\"webhook_endpoints\":null,\"first_charge\":\"chrg_test_4yq7duw15p9hdrjp8oq\",\"linked_account\":\"acc_id\",\"recurring_reason\":\"reason\",\"transaction_indicator\":\"trans_id\"}"); assertNotNull(charge); } diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges-get.json b/src/test/resources/testdata/fixtures/api.omise.co/charges-get.json index bdc0f299..400bb37a 100644 --- a/src/test/resources/testdata/fixtures/api.omise.co/charges-get.json +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges-get.json @@ -16,6 +16,8 @@ "description": "Charge for order 3947", "capture": true, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "transaction": "trxn_test_4yq7duwb9jts1vxgqua", "paid": true, "refunded_amount": 0, diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges-post.json b/src/test/resources/testdata/fixtures/api.omise.co/charges-post.json index 00544aed..8fdf2df8 100644 --- a/src/test/resources/testdata/fixtures/api.omise.co/charges-post.json +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges-post.json @@ -9,6 +9,8 @@ "status": "successful", "capture": true, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "transaction": "trxn_test_4yq7duwb9jts1vxgqua", "paid": true, "refunded_amount": 0, diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_3dsu8b8xyl0d3s-get.json b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_3dsu8b8xyl0d3s-get.json new file mode 100644 index 00000000..ea9163b9 --- /dev/null +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_3dsu8b8xyl0d3s-get.json @@ -0,0 +1,53 @@ +{ + "object": "charge", + "id": "chrg_test_3dsu8b8xyl0d3s", + "livemode": false, + "location": "/charges/chrg_test_3dsu8b8xyl0d3s", + "amount": 120000, + "currency": "thb", + "description": "Charge requiring 3DS", + "capture": true, + "authorized": true, + "authentication": "3DS", + "authenticated_by": "3DS", + "captured": true, + "transaction": "trxn_test_3dsz9q2u8k5c0l1p2", + "refunded_amount": 0, + "refunds": { + "object": "list", + "from": "1970-01-01T00:00:00+00:00", + "to": "2024-01-15T05:04:30+00:00", + "offset": 0, + "limit": 20, + "total": 0, + "data": [], + "location": "/charges/chrg_test_3dsu8b8xyl0d3s/refunds" + }, + "failure_code": null, + "failure_message": null, + "card": { + "object": "card", + "id": "card_test_3dsabcdefg12345", + "livemode": false, + "location": "/customers/cust_test_4yq6txdpfadhbaqnwp3/cards/card_test_3dsabcdefg12345", + "country": "", + "city": "Bangkok", + "postal_code": "10320", + "financing": "", + "last_digits": "5556", + "brand": "Visa", + "expiration_month": 12, + "expiration_year": 2026, + "fingerprint": "abcd1234efgh5678ijkl9012mnop3456qrst7890uvwx0123yzab4567cdef8901", + "name": "THREE DS CUSTOMER", + "security_code_check": true, + "created_at": "2024-01-10T04:03:40Z" + }, + "customer": "cust_test_4yq6txdpfadhbaqnwp3", + "ip": null, + "created_at": "2024-01-15T05:00:29Z", + "disputable": false, + "capturable": false, + "reversible": false, + "refundable": false +} diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-get.json b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-get.json index 345184f3..461f52e8 100644 --- a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-get.json +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-get.json @@ -8,6 +8,8 @@ "description": "Charge for order 3947", "capture": true, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "captured": true, "transaction": "trxn_test_4yq7duwb9jts1vxgqua", "refunded_amount": 10000, diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-patch.json b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-patch.json index c04319c7..50fcc777 100644 --- a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-patch.json +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq-patch.json @@ -8,6 +8,8 @@ "description": "Charge for order 3947 (XXL)", "capture": true, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "captured": true, "transaction": "trxn_test_4yq7duwb9jts1vxgqua", "refunded_amount": 10000, diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/capture-post.json b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/capture-post.json index 1358897b..45127130 100644 --- a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/capture-post.json +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/capture-post.json @@ -9,6 +9,8 @@ "status": "successful", "capture": false, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "reversed": false, "paid": true, "transaction": "trxn_test_4yq7duwb9jts1vxgqua", diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/reverse-post.json b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/reverse-post.json index 97f62665..6c62a356 100644 --- a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/reverse-post.json +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_4yq7duw15p9hdrjp8oq/reverse-post.json @@ -9,6 +9,8 @@ "status": "successful", "capture": false, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "reversed": true, "paid": true, "transaction": "trxn_test_4yq7duwb9jts1vxgqua", diff --git a/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_noauth-get.json b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_noauth-get.json new file mode 100644 index 00000000..619e1f9b --- /dev/null +++ b/src/test/resources/testdata/fixtures/api.omise.co/charges/chrg_test_noauth-get.json @@ -0,0 +1,36 @@ +{ + "object": "charge", + "id": "chrg_test_noauth", + "livemode": false, + "location": "/charges/chrg_test_noauth", + "amount": 50000, + "currency": "thb", + "description": "Charge without authenticated_by", + "capture": true, + "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": null, + "captured": true, + "transaction": null, + "refunded_amount": 0, + "refunds": { + "object": "list", + "from": "1970-01-01T00:00:00+00:00", + "to": "2024-01-15T05:04:30+00:00", + "offset": 0, + "limit": 20, + "total": 0, + "data": [], + "location": "/charges/chrg_test_noauth/refunds" + }, + "failure_code": null, + "failure_message": null, + "card": null, + "customer": null, + "ip": null, + "created_at": "2024-02-01T05:00:29Z", + "disputable": false, + "capturable": false, + "reversible": false, + "refundable": false +} diff --git a/src/test/resources/testdata/objects/charge_object.json b/src/test/resources/testdata/objects/charge_object.json index 15be0538..5b02e757 100644 --- a/src/test/resources/testdata/objects/charge_object.json +++ b/src/test/resources/testdata/objects/charge_object.json @@ -11,6 +11,8 @@ "description": null, "capture": true, "authorized": true, + "authentication": "PASSKEY", + "authenticated_by": "PASSKEY", "paid": true, "transaction": "trxn_test_5086xltqqbv4qpmu0ri", "refunded_amount": 0, From afdfbdab5844f062a58bb13c2bed83bf0958fb46 Mon Sep 17 00:00:00 2001 From: tyler-techcombine Date: Fri, 3 Oct 2025 10:46:06 +0700 Subject: [PATCH 2/5] add test for example class --- src/main/java/co/omise/Example.java | 13 ++ src/test/java/co/omise/ExampleTest.java | 267 ++++++++++++++++++++++++ 2 files changed, 280 insertions(+) create mode 100644 src/test/java/co/omise/ExampleTest.java diff --git a/src/main/java/co/omise/Example.java b/src/main/java/co/omise/Example.java index 12c450f3..4c43a68b 100644 --- a/src/main/java/co/omise/Example.java +++ b/src/main/java/co/omise/Example.java @@ -10,6 +10,15 @@ final class Example { private static final String OMISE_SKEY = "skey_test_123"; private static final String OMISE_PKEY = "pkey_test_123"; + private final Client testClient; + + Example() { + this(null); + } + + Example(Client testClient) { + this.testClient = testClient; + } void retrieveAccount() throws IOException, OmiseException, ClientException { Request getAccountRequest = new Account.GetRequestBuilder().build(); @@ -633,6 +642,10 @@ void getCapapabilities() throws ClientException, IOException, OmiseException { } private Client client() throws ClientException { + if (testClient != null) { + return testClient; + } + return new Client.Builder() .publicKey(OMISE_PKEY) .secretKey(OMISE_SKEY) diff --git a/src/test/java/co/omise/ExampleTest.java b/src/test/java/co/omise/ExampleTest.java new file mode 100644 index 00000000..23f16db8 --- /dev/null +++ b/src/test/java/co/omise/ExampleTest.java @@ -0,0 +1,267 @@ +package co.omise; + +import co.omise.models.*; +import co.omise.requests.Request; +import co.omise.requests.Requester; +import co.omise.requests.ResponseType; +import co.omise.models.schedules.Schedule; +import co.omise.models.schedules.Occurrence; +import okhttp3.OkHttpClient; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Collections; + +import static org.junit.Assert.assertNotNull; + +public class ExampleTest { + + @Test + public void coversAllExampleScenarios() throws Exception { + Client stubClient = new Client(new MockRequester()); + Example example = new Example(stubClient); + + PrintStream originalOut = System.out; + ByteArrayOutputStream sink = new ByteArrayOutputStream(); + System.setOut(new PrintStream(sink)); + try { + example.retrieveAccount(); + example.retrieveBalance(); + example.destroyCard(); + example.listCards(); + example.retrieveCard(); + example.updateCard(); + example.captureCharge(); + example.partialCaptureCharge(); + example.chargeWithCard(); + example.chargeWithAuthentication(AuthenticationType.ThreeDS); + example.chargeWithCustomer(); + example.chargeWithToken(); + example.createPartialCaptureCharge(); + example.listCharges(); + example.retrieveCharge(); + example.reverseCharge(); + example.updateCharge(); + example.attachCardToCustomer(); + example.createCustomerSimple(); + example.updateCustomer(); + example.destroyCustomer(); + example.listAllDisputes(); + example.listClosedDiputes(); + example.listOpenDiputes(); + example.listPendingDiputes(); + example.retrieveDispute(); + example.updateDispute(); + example.listEvents(); + example.retrieveEvent(); + example.retrieveCustomer(); + example.listCustomers(); + example.createTransfer(); + example.createTransferWithRecipient(); + example.destroyTransfer(); + example.listTransfers(); + example.retrieveTransfer(); + example.updateTransfer(); + example.createRecipient(); + example.destroyRecipient(); + example.listRecipients(); + example.retrieveRecipient(); + example.updateRecipient(); + example.createRefund(); + example.listRefunds(); + example.retrieveRefund(); + example.createToken(); + example.retrieveToken(); + example.listTransactions(); + example.retrieveTransaction(); + example.createLink(); + example.retrieveLink(); + example.listLinks(); + example.createSource(); + example.createSourceInstallment(); + example.retrieveSearch(); + example.retrieveSchedule(); + example.listSchedule(); + example.listChargeSchedule(); + example.listCustomerSchedule(); + example.listTransferSchedule(); + example.listRecipientSchedule(); + example.createSchedule(); + example.destroySchedule(); + example.retrieveOccurrence(); + example.listOccurrence(); + example.retrieveReceipt(); + example.listReceipt(); + example.getForex(); + example.getCapapabilities(); + } finally { + System.setOut(originalOut); + } + + Example realExample = new Example(); + Method clientMethod = Example.class.getDeclaredMethod("client"); + clientMethod.setAccessible(true); + Client actualClient = (Client) clientMethod.invoke(realExample); + assertNotNull(actualClient); + } + + private static final class MockRequester implements Requester { + private final OkHttpClient httpClient = new OkHttpClient(); + + @Override + public > T sendRequest(R request) { + ResponseType responseType = request.getType(); + + if (responseType.isClassType()) { + return responseType.getClassType().cast(createStubForClass(responseType.getClassType())); + } + + if (responseType.isTypeReference()) { + return castFromType(responseType.getTypeReference().getType()); + } + + throw new UnsupportedOperationException("Unhandled response type: " + responseType); + } + + @SuppressWarnings("unchecked") + private T castFromType(Type type) { + Object stub = createStubForType(type); + return (T) stub; + } + + private OmiseObjectBase createStubForClass(Class clazz) { + if (clazz.equals(Account.class)) { + return withId(new Account(), "acct_test"); + } + if (clazz.equals(Balance.class)) { + Balance balance = withId(new Balance(), "bal_test"); + balance.setTransferable(12345L); + return balance; + } + if (clazz.equals(Card.class)) { + Card card = withId(new Card(), "card_test"); + card.setLastDigits("4242"); + return card; + } + if (clazz.equals(Charge.class)) { + Charge charge = withId(new Charge(), "chrg_test"); + charge.setAmount(1000L); + charge.setDescription("desc"); + charge.setReversed(true); + charge.setAuthorizeUri("https://example.com/auth"); + return charge; + } + if (clazz.equals(Token.class)) { + Token token = withId(new Token(), "tokn_test"); + Card card = withId(new Card(), "card_token"); + card.setLastDigits("1111"); + token.setCard(card); + return token; + } + if (clazz.equals(Customer.class)) { + Customer customer = withId(new Customer(), "cust_test"); + customer.setEmail("user@example.com"); + return customer; + } + if (clazz.equals(Dispute.class)) { + Dispute dispute = withId(new Dispute(), "dspt_test"); + dispute.setAmount(5000L); + dispute.setMessage("message"); + return dispute; + } + if (clazz.equals(Event.class)) { + Event event = withId(new Event(), "evnt_test"); + event.setKey("charge.completed"); + return event; + } + if (clazz.equals(Transfer.class)) { + Transfer transfer = withId(new Transfer(), "trsf_test"); + transfer.setAmount(2000L); + return transfer; + } + if (clazz.equals(Recipient.class)) { + Recipient recipient = withId(new Recipient(), "recp_test"); + recipient.setEmail("recipient@example.com"); + return recipient; + } + if (clazz.equals(Refund.class)) { + Refund refund = withId(new Refund(), "rfnd_test"); + refund.setAmount(3000L); + return refund; + } + if (clazz.equals(Transaction.class)) { + Transaction transaction = withId(new Transaction(), "trxn_test"); + transaction.setAmount(4000L); + return transaction; + } + if (clazz.equals(Link.class)) { + return withId(new Link(), "link_test"); + } + if (clazz.equals(Source.class)) { + return withId(new Source(), "src_test"); + } + if (clazz.equals(Schedule.class)) { + return withId(new Schedule(), "schd_test"); + } + if (clazz.equals(Occurrence.class)) { + return withId(new Occurrence(), "occu_test"); + } + if (clazz.equals(Receipt.class)) { + return withId(new Receipt(), "rcpt_test"); + } + if (clazz.equals(Forex.class)) { + Forex forex = withId(new Forex(), "forx_test"); + forex.setRate(1.23d); + return forex; + } + if (clazz.equals(Capability.class)) { + Capability capability = withId(new Capability(), "cap_test"); + capability.setZeroInterestInstallments(true); + return capability; + } + + throw new UnsupportedOperationException("Unhandled class: " + clazz.getName()); + } + + private OmiseObjectBase createStubForType(Type type) { + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + Type rawType = parameterizedType.getRawType(); + + if (rawType == ScopedList.class) { + Type elementType = parameterizedType.getActualTypeArguments()[0]; + Class element = elementType instanceof Class + ? (Class) elementType + : Charge.class; + ScopedList scopedList = new ScopedList<>(); + scopedList.setTotal(1); + scopedList.setData(Collections.singletonList((Model) createStubForClass(element))); + return scopedList; + } + + if (rawType == SearchResult.class) { + SearchResult searchResult = new SearchResult<>(); + searchResult.setTotal(1); + searchResult.setData(Collections.singletonList((Model) createStubForClass(Charge.class))); + return searchResult; + } + } + + throw new UnsupportedOperationException("Unhandled type: " + type.getTypeName()); + } + + private T withId(T model, String id) { + model.setId(id); + return model; + } + + @Override + public OkHttpClient getHttpClient() { + return httpClient; + } + } +} From 395c0ef7ac79e1669466c728f195e2210c7ce42f Mon Sep 17 00:00:00 2001 From: tyler-techcombine Date: Fri, 3 Oct 2025 11:18:50 +0700 Subject: [PATCH 3/5] fix sonar warnings --- src/main/java/co/omise/Example.java | 166 ++++++++++-------- .../co/omise/models/AuthenticationType.java | 4 +- src/test/java/co/omise/ExampleTest.java | 2 +- .../co/omise/requests/ChargeRequestTest.java | 22 +-- 4 files changed, 103 insertions(+), 91 deletions(-) diff --git a/src/main/java/co/omise/Example.java b/src/main/java/co/omise/Example.java index 4c43a68b..120a5daa 100644 --- a/src/main/java/co/omise/Example.java +++ b/src/main/java/co/omise/Example.java @@ -6,12 +6,24 @@ import java.time.LocalDate; import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; final class Example { + private static final Logger LOGGER = Logger.getLogger(Example.class.getName()); private static final String OMISE_SKEY = "skey_test_123"; private static final String OMISE_PKEY = "pkey_test_123"; + private static final String CREATED_TOKEN_LOG_PREFIX = "created token: "; private final Client testClient; + private static void logInfo(String template, Object... args) { + LOGGER.log(Level.INFO, () -> String.format(template, args)); + } + + private static void logInfoMessage(String message) { + LOGGER.log(Level.INFO, message); + } + Example() { this(null); } @@ -23,13 +35,13 @@ final class Example { void retrieveAccount() throws IOException, OmiseException, ClientException { Request getAccountRequest = new Account.GetRequestBuilder().build(); Account account = client().sendRequest(getAccountRequest); - System.out.printf("account id: %s", account.getId()); + logInfo("account id: %s", account.getId()); } void retrieveBalance() throws IOException, OmiseException, ClientException { Request getBalanceRequest = new Balance.GetRequestBuilder().build(); Balance balance = client().sendRequest(getBalanceRequest); - System.out.printf("transferable balance: %d", balance.getTransferable()); + logInfo("transferable balance: %d", balance.getTransferable()); } void destroyCard() throws IOException, OmiseException, ClientException { @@ -37,15 +49,15 @@ void destroyCard() throws IOException, OmiseException, ClientException { "card_test_4xsjw0t21xaxnuzi9gs", "cust_test_4xsjvylia03ur542vn6") .build(); Card card = client().sendRequest(request); - System.out.printf("destroyed card: %s", card.getId()); + logInfo("destroyed card: %s", card.getId()); } void listCards() throws IOException, OmiseException, ClientException { Request> request = new Card.ListRequestBuilder("cust_test_4xsjvylia03ur542vn6").build(); ScopedList cards = client().sendRequest(request); - System.out.printf("returned cards: %d", cards.getData().size()); - System.out.printf("total no. of cards: %d", cards.getTotal()); + logInfo("returned cards: %d", cards.getData().size()); + logInfo("total no. of cards: %d", cards.getTotal()); } void retrieveCard() throws IOException, OmiseException, ClientException { @@ -53,7 +65,7 @@ void retrieveCard() throws IOException, OmiseException, ClientException { new Card.GetRequestBuilder("card_test_4xsjw0t21xaxnuzi9gs", "cust_test_4xsjvylia03ur542vn6") .build(); Card card = client().sendRequest(request); - System.out.printf("card last digits: %s", card.getLastDigits()); + logInfo("card last digits: %s", card.getLastDigits()); } void updateCard() throws IOException, OmiseException, ClientException { @@ -66,7 +78,7 @@ void updateCard() throws IOException, OmiseException, ClientException { .postalCode("10310") .build(); Card card = client().sendRequest(request); - System.out.printf("updated card: %s", card.getId()); + logInfo("updated card: %s", card.getId()); } void captureCharge() throws IOException, OmiseException, ClientException { @@ -75,7 +87,7 @@ void captureCharge() throws IOException, OmiseException, ClientException { .build(); Charge charge = client().sendRequest(captureChargeRequest); - System.out.printf("captured charge: %s", charge.getId()); + logInfo("captured charge: %s", charge.getId()); } void partialCaptureCharge() throws IOException, OmiseException, ClientException { @@ -85,7 +97,7 @@ void partialCaptureCharge() throws IOException, OmiseException, ClientException .build(); Charge charge = client().sendRequest(captureChargeRequest); - System.out.printf("captured charge: %s", charge.getId()); + logInfo("captured charge: %s", charge.getId()); } void chargeWithCard() throws IOException, OmiseException, ClientException { @@ -97,7 +109,7 @@ void chargeWithCard() throws IOException, OmiseException, ClientException { .card("card_test_4xtsoy2nbfs7ujngyyq") .build(); Charge charge = client().sendRequest(createChargeRequest); - System.out.printf("created charge: %s", charge.getId()); + logInfo("created charge: %s", charge.getId()); } void chargeWithAuthentication(AuthenticationType authenticationType) @@ -115,7 +127,7 @@ void chargeWithAuthentication(AuthenticationType authenticationType) .securityCode("123")) .build(); Token token = client().sendRequest(request); - System.out.println("created token: " + token.getId()); + logInfoMessage(CREATED_TOKEN_LOG_PREFIX + token.getId()); Request createChargeRequest = new Charge.CreateRequestBuilder() @@ -127,7 +139,7 @@ void chargeWithAuthentication(AuthenticationType authenticationType) .build(); Charge charge = client().sendRequest(createChargeRequest); - System.out.printf( + logInfo( "created charge with %s authentication: %s and auth url: %s ", authenticationType.name(), charge.getId(), @@ -143,7 +155,7 @@ void chargeWithCustomer() throws IOException, OmiseException, ClientException { .build(); Charge charge = client().sendRequest(createChargeRequest); - System.out.printf("created charge: %s", charge.getId()); + logInfo("created charge: %s", charge.getId()); } void chargeWithToken() throws IOException, OmiseException, ClientException { @@ -159,7 +171,7 @@ void chargeWithToken() throws IOException, OmiseException, ClientException { .build(); Token token = client().sendRequest(request); - System.out.println("created token: " + token.getId()); + logInfoMessage(CREATED_TOKEN_LOG_PREFIX + token.getId()); Request createChargeRequest = new Charge.CreateRequestBuilder() @@ -169,7 +181,7 @@ void chargeWithToken() throws IOException, OmiseException, ClientException { .build(); Charge charge = client().sendRequest(createChargeRequest); - System.out.printf("created charge: %s", charge.getId()); + logInfo("created charge: %s", charge.getId()); } void createPartialCaptureCharge() throws IOException, OmiseException, ClientException { @@ -185,7 +197,7 @@ void createPartialCaptureCharge() throws IOException, OmiseException, ClientExce .build(); Token token = client().sendRequest(request); - System.out.println("created token: " + token.getId()); + logInfoMessage(CREATED_TOKEN_LOG_PREFIX + token.getId()); Request createChargeRequest = new Charge.CreateRequestBuilder() @@ -197,22 +209,22 @@ void createPartialCaptureCharge() throws IOException, OmiseException, ClientExce .build(); Charge charge = client().sendRequest(createChargeRequest); - System.out.printf("created charge: %s", charge.getId()); + logInfo("created charge: %s", charge.getId()); } void listCharges() throws IOException, OmiseException, ClientException { Request> listChargeRequest = new Charge.ListRequestBuilder().build(); ScopedList charges = client().sendRequest(listChargeRequest); - System.out.printf("returned charges: %d", charges.getData().size()); - System.out.printf("total no. of charges: %d", charges.getTotal()); + logInfo("returned charges: %d", charges.getData().size()); + logInfo("total no. of charges: %d", charges.getTotal()); } void retrieveCharge() throws IOException, OmiseException, ClientException { Request getChargeRequest = new Charge.GetRequestBuilder("chrg_test_4xso2s8ivdej29pqnhz").build(); Charge charge = client().sendRequest(getChargeRequest); - System.out.printf("charge amount: %d", charge.getAmount()); + logInfo("charge amount: %d", charge.getAmount()); } void reverseCharge() throws IOException, OmiseException, ClientException { @@ -220,7 +232,7 @@ void reverseCharge() throws IOException, OmiseException, ClientException { new Charge.ReverseRequestBuilder("chrg_test_4xso2s8ivdej29pqnhz").build(); Charge charge = client().sendRequest(reverseChargeRequest); - System.out.printf("charge reversal: %s", charge.isReversed()); + logInfo("charge reversal: %s", charge.isReversed()); } void updateCharge() throws IOException, OmiseException, ClientException { @@ -231,7 +243,7 @@ void updateCharge() throws IOException, OmiseException, ClientException { Charge charge = client().sendRequest(updateChargeRequest); - System.out.printf("updated description: %s", charge.getDescription()); + logInfo("updated description: %s", charge.getDescription()); } void attachCardToCustomer() throws IOException, OmiseException, ClientException { @@ -239,7 +251,7 @@ void attachCardToCustomer() throws IOException, OmiseException, ClientException .card("tokn_test_4xs9408a642a1htto8z") .build(); Customer customer = client().sendRequest(request); - System.out.printf("updated customer: %s", customer.getId()); + logInfo("updated customer: %s", customer.getId()); } void createCustomerSimple() throws IOException, OmiseException, ClientException { @@ -248,7 +260,7 @@ void createCustomerSimple() throws IOException, OmiseException, ClientException .description("John Doe (id: 30)") .build(); Customer customer = client().sendRequest(request); - System.out.printf("created customer: %s", customer.getId()); + logInfo("created customer: %s", customer.getId()); } void updateCustomer() throws IOException, OmiseException, ClientException { @@ -257,43 +269,43 @@ void updateCustomer() throws IOException, OmiseException, ClientException { .description("Another description") .build(); Customer customer = client().sendRequest(request); - System.out.printf("updated email: %s", customer.getEmail()); + logInfo("updated email: %s", customer.getEmail()); } void destroyCustomer() throws IOException, OmiseException, ClientException { Request request = new Customer.DeleteRequestBuilder("cust_test_4xtrb759599jsxlhkrb").build(); Customer customer = client().sendRequest(request); - System.out.printf("destroy customer: %s", customer.getId()); + logInfo("destroy customer: %s", customer.getId()); } void listAllDisputes() throws IOException, OmiseException, ClientException { Request> request = new Dispute.ListRequestBuilder().build(); ScopedList disputes = client().sendRequest(request); - System.out.printf("total no. of disputes: %d", disputes.getTotal()); + logInfo("total no. of disputes: %d", disputes.getTotal()); } void listClosedDiputes() throws IOException, OmiseException, ClientException { Request> request = new Dispute.ListRequestBuilder().status(DisputeStatus.Closed).build(); ScopedList disputes = client().sendRequest(request); - System.out.printf("closed disputes: %d", disputes.getTotal()); + logInfo("closed disputes: %d", disputes.getTotal()); } void listOpenDiputes() throws IOException, OmiseException, ClientException { Request> request = new Dispute.ListRequestBuilder().status(DisputeStatus.Open).build(); ScopedList disputes = client().sendRequest(request); - System.out.printf("open disputes: %d", disputes.getTotal()); + logInfo("open disputes: %d", disputes.getTotal()); } void listPendingDiputes() throws IOException, OmiseException, ClientException { Request> request = new Dispute.ListRequestBuilder().status(DisputeStatus.Pending).build(); ScopedList disputes = client().sendRequest(request); - System.out.printf("pending disputes: %d", disputes.getTotal()); + logInfo("pending disputes: %d", disputes.getTotal()); } void retrieveDispute() throws IOException, OmiseException, ClientException { Request request = new Dispute.GetRequestBuilder("dspt_test_4zgf15h89w8t775kcm8").build(); Dispute dispute = client().sendRequest(request); - System.out.printf("disputed amount: %d", dispute.getAmount()); + logInfo("disputed amount: %d", dispute.getAmount()); } void updateDispute() throws IOException, OmiseException, ClientException { @@ -301,32 +313,32 @@ void updateDispute() throws IOException, OmiseException, ClientException { .message("Proofs and other information...") .build(); Dispute dispute = client().sendRequest(request); - System.out.printf("updated dispute: %s", dispute.getMessage()); + logInfo("updated dispute: %s", dispute.getMessage()); } void listEvents() throws IOException, OmiseException, ClientException { Request> request = new Event.ListRequestBuilder().build(); ScopedList events = client().sendRequest(request); - System.out.printf("total no. of events: %d", events.getTotal()); + logInfo("total no. of events: %d", events.getTotal()); } void retrieveEvent() throws IOException, OmiseException, ClientException { Request request = new Event.GetRequestBuilder("evnt_test_5vxs0ajpo78").build(); Event event = client().sendRequest(request); - System.out.printf("key of event: %s", event.getKey()); + logInfo("key of event: %s", event.getKey()); } void retrieveCustomer() throws IOException, OmiseException, ClientException { Request request = new Customer.GetRequestBuilder("cust_test_4xtrb759599jsxlhkrb").build(); Customer customer = client().sendRequest(request); - System.out.printf("customer email: %s", customer.getEmail()); + logInfo("customer email: %s", customer.getEmail()); } void listCustomers() throws IOException, OmiseException, ClientException { Request> request = new Customer.ListRequestBuilder().build(); ScopedList customers = client().sendRequest(request); - System.out.printf("returned customers: %d", customers.getData().size()); - System.out.printf("total no. of customers: %d", customers.getTotal()); + logInfo("returned customers: %d", customers.getData().size()); + logInfo("total no. of customers: %d", customers.getTotal()); } void createTransfer() throws IOException, OmiseException, ClientException { @@ -334,7 +346,7 @@ void createTransfer() throws IOException, OmiseException, ClientException { .amount(100000) .build(); Transfer transfer = client().sendRequest(request); - System.out.printf("created transfer: %s", transfer.getId()); + logInfo("created transfer: %s", transfer.getId()); } void createTransferWithRecipient() throws IOException, OmiseException, ClientException { @@ -343,29 +355,29 @@ void createTransferWithRecipient() throws IOException, OmiseException, ClientExc .recipient("recp_test_4z6p7e0m4k40txecj5o") .build(); Transfer transfer = client().sendRequest(request); - System.out.printf("created transfer: %s", transfer.getId()); + logInfo("created transfer: %s", transfer.getId()); } void destroyTransfer() throws IOException, OmiseException, ClientException { Request request = new Transfer.DeleteRequestBuilder("trsf_test_4xs5px8c36dsanuwztf") .build(); Transfer transfer = client().sendRequest(request); - System.out.printf("destroyed transfer: %s", transfer.getId()); + logInfo("destroyed transfer: %s", transfer.getId()); } void listTransfers() throws IOException, OmiseException, ClientException { Request> request = new Transfer.ListRequestBuilder() .build(); ScopedList transfers = client().sendRequest(request); - System.out.printf("returned transfers: %d", transfers.getData().size()); - System.out.printf("total no. of transfers: %d", transfers.getTotal()); + logInfo("returned transfers: %d", transfers.getData().size()); + logInfo("total no. of transfers: %d", transfers.getTotal()); } void retrieveTransfer() throws IOException, OmiseException, ClientException { Request request = new Transfer.GetRequestBuilder("trsf_test_4xs5px8c36dsanuwztf") .build(); Transfer transfer = client().sendRequest(request); - System.out.printf("transfer amount: %d", transfer.getAmount()); + logInfo("transfer amount: %d", transfer.getAmount()); } void updateTransfer() throws IOException, OmiseException, ClientException { @@ -373,7 +385,7 @@ void updateTransfer() throws IOException, OmiseException, ClientException { .amount(100000) .build(); Transfer transfer = client().sendRequest(request); - System.out.printf("transfer amount: %d", transfer.getAmount()); + logInfo("transfer amount: %d", transfer.getAmount()); } void createRecipient() throws IOException, OmiseException, ClientException { @@ -387,26 +399,26 @@ void createRecipient() throws IOException, OmiseException, ClientException { .name("SOMCHAI PRASErT")) .build(); Recipient recipient = client().sendRequest(request); - System.out.printf("created recipient: %s", recipient.getId()); + logInfo("created recipient: %s", recipient.getId()); } void destroyRecipient() throws IOException, OmiseException, ClientException { Request request = new Recipient.DeleteRequestBuilder("recp_test_4z6p7e0m4k40txecj5o").build(); Recipient recipient = client().sendRequest(request); - System.out.printf("destroyed recipient: %s", recipient.getId()); + logInfo("destroyed recipient: %s", recipient.getId()); } void listRecipients() throws IOException, OmiseException, ClientException { Request> request = new Recipient.ListRequestBuilder().build(); ScopedList recipients = client().sendRequest(request); - System.out.printf("returned recipients: %d", recipients.getData().size()); - System.out.printf("total no. of recipients: %d", recipients.getTotal()); + logInfo("returned recipients: %d", recipients.getData().size()); + logInfo("total no. of recipients: %d", recipients.getTotal()); } void retrieveRecipient() throws IOException, OmiseException, ClientException { Request request = new Recipient.GetRequestBuilder("recp_test_4z6p7e0m4k40txecj5o").build(); Recipient recipient = client().sendRequest(request); - System.out.printf("recipient's email: %s", recipient.getEmail()); + logInfo("recipient's email: %s", recipient.getEmail()); } void updateRecipient() throws IOException, OmiseException, ClientException { @@ -418,7 +430,7 @@ void updateRecipient() throws IOException, OmiseException, ClientException { .name("SOMCHAI PRASERT")) .build(); Recipient recipient = client().sendRequest(request); - System.out.printf("updated recipient: %s", recipient.getId()); + logInfo("updated recipient: %s", recipient.getId()); } void createRefund() throws IOException, OmiseException, ClientException { @@ -426,19 +438,19 @@ void createRefund() throws IOException, OmiseException, ClientException { .amount(10000) .build(); Refund refund = client().sendRequest(request); - System.out.printf("created refund: %s", refund.getId()); + logInfo("created refund: %s", refund.getId()); } void listRefunds() throws IOException, OmiseException, ClientException { Request> request = new Refund.ListRequestBuilder("chrg_test_4xso2s8ivdej29pqnhz").build(); ScopedList refunds = client().sendRequest(request); - System.out.printf("total no. of refunds: %d", refunds.getTotal()); + logInfo("total no. of refunds: %d", refunds.getTotal()); } void retrieveRefund() throws IOException, OmiseException, ClientException { Request request = new Refund.GetRequestBuilder("chrg_test_4xso2s8ivdej29pqnhz", "rfnd_test_4ypebtxon6oye5o8myu").build(); Refund refund = client().sendRequest(request); - System.out.printf("refunded amount: %d", refund.getAmount()); + logInfo("refunded amount: %d", refund.getAmount()); } void createToken() throws IOException, OmiseException, ClientException { @@ -453,25 +465,25 @@ void createToken() throws IOException, OmiseException, ClientException { .securityCode("123")) .build(); Token token = client().sendRequest(request); - System.out.printf("created token: %s", token.getId()); + logInfo("created token: %s", token.getId()); } void retrieveToken() throws IOException, OmiseException, ClientException { Request request = new Token.GetRequestBuilder("tokn_test_4xs9408a642a1htto8z").build(); Token token = client().sendRequest(request); - System.out.printf("token last digits: %s", token.getCard().getLastDigits()); + logInfo("token last digits: %s", token.getCard().getLastDigits()); } void listTransactions() throws IOException, OmiseException, ClientException { Request> request = new Transaction.ListRequestBuilder().build(); ScopedList transactions = client().sendRequest(request); - System.out.printf("total no. of transactions: %d", transactions.getTotal()); + logInfo("total no. of transactions: %d", transactions.getTotal()); } void retrieveTransaction() throws IOException, OmiseException, ClientException { Request request = new Transaction.GetRequestBuilder("trxn_test_4xuy2z4w5vmvq4x5pfs").build(); Transaction transaction = client().sendRequest(request); - System.out.printf("transaction amount: %d", transaction.getAmount()); + logInfo("transaction amount: %d", transaction.getAmount()); } void createLink() throws IOException, OmiseException, ClientException { @@ -484,21 +496,21 @@ void createLink() throws IOException, OmiseException, ClientException { .build(); Link link = client().sendRequest(request); - System.out.printf("link created: %s", link.getId()); + logInfo("link created: %s", link.getId()); } void retrieveLink() throws IOException, OmiseException, ClientException { Request request = new Link.GetRequestBuilder("link_test_6csdepgdsdob7ee47sf").build(); Link link = client().sendRequest(request); - System.out.printf("link retrieved: %s", link.getId()); + logInfo("link retrieved: %s", link.getId()); } void listLinks() throws IOException, OmiseException, ClientException { Request> request = new Link.ListRequestBuilder().build(); ScopedList links = client().sendRequest(request); - System.out.printf("total no. of links: %d", links.getTotal()); + logInfo("total no. of links: %d", links.getTotal()); } void createSource() throws IOException, OmiseException, ClientException { @@ -512,7 +524,7 @@ void createSource() throws IOException, OmiseException, ClientException { .build(); Source source = client().sendRequest(request); - System.out.printf("source created: %s", source.getId()); + logInfo("source created: %s", source.getId()); } void createSourceInstallment() throws IOException, ClientException, OmiseException { @@ -524,7 +536,7 @@ void createSourceInstallment() throws IOException, ClientException, OmiseExcepti .build(); Source source = client().sendRequest(request); - System.out.printf("source created: %s", source.getId()); + logInfo("source created: %s", source.getId()); } void retrieveSearch() throws ClientException, IOException, OmiseException { @@ -534,49 +546,49 @@ void retrieveSearch() throws ClientException, IOException, OmiseException { .query("chrg_test_4xso2s8ivdej29pqnhz")) .build(); SearchResult searchResult = client().sendRequest(request); - System.out.printf("total no. of search result: %d", searchResult.getTotal()); + logInfo("total no. of search result: %d", searchResult.getTotal()); } void retrieveSchedule() throws IOException, ClientException, OmiseException { Request request = new Schedule.GetRequestBuilder("schd_test_57wedy7pc6v9i59xpbx").build(); Schedule schedule = client().sendRequest(request); - System.out.printf("schedule retrieved: %s", schedule.getId()); + logInfo("schedule retrieved: %s", schedule.getId()); } void listSchedule() throws IOException, ClientException, OmiseException { Request> request = new Schedule.ListRequestBuilder().build(); ScopedList schedules = client().sendRequest(request); - System.out.printf("total no. of schedules: %d", schedules.getTotal()); + logInfo("total no. of schedules: %d", schedules.getTotal()); } void listChargeSchedule() throws IOException, ClientException, OmiseException { Request> request = new Charge.ListSchedulesRequestBuilder().build(); ScopedList schedules = client().sendRequest(request); - System.out.printf("total no. of charge schedules: %d", schedules.getTotal()); + logInfo("total no. of charge schedules: %d", schedules.getTotal()); } void listCustomerSchedule() throws IOException, ClientException, OmiseException { Request> request = new Schedule.CustomerScheduleListRequestBuilder("cust_test_4yq6txdpfadhbaqnwp3").build(); ScopedList schedules = client().sendRequest(request); - System.out.printf("total no. of customer schedules: %d", schedules.getTotal()); + logInfo("total no. of customer schedules: %d", schedules.getTotal()); } void listTransferSchedule() throws IOException, ClientException, OmiseException { Request> request = new Schedule.TransferScheduleListRequestBuilder().build(); ScopedList schedules = client().sendRequest(request); - System.out.printf("total no. of transfer schedules: %d", schedules.getTotal()); + logInfo("total no. of transfer schedules: %d", schedules.getTotal()); } void listRecipientSchedule() throws IOException, ClientException, OmiseException { Request> request = new Schedule.RecipientScheduleListRequestBuilder("recp_test_50894vc13y8z4v51iuc").build(); ScopedList schedules = client().sendRequest(request); - System.out.printf("total no. of recipient schedules: %d", schedules.getTotal()); + logInfo("total no. of recipient schedules: %d", schedules.getTotal()); } void createSchedule() throws ClientException, IOException, OmiseException { @@ -593,52 +605,52 @@ void createSchedule() throws ClientException, IOException, OmiseException { .build(); Schedule schedule = client().sendRequest(request); - System.out.printf("schedule created: %s", schedule.getId()); + logInfo("schedule created: %s", schedule.getId()); } void destroySchedule() throws ClientException, IOException, OmiseException { Request request = new Schedule.DeleteRequestBuilder("schd_test_57s33hm9fg1pzcqihxs").build(); Schedule schedule = client().sendRequest(request); - System.out.printf("destroyed schedule: %s", schedule.getId()); + logInfo("destroyed schedule: %s", schedule.getId()); } void retrieveOccurrence() throws IOException, ClientException, OmiseException { Request request = new Occurrence.GetRequestBuilder("occu_test_59wupnlrayrqccw6lob").build(); Occurrence occurrence = client().sendRequest(request); - System.out.printf("occurrence retrieved: %s", occurrence.getId()); + logInfo("occurrence retrieved: %s", occurrence.getId()); } void listOccurrence() throws IOException, ClientException, OmiseException { Request> request = new Occurrence.ListRequestBuilder("schd_test_59wupnlq9lej6bi12i8").build(); ScopedList occurrences = client().sendRequest(request); - System.out.printf("total no. of occurrences: %d", occurrences.getTotal()); + logInfo("total no. of occurrences: %d", occurrences.getTotal()); } void retrieveReceipt() throws ClientException, IOException, OmiseException { Request request = new Receipt.GetRequestBuilder("rcpt_59lezici7p7gt85hfwr").build(); Receipt receipt = client().sendRequest(request); - System.out.printf("retrieved receipt: %s", receipt.getId()); + logInfo("retrieved receipt: %s", receipt.getId()); } void listReceipt() throws ClientException, IOException, OmiseException { Request> request = new Receipt.ListRequestBuilder().build(); ScopedList receipts = client().sendRequest(request); - System.out.printf("total no. of receipts: %d", receipts.getTotal()); + logInfo("total no. of receipts: %d", receipts.getTotal()); } void getForex() throws ClientException, IOException, OmiseException { Request request = new Forex.GetRequestBuilder("usd").build(); Forex forex = client().sendRequest(request); - System.out.printf("forex rate: %f", forex.getRate()); + logInfo("forex rate: %f", forex.getRate()); } void getCapapabilities() throws ClientException, IOException, OmiseException { Request request = new Capability.GetRequestBuilder().build(); Capability capability = client().sendRequest(request); - System.out.printf("capability isZeroInterestInstallments flag: %b", capability.isZeroInterestInstallments()); + logInfo("capability isZeroInterestInstallments flag: %b", capability.isZeroInterestInstallments()); } private Client client() throws ClientException { diff --git a/src/main/java/co/omise/models/AuthenticationType.java b/src/main/java/co/omise/models/AuthenticationType.java index 00c96432..7859b6ef 100644 --- a/src/main/java/co/omise/models/AuthenticationType.java +++ b/src/main/java/co/omise/models/AuthenticationType.java @@ -4,7 +4,7 @@ public enum AuthenticationType { @JsonProperty("3DS") - ThreeDS, + THREE_DS, @JsonProperty("PASSKEY") - Passkey + PASSKEY } diff --git a/src/test/java/co/omise/ExampleTest.java b/src/test/java/co/omise/ExampleTest.java index 23f16db8..a4408052 100644 --- a/src/test/java/co/omise/ExampleTest.java +++ b/src/test/java/co/omise/ExampleTest.java @@ -38,7 +38,7 @@ public void coversAllExampleScenarios() throws Exception { example.captureCharge(); example.partialCaptureCharge(); example.chargeWithCard(); - example.chargeWithAuthentication(AuthenticationType.ThreeDS); + example.chargeWithAuthentication(AuthenticationType.THREE_DS); example.chargeWithCustomer(); example.chargeWithToken(); example.createPartialCaptureCharge(); diff --git a/src/test/java/co/omise/requests/ChargeRequestTest.java b/src/test/java/co/omise/requests/ChargeRequestTest.java index 13f26d8e..19575faa 100644 --- a/src/test/java/co/omise/requests/ChargeRequestTest.java +++ b/src/test/java/co/omise/requests/ChargeRequestTest.java @@ -14,8 +14,8 @@ public class ChargeRequestTest extends RequestTest { private final String CHARGE_ID = "chrg_test_4yq7duw15p9hdrjp8oq"; - private final String THREE_DS_CHARGE_ID = "chrg_test_3dsu8b8xyl0d3s"; - private final String NO_AUTH_CHARGE_ID = "chrg_test_noauth"; + private final String threeDsChargeId = "chrg_test_3dsu8b8xyl0d3s"; + private final String noAuthChargeId = "chrg_test_noauth"; @Test public void testGet() throws IOException, OmiseException { @@ -30,30 +30,30 @@ public void testGet() throws IOException, OmiseException { assertEquals("trxn_test_4yq7duwb9jts1vxgqua", charge.getTransaction()); assertEquals("Test advice", charge.getMerchantAdvice()); assertEquals("Test advice code", charge.getMerchantAdviceCode()); - assertEquals(AuthenticationType.Passkey, charge.getAuthentication()); + assertEquals(AuthenticationType.PASSKEY, charge.getAuthentication()); assertEquals("PASSKEY", charge.getAuthenticatedBy()); assertEquals(Collections.singletonList("email"), charge.getMissing3DSFields()); } @Test public void testGetThreeDSCharge() throws IOException, OmiseException { - Request getChargeRequest = new Charge.GetRequestBuilder(THREE_DS_CHARGE_ID).build(); + Request getChargeRequest = new Charge.GetRequestBuilder(threeDsChargeId).build(); Charge charge = getTestRequester().sendRequest(getChargeRequest); - assertRequested("GET", "/charges/" + THREE_DS_CHARGE_ID, 200); - assertEquals(AuthenticationType.ThreeDS, charge.getAuthentication()); + assertRequested("GET", "/charges/" + threeDsChargeId, 200); + assertEquals(AuthenticationType.THREE_DS, charge.getAuthentication()); assertEquals("3DS", charge.getAuthenticatedBy()); } @Test public void testGetChargeWithoutAuthenticatedBy() throws IOException, OmiseException { - Request getChargeRequest = new Charge.GetRequestBuilder(NO_AUTH_CHARGE_ID).build(); + Request getChargeRequest = new Charge.GetRequestBuilder(noAuthChargeId).build(); Charge charge = getTestRequester().sendRequest(getChargeRequest); - assertRequested("GET", "/charges/" + NO_AUTH_CHARGE_ID, 200); - assertEquals(AuthenticationType.Passkey, charge.getAuthentication()); + assertRequested("GET", "/charges/" + noAuthChargeId, 200); + assertEquals(AuthenticationType.PASSKEY, charge.getAuthentication()); assertNull(charge.getAuthenticatedBy()); } @@ -74,7 +74,7 @@ public void testCreate() throws IOException, OmiseException { assertEquals(100000L, charge.getAmount()); assertEquals("thb", charge.getCurrency()); assertEquals("trxn_test_4yq7duwb9jts1vxgqua", charge.getTransaction()); - assertEquals(AuthenticationType.Passkey, charge.getAuthentication()); + assertEquals(AuthenticationType.PASSKEY, charge.getAuthentication()); assertEquals("PASSKEY", charge.getAuthenticatedBy()); } @@ -122,7 +122,7 @@ public void testCreatePartialCaptureCharge() throws IOException, OmiseException .amount(100000) .currency("thb") .capture(false) - .authentication(AuthenticationType.Passkey) + .authentication(AuthenticationType.PASSKEY) .authorizationType(AuthorizationType.PreAuth) .returnUri("http://example.com/orders/345678/complete") .firstCharge(CHARGE_ID) From dcc26620f47e2d15871626ea16d428f2fd826389 Mon Sep 17 00:00:00 2001 From: tyler-techcombine Date: Fri, 3 Oct 2025 11:38:49 +0700 Subject: [PATCH 4/5] resolve sonar warning: duplicate literal --- src/main/java/co/omise/Example.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/co/omise/Example.java b/src/main/java/co/omise/Example.java index 120a5daa..b07128d5 100644 --- a/src/main/java/co/omise/Example.java +++ b/src/main/java/co/omise/Example.java @@ -14,6 +14,7 @@ final class Example { private static final String OMISE_SKEY = "skey_test_123"; private static final String OMISE_PKEY = "pkey_test_123"; private static final String CREATED_TOKEN_LOG_PREFIX = "created token: "; + private static final String CREATED_CHARGE_LOG_TEMPLATE = "created charge: %s"; private final Client testClient; private static void logInfo(String template, Object... args) { @@ -109,7 +110,7 @@ void chargeWithCard() throws IOException, OmiseException, ClientException { .card("card_test_4xtsoy2nbfs7ujngyyq") .build(); Charge charge = client().sendRequest(createChargeRequest); - logInfo("created charge: %s", charge.getId()); + logInfo(CREATED_CHARGE_LOG_TEMPLATE, charge.getId()); } void chargeWithAuthentication(AuthenticationType authenticationType) @@ -155,7 +156,7 @@ void chargeWithCustomer() throws IOException, OmiseException, ClientException { .build(); Charge charge = client().sendRequest(createChargeRequest); - logInfo("created charge: %s", charge.getId()); + logInfo(CREATED_CHARGE_LOG_TEMPLATE, charge.getId()); } void chargeWithToken() throws IOException, OmiseException, ClientException { @@ -181,7 +182,7 @@ void chargeWithToken() throws IOException, OmiseException, ClientException { .build(); Charge charge = client().sendRequest(createChargeRequest); - logInfo("created charge: %s", charge.getId()); + logInfo(CREATED_CHARGE_LOG_TEMPLATE, charge.getId()); } void createPartialCaptureCharge() throws IOException, OmiseException, ClientException { @@ -209,7 +210,7 @@ void createPartialCaptureCharge() throws IOException, OmiseException, ClientExce .build(); Charge charge = client().sendRequest(createChargeRequest); - logInfo("created charge: %s", charge.getId()); + logInfo(CREATED_CHARGE_LOG_TEMPLATE, charge.getId()); } void listCharges() throws IOException, OmiseException, ClientException { From 43c08cb51c8bc1659217307930ed4da0686c9598 Mon Sep 17 00:00:00 2001 From: tyler-techcombine Date: Fri, 3 Oct 2025 14:22:14 +0700 Subject: [PATCH 5/5] Log authentication wire values --- src/main/java/co/omise/Example.java | 2 +- .../java/co/omise/models/AuthenticationType.java | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/co/omise/Example.java b/src/main/java/co/omise/Example.java index b07128d5..db847970 100644 --- a/src/main/java/co/omise/Example.java +++ b/src/main/java/co/omise/Example.java @@ -142,7 +142,7 @@ void chargeWithAuthentication(AuthenticationType authenticationType) logInfo( "created charge with %s authentication: %s and auth url: %s ", - authenticationType.name(), + authenticationType.getWireValue(), charge.getId(), charge.getAuthorizeUri()); } diff --git a/src/main/java/co/omise/models/AuthenticationType.java b/src/main/java/co/omise/models/AuthenticationType.java index 7859b6ef..3ae9a951 100644 --- a/src/main/java/co/omise/models/AuthenticationType.java +++ b/src/main/java/co/omise/models/AuthenticationType.java @@ -4,7 +4,17 @@ public enum AuthenticationType { @JsonProperty("3DS") - THREE_DS, + THREE_DS("3DS"), @JsonProperty("PASSKEY") - PASSKEY + PASSKEY("PASSKEY"); + + private final String wireValue; + + AuthenticationType(String wireValue) { + this.wireValue = wireValue; + } + + public String getWireValue() { + return wireValue; + } }