Skip to content

Commit

Permalink
move updateSourcesWithResponse out of setter
Browse files Browse the repository at this point in the history
  • Loading branch information
bg-stripe committed Dec 18, 2017
1 parent f6ea528 commit f3b2d72
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
13 changes: 7 additions & 6 deletions Stripe/STPCustomer.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

@interface STPCustomer()

@property (nonatomic, copy) NSString *stripeID;
@property (nonatomic) id<STPSourceProtocol> defaultSource;
@property (nonatomic) NSArray<id<STPSourceProtocol>> *sources;
@property (nonatomic) STPAddress *shippingAddress;
@property (nonatomic, readwrite, nonnull, copy) NSDictionary *allResponseFields;
@property (nonatomic, copy, readwrite) NSString *stripeID;
@property (nonatomic, strong, nullable, readwrite) id<STPSourceProtocol> defaultSource;
@property (nonatomic, strong, readwrite) NSArray<id<STPSourceProtocol>> *sources;
@property (nonatomic, strong, nullable, readwrite) STPAddress *shippingAddress;
@property (nonatomic, copy, readwrite) NSDictionary *allResponseFields;

@end

Expand Down Expand Up @@ -97,14 +97,15 @@ - (void)updateSourcesWithResponse:(NSDictionary *)response
if (![data isKindOfClass:[NSArray class]]) {
return;
}
self.defaultSource = nil;
NSString *defaultSourceId;
if ([response[@"default_source"] isKindOfClass:[NSString class]]) {
defaultSourceId = response[@"default_source"];
}
NSMutableArray *sources = [NSMutableArray new];
for (id contents in data) {
if ([contents isKindOfClass:[NSDictionary class]]) {
if ([contents[@"object"] isEqualToString:@"card"]) {
if ([contents[@"object"] isEqual:@"card"]) {
STPCard *card = [STPCard decodedObjectFromAPIResponse:contents];
BOOL includeCard = card != nil;
// ignore apple pay cards from the response
Expand Down
18 changes: 13 additions & 5 deletions Stripe/STPCustomerContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ - (void)clearCachedCustomer {
}

- (void)setCustomer:(STPCustomer *)customer {
if (self.includeApplePaySources) {
[customer updateSourcesWithResponse:customer.allResponseFields
filteringApplePay:NO];
}
_customer = customer;
_customerRetrievedDate = (customer) ? [NSDate date] : nil;
}

- (void)setIncludeApplePaySources:(BOOL)includeApplePaySources {
_includeApplePaySources = includeApplePaySources;
[self.customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!includeApplePaySources];
}

- (BOOL)shouldUseCachedCustomer {
if (!self.customer || !self.customerRetrievedDate) {
return NO;
Expand Down Expand Up @@ -85,11 +87,13 @@ - (void)retrieveCustomer:(STPCustomerCompletionBlock)completion {
}
[STPAPIClient retrieveCustomerUsingKey:ephemeralKey completion:^(STPCustomer *customer, NSError *error) {
if (customer) {
[customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!self.includeApplePaySources];
self.customer = customer;
}
if (completion) {
stpDispatchToMainThreadIfNecessary(^{
completion(self.customer, error);
completion(customer, error);
});
}
}];
Expand Down Expand Up @@ -134,6 +138,8 @@ - (void)selectDefaultCustomerSource:(id<STPSourceProtocol>)source completion:(ST
usingKey:ephemeralKey
completion:^(STPCustomer *customer, NSError *error) {
if (customer) {
[customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!self.includeApplePaySources];
self.customer = customer;
}
if (completion) {
Expand Down Expand Up @@ -162,6 +168,8 @@ - (void)updateCustomerWithShippingAddress:(STPAddress *)shipping completion:(STP
usingKey:ephemeralKey
completion:^(STPCustomer *customer, NSError *error) {
if (customer) {
[customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!self.includeApplePaySources];
self.customer = customer;
}
if (completion) {
Expand Down
6 changes: 2 additions & 4 deletions Tests/Tests/STPCustomerContextTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,9 @@ - (void)testFiltersApplePaySourcesByDefault {
STPCustomer *expectedCustomer = [STPFixtures customerWithCardAndApplePaySources];
[self stubRetrieveCustomerUsingKey:customerKey
returningCustomer:expectedCustomer
expectedCount:2];
expectedCount:1];
id mockKeyManager = [self mockKeyManagerWithKey:customerKey];
STPCustomerContext *sut = [[STPCustomerContext alloc] initWithKeyManager:mockKeyManager];
[sut clearCachedCustomer];
XCTestExpectation *exp = [self expectationWithDescription:@"retrieveCustomer"];
[sut retrieveCustomer:^(STPCustomer *customer, __unused NSError *error) {
XCTAssertEqual(customer.sources.count, (unsigned int)1);
Expand All @@ -330,11 +329,10 @@ - (void)testIncludeApplePaySources {
STPCustomer *expectedCustomer = [STPFixtures customerWithCardAndApplePaySources];
[self stubRetrieveCustomerUsingKey:customerKey
returningCustomer:expectedCustomer
expectedCount:2];
expectedCount:1];
id mockKeyManager = [self mockKeyManagerWithKey:customerKey];
STPCustomerContext *sut = [[STPCustomerContext alloc] initWithKeyManager:mockKeyManager];
sut.includeApplePaySources = YES;
[sut clearCachedCustomer];
XCTestExpectation *exp = [self expectationWithDescription:@"retrieveCustomer"];
[sut retrieveCustomer:^(STPCustomer *customer, __unused NSError *error) {
XCTAssertEqual(customer.sources.count, (unsigned int)2);
Expand Down

0 comments on commit f3b2d72

Please sign in to comment.