void addCustomerSourceIfSupported(CustomerSource... customerSources) { if (customerSources == null) { return; } for (CustomerSource customerSource : customerSources) { if (customerSource.asCard() != null || canDisplaySource(customerSource.asSource())) { // If it's a card, we can display it. mCustomerSourceList.add(customerSource); } } notifyDataSetChanged(); }
private void finishWithSelection(String selectedSourceId) { CustomerSource customerSource = mCustomer.getSourceById(selectedSourceId); if (customerSource != null) { Intent intent = new Intent(); intent.putExtra(EXTRA_SELECTED_PAYMENT, customerSource.toString()); setResult(RESULT_OK, intent); } else { setResult(RESULT_CANCELED); } finish(); }
@Nullable public static CustomerSource fromString(@Nullable String jsonString) { try { return fromJson(new JSONObject(jsonString)); } catch (JSONException ignored) { return null; } }
if (selectedSource == null || selectedSource.getId() == null) { return; this, selectedSource.getId(), selectedSource.getSourceType(), listener); } else { mCustomerSessionProxy.setCustomerDefaultSource( selectedSource.getId(), selectedSource.getSourceType(), listener);
@Test public void fromJson_whenCard_createsCustomerSourceData() { try { JSONObject jsonCard = new JSONObject(JSON_CARD); CustomerSource sourceData = CustomerSource.fromJson(jsonCard); assertNotNull(sourceData); assertNotNull(sourceData.asCard()); assertEquals("card_189fi32eZvKYlo2CHK8NPRME", sourceData.getId()); assertNull(sourceData.getTokenizationMethod()); } catch (JSONException jsonException) { fail("Test data failure: " + jsonException.getMessage()); } }
@Test public void fromJson_whenSource_createsCustomerSourceData() { CustomerSource sourceData = CustomerSource.fromString(EXAMPLE_JSON_SOURCE_WITHOUT_NULLS); assertNotNull(sourceData); assertNotNull(sourceData.asSource()); assertEquals("src_19t3xKBZqEXluyI4uz2dxAfQ", sourceData.getId()); }
CustomerSource source = mCustomer.getSourceById(data.getSelectedPaymentMethodId()); if (source != null) { Source cardSource = source.asSource(); stringBuilder.append("Payment Info:\n"); if (cardSource != null) { .append(scd.getLast4()); } else { stringBuilder.append('\n').append(source.toString()).append('\n');
@Test public void setCustomerSource_withCardObject_setsCorrectData() { CustomerSource customerSource = CustomerSource.fromString(JSON_CARD); assertNotNull(customerSource); assertNotNull(customerSource.asCard()); assertNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertEquals(Card.MASTERCARD, mMaskedCardView.getCardBrand()); assertEquals("5555", mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }
@Test public void setCustomerSource_withCardSource_setsCorrectData() { CustomerSource customerSource = CustomerSource.fromString(EXAMPLE_JSON_CARD_SOURCE); assertNotNull(customerSource); assertNotNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertEquals(Card.VISA, mMaskedCardView.getCardBrand()); assertEquals("4242", mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }
@Override public void onCustomerRetrieved(@NonNull Customer customer) { String sourceId = customer.getDefaultSource(); if (sourceId == null) { displayError("No payment method selected"); return; } CustomerSource source = customer.getSourceById(sourceId); proceedWithPurchaseIf3DSCheckIsNotNecessary(source.asSource(), customer.getId()); }
try { JSONObject customerSourceObject = dataArray.getJSONObject(i); CustomerSource sourceData = CustomerSource.fromJson(customerSourceObject); if (sourceData == null || VALUE_APPLE_PAY.equals(sourceData.getTokenizationMethod())) { continue;
@Test public void getSourceType_whenCard_returnsCard() { try { JSONObject jsonCard = new JSONObject(JSON_CARD); CustomerSource sourceData = CustomerSource.fromJson(jsonCard); assertNotNull(sourceData); assertEquals(Source.CARD, sourceData.getSourceType()); } catch (JSONException jsonException) { fail("Test data failure: " + jsonException.getMessage()); } }
@Test public void setSelection_changesSelection() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource secondCardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, secondCardSource); assertEquals(2, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver, times(1)).onChanged(); assertNull(mMaskedCardAdapter.getSelectedSource()); assertNotNull(secondCardSource); assertNotNull(secondCardSource.getId()); mMaskedCardAdapter.setSelectedSource(secondCardSource.getId()); verify(mAdapterDataObserver, times(2)).onChanged(); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(secondCardSource.getId(), mMaskedCardAdapter.getSelectedSource().getId()); }
@Nullable public CustomerSource getSourceById(@NonNull String sourceId) { for (CustomerSource source : mSources) { if (sourceId.equals(source.getId())) { return source; } } return null; }
@Test public void getSourceType_whenSourceThatIsNotCard_returnsSourceType() { CustomerSource alipaySource = CustomerSource.fromString(EXAMPLE_ALIPAY_SOURCE); assertNotNull(alipaySource); assertEquals(Source.ALIPAY, alipaySource.getSourceType()); } }
@Test public void fromExampleJsonSource_toJson_createsSameObject() { try { JSONObject original = new JSONObject(EXAMPLE_JSON_SOURCE_WITHOUT_NULLS); CustomerSource sourceData = CustomerSource.fromJson(original); assertNotNull(sourceData); JsonTestUtils.assertJsonEquals(original, sourceData.toJson()); } catch (JSONException exception) { fail("Test data failure: " + exception.getMessage()); } }
@Test public void setCustomerSourceList_filtersOutNonCardSources() { List<CustomerSource> customerSourceList = new ArrayList<>(); CustomerSource cardCustomerSource = Mockito.mock(CustomerSource.class); Card card = Mockito.mock(Card.class); Mockito.when(cardCustomerSource.asCard()).thenReturn(card); CustomerSource nonCardCustomerSource = Mockito.mock(CustomerSource.class); customerSourceList.add(cardCustomerSource); customerSourceList.add(nonCardCustomerSource); mMaskedCardAdapter.setCustomerSourceList(customerSourceList); assertEquals(1, mMaskedCardAdapter.getItemCount()); } }
@Test public void fromJson_whenCardWithTokenization_createsSourceDataWithTokenization() { try { JSONObject jsonCard = new JSONObject(JSON_APPLE_PAY_CARD); CustomerSource sourceData = CustomerSource.fromJson(jsonCard); assertNotNull(sourceData); assertNotNull(sourceData.asCard()); assertEquals("card_189fi32eZvKYlo2CHK8NPRME", sourceData.getId()); assertEquals("apple_pay", sourceData.getTokenizationMethod()); } catch (JSONException jsonException) { fail("Test data failure: " + jsonException.getMessage()); } }
@Test public void setCustomerSource_withNonCardSource_setsNoData() { CustomerSource customerSource = CustomerSource.fromString(EXAMPLE_JSON_SOURCE_BITCOIN); assertNotNull(customerSource); assertNotNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertNull(mMaskedCardView.getCardBrand()); assertNull(mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }