@NonNull private static Map<String, Object> hashMapFromBankAccount( @Nullable UidProvider provider, @NonNull Context context, @NonNull BankAccount bankAccount) { Map<String, Object> tokenParams = new HashMap<>(); Map<String, Object> accountParams = new HashMap<>(); accountParams.put("country", bankAccount.getCountryCode()); accountParams.put("currency", bankAccount.getCurrency()); accountParams.put("account_number", bankAccount.getAccountNumber()); accountParams.put("routing_number", StripeTextUtils.nullIfBlank(bankAccount.getRoutingNumber())); accountParams.put("account_holder_name", StripeTextUtils.nullIfBlank(bankAccount.getAccountHolderName())); accountParams.put("account_holder_type", StripeTextUtils.nullIfBlank(bankAccount.getAccountHolderType())); // Remove all null values; they cause validation errors removeNullAndEmptyParams(accountParams); tokenParams.put(Token.TYPE_BANK_ACCOUNT, accountParams); addUidParams(provider, context, tokenParams); return tokenParams; }
@Nullable public static BankAccount fromString(@Nullable String jsonString) { try { JSONObject accountObject = new JSONObject(jsonString); return fromJson(accountObject); } catch (JSONException jsonException) { return null; } }
@Test public void parseSampleAccount_returnsExpectedValue() { BankAccount expectedAccount = new BankAccount( "Jane Austen", BankAccount.TYPE_INDIVIDUAL, "STRIPE TEST BANK", "US", "usd", "1JWtPxqbdX5Gamtc", "6789", "110000000"); BankAccount actualAccount = BankAccount.fromString(RAW_BANK_ACCOUNT); assertNotNull(actualAccount); assertEquals(expectedAccount.getAccountHolderName(), actualAccount.getAccountHolderName()); assertEquals(expectedAccount.getAccountHolderType(), actualAccount.getAccountHolderType()); assertEquals(expectedAccount.getBankName(), actualAccount.getBankName()); assertEquals(expectedAccount.getCountryCode(), actualAccount.getCountryCode()); assertEquals(expectedAccount.getCurrency(), actualAccount.getCurrency()); assertEquals(expectedAccount.getFingerprint(), actualAccount.getFingerprint()); assertEquals(expectedAccount.getLast4(), actualAccount.getLast4()); assertEquals(expectedAccount.getRoutingNumber(), actualAccount.getRoutingNumber()); } }
@Test public void createBankAccountTokenSynchronous_withValidBankAccount_returnsToken() { try { Stripe stripe = getNonLoggingStripe(mContext, FUNCTIONAL_PUBLISHABLE_KEY); Token token = stripe.createBankAccountTokenSynchronous(mBankAccount); assertNotNull(token); assertEquals(Token.TYPE_BANK_ACCOUNT, token.getType()); assertNull(token.getCard()); BankAccount returnedBankAccount = token.getBankAccount(); String expectedLast4 = TEST_BANK_ACCOUNT_NUMBER .substring(TEST_BANK_ACCOUNT_NUMBER.length() - 4); assertEquals(expectedLast4, returnedBankAccount.getLast4()); assertEquals(mBankAccount.getCountryCode(), returnedBankAccount.getCountryCode()); assertEquals(mBankAccount.getCurrency(), returnedBankAccount.getCurrency()); assertEquals(mBankAccount.getRoutingNumber(), returnedBankAccount.getRoutingNumber()); } catch (AuthenticationException authEx) { fail("Unexpected error: " + authEx.getLocalizedMessage()); } catch (StripeException stripeEx) { fail("Unexpected error when connecting to Stripe API: " + stripeEx.getLocalizedMessage()); } }
@Before public void setup() { mContext = ApplicationProvider.getApplicationContext(); String cvc = "123"; int month = 12; Calendar rightNow = Calendar.getInstance(); // Try to make the test card always expire next year mYear = rightNow.get(Calendar.YEAR) + 1; mCard = new Card(TEST_CARD_NUMBER, month, mYear, cvc); mBankAccount = new BankAccount( TEST_BANK_ACCOUNT_NUMBER, "US", "usd", TEST_BANK_ROUTING_NUMBER); }
@Nullable public static BankAccount fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } return new BankAccount( StripeJsonUtils.optString(jsonObject, FIELD_ACCOUNT_HOLDER_NAME), asBankAccountType( StripeJsonUtils.optString(jsonObject, FIELD_ACCOUNT_HOLDER_TYPE)), StripeJsonUtils.optString(jsonObject, FIELD_BANK_NAME), StripeJsonUtils.optCountryCode(jsonObject, FIELD_COUNTRY), StripeJsonUtils.optCurrency(jsonObject, FIELD_CURRENCY), StripeJsonUtils.optString(jsonObject, FIELD_FINGERPRINT), StripeJsonUtils.optString(jsonObject, FIELD_LAST4), StripeJsonUtils.optString(jsonObject, FIELD_ROUTING_NUMBER)); } }
@Test public void hashMapFromBankAccount_omitsEmptyFields() { BankAccount bankAccount = new BankAccount( BANK_ACCOUNT_NUMBER, "US", "usd", BANK_ROUTING_NUMBER); Map<String, Object> bankAccountMap = getMapFromHashMappedBankAccount(bankAccount); assertEquals(BANK_ACCOUNT_NUMBER, bankAccountMap.get("account_number")); assertEquals(BANK_ROUTING_NUMBER, bankAccountMap.get("routing_number")); assertEquals("US", bankAccountMap.get("country")); assertEquals("usd", bankAccountMap.get("currency")); assertFalse(bankAccountMap.containsKey("account_holder_name")); assertFalse(bankAccountMap.containsKey("account_holder_type")); }
@Test public void hashMapFromBankAccount_mapsCorrectFields() { final BankAccount bankAccount = new BankAccount(BANK_ACCOUNT_NUMBER, BANK_ACCOUNT_HOLDER_NAME, BankAccount.TYPE_INDIVIDUAL, null, "US", "usd", null, null, BANK_ROUTING_NUMBER); final Map<String, Object> bankAccountMap = getMapFromHashMappedBankAccount(bankAccount); assertEquals(BANK_ACCOUNT_NUMBER, bankAccountMap.get("account_number")); assertEquals(BANK_ROUTING_NUMBER, bankAccountMap.get("routing_number")); assertEquals("US", bankAccountMap.get("country")); assertEquals("usd", bankAccountMap.get("currency")); assertEquals(BANK_ACCOUNT_HOLDER_NAME, bankAccountMap.get("account_holder_name")); assertEquals(BankAccount.TYPE_INDIVIDUAL, bankAccountMap.get("account_holder_type")); }
BankAccount.fromJson(bankAccountObject)); } else if (Token.TYPE_CARD.equals(tokenType)) { final JSONObject cardObject = jsonObject.optJSONObject(FIELD_CARD);