Skip to content

Commit

Permalink
Create StripeRepository interface (#1387)
Browse files Browse the repository at this point in the history
* Create StripeRepository interface

`StripeRepository` represents data operations on Stripe API objects.
Creating an interface allow us to create fakes in tests.
  • Loading branch information
mshafrir-stripe authored Aug 15, 2019
1 parent f344ff2 commit ebfea18
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 174 deletions.
22 changes: 11 additions & 11 deletions stripe/src/main/java/com/stripe/android/CustomerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class CustomerSession {
@NonNull private final Set<String> mProductUsageTokens;
@Nullable private final Calendar mProxyNowCalendar;
@NonNull private final ThreadPoolExecutor mThreadPoolExecutor;
@NonNull private final StripeApiHandler mApiHandler;
@NonNull private final StripeRepository mStripeRepository;
@NonNull private final String mPublishableKey;
@Nullable private final String mStripeAccountId;

Expand Down Expand Up @@ -240,7 +240,7 @@ private CustomerSession(@NonNull Context context, @NonNull EphemeralKeyProvider
@NonNull EphemeralKeyProvider keyProvider,
@Nullable Calendar proxyNowCalendar,
@NonNull ThreadPoolExecutor threadPoolExecutor,
@NonNull StripeApiHandler apiHandler,
@NonNull StripeRepository stripeRepository,
@NonNull String publishableKey,
@Nullable String stripeAccountId,
boolean shouldPrefetchEphemeralKey) {
Expand All @@ -249,7 +249,7 @@ private CustomerSession(@NonNull Context context, @NonNull EphemeralKeyProvider
mThreadPoolExecutor = threadPoolExecutor;
mProxyNowCalendar = proxyNowCalendar;
mProductUsageTokens = new HashSet<>();
mApiHandler = apiHandler;
mStripeRepository = stripeRepository;
mStripeAccountId = stripeAccountId;
mPublishableKey = publishableKey;
mUiThreadHandler = new CustomerSessionHandler(new CustomerSessionHandler.Listener() {
Expand Down Expand Up @@ -774,7 +774,7 @@ private Source addCustomerSourceWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull String sourceId,
@NonNull @Source.SourceType String sourceType) throws StripeException {
return mApiHandler.addCustomerSource(
return mStripeRepository.addCustomerSource(
key.getCustomerId(),
mPublishableKey,
new ArrayList<>(mProductUsageTokens),
Expand All @@ -788,7 +788,7 @@ private Source addCustomerSourceWithKey(
private Source deleteCustomerSourceWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull String sourceId) throws StripeException {
return mApiHandler.deleteCustomerSource(
return mStripeRepository.deleteCustomerSource(
key.getCustomerId(),
mPublishableKey,
new ArrayList<>(mProductUsageTokens),
Expand All @@ -801,7 +801,7 @@ private Source deleteCustomerSourceWithKey(
private PaymentMethod attachCustomerPaymentMethodWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull String paymentMethodId) throws StripeException {
return mApiHandler.attachPaymentMethod(
return mStripeRepository.attachPaymentMethod(
key.getCustomerId(),
mPublishableKey,
new ArrayList<>(mProductUsageTokens),
Expand All @@ -814,7 +814,7 @@ private PaymentMethod attachCustomerPaymentMethodWithKey(
private PaymentMethod detachCustomerPaymentMethodWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull String paymentMethodId) throws StripeException {
return mApiHandler.detachPaymentMethod(
return mStripeRepository.detachPaymentMethod(
mPublishableKey,
new ArrayList<>(mProductUsageTokens),
paymentMethodId,
Expand All @@ -826,7 +826,7 @@ private PaymentMethod detachCustomerPaymentMethodWithKey(
private List<PaymentMethod> getCustomerPaymentMethodsWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull String paymentMethodType) throws StripeException {
return mApiHandler.getPaymentMethods(
return mStripeRepository.getPaymentMethods(
key.getCustomerId(),
paymentMethodType,
mPublishableKey,
Expand All @@ -839,7 +839,7 @@ private List<PaymentMethod> getCustomerPaymentMethodsWithKey(
private Customer setCustomerShippingInfoWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull ShippingInformation shippingInformation) throws StripeException {
return mApiHandler.setCustomerShippingInfo(
return mStripeRepository.setCustomerShippingInfo(
key.getCustomerId(),
mPublishableKey,
new ArrayList<>(mProductUsageTokens),
Expand All @@ -853,7 +853,7 @@ private Customer setCustomerSourceDefaultWithKey(
@NonNull CustomerEphemeralKey key,
@NonNull String sourceId,
@NonNull @Source.SourceType String sourceType) throws StripeException {
return mApiHandler.setDefaultCustomerSource(
return mStripeRepository.setDefaultCustomerSource(
key.getCustomerId(),
mPublishableKey,
new ArrayList<>(mProductUsageTokens),
Expand All @@ -875,7 +875,7 @@ private Customer setCustomerSourceDefaultWithKey(
@Nullable
private Customer retrieveCustomerWithKey(@NonNull CustomerEphemeralKey key)
throws StripeException {
return mApiHandler.retrieveCustomer(key.getCustomerId(),
return mStripeRepository.retrieveCustomer(key.getCustomerId(),
ApiRequest.Options.create(key.getSecret(), mStripeAccountId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class IssuingCardPinService
@NonNull
private final EphemeralKeyManager<IssuingCardEphemeralKey> mEphemeralKeyManager;
@NonNull
private final StripeApiHandler mApiHandler;
private final StripeRepository mStripeRepository;
@NonNull
private final OperationIdFactory mOperationIdFactory;
@NonNull
Expand Down Expand Up @@ -70,10 +70,10 @@ private IssuingCardPinService(
@VisibleForTesting
IssuingCardPinService(
@NonNull EphemeralKeyProvider keyProvider,
@NonNull StripeApiHandler apiHandler,
@NonNull StripeRepository stripeRepository,
@NonNull OperationIdFactory operationIdFactory) {
mOperationIdFactory = operationIdFactory;
mApiHandler = apiHandler;
mStripeRepository = stripeRepository;
mEphemeralKeyManager = new EphemeralKeyManager<>(
keyProvider,
this,
Expand Down Expand Up @@ -166,7 +166,7 @@ public void onKeyUpdate(@NonNull IssuingCardEphemeralKey ephemeralKey,
final String userOneTimeCode =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_ONE_TIME_CODE));
try {
final String pin = mApiHandler.retrieveIssuingCardPin(cardId, verificationId,
final String pin = mStripeRepository.retrieveIssuingCardPin(cardId, verificationId,
userOneTimeCode, ephemeralKey.getSecret());
listener.onIssuingCardPinRetrieved(pin);

Expand Down Expand Up @@ -237,8 +237,8 @@ public void onKeyUpdate(@NonNull IssuingCardEphemeralKey ephemeralKey,
final String userOneTimeCode =
(String) Objects.requireNonNull(arguments.get(ARGUMENT_ONE_TIME_CODE));
try {
mApiHandler.updateIssuingCardPin(cardId, newPin, verificationId, userOneTimeCode,
ephemeralKey.getSecret());
mStripeRepository.updateIssuingCardPin(cardId, newPin, verificationId,
userOneTimeCode, ephemeralKey.getSecret());
listener.onIssuingCardPinUpdated();
} catch (InvalidRequestException e) {
if ("expired".equals(e.getErrorCode())) {
Expand Down
Loading

0 comments on commit ebfea18

Please sign in to comment.