Skip to content

Commit be2937a

Browse files
authored
Merge pull request #137 from wangdaliu/feat-support-categories
feat: Add 'categories' field to item in checkout request
2 parents 7ad184c + f90a6d4 commit be2937a

File tree

8 files changed

+28
-6
lines changed

8 files changed

+28
-6
lines changed

affirm/src/main/java/com/affirm/android/PromoRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public String url() {
183183
builder.appendQueryParameter(PROMO_LOGO_TYPE, affirmLogoType.getType());
184184
if (items != null) {
185185
builder.appendQueryParameter(PROMO_ITEMS,
186-
Uri.encode(AffirmPlugins.get().gson().toJson(items))
186+
AffirmPlugins.get().gson().toJson(items)
187187
);
188188
}
189189
builder.appendQueryParameter(PROMO_LOCALE, AffirmPlugins.get().locale());

affirm/src/main/java/com/affirm/android/model/Item.java

+11
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.google.gson.annotations.SerializedName;
1010

1111
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
1213

1314
import java.math.BigDecimal;
15+
import java.util.List;
1416

1517
@AutoValue
1618
public abstract class Item implements Parcelable {
@@ -44,6 +46,13 @@ public static TypeAdapter<Item> typeAdapter(Gson gson) {
4446
@SerializedName("item_image_url")
4547
public abstract String imageUrl();
4648

49+
// An array of lists that indicate the various categories that apply to this product, and
50+
// the hierarchy of those category definitions. Each list in the array contains one or more
51+
// comma-separated strings, with the first string being the highest-level (widest) category.
52+
@Nullable
53+
@SerializedName("categories")
54+
public abstract List<List<String>> categories();
55+
4756
@AutoValue.Builder
4857
public abstract static class Builder {
4958
private BigDecimal mUnitPrice;
@@ -60,6 +69,8 @@ public abstract static class Builder {
6069

6170
public abstract Builder setImageUrl(String value);
6271

72+
public abstract Builder setCategories(@Nullable List<List<String>> categories);
73+
6374
abstract Item autoBuild();
6475

6576
public Builder setUnitPrice(@NonNull BigDecimal value) {

affirm/src/test/java/com/affirm/android/CheckoutFactory.java

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.affirm.android.model.Shipping;
1010

1111
import java.math.BigDecimal;
12+
import java.util.Arrays;
1213
import java.util.HashMap;
1314
import java.util.Map;
1415

@@ -22,6 +23,10 @@ public static Checkout create() {
2223
.setSku("wheel")
2324
.setUnitPrice(BigDecimal.valueOf(1000.0))
2425
.setUrl("http://merchant.com/great_deal_wheel")
26+
.setCategories(Arrays.asList(
27+
Arrays.asList("Apparel", "Pants"),
28+
Arrays.asList("Mens", "Apparel", "Pants")
29+
))
2530
.build();
2631

2732
final Map<String, Item> items = new HashMap<>();

affirm/src/test/java/com/affirm/android/CheckoutRequestTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
public class CheckoutRequestTest {
2222

2323
private static final String expectedCheckoutBody =
24-
"{\"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\"}}}";
24+
"{\"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\"}}}";
2525

2626
private static final String expectedCheckoutWithCaasBody =
27-
"{\"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\"}}}";
27+
"{\"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\"}}}";
2828

2929
private static final String expectedCheckoutWithCardAuthWindowBody =
30-
"{\"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\"}}}";
30+
"{\"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\"}}}";
3131

3232
private static String generateExpectedCheckoutBody() {
3333
return String.format(expectedCheckoutBody, BuildConfig.VERSION_NAME);

affirm/src/test/java/com/affirm/android/PromoRequestTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class PromoRequestTest {
2626

2727
private static final String expectedPromoUrl =
28-
"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";
28+
"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";
2929

3030
@Before
3131
public void setup() {

affirm/src/test/java/com/affirm/android/model/CheckoutTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@RunWith(RobolectricTestRunner.class)
1414
public class CheckoutTest {
1515

16-
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\"}}";
16+
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\"}}";
1717

1818
private final Gson gson = new GsonBuilder()
1919
.registerTypeAdapterFactory(AffirmAdapterFactory.create())

samples-java/src/main/java/com/affirm/samples/MainFragment.java

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.io.InputStreamReader;
4848
import java.math.BigDecimal;
4949
import java.util.ArrayList;
50+
import java.util.Arrays;
5051
import java.util.HashMap;
5152
import java.util.List;
5253
import java.util.Map;
@@ -273,6 +274,10 @@ private Checkout checkoutModel() {
273274
.setSku("wheel")
274275
.setUnitPrice(BigDecimal.valueOf(1000.0))
275276
.setUrl("http://merchant.com/great_deal_wheel")
277+
.setCategories(Arrays.asList(
278+
Arrays.asList("Apparel", "Pants"),
279+
Arrays.asList("Mens", "Apparel", "Pants")
280+
))
276281
.build();
277282

278283
final Map<String, Item> items = new HashMap<>();

samples-kotlin/src/main/java/com/affirm/sampleskt/MainActivity.kt

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class MainActivity : AppCompatActivity(), Affirm.CheckoutCallbacks, Affirm.VcnCh
119119
.setSku("wheel")
120120
.setUnitPrice(BigDecimal.valueOf(1000.0))
121121
.setUrl("http://merchant.com/great_deal_wheel")
122+
.setCategories(listOf(listOf("Apparel", "Pants"), listOf("Mens", "Apparel", "Pants")))
122123
.build()
123124
val items: MutableMap<String, Item> = HashMap()
124125
items["wheel"] = item

0 commit comments

Comments
 (0)