Skip to content

Commit

Permalink
Merge pull request #853 from stripe/danj/bugfix/840-optional-zip
Browse files Browse the repository at this point in the history
Fix `STPAddressViewModel.isValid` to use country when `requiredBillingFields = .Zip`
  • Loading branch information
danj-stripe authored Dec 12, 2017
2 parents a66870a + 9a1b3db commit 2459cb7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
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

0 comments on commit 2459cb7

Please sign in to comment.