Skip to content

Commit

Permalink
Merge pull request #1020 from stripe/danj/bugfix/changing-selected-so…
Browse files Browse the repository at this point in the history
…urce

Remove `response` parameter from `STPCustomer` method to update Sources with/without Apple Pay
  • Loading branch information
danj-stripe authored Sep 18, 2018
2 parents ec45e49 + 4f01c93 commit 46e99bb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
9 changes: 4 additions & 5 deletions Stripe/STPCustomer+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ NS_ASSUME_NONNULL_BEGIN
@interface STPCustomer ()

/**
Replaces the customer's sources and defaultSource with the contents of a
Customer API response.
Replaces the customer's `sources` and `defaultSource` based on whether or not
they should include Apple Pay sources. More details on documentation for
`STPCustomerContext includeApplePaySources`
@param response The Customer API response
@param filterApplePay If YES, Apple Pay sources will be ignored
*/
- (void)updateSourcesWithResponse:(NSDictionary *)response
filteringApplePay:(BOOL)filterApplePay;
- (void)updateSourcesFilteringApplePay:(BOOL)filterApplePay;

@end

Expand Down
6 changes: 3 additions & 3 deletions Stripe/STPCustomer.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ + (nullable instancetype)decodedObjectFromAPIResponse:(nullable NSDictionary *)r
}
customer.sources = @[];
customer.defaultSource = nil;
[customer updateSourcesWithResponse:dict filteringApplePay:YES];
customer.allResponseFields = dict;
[customer updateSourcesFilteringApplePay:YES];
return customer;
}

- (void)updateSourcesWithResponse:(NSDictionary *)response
filteringApplePay:(BOOL)filterApplePay {
- (void)updateSourcesFilteringApplePay:(BOOL)filterApplePay {
NSDictionary *response = self.allResponseFields;
NSArray *data;
NSDictionary *sourcesDict = [response stp_dictionaryForKey:@"sources"];
if (sourcesDict) {
Expand Down
12 changes: 4 additions & 8 deletions Stripe/STPCustomerContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ - (void)setCustomer:(STPCustomer *)customer {

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

- (BOOL)shouldUseCachedCustomer {
Expand Down Expand Up @@ -87,8 +86,7 @@ - (void)retrieveCustomer:(STPCustomerCompletionBlock)completion {
}
[STPAPIClient retrieveCustomerUsingKey:ephemeralKey completion:^(STPCustomer *customer, NSError *error) {
if (customer) {
[customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!self.includeApplePaySources];
[customer updateSourcesFilteringApplePay:!self.includeApplePaySources];
self.customer = customer;
}
if (completion) {
Expand Down Expand Up @@ -138,8 +136,7 @@ - (void)selectDefaultCustomerSource:(id<STPSourceProtocol>)source completion:(ST
usingKey:ephemeralKey
completion:^(STPCustomer *customer, NSError *error) {
if (customer) {
[customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!self.includeApplePaySources];
[customer updateSourcesFilteringApplePay:!self.includeApplePaySources];
self.customer = customer;
}
if (completion) {
Expand Down Expand Up @@ -168,8 +165,7 @@ - (void)updateCustomerWithShippingAddress:(STPAddress *)shipping completion:(STP
usingKey:ephemeralKey
completion:^(STPCustomer *customer, NSError *error) {
if (customer) {
[customer updateSourcesWithResponse:self.customer.allResponseFields
filteringApplePay:!self.includeApplePaySources];
[customer updateSourcesFilteringApplePay:!self.includeApplePaySources];
self.customer = customer;
}
if (completion) {
Expand Down
23 changes: 17 additions & 6 deletions Tests/Tests/STPCustomerContextTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,40 @@ - (void)testAttachSourceToCustomerCallsAPIClientCorrectly {

- (void)testSelectDefaultCustomerSourceCallsAPIClientCorrectly {
STPEphemeralKey *customerKey = [STPFixtures ephemeralKey];
STPCustomer *expectedCustomer = [STPFixtures customerWithSingleCardTokenSource];
STPCustomer *initialCustomer = [STPFixtures customerWithSourcesFromJSONKeys:@[STPTestJSONCard,
STPTestJSONSourceCard]
defaultSource:STPTestJSONCard];
id<STPSourceProtocol> initialSource = initialCustomer.defaultSource;
id<STPSourceProtocol> changedSource = [initialCustomer.sources lastObject];

id mockAPIClient = OCMClassMock([STPAPIClient class]);
[self stubRetrieveCustomerUsingKey:customerKey
returningCustomer:expectedCustomer
returningCustomer:initialCustomer
expectedCount:1
mockAPIClient:mockAPIClient];
STPSource *expectedSource = [STPFixtures cardSource];

XCTAssertNotEqual(initialSource, changedSource, @"ensure call to selectDefaultCustomerSource: is changing the defaultSource");
XCTestExpectation *exp = [self expectationWithDescription:@"updateCustomer"];
NSDictionary *expectedParams = @{@"default_source": expectedSource.stripeID};
NSDictionary *expectedParams = @{@"default_source": changedSource.stripeID};
OCMStub([mockAPIClient updateCustomerWithParameters:[OCMArg isEqual:expectedParams]
usingKey:[OCMArg isEqual:customerKey]
completion:[OCMArg any]])
.andDo(^(NSInvocation *invocation) {
STPCustomerCompletionBlock completion;
[invocation getArgument:&completion atIndex:4];
completion([STPFixtures customerWithSingleCardTokenSource], nil);
completion([STPFixtures customerWithSourcesFromJSONKeys:@[STPTestJSONCard,
STPTestJSONSourceCard]
defaultSource:STPTestJSONSourceCard],
nil);
[exp fulfill];
});
id mockKeyManager = [self mockKeyManagerWithKey:customerKey];
XCTestExpectation *exp2 = [self expectationWithDescription:@"selectDefaultSource"];
STPCustomerContext *sut = [[STPCustomerContext alloc] initWithKeyManager:mockKeyManager];
[sut selectDefaultCustomerSource:expectedSource completion:^(NSError *error) {
XCTAssertEqualObjects(sut.customer.defaultSource, initialSource, @"defaultSource should be the defaultSource of the Customer returned by the API");
[sut selectDefaultCustomerSource:changedSource completion:^(NSError *error) {
XCTAssertNil(error);
XCTAssertEqualObjects(sut.customer.defaultSource, changedSource, @"defaultSource should be the new source");
[exp2 fulfill];
}];

Expand Down

0 comments on commit 46e99bb

Please sign in to comment.