Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix STPAddressViewModel.isValid to use country when requiredBillingFields = .Zip #853

Merged
merged 3 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Stripe/STPAddCardViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ - (void)createAndSetupViews {

self.inputAccessoryToolbar = [UIToolbar stp_inputAccessoryToolbarWithTarget:self action:@selector(paymentFieldNextTapped)];
[self.inputAccessoryToolbar stp_setEnabled:NO];
if (self.configuration.requiredBillingAddressFields != STPBillingAddressFieldsNone) {
paymentCell.inputAccessoryView = self.inputAccessoryToolbar;
}
[self updateInputAccessoryVisiblity];
self.tableView.dataSource = self;
self.tableView.delegate = self;

Expand Down Expand Up @@ -316,6 +314,14 @@ - (void)updateDoneButton {
);
}

- (void)updateInputAccessoryVisiblity {
// The inputAccessoryToolbar switches from the paymentCell to the first address field.
// It should only be shown when there *is* an address field. This compensates for the lack
// of a 'Return' key on the number pad used for paymentCell entry
BOOL hasAddressCells = self.addressViewModel.addressCells.count > 0;
self.paymentCell.inputAccessoryView = hasAddressCells ? self.inputAccessoryToolbar : nil;
}

- (void)setCustomFooterView:(UIView *)footerView {
_customFooterView = footerView;
[self.stp_willAppearPromise voidOnSuccess:^{
Expand Down Expand Up @@ -360,11 +366,13 @@ - (void)paymentCardTextFieldDidEndEditingCVC:(__unused STPPaymentCardTextField *
- (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel addedCellAtIndex:(NSUInteger)index {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self updateInputAccessoryVisiblity];
}

- (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel removedCellAtIndex:(NSUInteger)index {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self updateInputAccessoryVisiblity];
}

- (void)addressViewModelDidChange:(__unused STPAddressViewModel *)addressViewModel {
Expand Down
3 changes: 3 additions & 0 deletions Stripe/STPAddressViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ - (STPAddress *)address {
break;
}
}
// Prefer to use the contents of STPAddressFieldTypeCountry, but fallback to
// `addressFieldTableViewCountryCode` if nil (important for STPBillingAddressFieldsZip)
address.country = address.country ?: self.addressFieldTableViewCountryCode;
return address;
}

Expand Down
25 changes: 24 additions & 1 deletion Tests/Tests/STPAddressViewModelTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ @implementation STPAddressViewModelTest
- (void)testInitWithRequiredBillingFields {
STPAddressViewModel *sut = [[STPAddressViewModel alloc] initWithRequiredBillingFields:STPBillingAddressFieldsNone];
XCTAssertTrue([sut.addressCells count] == 0);
XCTAssertTrue(sut.isValid);

sut = [[STPAddressViewModel alloc] initWithRequiredBillingFields:STPBillingAddressFieldsZip];
XCTAssertTrue([sut.addressCells count] == 1);
Expand Down Expand Up @@ -126,7 +127,29 @@ - (void)testSetAddress {
XCTAssertEqualObjects(sut.addressCells[8].contents, @"555-555-5555");
}

- (void)testIsValid {
- (void)testIsValid_Zip {
STPAddressViewModel *sut = [[STPAddressViewModel alloc] initWithRequiredBillingFields:STPBillingAddressFieldsZip];

STPAddress *address = [STPAddress new];

address.country = @"US";
sut.address = address;
XCTAssertEqual(sut.addressCells.count, 1ul);
XCTAssertFalse(sut.isValid, @"US addresses have postalCode, require it when requiredBillingFields is .Zip");

address.postalCode = @"94016";
sut.address = address;
XCTAssertTrue(sut.isValid);

address.country = @"MO"; // in Macao, postalCode is optional
address.postalCode = nil;
sut.address = address;
XCTAssertEqual(sut.addressCells.count, 0ul);
XCTAssertTrue(sut.isValid, @"in Macao, postalCode is optional, valid without one");
}


- (void)testIsValid_Full {
STPAddressViewModel *sut = [[STPAddressViewModel alloc] initWithRequiredBillingFields:STPBillingAddressFieldsFull];
XCTAssertFalse(sut.isValid);
sut.addressCells[0].contents = @"John Smith";
Expand Down