@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()); }
@Test public void fromJson_whenCustomer_returnsExpectedCustomer() { Customer customer = Customer.fromString(TEST_CUSTOMER_OBJECT); assertNotNull(customer); assertEquals("cus_AQsHpvKfKwJDrF", customer.getId()); assertEquals("abc123", customer.getDefaultSource()); assertNull(customer.getShippingInformation()); assertNotNull(customer.getSources()); assertEquals("/v1/customers/cus_AQsHpvKfKwJDrF/sources", customer.getUrl()); assertFalse(customer.getHasMore()); assertEquals(Integer.valueOf(0), customer.getTotalCount()); }
return null; Customer customer = new Customer(); customer.mId = optString(jsonObject, FIELD_ID); customer.mDefaultSource = optString(jsonObject, FIELD_DEFAULT_SOURCE);
void updateCustomer(@NonNull Customer customer) { mCustomerSourceList.clear(); CustomerSource[] customerSources = new CustomerSource[customer.getSources().size()]; addCustomerSourceIfSupported(customer.getSources().toArray(customerSources)); String sourceId = customer.getDefaultSource(); if (sourceId == null) { updateSelectedIndex(NO_SELECTION); } else { setSelectedSource(sourceId); } notifyDataSetChanged(); }
@Test public void onSaveMenuItem_sendsSelectionToApi_finishedWithExpectedResult() { Customer customer = Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(customer); assertNotNull(customer.getDefaultSource()); List<CustomerSource> sourceList = customer.getSources(); assertEquals(customer.getDefaultSource(), sourceList.get(0).getId()); listenerArgumentCaptor.capture()); assertEquals(customer.getDefaultSource(), selectionArgumentCaptor.getValue()); CustomerSession.CustomerRetrievalListener listener = listenerArgumentCaptor.getValue(); assertNotNull(listener); assertNotNull(intent); CustomerSource customerSource = customer.getSourceById(customer.getDefaultSource()); assertNotNull(customerSource); assertEquals(customerSource.toString(), intent.getStringExtra(EXTRA_SELECTED_PAYMENT));
@Nullable static Customer retrieveCustomer(@NonNull String customerId, @NonNull String secret) throws InvalidRequestException, APIConnectionException, APIException, AuthenticationException, CardException { final StripeResponse response = getStripeResponse( GET, getRetrieveCustomerUrl(customerId), null, RequestOptions.builder(secret).setApiVersion(API_VERSION).build()); convertErrorsToExceptionsAndThrowIfNecessary(response); return Customer.fromString(response.getResponseBody()); }
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(); }
@Test public void fromJson_whenCustomerHasApplePay_returnsCustomerWithoutApplePaySources() { Customer customer = Customer.fromString(createTestCustomerObjectWithApplePaySource()); assertNotNull(customer); assertEquals(2, customer.getSources().size()); // Note that filtering the apple_pay sources intentionally does not change the total // count value. assertEquals(Integer.valueOf(5), customer.getTotalCount()); }
session.addProductUsageTokenIfValid("PaymentMethodsActivity"); Customer customerWithShippingInfo = Objects .requireNonNull(Customer.fromString(FIRST_TEST_CUSTOMER_OBJECT_WITH_SHIPPING_INFO)); ShippingInformation shippingInformation = Objects.requireNonNull(customerWithShippingInfo .getShippingInformation()); session.setCustomerShippingInformation(ApplicationProvider.getApplicationContext(), shippingInformation); verify(mStripeApiProxy).setCustomerShippingInfoWithKey( eq(ApplicationProvider.getApplicationContext()), eq(mFirstCustomer.getId()), eq("pk_test_abc123"), mListArgumentCaptor.capture(),
@Test public void updateCustomer_removesExistingSourcesAndAddsAllCustomerSources() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource); assertEquals(1, mMaskedCardAdapter.getItemCount()); assertNull(mMaskedCardAdapter.getSelectedSource()); Customer customer = Customer.fromString(PaymentMethodsActivityTest.TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(customer); mMaskedCardAdapter.updateCustomer(customer); assertEquals(2, mMaskedCardAdapter.getItemCount()); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(customer.getDefaultSource(), mMaskedCardAdapter.getSelectedSource().getId()); verify(mAdapterDataObserver, times(4)).onChanged(); }
verify(mStripeApiProxy).setDefaultCustomerSourceWithKey( eq(ApplicationProvider.getApplicationContext()), eq(mFirstCustomer.getId()), eq("pk_test_abc123"), mListArgumentCaptor.capture(), Customer capturedCustomer = customerArgumentCaptor.getValue(); assertNotNull(capturedCustomer); assertEquals(mSecondCustomer.getId(), capturedCustomer.getId());
@Override public void onCustomerRetrieved(@NonNull Customer customer) { mCustomer = customer; finishWithSelection(customer.getDefaultSource()); setCommunicatingProgress(false); }
@Test public void onActivityResult_whenOneSourceButNoSelection_updatesSelectedItem() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_ONE_SOURCE_NO_SELECTION); assertNotNull(updatedCustomer); assertEquals(updatedCustomer.getSources().get(0).getId(), stringArgumentCaptor.getValue()); assertNotNull(updateListener); Customer anotherCustomer = Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(anotherCustomer);
@Test public void fromJson_toJson_createsSameObject() { try { JSONObject rawJsonCustomer = new JSONObject(TEST_CUSTOMER_OBJECT); Customer customer = Customer.fromString(TEST_CUSTOMER_OBJECT); assertNotNull(customer); JsonTestUtils.assertJsonEquals(rawJsonCustomer, customer.toJson()); } catch (JSONException testDataException) { fail("Test data failure: " + testDataException.getMessage()); } }
@Test public void updateCustomer_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); Customer customer = Mockito.mock(Customer.class); Mockito.when(customer.getSources()).thenReturn(customerSourceList); mMaskedCardAdapter.updateCustomer(customer); assertEquals(1, mMaskedCardAdapter.getItemCount()); }
@Nullable public static Customer fromString(String jsonString) { if (jsonString == null) { return null; } try { return fromJson(new JSONObject(jsonString)); } catch (JSONException ignored) { return null; } }
if (!TextUtils.isEmpty(customer.getDefaultSource()) || customer.getSources().size() != 1) { updateAdapterWithCustomer(customer); return; CustomerSource customerSource = customer.getSources().get(0); if (customerSource == null || customerSource.getId() == null) {
return Customer.fromString(response.getResponseBody());
CustomerSource source = mCustomer.getSourceById(data.getSelectedPaymentMethodId()); if (source != null) { Source cardSource = source.asSource();
assertEquals(firstKey.getCustomerId(), mFirstCustomer.getId()); assertEquals(firstKey.getCustomerId(), session.getCustomer().getId()); assertEquals(mFirstCustomer.getId(), capturedCustomer.getId()); assertNotNull(session.getCustomer()); assertEquals(mFirstCustomer.getId(), session.getCustomer().getId()); verifyNoMoreInteractions(mStripeApiProxy);