diff --git a/README.md b/README.md index 2bc8afc..0b9c31d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ And then download the artifact incognia-api-client com.incognia incognia-api-client - 2.4.1 + 2.5.0 ``` @@ -39,7 +39,7 @@ repositories { And then add the dependency ```gradle dependencies { - implementation 'com.incognia:incognia-api-client:2.4.1' + implementation 'com.incognia:incognia-api-client:2.5.0' } ``` We support Java 8+. @@ -141,6 +141,29 @@ try { } ``` +#### Registering Web Login + +This method registers a new web login for the given installation and account, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence. + +```java +IncogniaAPI api = new IncogniaAPI("client-id", "client-secret"); +try { + RegisterLoginRequest registerLoginRequest = + RegisterLoginRequest.builder() + .installationId("installation id") + .accountId("account id") + .externalId("external id") + .sessionToken("session-token") + .evaluateTransaction(true) // can be omitted as it uses true as the default value + .build(); + TransactionAssessment assessment = api.registerWebLogin(registerLoginRequest); +} catch (IncogniaAPIException e) { + //Some api error happened (invalid data, invalid credentials) +} catch (IncogniaException e) { + //Something unexpected happened +} +``` + #### Registering Payment This method registers a new payment for the given installation and account, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence. diff --git a/build.gradle b/build.gradle index 7491e7b..1517978 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { } group = "com.incognia" -version = "2.4.1" +version = "2.5.0" task createProjectVersionFile { def projectVersionDir = "$projectDir/src/main/java/com/incognia/api" diff --git a/src/main/java/com/incognia/api/IncogniaAPI.java b/src/main/java/com/incognia/api/IncogniaAPI.java index 9567274..e7ebb27 100644 --- a/src/main/java/com/incognia/api/IncogniaAPI.java +++ b/src/main/java/com/incognia/api/IncogniaAPI.java @@ -16,6 +16,7 @@ import com.incognia.transaction.TransactionAddress; import com.incognia.transaction.TransactionAssessment; import com.incognia.transaction.login.RegisterLoginRequest; +import com.incognia.transaction.login.RegisterWebLoginRequest; import com.incognia.transaction.payment.RegisterPaymentRequest; import java.time.Instant; import java.util.HashMap; @@ -199,6 +200,61 @@ public TransactionAssessment registerLogin(RegisterLoginRequest request) queryParameters); } + /** + * Registers a web login to obtain a risk assessment. Check the docs
+ * Example: + * + *
{@code
+   * IncogniaAPI api = new IncogniaAPI("client-id", "client-secret", Region.BR);
+   * try {
+   *     RegisterLoginRequest loginRequest = RegisterLoginRequest.builder()
+   *         .installationId("installation-id")
+   *         .accountId("account-id")
+   *         .externalId("external-id")
+   *         .sessionToken("session-token")
+   *         .evaluateTransaction(true) // can be omitted as it uses true as the default value
+   *         .build();
+   *      TransactionAssessment assessment = api.registerLogin(loginRequest);
+   * } catch (IncogniaAPIException e) {
+   *      //Some api error happened (invalid data, invalid credentials)
+   * } catch (IncogniaException e) {
+   *      //Something unexpected happened
+   * }
+   * }
+ * + * @param request the {@link RegisterWebLoginRequest} model with the properties we need to make + * the assessment + * @return the assessment for the login + * @throws IncogniaAPIException in case of api errors + * @throws IncogniaException in case of unexpected errors + */ + public TransactionAssessment registerWebLogin(RegisterWebLoginRequest request) + throws IncogniaException { + Asserts.assertNotNull(request, "register login request"); + Asserts.assertNotEmpty(request.getInstallationId(), "installation id"); + Asserts.assertNotEmpty(request.getAccountId(), "account id"); + Asserts.assertNotEmpty(request.getSessionToken(), "session token"); + PostTransactionRequestBody requestBody = + PostTransactionRequestBody.builder() + .installationId(request.getInstallationId()) + .accountId(request.getAccountId()) + .externalId(request.getExternalId()) + .sessionToken(request.getSessionToken()) + .type("login") + .build(); + + Map queryParameters = new HashMap<>(); + if (request.shouldEvaluateTransaction() != null) { + queryParameters.put(EVALUATION_PARAMETER, request.shouldEvaluateTransaction().toString()); + } + return tokenAwareNetworkingClient.doPost( + "api/v2/authentication/transactions", + requestBody, + TransactionAssessment.class, + queryParameters); + } + /** * Registers a payment to obtain a risk assessment. Check the docs
diff --git a/src/main/java/com/incognia/transaction/PostTransactionRequestBody.java b/src/main/java/com/incognia/transaction/PostTransactionRequestBody.java index 56f1251..e36d416 100644 --- a/src/main/java/com/incognia/transaction/PostTransactionRequestBody.java +++ b/src/main/java/com/incognia/transaction/PostTransactionRequestBody.java @@ -13,6 +13,7 @@ public class PostTransactionRequestBody { String installationId; String accountId; + String sessionToken; String type; String externalId; diff --git a/src/main/java/com/incognia/transaction/login/RegisterWebLoginRequest.java b/src/main/java/com/incognia/transaction/login/RegisterWebLoginRequest.java new file mode 100644 index 0000000..3c43b1a --- /dev/null +++ b/src/main/java/com/incognia/transaction/login/RegisterWebLoginRequest.java @@ -0,0 +1,22 @@ +package com.incognia.transaction.login; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.Value; + +@Value +@Builder +public class RegisterWebLoginRequest { + String installationId; + String accountId; + String externalId; + String sessionToken; + + @Getter(AccessLevel.NONE) + Boolean evaluateTransaction; + + public Boolean shouldEvaluateTransaction() { + return this.evaluateTransaction; + } +} diff --git a/src/test/java/com/incognia/api/IncogniaAPITest.java b/src/test/java/com/incognia/api/IncogniaAPITest.java index ea0a83e..d902724 100644 --- a/src/test/java/com/incognia/api/IncogniaAPITest.java +++ b/src/test/java/com/incognia/api/IncogniaAPITest.java @@ -22,6 +22,7 @@ import com.incognia.transaction.TransactionAddress; import com.incognia.transaction.TransactionAssessment; import com.incognia.transaction.login.RegisterLoginRequest; +import com.incognia.transaction.login.RegisterWebLoginRequest; import com.incognia.transaction.payment.CardInfo; import com.incognia.transaction.payment.PaymentMethod; import com.incognia.transaction.payment.PaymentType; @@ -219,6 +220,42 @@ void testRegisterLogin_whenDataIsValid(Boolean eval) { assertTransactionAssessment(transactionAssessment); } + @ParameterizedTest + @ValueSource(booleans = {true}) + @NullSource + @DisplayName("should return the expected web login transaction response") + @SneakyThrows + void testRegisterWebLogin_whenDataIsValid(Boolean eval) { + String token = TokenCreationFixture.createToken(); + String installationId = "installation-id"; + String accountId = "account-id"; + String externalId = "external-id"; + String sessionToken = "session-token"; + + TokenAwareDispatcher dispatcher = new TokenAwareDispatcher(token, CLIENT_ID, CLIENT_SECRET); + dispatcher.setExpectedTransactionRequestBody( + PostTransactionRequestBody.builder() + .installationId(installationId) + .externalId(externalId) + .accountId(accountId) + .type("login") + .sessionToken(sessionToken) + .addresses(null) + .paymentMethods(null) + .build()); + mockServer.setDispatcher(dispatcher); + RegisterWebLoginRequest loginRequest = + RegisterWebLoginRequest.builder() + .installationId(installationId) + .accountId(accountId) + .externalId(externalId) + .evaluateTransaction(eval) + .sessionToken(sessionToken) + .build(); + TransactionAssessment transactionAssessment = client.registerWebLogin(loginRequest); + assertTransactionAssessment(transactionAssessment); + } + @Test @DisplayName("should return an empty response") @SneakyThrows