Skip to content

Commit a99f81b

Browse files
authored
Merge pull request #367 from Iterable/updateCart-addition
updateCart/trackPurchase additions
2 parents de7100c + d625ba5 commit a99f81b

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

iterableapi/src/androidTest/java/com/iterable/iterableapi/IterableApiRequestsTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public void testGetRequestHeaders() throws Exception {
6464
assertEquals("fake_key", request.getHeader(IterableConstants.HEADER_API_KEY));
6565
}
6666

67+
@Test
68+
public void testUpdateCart() throws Exception {
69+
CommerceItem item1 = new CommerceItem("sku123", "Item", 50.0, 2);
70+
List<CommerceItem> items = new ArrayList<CommerceItem>();
71+
items.add(item1);
72+
73+
IterableApi.sharedInstance.updateCart(items);
74+
75+
RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
76+
assertNotNull(request);
77+
assertEquals("/" + IterableConstants.ENDPOINT_UPDATE_CART, request.getPath());
78+
79+
String expectedRequest = new StringBuilder(
80+
new StringBuffer("{\"user\":{\"email\":\"test_email\"},")
81+
.append("\"items\":[{\"id\":\"sku123\",\"name\":\"Item\",\"price\":50,\"quantity\":2}],")
82+
.append("\"createdAt\":")
83+
.append(new Date().getTime() / 1000)
84+
.append("}")).toString();
85+
86+
assertEquals(expectedRequest, request.getBody().readUtf8());
87+
}
88+
6789
@Test
6890
public void testTrackPurchase() throws Exception {
6991
String expectedRequest = new StringBuilder(new StringBuffer("{\"user\":{\"email\":\"test_email\"},\"items\":[{\"id\":\"sku123\",\"name\":\"Item\",\"price\":50,\"quantity\":2}],\"total\":100").append(",\"createdAt\":").append(new Date().getTime() / 1000).append("}")).toString();
@@ -81,6 +103,10 @@ public void testTrackPurchase() throws Exception {
81103

82104
@Test
83105
public void testTrackPurchaseWithOptionalParameters() throws Exception {
106+
JSONObject dataFields = new JSONObject();
107+
dataFields.put("color", "yellow");
108+
dataFields.put("count", 8);
109+
84110
CommerceItem item = new CommerceItem("273",
85111
"Bow and Arrow",
86112
42,
@@ -89,7 +115,8 @@ public void testTrackPurchaseWithOptionalParameters() throws Exception {
89115
"When a living creature is pierced by one of the Arrows, it will catalyze and awaken the individual’s dormant Stand.",
90116
"placeholderUrl",
91117
"placeholderImageUrl",
92-
new String[] {"bow", "arrow"});
118+
new String[] {"bow", "arrow"},
119+
dataFields);
93120
List<CommerceItem> items = new ArrayList<CommerceItem>();
94121
items.add(item);
95122

@@ -100,7 +127,7 @@ public void testTrackPurchaseWithOptionalParameters() throws Exception {
100127

101128
String expectedRequest = new StringBuilder(
102129
new StringBuffer("{\"user\":{\"email\":\"test_email\"},")
103-
.append("\"items\":[{\"id\":\"273\",\"name\":\"Bow and Arrow\",\"price\":42,\"quantity\":1,\"sku\":\"DIAMOND-IS-UNBREAKABLE\",\"description\":\"When a living creature is pierced by one of the Arrows, it will catalyze and awaken the individual’s dormant Stand.\",\"url\":\"placeholderUrl\",\"imageUrl\":\"placeholderImageUrl\",\"categories\":[\"bow\",\"arrow\"]}],")
130+
.append("\"items\":[{\"id\":\"273\",\"name\":\"Bow and Arrow\",\"price\":42,\"quantity\":1,\"sku\":\"DIAMOND-IS-UNBREAKABLE\",\"description\":\"When a living creature is pierced by one of the Arrows, it will catalyze and awaken the individual’s dormant Stand.\",\"url\":\"placeholderUrl\",\"imageUrl\":\"placeholderImageUrl\",\"dataFields\":{\"color\":\"yellow\",\"count\":8},\"categories\":[\"bow\",\"arrow\"]}],")
104131
.append("\"total\":42,")
105132
.append("\"createdAt\":")
106133
.append(new Date().getTime() / 1000)

iterableapi/src/main/java/com/iterable/iterableapi/CommerceItem.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public class CommerceItem {
4646
@Nullable
4747
public final String[] categories;
4848

49+
/** data fields as part of this product **/
50+
@Nullable
51+
public final JSONObject dataFields;
52+
4953
/**
5054
* Creates a {@link CommerceItem} with the specified properties
5155
* @param id id of the product
@@ -57,7 +61,7 @@ public CommerceItem(@NonNull String id,
5761
@NonNull String name,
5862
double price,
5963
int quantity) {
60-
this(id, name, price, quantity, null, null, null, null, null);
64+
this(id, name, price, quantity, null, null, null, null, null, null);
6165
}
6266

6367
/**
@@ -71,6 +75,7 @@ public CommerceItem(@NonNull String id,
7175
* @param url URL of the product
7276
* @param imageUrl URL of the product's image
7377
* @param categories categories this product belongs to
78+
* @param dataFields data fields for this CommerceItem
7479
*/
7580
public CommerceItem(@NonNull String id,
7681
@NonNull String name,
@@ -80,7 +85,8 @@ public CommerceItem(@NonNull String id,
8085
@Nullable String description,
8186
@Nullable String url,
8287
@Nullable String imageUrl,
83-
@Nullable String[] categories) {
88+
@Nullable String[] categories,
89+
@Nullable JSONObject dataFields) {
8490
this.id = id;
8591
this.name = name;
8692
this.price = price;
@@ -90,6 +96,7 @@ public CommerceItem(@NonNull String id,
9096
this.url = url;
9197
this.imageUrl = imageUrl;
9298
this.categories = categories;
99+
this.dataFields = dataFields;
93100
}
94101

95102
/**
@@ -108,6 +115,7 @@ public JSONObject toJSONObject() throws JSONException {
108115
jsonObject.putOpt("description", description);
109116
jsonObject.putOpt("url", url);
110117
jsonObject.putOpt("imageUrl", imageUrl);
118+
jsonObject.putOpt("dataFields", dataFields);
111119

112120
if (categories != null) {
113121
JSONArray categoriesArray = new JSONArray();

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,18 @@ public void track(@NonNull String eventName, int campaignId, int templateId, @Nu
517517
apiClient.track(eventName, campaignId, templateId, dataFields);
518518
}
519519

520+
/**
521+
* Updates the status of the cart
522+
* @param items
523+
*/
524+
public void updateCart(@NonNull List<CommerceItem> items) {
525+
if (!checkSDKInitialization()) {
526+
return;
527+
}
528+
529+
apiClient.updateCart(items);
530+
}
531+
520532
/**
521533
* Tracks a purchase.
522534
* @param total total purchase amount

iterableapi/src/main/java/com/iterable/iterableapi/IterableApiClient.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ public void track(@NonNull String eventName, int campaignId, int templateId, @Nu
9393
}
9494
}
9595

96+
public void updateCart(@NonNull List<CommerceItem> items) {
97+
JSONObject requestJSON = new JSONObject();
98+
99+
try {
100+
JSONArray itemsArray = new JSONArray();
101+
for (CommerceItem item : items) {
102+
itemsArray.put(item.toJSONObject());
103+
}
104+
105+
JSONObject userObject = new JSONObject();
106+
addEmailOrUserIdToJson(userObject);
107+
requestJSON.put(IterableConstants.KEY_USER, userObject);
108+
109+
requestJSON.put(IterableConstants.KEY_ITEMS, itemsArray);
110+
111+
sendPostRequest(IterableConstants.ENDPOINT_UPDATE_CART, requestJSON);
112+
} catch (JSONException e) {
113+
e.printStackTrace();
114+
}
115+
}
116+
96117
public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Nullable JSONObject dataFields) {
97118
JSONObject requestJSON = new JSONObject();
98119
try {

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public final class IterableConstants {
6565
public static final String ENDPOINT_TRACK_INAPP_OPEN = "events/trackInAppOpen";
6666
public static final String ENDPOINT_TRACK_INAPP_DELIVERY = "events/trackInAppDelivery";
6767
public static final String ENDPOINT_TRACK_INBOX_SESSION = "events/trackInboxSession";
68+
public static final String ENDPOINT_UPDATE_CART = "commerce/updateCart";
6869
public static final String ENDPOINT_TRACK_PURCHASE = "commerce/trackPurchase";
6970
public static final String ENDPOINT_TRACK_PUSH_OPEN = "events/trackPushOpen";
7071
public static final String ENDPOINT_UPDATE_USER = "users/update";

0 commit comments

Comments
 (0)