@Nullable static SourceCardData fromJson(@Nullable JSONObject jsonObject) { if (jsonObject == null) { return null; } SourceCardData cardData = new SourceCardData(); cardData.setAddressLine1Check(optString(jsonObject, FIELD_ADDRESS_LINE1_CHECK)) .setAddressZipCheck(optString(jsonObject, FIELD_ADDRESS_ZIP_CHECK)) .setBrand(Card.asCardBrand(optString(jsonObject, FIELD_BRAND))) .setCountry(optString(jsonObject, FIELD_COUNTRY)) .setCvcCheck(optString(jsonObject, FIELD_CVC_CHECK)) .setDynamicLast4(optString(jsonObject, FIELD_DYNAMIC_LAST4)) .setExpiryMonth(optInteger(jsonObject, FIELD_EXP_MONTH)) .setExpiryYear(optInteger(jsonObject, FIELD_EXP_YEAR)) .setFunding(Card.asFundingType(optString(jsonObject, FIELD_FUNDING))) .setLast4(optString(jsonObject, FIELD_LAST4)) .setThreeDSecureStatus(asThreeDSecureStatus(optString(jsonObject, FIELD_THREE_D_SECURE))) .setTokenizationMethod(optString(jsonObject, FIELD_TOKENIZATION_METHOD)); Map<String, Object> nonStandardFields = jsonObjectToMapWithoutKeys(jsonObject, cardData.mStandardFields); if (nonStandardFields != null) { cardData.setAdditionalFields(nonStandardFields); } return cardData; }
private SourceCardData() { super(); addStandardFields( FIELD_ADDRESS_LINE1_CHECK, FIELD_ADDRESS_ZIP_CHECK, FIELD_BRAND, FIELD_COUNTRY, FIELD_CVC_CHECK, FIELD_DYNAMIC_LAST4, FIELD_EXP_MONTH, FIELD_EXP_YEAR, FIELD_FUNDING, FIELD_LAST4, FIELD_THREE_D_SECURE, FIELD_TOKENIZATION_METHOD); }
@Test public void fromExampleJsonCard_createsExpectedObject() { SourceCardData cardData = SourceCardData.fromString(EXAMPLE_JSON_SOURCE_CARD_DATA_WITH_APPLE_PAY); assertNotNull(cardData); assertEquals(Card.VISA, cardData.getBrand()); assertEquals(0, cardData.getAdditionalFields().size()); assertEquals(Card.FUNDING_CREDIT, cardData.getFunding()); assertEquals("4242", cardData.getLast4()); assertNotNull(cardData.getExpiryMonth()); assertNotNull(cardData.getExpiryYear()); assertEquals(12, cardData.getExpiryMonth().intValue()); assertEquals(2050, cardData.getExpiryYear().intValue()); assertEquals("US", cardData.getCountry()); assertEquals("optional", cardData.getThreeDSecureStatus()); assertEquals("apple_pay", cardData.getTokenizationMethod()); }
private String buildCardString(@NonNull SourceCardData data) { return getString(R.string.credit_card_format_string, data.getLast4()); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data == null) { // Data might be null if user tapped the back button from credit card selection return; } String selectedSource = data.getStringExtra(PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT); Source source = Source.fromString(selectedSource); // Note: It isn't possible for a null or non-card source to be returned. if (source != null && Source.CARD.equals(source.getType())) { SourceCardData cardData = (SourceCardData) source.getSourceTypeModel(); TextView methodTextView = findViewById(R.id.selected_payment_method_text_view); TextView cardBrandTextView = findViewById(R.id.selected_payment_method_brand_text_view); methodTextView.setText(buildCardString(cardData)); cardBrandTextView.setText(cardData.getBrand()); selectMethodSwitcher.showNext(); didSelectPaymentMethod = true; updateBuyButton(); } }
@Nullable public String getTokenizationMethod() { Source paymentAsSource = asSource(); Card paymentAsCard = asCard(); if (paymentAsSource != null && paymentAsSource.getType().equals(Source.CARD)) { SourceCardData cardData = (SourceCardData) paymentAsSource.getSourceTypeModel(); if (cardData != null) { return cardData.getTokenizationMethod(); } } else if (paymentAsCard != null) { return paymentAsCard.getTokenizationMethod(); } return null; }
private void proceedWithPurchaseIf3DSCheckIsNotNecessary(Source source, String customerId) { if (source == null || !Source.CARD.equals(source.getType())) { displayError("Something went wrong - this should be rare"); return; } SourceCardData cardData = (SourceCardData) source.getSourceTypeModel(); if (SourceCardData.REQUIRED.equals(cardData.getThreeDSecureStatus())) { // In this case, you would need to ask the user to verify the purchase. // You can see an example of how to do this in the 3DS example application. // In stripe-android/example. } else { // If 3DS is not required, you can charge the source. completePurchase(source.getId(), customerId); } }
private void addToList(@Nullable Source source) { if (source == null || !Source.CARD.equals(source.getType())) { return; } SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel(); String endingIn = getString(R.string.endingIn); Map<String, String> map = new HashMap<>(); map.put("last4", endingIn + " " + sourceCardData.getLast4()); map.put("tokenId", source.getId()); mCardSources.add(map); mSimpleAdapter.notifyDataSetChanged(); }
/** * Set the card data displayed using a {@link SourceCardData} object. * * @param sourceCardData the {@link SourceCardData} to be partially displayed */ void setSourceCardData(@NonNull SourceCardData sourceCardData) { mCardBrand = sourceCardData.getBrand(); mLast4 = sourceCardData.getLast4(); updateBrandIcon(); updateCardInformation(); }
private String formatSourceDescription(Source source) { if (Source.CARD.equals(source.getType())) { SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(sourceCardData.getBrand()).append(getString(R.string.ending_in)).append(sourceCardData.getLast4()); return stringBuilder.toString(); } return source.getType(); }
@Test public void whenSourceNotCard_doesNotCrash() { SourceCardData sourceCardData = Mockito.mock(SourceCardData.class); Mockito.when(sourceCardData.getBrand()).thenReturn("unrecognized_brand"); Mockito.when(sourceCardData.getLast4()).thenReturn(""); mMaskedCardView.setSourceCardData(sourceCardData); } }