@NonNull @Source.SourceType public String getSourceType() { if (mStripePaymentSource instanceof Card) { return Source.CARD; } else if (mStripePaymentSource instanceof Source) { return ((Source) mStripePaymentSource).getType(); } else { return Source.UNKNOWN; } }
@Test public void fromJsonString_withCustomType_createsSourceWithCustomType() { Source customSource = Source.fromString(EXAMPLE_JSON_SOURCE_CUSTOM_TYPE); assertNotNull("Parsing failure", customSource); assertEquals(Source.UNKNOWN, customSource.getType()); assertEquals(DOGE_COIN, customSource.getTypeRaw()); assertNull(customSource.getSourceTypeModel()); assertNotNull("Failed to find custom api params", customSource.getSourceTypeData()); } }
boolean canDisplaySource(@Nullable Source source) { return source != null && Source.CARD.equals(source.getType()); }
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(); }
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(); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_SELECT_SOURCE && resultCode == RESULT_OK) { 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(); mSelectedSourceTextView.setText(buildCardString(cardData)); } } }
@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 updateSourceList(@Nullable Source source) { if (source == null) { mRedirectAdapter.addItem( "No source found", "Stopped", "Error", "None"); return; } mRedirectAdapter.addItem( source.getStatus(), "complete", source.getId(), source.getType()); }
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); } }
@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(); } }
/** * Set the card data displayed using a {@link CustomerSource} object. If the object * is not a {@link Source} object that represents a card and the object returns {@code null} * from its {@link CustomerSource#asCard()} method, then no values will be set on this control. * * @param customerSource the {@link CustomerSource} to be partially displayed */ void setCustomerSource(@NonNull CustomerSource customerSource) { Source source = customerSource.asSource(); if (source != null && source.getSourceTypeModel() != null && Source.CARD.equals(source.getType()) && source.getSourceTypeModel() instanceof SourceCardData) { SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel(); setSourceCardData(sourceCardData); return; } Card card = customerSource.asCard(); if (card != null) { setCard(card); } }
public void logEventSynchronous( @NonNull List<String> productUsageTokens, @NonNull StripePaymentSource paymentSource) { RequestOptions.RequestOptionsBuilder builder = RequestOptions.builder(mDefaultPublishableKey); if (mStripeAccount != null) { builder.setStripeAccount(mStripeAccount); } RequestOptions options = builder.build(); final Map<String, Object> loggingMap; if (paymentSource instanceof Token) { Token token = (Token) paymentSource; loggingMap = LoggingUtils.getTokenCreationParams( mContext, productUsageTokens, mDefaultPublishableKey, token.getType()); } else { Source source = (Source) paymentSource; loggingMap = LoggingUtils.getSourceCreationParams( mContext, productUsageTokens, mDefaultPublishableKey, source.getType()); } StripeApiHandler.logApiCall(loggingMap, options, mLoggingResponseListener); }
@Test public void createSourceSynchronous_withSepaDebitParamsWithMinimalValues_passesIntegrationTest() { Stripe stripe = getNonLoggingStripe(mContext); String validIban = "DE89370400440532013000"; SourceParams params = SourceParams.createSepaDebitParams( "Sepa Account Holder", validIban, null, null, null, null, null); Map<String, String> metamap = new HashMap<String, String>() {{ put("water source", "well"); put("type", "brackish"); put("value", "100000"); }}; params.setMetaData(metamap); try { Source sepaDebitSource = stripe.createSourceSynchronous(params, FUNCTIONAL_SOURCE_PUBLISHABLE_KEY); assertNotNull(sepaDebitSource); assertNotNull(sepaDebitSource.getClientSecret()); assertNotNull(sepaDebitSource.getId()); assertEquals(Source.SEPA_DEBIT, sepaDebitSource.getType()); JsonTestUtils.assertMapEquals(metamap ,sepaDebitSource.getMetaData()); } catch (StripeException stripeEx) { fail("Unexpected error: " + stripeEx.getLocalizedMessage()); } }
assertNotNull(idealSource.getId()); assertEquals(5500L, idealSource.getAmount().longValue()); assertEquals(Source.IDEAL, idealSource.getType()); assertEquals("eur", idealSource.getCurrency()); assertNotNull(idealSource.getSourceTypeData());
assertNotNull(idealSource.getId()); assertEquals(5500L, idealSource.getAmount().longValue()); assertEquals(Source.IDEAL, idealSource.getType()); assertEquals("eur", idealSource.getCurrency()); assertNotNull(idealSource.getSourceTypeData());
assertNotNull(sofortSource.getClientSecret()); assertNotNull(sofortSource.getId()); assertEquals(Source.SOFORT, sofortSource.getType()); assertEquals("eur", sofortSource.getCurrency()); assertNotNull(sofortSource.getSourceTypeData());
@Test public void createSourceSynchronous_withAlipaySingleUseParams_passesIntegrationTest() { Stripe stripe = getNonLoggingStripe(mContext); SourceParams alipayParams = SourceParams.createAlipaySingleUseParams( 1000L, "usd", "Example Payer", "abc@def.com", "stripe://start"); try { Source alipaySource = stripe.createSourceSynchronous(alipayParams, FUNCTIONAL_SOURCE_PUBLISHABLE_KEY); assertNotNull(alipaySource); assertNotNull(alipaySource.getId()); assertNotNull(alipaySource.getClientSecret()); assertNotNull(alipaySource.getAmount()); assertEquals(1000L, alipaySource.getAmount().longValue()); assertEquals(Source.ALIPAY, alipaySource.getType()); assertEquals("redirect", alipaySource.getFlow()); assertNotNull(alipaySource.getOwner()); assertEquals("Example Payer", alipaySource.getOwner().getName()); assertEquals("abc@def.com", alipaySource.getOwner().getEmail()); assertEquals("usd", alipaySource.getCurrency()); assertEquals(Source.SINGLE_USE, alipaySource.getUsage()); assertNotNull(alipaySource.getRedirect()); assertEquals("stripe://start", alipaySource.getRedirect().getReturnUrl()); } catch (StripeException stripeEx) { fail("Unexpected error: " + stripeEx.getLocalizedMessage()); } }
assertNotNull(idealSource.getId()); assertEquals(5500L, idealSource.getAmount().longValue()); assertEquals(Source.IDEAL, idealSource.getType()); assertEquals("eur", idealSource.getCurrency()); assertNotNull(idealSource.getSourceTypeData());
@Test public void createSourceSynchronous_withAlipayReusableParams_passesIntegrationTest() { Stripe stripe = getNonLoggingStripe(mContext); SourceParams alipayParams = SourceParams.createAlipayReusableParams( "usd", "Example Payer", "abc@def.com", "stripe://start"); try { Source alipaySource = stripe.createSourceSynchronous(alipayParams, FUNCTIONAL_SOURCE_PUBLISHABLE_KEY); assertNotNull(alipaySource); assertNotNull(alipaySource.getId()); assertNotNull(alipaySource.getClientSecret()); assertEquals(Source.ALIPAY, alipaySource.getType()); assertEquals("redirect", alipaySource.getFlow()); assertNotNull(alipaySource.getOwner()); assertEquals("Example Payer", alipaySource.getOwner().getName()); assertEquals("abc@def.com", alipaySource.getOwner().getEmail()); assertEquals("usd", alipaySource.getCurrency()); assertEquals(Source.REUSABLE, alipaySource.getUsage()); assertNotNull(alipaySource.getRedirect()); assertEquals("stripe://start", alipaySource.getRedirect().getReturnUrl()); } catch (StripeException stripeEx) { fail("Unexpected error: " + stripeEx.getLocalizedMessage()); } }
@Test public void createSourceSynchronous_withP24Params_passesIntegrationTest() { Stripe stripe = getNonLoggingStripe(mContext); SourceParams p24Params = SourceParams.createP24Params( 100, "eur", "Example Payer", "abc@def.com", "stripe://start"); try { Source p24Source = stripe.createSourceSynchronous(p24Params, FUNCTIONAL_SOURCE_PUBLISHABLE_KEY); assertNotNull(p24Source); assertNotNull(p24Source.getId()); assertNotNull(p24Source.getClientSecret()); assertEquals(Source.P24, p24Source.getType()); assertEquals("redirect", p24Source.getFlow()); assertNotNull(p24Source.getOwner()); assertEquals("Example Payer", p24Source.getOwner().getName()); assertEquals("abc@def.com", p24Source.getOwner().getEmail()); assertEquals("eur", p24Source.getCurrency()); assertEquals(Source.SINGLE_USE, p24Source.getUsage()); assertNotNull(p24Source.getRedirect()); assertEquals("stripe://start", p24Source.getRedirect().getReturnUrl()); } catch (StripeException stripeEx) { fail("Unexpected error: " + stripeEx.getLocalizedMessage()); } }