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
2 changes: 1 addition & 1 deletion riskified-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.riskified</groupId>
<artifactId>riskified-sdk</artifactId>
<version>5.0.1-rc.2</version>
<version>5.0.1-rc.3</version>
<name>Riskified SDK</name>
<description>Riskified rest api SDK for java</description>
<url>https://www.riskified.com</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import com.riskified.models.AuthenticationResult;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Gson TypeAdapterFactory for AuthenticationResult that handles backward
Expand All @@ -26,7 +24,7 @@
* <p>
* Handles field name changes between legacy and current formats:
* <ul>
* <li><b>Legacy:</b> tran_status, tran_status_reason, tra_exemption</li>
* <li><b>Legacy:</b> tran_status, tran_status_reason, tra_exemption (or their camelCase equivalents)</li>
* <li><b>Current:</b> trans_status, trans_status_reason, TRA_exemption</li>
* </ul>
*
Expand Down Expand Up @@ -54,13 +52,32 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
*/
private static class AuthenticationResultTypeAdapter extends TypeAdapter<AuthenticationResult> {
private final TypeAdapter<AuthenticationResult> delegateAdapter;
private final Set<String> manuallyHandledKeys = new HashSet<String>(Arrays.asList(new String[] { "tran_status",
"trans_status", "tran_status_reason", "trans_status_reason", "tra_exemption", "TRA_exemption" }));
private final Map<String, String> propertyMap = new HashMap<>();

AuthenticationResultTypeAdapter(Gson gson, TypeAdapterFactory skipPast) {
// Get delegate adapter to avoid infinite recursion
this.delegateAdapter = gson.getDelegateAdapter(skipPast,
TypeToken.get(AuthenticationResult.class));

this.populateMap();

}

private void populateMap() {
this.propertyMap.put("tranStatus", "trans_status");
this.propertyMap.put("tran_status", "trans_status");

this.propertyMap.put("tranStatusReason", "trans_status_reason");
this.propertyMap.put("tran_status_reason", "trans_status_reason");

this.propertyMap.put("threeDChallenge", "three_d_challenge");

this.propertyMap.put("tra_exemption", "TRA_exemption");
this.propertyMap.put("traExemption", "TRA_exemption");

this.propertyMap.put("liabilityShift", "liability_shift");

this.propertyMap.put("createdAt", "created_at");
}

@Override
Expand All @@ -84,28 +101,14 @@ public AuthenticationResult read(JsonReader in) throws IOException {
JsonObject original = element.getAsJsonObject();
JsonObject transformed = new JsonObject();

if (original.has("tran_status")) {
transformed.add("trans_status", original.get("tran_status"));
} else if (original.has("trans_status")) {
transformed.add("trans_status", original.get("trans_status"));
}

if (original.has("tran_status_reason")) {
transformed.add("trans_status_reason", original.get("tran_status_reason"));
} else if (original.has("trans_status_reason")) {
transformed.add("trans_status_reason", original.get("trans_status_reason"));
}

if (original.has("tra_exemption")) {
transformed.add("TRA_exemption", original.get("tra_exemption"));
} else if (original.has("TRA_exemption")) {
transformed.add("TRA_exemption", original.get("TRA_exemption"));
}

for (Map.Entry<String, JsonElement> entry : original.entrySet()) {
String key = entry.getKey();
if (!manuallyHandledKeys.contains(key)) {
transformed.add(key, entry.getValue());
JsonElement value = entry.getValue();

if (this.propertyMap.containsKey(key)) {
transformed.add(this.propertyMap.get(key), value);
} else {
transformed.add(key, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,27 @@ public void testPaypalPaymentDetails() {
assertFalse("3D challenge should be false", result.get3DChallenge());
assertTrue("TRA exemption should be true", result.getTRAExemption());
}

@Test
public void testLegacyFormatCamelCase() {
String json = "{\"credit_card_bin\":\"123456\",\"credit_card_number\":\"****1234\"," +
"\"credit_card_company\":\"Visa\",\"authentication_result\":{\"eci\":\"05\"," +
"\"cavv\":\"AAABCZIhcQAAAABZlyFxAAAAAAA=\",\"tranStatus\":\"Y\"," +
"\"tranStatusReason\":\"01\",\"liability_shift\":true," +
"\"threeDChallenge\":false,\"traExemption\":true}}";

CreditCardPaymentDetails details = gson.fromJson(json, CreditCardPaymentDetails.class);

assertNotNull("Payment details should not be null", details);
assertNotNull("Authentication result should not be null", details.getAuthenticationResults());

AuthenticationResult result = details.getAuthenticationResults();
assertEquals("ECI should match", "05", result.getEci());
assertEquals("CAVV should match", "AAABCZIhcQAAAABZlyFxAAAAAAA=", result.getCavv());
assertEquals("TransStatus should be Y", TransStatus.Y, result.getTransStatus());
assertEquals("TransStatusReason should be Zero_One", TransStatusReason.Zero_One, result.getTransStatusReason());
assertTrue("Liability shift should be true", result.getLiabilityShift());
assertFalse("3D challenge should be false", result.get3DChallenge());
assertTrue("TRA exemption should be true", result.getTRAExemption());
}
}