From a9d37253d8ac45d3c443a94dae414dd5110baae2 Mon Sep 17 00:00:00 2001 From: Evan Date: Sat, 24 Aug 2024 18:09:08 +0800 Subject: [PATCH 1/2] feat: Add 'categories' field to item in checkout request --- .../src/main/java/com/affirm/android/model/Item.java | 11 +++++++++++ .../test/java/com/affirm/android/CheckoutFactory.java | 5 +++++ .../java/com/affirm/android/CheckoutRequestTest.java | 6 +++--- .../java/com/affirm/android/model/CheckoutTest.java | 2 +- .../main/java/com/affirm/samples/MainFragment.java | 5 +++++ .../main/java/com/affirm/sampleskt/MainActivity.kt | 1 + 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/affirm/src/main/java/com/affirm/android/model/Item.java b/affirm/src/main/java/com/affirm/android/model/Item.java index 36ab838d..220c3f84 100755 --- a/affirm/src/main/java/com/affirm/android/model/Item.java +++ b/affirm/src/main/java/com/affirm/android/model/Item.java @@ -9,8 +9,10 @@ import com.google.gson.annotations.SerializedName; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.math.BigDecimal; +import java.util.List; @AutoValue public abstract class Item implements Parcelable { @@ -44,6 +46,13 @@ public static TypeAdapter typeAdapter(Gson gson) { @SerializedName("item_image_url") public abstract String imageUrl(); + // An array of lists that indicate the various categories that apply to this product, and + // the hierarchy of those category definitions. Each list in the array contains one or more + // comma-separated strings, with the first string being the highest-level (widest) category. + @Nullable + @SerializedName("categories") + public abstract List> categories(); + @AutoValue.Builder public abstract static class Builder { private BigDecimal mUnitPrice; @@ -60,6 +69,8 @@ public abstract static class Builder { public abstract Builder setImageUrl(String value); + public abstract Builder setCategories(@Nullable List> categories); + abstract Item autoBuild(); public Builder setUnitPrice(@NonNull BigDecimal value) { diff --git a/affirm/src/test/java/com/affirm/android/CheckoutFactory.java b/affirm/src/test/java/com/affirm/android/CheckoutFactory.java index 8d9f67cd..ac29fc93 100755 --- a/affirm/src/test/java/com/affirm/android/CheckoutFactory.java +++ b/affirm/src/test/java/com/affirm/android/CheckoutFactory.java @@ -9,6 +9,7 @@ import com.affirm.android.model.Shipping; import java.math.BigDecimal; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -22,6 +23,10 @@ public static Checkout create() { .setSku("wheel") .setUnitPrice(BigDecimal.valueOf(1000.0)) .setUrl("http://merchant.com/great_deal_wheel") + .setCategories(Arrays.asList( + Arrays.asList("Apparel", "Pants"), + Arrays.asList("Mens", "Apparel", "Pants") + )) .build(); final Map items = new HashMap<>(); diff --git a/affirm/src/test/java/com/affirm/android/CheckoutRequestTest.java b/affirm/src/test/java/com/affirm/android/CheckoutRequestTest.java index f8801c65..8500f23b 100755 --- a/affirm/src/test/java/com/affirm/android/CheckoutRequestTest.java +++ b/affirm/src/test/java/com/affirm/android/CheckoutRequestTest.java @@ -21,13 +21,13 @@ public class CheckoutRequestTest { private static final String expectedCheckoutBody = - "{\"checkout\":{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\"}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\",\"platform_type\":\"Affirm Android SDK\",\"platform_affirm\":\"\"},\"merchant\":{\"public_api_key\":\"Y8CQXFF044903JC0\",\"user_confirmation_url\":\"affirm://checkout/confirmed\",\"user_cancel_url\":\"affirm://checkout/cancelled\",\"user_confirmation_url_action\":\"GET\"},\"api_version\":\"v2\",\"meta\":{\"locale\":\"en_US\"}}}"; + "{\"checkout\":{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\",\"categories\":[[\"Apparel\",\"Pants\"],[\"Mens\",\"Apparel\",\"Pants\"]]}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\",\"platform_type\":\"Affirm Android SDK\",\"platform_affirm\":\"\"},\"merchant\":{\"public_api_key\":\"Y8CQXFF044903JC0\",\"user_confirmation_url\":\"affirm://checkout/confirmed\",\"user_cancel_url\":\"affirm://checkout/cancelled\",\"user_confirmation_url_action\":\"GET\"},\"api_version\":\"v2\",\"meta\":{\"locale\":\"en_US\"}}}"; private static final String expectedCheckoutWithCaasBody = - "{\"checkout\":{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\"}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\",\"platform_type\":\"Affirm Android SDK\",\"platform_affirm\":\"\"},\"merchant\":{\"public_api_key\":\"Y8CQXFF044903JC0\",\"user_confirmation_url\":\"affirm://checkout/confirmed\",\"user_cancel_url\":\"affirm://checkout/cancelled\",\"caas\":\"4626b631-c5bc-4c4e-800b-dd5fa27ef8b8\",\"user_confirmation_url_action\":\"GET\"},\"api_version\":\"v2\",\"meta\":{\"locale\":\"en_US\"}}}"; + "{\"checkout\":{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\",\"categories\":[[\"Apparel\",\"Pants\"],[\"Mens\",\"Apparel\",\"Pants\"]]}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\",\"platform_type\":\"Affirm Android SDK\",\"platform_affirm\":\"\"},\"merchant\":{\"public_api_key\":\"Y8CQXFF044903JC0\",\"user_confirmation_url\":\"affirm://checkout/confirmed\",\"user_cancel_url\":\"affirm://checkout/cancelled\",\"caas\":\"4626b631-c5bc-4c4e-800b-dd5fa27ef8b8\",\"user_confirmation_url_action\":\"GET\"},\"api_version\":\"v2\",\"meta\":{\"locale\":\"en_US\"}}}"; private static final String expectedCheckoutWithCardAuthWindowBody = - "{\"checkout\":{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\"}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\",\"platform_type\":\"Affirm Android SDK\",\"platform_affirm\":\"\"},\"merchant\":{\"public_api_key\":\"Y8CQXFF044903JC0\",\"user_confirmation_url\":\"affirm://checkout/confirmed\",\"user_cancel_url\":\"affirm://checkout/cancelled\",\"card_auth_window\":10,\"user_confirmation_url_action\":\"GET\"},\"api_version\":\"v2\",\"meta\":{\"locale\":\"en_US\"}}}"; + "{\"checkout\":{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\",\"categories\":[[\"Apparel\",\"Pants\"],[\"Mens\",\"Apparel\",\"Pants\"]]}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\",\"platform_type\":\"Affirm Android SDK\",\"platform_affirm\":\"\"},\"merchant\":{\"public_api_key\":\"Y8CQXFF044903JC0\",\"user_confirmation_url\":\"affirm://checkout/confirmed\",\"user_cancel_url\":\"affirm://checkout/cancelled\",\"card_auth_window\":10,\"user_confirmation_url_action\":\"GET\"},\"api_version\":\"v2\",\"meta\":{\"locale\":\"en_US\"}}}"; private static String generateExpectedCheckoutBody() { return String.format(expectedCheckoutBody, BuildConfig.VERSION_NAME); diff --git a/affirm/src/test/java/com/affirm/android/model/CheckoutTest.java b/affirm/src/test/java/com/affirm/android/model/CheckoutTest.java index b4b11459..4f9cab09 100644 --- a/affirm/src/test/java/com/affirm/android/model/CheckoutTest.java +++ b/affirm/src/test/java/com/affirm/android/model/CheckoutTest.java @@ -13,7 +13,7 @@ @RunWith(RobolectricTestRunner.class) public class CheckoutTest { - private static final String expectCheckout = "{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\"}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\"}}"; + private static final String expectCheckout = "{\"items\":{\"wheel\":{\"display_name\":\"Great Deal Wheel\",\"sku\":\"wheel\",\"unit_price\":100000,\"qty\":1,\"item_url\":\"http://merchant.com/great_deal_wheel\",\"item_image_url\":\"http://www.image.com/111\",\"categories\":[[\"Apparel\",\"Pants\"],[\"Mens\",\"Apparel\",\"Pants\"]]}},\"currency\":\"USD\",\"shipping\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"billing\":{\"address\":{\"street1\":\"333 Kansas st\",\"city\":\"San Francisco\",\"region1_code\":\"CA\",\"postal_code\":\"94103\",\"country\":\"USA\"},\"name\":{\"full\":\"John Smith\"}},\"shipping_amount\":100000,\"tax_amount\":10000,\"total\":110000,\"metadata\":{\"entity_name\":\"internal-sub_brand-name\",\"shipping_type\":\"UPS Ground\",\"webhook_session_id\":\"ABC123\"}}"; private final Gson gson = new GsonBuilder() .registerTypeAdapterFactory(AffirmAdapterFactory.create()) diff --git a/samples-java/src/main/java/com/affirm/samples/MainFragment.java b/samples-java/src/main/java/com/affirm/samples/MainFragment.java index 3699babf..65e40e1e 100644 --- a/samples-java/src/main/java/com/affirm/samples/MainFragment.java +++ b/samples-java/src/main/java/com/affirm/samples/MainFragment.java @@ -47,6 +47,7 @@ import java.io.InputStreamReader; import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -273,6 +274,10 @@ private Checkout checkoutModel() { .setSku("wheel") .setUnitPrice(BigDecimal.valueOf(1000.0)) .setUrl("http://merchant.com/great_deal_wheel") + .setCategories(Arrays.asList( + Arrays.asList("Apparel", "Pants"), + Arrays.asList("Mens", "Apparel", "Pants") + )) .build(); final Map items = new HashMap<>(); diff --git a/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt b/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt index 9589664b..01605104 100755 --- a/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt +++ b/samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt @@ -119,6 +119,7 @@ class MainActivity : AppCompatActivity(), Affirm.CheckoutCallbacks, Affirm.VcnCh .setSku("wheel") .setUnitPrice(BigDecimal.valueOf(1000.0)) .setUrl("http://merchant.com/great_deal_wheel") + .setCategories(listOf(listOf("Apparel", "Pants"), listOf("Mens", "Apparel", "Pants"))) .build() val items: MutableMap = HashMap() items["wheel"] = item From f90a6d4e3303bcadc1bd3915bc94ff8c792bbe3f Mon Sep 17 00:00:00 2001 From: Evan Date: Mon, 26 Aug 2024 13:51:08 +0800 Subject: [PATCH 2/2] fix: Avoid double encoding of URL parameters --- affirm/src/main/java/com/affirm/android/PromoRequest.java | 2 +- affirm/src/test/java/com/affirm/android/PromoRequestTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/affirm/src/main/java/com/affirm/android/PromoRequest.java b/affirm/src/main/java/com/affirm/android/PromoRequest.java index d364c7c3..7f075dfc 100755 --- a/affirm/src/main/java/com/affirm/android/PromoRequest.java +++ b/affirm/src/main/java/com/affirm/android/PromoRequest.java @@ -183,7 +183,7 @@ public String url() { builder.appendQueryParameter(PROMO_LOGO_TYPE, affirmLogoType.getType()); if (items != null) { builder.appendQueryParameter(PROMO_ITEMS, - Uri.encode(AffirmPlugins.get().gson().toJson(items)) + AffirmPlugins.get().gson().toJson(items) ); } builder.appendQueryParameter(PROMO_LOCALE, AffirmPlugins.get().locale()); diff --git a/affirm/src/test/java/com/affirm/android/PromoRequestTest.java b/affirm/src/test/java/com/affirm/android/PromoRequestTest.java index 76422d39..9a398458 100755 --- a/affirm/src/test/java/com/affirm/android/PromoRequestTest.java +++ b/affirm/src/test/java/com/affirm/android/PromoRequestTest.java @@ -25,7 +25,7 @@ public class PromoRequestTest { private static final String expectedPromoUrl = - "https://sandbox.affirm.com/api/promos/v2/Y8CQXFF044903JC0?is_sdk=true&field=ala&amount=110000&show_cta=false&logo_color=blue&logo_type=logo&items=%255B%257B%2522display_name%2522%253A%2522Great%2520Deal%2520Wheel%2522%252C%2522sku%2522%253A%2522wheel%2522%252C%2522unit_price%2522%253A100000%252C%2522qty%2522%253A1%252C%2522item_url%2522%253A%2522http%253A%252F%252Fmerchant.com%252Fgreat_deal_wheel%2522%252C%2522item_image_url%2522%253A%2522http%253A%252F%252Fwww.m2motorsportinc.com%252Fmedia%252Fcatalog%252Fproduct%252Fcache%252F1%252Fthumbnail%252F9df78eab33525d08d6e5fb8d27136e95%252Fv%252Fe%252Fvelocity-vw125-wheels-rims.jpg%2522%257D%255D&locale=en_US"; + "https://sandbox.affirm.com/api/promos/v2/Y8CQXFF044903JC0?is_sdk=true&field=ala&amount=110000&show_cta=false&logo_color=blue&logo_type=logo&items=%5B%7B%22display_name%22%3A%22Great%20Deal%20Wheel%22%2C%22sku%22%3A%22wheel%22%2C%22unit_price%22%3A100000%2C%22qty%22%3A1%2C%22item_url%22%3A%22http%3A%2F%2Fmerchant.com%2Fgreat_deal_wheel%22%2C%22item_image_url%22%3A%22http%3A%2F%2Fwww.m2motorsportinc.com%2Fmedia%2Fcatalog%2Fproduct%2Fcache%2F1%2Fthumbnail%2F9df78eab33525d08d6e5fb8d27136e95%2Fv%2Fe%2Fvelocity-vw125-wheels-rims.jpg%22%7D%5D&locale=en_US"; @Before public void setup() {