@Test public void validateLineItemList_whenInvalidCurrencyCode_hasExpectedCartError() { List<CartError> errors = validateLineItemList(new ArrayList<LineItem>(), "fakebucks"); assertEquals(1, errors.size()); CartError error = errors.get(0); assertEquals(CartError.CART_CURRENCY, error.getErrorType()); assertEquals("Cart does not have a valid currency code. " + "fakebucks was used, but not recognized.", error.getMessage()); assertNull(error.getLineItem()); }
@NonNull public static List<CartError> validateLineItemList( List<LineItem> lineItems, @NonNull String currencyCode) { List<CartError> cartErrors = new ArrayList<>(); if (lineItems == null) { return cartErrors; } try { Currency.getInstance(currencyCode); } catch (IllegalArgumentException illegalArgumentException) { cartErrors.add(new CartError(CartError.CART_CURRENCY, String.format(Locale.ENGLISH, "Cart does not have a valid currency code. " + "%s was used, but not recognized.", TextUtils.isEmpty(currencyCode) ? "[empty]": currencyCode))); } boolean hasTax = false; for (LineItem item : lineItems) { if (!currencyCode.equals(item.getCurrencyCode())) { cartErrors.add(new CartError( CartError.LINE_ITEM_CURRENCY, String.format(Locale.ENGLISH, "Line item currency of %s does not match cart currency of %s.", TextUtils.isEmpty(item.getCurrencyCode()) ? "[empty]": item.getCurrencyCode(), currencyCode), item)); } if (LineItem.Role.TAX == item.getRole()) { if (hasTax) { cartErrors.add(new CartError( CartError.DUPLICATE_TAX, "A cart may only have one item with a role of " + "LineItem.Role.TAX, but more than one was found.", item)); } else { hasTax = true; } } CartError lineItemError = searchLineItemForErrors(item); if (lineItemError != null) { cartErrors.add(lineItemError); } } return cartErrors; }
PaymentUtils { @NonNull public static List<CartError> validateLineItemList( List<LineItem> lineItems, @NonNull String currencyCode) { List<CartError> cartErrors = new ArrayList<>(); if (lineItems == null) { return cartErrors; } try { Currency.getInstance(currencyCode); } catch (IllegalArgumentException illegalArgumentException) { cartErrors.add(new CartError(CartError.CART_CURRENCY, String.format(Locale.ENGLISH, "Cart does not have a valid currency code. " + "%s was used, but not recognized.", TextUtils.isEmpty(currencyCode) ? "[empty]": currencyCode))); } boolean hasTax = false; for (LineItem item : lineItems) { if (!currencyCode.equals(item.getCurrencyCode())) { cartErrors.add(new CartError( CartError.LINE_ITEM_CURRENCY, String.format(Locale.ENGLISH, "Line item currency of %s does not match cart currency of %s.", TextUtils.isEmpty(item.getCurrencyCode()) ? "[empty]": item.getCurrencyCode(), currencyCode), item)); } if (LineItem.Role.TAX == item.getRole()) { if (hasTax) { cartErrors.add(new CartError( CartError.DUPLICATE_TAX, "A cart may only have one item with a role of " + "LineItem.Role.TAX, but more than one was found.", item)); } else { hasTax = true; } } CartError lineItemError = searchLineItemForErrors(item); if (lineItemError != null) { cartErrors.add(lineItemError); } } return cartErrors; } }
PaymentUtils { @NonNull public static List<CartError> validateLineItemList( List<LineItem> lineItems, @NonNull String currencyCode) { List<CartError> cartErrors = new ArrayList<>(); if (lineItems == null) { return cartErrors; } try { Currency.getInstance(currencyCode); } catch (IllegalArgumentException illegalArgumentException) { cartErrors.add(new CartError(CartError.CART_CURRENCY, String.format(Locale.ENGLISH, "Cart does not have a valid currency code. " + "%s was used, but not recognized.", TextUtils.isEmpty(currencyCode) ? "[empty]": currencyCode))); } boolean hasTax = false; for (LineItem item : lineItems) { if (!currencyCode.equals(item.getCurrencyCode())) { cartErrors.add(new CartError( CartError.LINE_ITEM_CURRENCY, String.format(Locale.ENGLISH, "Line item currency of %s does not match cart currency of %s.", TextUtils.isEmpty(item.getCurrencyCode()) ? "[empty]": item.getCurrencyCode(), currencyCode), item)); } if (LineItem.Role.TAX == item.getRole()) { if (hasTax) { cartErrors.add(new CartError( CartError.DUPLICATE_TAX, "A cart may only have one item with a role of " + "LineItem.Role.TAX, but more than one was found.", item)); } else { hasTax = true; } } CartError lineItemError = searchLineItemForErrors(item); if (lineItemError != null) { cartErrors.add(lineItemError); } } return cartErrors; }  }
PaymentUtils { @NonNull public static List<CartError> validateLineItemList( List<LineItem> lineItems, @NonNull String currencyCode) { List<CartError> cartErrors = new ArrayList<>(); if (lineItems == null) { return cartErrors; } try { Currency.getInstance(currencyCode); } catch (IllegalArgumentException illegalArgumentException) { cartErrors.add(new CartError(CartError.CART_CURRENCY, String.format(Locale.ENGLISH, "Cart does not have a valid currency code. " + "%s was used, but not recognized.", TextUtils.isEmpty(currencyCode) ? "[empty]": currencyCode))); } boolean hasTax = false; for (LineItem item : lineItems) { if (!currencyCode.equals(item.getCurrencyCode())) { cartErrors.add(new CartError( CartError.LINE_ITEM_CURRENCY, String.format(Locale.ENGLISH, "Line item currency of %s does not match cart currency of %s.", TextUtils.isEmpty(item.getCurrencyCode()) ? "[empty]": item.getCurrencyCode(), currencyCode), item)); } if (LineItem.Role.TAX == item.getRole()) { if (hasTax) { cartErrors.add(new CartError( CartError.DUPLICATE_TAX, "A cart may only have one item with a role of " + "LineItem.Role.TAX, but more than one was found.", item)); } else { hasTax = true; } } CartError lineItemError = searchLineItemForErrors(item); if (lineItemError != null) { cartErrors.add(lineItemError); } } return cartErrors; }  static boolean matchesCurrencyPatternOrEmpty(@Nullable String priceString); static boolean matchesQuantityPatternOrEmpty(@Nullable String quantityString); @NonNull static List<CartError> validateLineItemList(
            List<LineItem> lineItems,
            @NonNull String currencyCode); static CartError searchLineItemForErrors(LineItem lineItem); @NonNull static String getPriceString(long price); @NonNull static List<CartError> removeErrorType(
            @NonNull List<CartError> errors,
            @NonNull @CartError.CartErrorType String errorType); @NonNull static String getPriceString(Long price, @NonNull Currency currency); static IsReadyToPayRequest getStripeIsReadyToPayRequest(); }
PaymentUtils { @NonNull public static List<CartError> validateLineItemList( List<LineItem> lineItems, @NonNull String currencyCode) { List<CartError> cartErrors = new ArrayList<>(); if (lineItems == null) { return cartErrors; } try { Currency.getInstance(currencyCode); } catch (IllegalArgumentException illegalArgumentException) { cartErrors.add(new CartError(CartError.CART_CURRENCY, String.format(Locale.ENGLISH, "Cart does not have a valid currency code. " + "%s was used, but not recognized.", TextUtils.isEmpty(currencyCode) ? "[empty]": currencyCode))); } boolean hasTax = false; for (LineItem item : lineItems) { if (!currencyCode.equals(item.getCurrencyCode())) { cartErrors.add(new CartError( CartError.LINE_ITEM_CURRENCY, String.format(Locale.ENGLISH, "Line item currency of %s does not match cart currency of %s.", TextUtils.isEmpty(item.getCurrencyCode()) ? "[empty]": item.getCurrencyCode(), currencyCode), item)); } if (LineItem.Role.TAX == item.getRole()) { if (hasTax) { cartErrors.add(new CartError( CartError.DUPLICATE_TAX, "A cart may only have one item with a role of " + "LineItem.Role.TAX, but more than one was found.", item)); } else { hasTax = true; } } CartError lineItemError = searchLineItemForErrors(item); if (lineItemError != null) { cartErrors.add(lineItemError); } } return cartErrors; }  static boolean matchesCurrencyPatternOrEmpty(@Nullable String priceString); static boolean matchesQuantityPatternOrEmpty(@Nullable String quantityString); @NonNull static List<CartError> validateLineItemList(
            List<LineItem> lineItems,
            @NonNull String currencyCode); static CartError searchLineItemForErrors(LineItem lineItem); @NonNull static String getPriceString(long price); @NonNull static List<CartError> removeErrorType(
            @NonNull List<CartError> errors,
            @NonNull @CartError.CartErrorType String errorType); @NonNull static String getPriceString(Long price, @NonNull Currency currency); static IsReadyToPayRequest getStripeIsReadyToPayRequest();  }