public CommerceExtendedData setItems(JSONArray items) throws JSONException { if (items != null) { for (int i = 0; i < items.length(); i++) { addItem(new Item((items.getJSONObject(i)).toString())); } } return this; }
@Override public JSONObject toJsonObject() throws JSONException { JSONObject ret = super.toJsonObject(); ret.put(KEY_ID, id); ret.put(KEY_AFFILIATION, affiliation); ret.put(KEY_REVENUE, revenue); ret.put(KEY_SHIPPING, shipping); ret.put(KEY_TAX, tax); ret.put(KEY_CURRENCY, currency); JSONArray itemsArray = new JSONArray(); for (Item item : items) { itemsArray.put(item.toJsonObject()); } ret.put(KEY_ITEMS, itemsArray); return ret; }
.setTax(4.38f) .setCurrency("USD"); CommerceExtendedData.Item item = new CommerceExtendedData.Item(22222222, "Item Name", "Category", 20, 5.0d, "USD"); commerce.addItem(item); } catch (JSONException e) {
/** * Create a record of an item that was purchased. * * @param id The ID of this item. May be a String or Number. Ignored if null. * @param name The name of this item. May be a String or Number. Ignored if null. * @param category The item category. Ignored if null. * @param price The individual item price. Ignored if null. * @param quantity The number of units purchased. Ignored if null. * @param currency The currency code for the currency used in this transaction. Ignored if null. * @throws JSONException if values cannot be set. */ public Item(Object id, String name, String category, double price, double quantity, String currency) throws JSONException { super(); if (id != null) { setId(id); } if (name != null) { setName(name); } if (category != null) { setCategory(category); } setPrice(price); setQuantity(quantity); if (currency != null) { setCurrency(currency); } }
@Test public void commerceExtendedData() throws JSONException { ApptentiveLog.e("testCommerceExtendedData()"); ExtendedData expected = new CommerceExtendedData(loadTextAssetAsString(TEST_DATA_DIR + "testCommerceExtendedData.json")); CommerceExtendedData actual = new CommerceExtendedData() .setId("commerce_id") .setAffiliation("1111111111") .setRevenue(100d) .setShipping(5L) .setTax(4.38f) .setCurrency("USD"); CommerceExtendedData.Item item = new CommerceExtendedData.Item(22222222, "Item Name", "Category", 20, 5.0d, "USD"); actual.addItem(item); assertTrue(JsonDiffer.areObjectsEqual(expected.toJsonObject(), actual.toJsonObject())); }