Skip to content

Commit

Permalink
Merge pull request #367 from Iterable/updateCart-addition
Browse files Browse the repository at this point in the history
updateCart/trackPurchase additions
  • Loading branch information
roninopf authored Aug 5, 2021
2 parents de7100c + d625ba5 commit a99f81b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ public void testGetRequestHeaders() throws Exception {
assertEquals("fake_key", request.getHeader(IterableConstants.HEADER_API_KEY));
}

@Test
public void testUpdateCart() throws Exception {
CommerceItem item1 = new CommerceItem("sku123", "Item", 50.0, 2);
List<CommerceItem> items = new ArrayList<CommerceItem>();
items.add(item1);

IterableApi.sharedInstance.updateCart(items);

RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
assertNotNull(request);
assertEquals("/" + IterableConstants.ENDPOINT_UPDATE_CART, request.getPath());

String expectedRequest = new StringBuilder(
new StringBuffer("{\"user\":{\"email\":\"test_email\"},")
.append("\"items\":[{\"id\":\"sku123\",\"name\":\"Item\",\"price\":50,\"quantity\":2}],")
.append("\"createdAt\":")
.append(new Date().getTime() / 1000)
.append("}")).toString();

assertEquals(expectedRequest, request.getBody().readUtf8());
}

@Test
public void testTrackPurchase() throws Exception {
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();
Expand All @@ -81,6 +103,10 @@ public void testTrackPurchase() throws Exception {

@Test
public void testTrackPurchaseWithOptionalParameters() throws Exception {
JSONObject dataFields = new JSONObject();
dataFields.put("color", "yellow");
dataFields.put("count", 8);

CommerceItem item = new CommerceItem("273",
"Bow and Arrow",
42,
Expand All @@ -89,7 +115,8 @@ public void testTrackPurchaseWithOptionalParameters() throws Exception {
"When a living creature is pierced by one of the Arrows, it will catalyze and awaken the individual’s dormant Stand.",
"placeholderUrl",
"placeholderImageUrl",
new String[] {"bow", "arrow"});
new String[] {"bow", "arrow"},
dataFields);
List<CommerceItem> items = new ArrayList<CommerceItem>();
items.add(item);

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

String expectedRequest = new StringBuilder(
new StringBuffer("{\"user\":{\"email\":\"test_email\"},")
.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\"]}],")
.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\"]}],")
.append("\"total\":42,")
.append("\"createdAt\":")
.append(new Date().getTime() / 1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class CommerceItem {
@Nullable
public final String[] categories;

/** data fields as part of this product **/
@Nullable
public final JSONObject dataFields;

/**
* Creates a {@link CommerceItem} with the specified properties
* @param id id of the product
Expand All @@ -57,7 +61,7 @@ public CommerceItem(@NonNull String id,
@NonNull String name,
double price,
int quantity) {
this(id, name, price, quantity, null, null, null, null, null);
this(id, name, price, quantity, null, null, null, null, null, null);
}

/**
Expand All @@ -71,6 +75,7 @@ public CommerceItem(@NonNull String id,
* @param url URL of the product
* @param imageUrl URL of the product's image
* @param categories categories this product belongs to
* @param dataFields data fields for this CommerceItem
*/
public CommerceItem(@NonNull String id,
@NonNull String name,
Expand All @@ -80,7 +85,8 @@ public CommerceItem(@NonNull String id,
@Nullable String description,
@Nullable String url,
@Nullable String imageUrl,
@Nullable String[] categories) {
@Nullable String[] categories,
@Nullable JSONObject dataFields) {
this.id = id;
this.name = name;
this.price = price;
Expand All @@ -90,6 +96,7 @@ public CommerceItem(@NonNull String id,
this.url = url;
this.imageUrl = imageUrl;
this.categories = categories;
this.dataFields = dataFields;
}

/**
Expand All @@ -108,6 +115,7 @@ public JSONObject toJSONObject() throws JSONException {
jsonObject.putOpt("description", description);
jsonObject.putOpt("url", url);
jsonObject.putOpt("imageUrl", imageUrl);
jsonObject.putOpt("dataFields", dataFields);

if (categories != null) {
JSONArray categoriesArray = new JSONArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,18 @@ public void track(@NonNull String eventName, int campaignId, int templateId, @Nu
apiClient.track(eventName, campaignId, templateId, dataFields);
}

/**
* Updates the status of the cart
* @param items
*/
public void updateCart(@NonNull List<CommerceItem> items) {
if (!checkSDKInitialization()) {
return;
}

apiClient.updateCart(items);
}

/**
* Tracks a purchase.
* @param total total purchase amount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ public void track(@NonNull String eventName, int campaignId, int templateId, @Nu
}
}

public void updateCart(@NonNull List<CommerceItem> items) {
JSONObject requestJSON = new JSONObject();

try {
JSONArray itemsArray = new JSONArray();
for (CommerceItem item : items) {
itemsArray.put(item.toJSONObject());
}

JSONObject userObject = new JSONObject();
addEmailOrUserIdToJson(userObject);
requestJSON.put(IterableConstants.KEY_USER, userObject);

requestJSON.put(IterableConstants.KEY_ITEMS, itemsArray);

sendPostRequest(IterableConstants.ENDPOINT_UPDATE_CART, requestJSON);
} catch (JSONException e) {
e.printStackTrace();
}
}

public void trackPurchase(double total, @NonNull List<CommerceItem> items, @Nullable JSONObject dataFields) {
JSONObject requestJSON = new JSONObject();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class IterableConstants {
public static final String ENDPOINT_TRACK_INAPP_OPEN = "events/trackInAppOpen";
public static final String ENDPOINT_TRACK_INAPP_DELIVERY = "events/trackInAppDelivery";
public static final String ENDPOINT_TRACK_INBOX_SESSION = "events/trackInboxSession";
public static final String ENDPOINT_UPDATE_CART = "commerce/updateCart";
public static final String ENDPOINT_TRACK_PURCHASE = "commerce/trackPurchase";
public static final String ENDPOINT_TRACK_PUSH_OPEN = "events/trackPushOpen";
public static final String ENDPOINT_UPDATE_USER = "users/update";
Expand Down

0 comments on commit a99f81b

Please sign in to comment.