Skip to content

Commit

Permalink
Add STPBillingAddressFieldsName option for STPBillingAddressFields (
Browse files Browse the repository at this point in the history
  • Loading branch information
joeydong-stripe authored Jun 11, 2018
1 parent cbed7bb commit 45f176c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* Fixes payment method label overlapping the checkmark, for Amex on small devices [#952](https://github.com/stripe/stripe-ios/pull/952)
* Adds EPS and Multibanco support to `STPSourceParams` [#961](https://github.com/stripe/stripe-ios/pull/961)
* Adds `STPBillingAddressFieldsName` option to `STPBillingAddressFields` [#964](https://github.com/stripe/stripe-ios/pull/964)

## 13.0.2 2018-05-24
* Makes iDEAL `name` parameter optional, also accepts empty string as `nil` [#940](https://github.com/stripe/stripe-ios/pull/940)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ class SettingsViewController: UITableViewController {
fileprivate enum RequiredBillingAddressFields: String {
case None = "None"
case Zip = "Zip"
case Name = "Name"
case Full = "Full"

init(row: Int) {
switch row {
case 0: self = .None
case 1: self = .Zip
case 2: self = .Name
default: self = .Full
}
}
Expand All @@ -125,6 +127,7 @@ class SettingsViewController: UITableViewController {
switch self {
case .None: return .none
case .Zip: return .zip
case .Name: return .name
case .Full: return .full
}
}
Expand Down Expand Up @@ -196,7 +199,7 @@ class SettingsViewController: UITableViewController {
switch Section(section: section) {
case .Theme: return 3
case .ApplePay: return 2
case .RequiredBillingAddressFields: return 3
case .RequiredBillingAddressFields: return 4
case .RequiredShippingAddressFields: return 4
case .ShippingType: return 2
case .Session: return 1
Expand Down
5 changes: 5 additions & 0 deletions Stripe/PublicHeaders/STPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ typedef NS_ENUM(NSUInteger, STPBillingAddressFields) {
Request the user's full billing address
*/
STPBillingAddressFieldsFull,

/**
Just request the user's billing name
*/
STPBillingAddressFieldsName,
};


Expand Down
6 changes: 6 additions & 0 deletions Stripe/STPAddress.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ - (BOOL)containsRequiredFields:(STPBillingAddressFields)requiredFields {
countryCode:self.country] == STPCardValidationStateValid);
case STPBillingAddressFieldsFull:
return [self hasValidPostalAddress];
case STPBillingAddressFieldsName:
return self.name.length > 0;
}
return containsFields;
}
Expand All @@ -181,6 +183,8 @@ - (BOOL)containsContentForBillingAddressFields:(STPBillingAddressFields)desiredF
return self.postalCode.length > 0;
case STPBillingAddressFieldsFull:
return [self hasPartialPostalAddress];
case STPBillingAddressFieldsName:
return self.name.length > 0;
}

return NO;
Expand Down Expand Up @@ -242,6 +246,8 @@ + (PKAddressField)applePayAddressFieldsFromBillingAddressFields:(STPBillingAddre
case STPBillingAddressFieldsZip:
case STPBillingAddressFieldsFull:
return PKAddressFieldPostalAddress;
case STPBillingAddressFieldsName:
return PKAddressFieldName;
}
}

Expand Down
5 changes: 5 additions & 0 deletions Stripe/STPAddressViewModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ - (instancetype)initWithRequiredBillingFields:(STPBillingAddressFields)requiredB
[[STPAddressFieldTableViewCell alloc] initWithType:STPAddressFieldTypeCountry contents:_addressFieldTableViewCountryCode lastInList:YES delegate:self],
];
break;
case STPBillingAddressFieldsName:
_addressCells = @[
[[STPAddressFieldTableViewCell alloc] initWithType:STPAddressFieldTypeName contents:@"" lastInList:YES delegate:self]
];
break;
}
[self commonInit];
}
Expand Down
2 changes: 2 additions & 0 deletions Stripe/STPAnalyticsClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ + (NSDictionary *)serializeConfiguration:(STPPaymentConfiguration *)configuratio
dictionary[@"required_billing_address_fields"] = @"zip";
case STPBillingAddressFieldsFull:
dictionary[@"required_billing_address_fields"] = @"full";
case STPBillingAddressFieldsName:
dictionary[@"required_billing_address_fields"] = @"name";
}
NSMutableArray<NSString *> *shippingFields = [NSMutableArray new];
if ([configuration.requiredShippingAddressFields containsObject:STPContactFieldName]) {
Expand Down
3 changes: 3 additions & 0 deletions Stripe/STPPaymentConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ - (NSString *)description {
case STPBillingAddressFieldsFull:
requiredBillingAddressFieldsDescription = @"STPBillingAddressFieldsFull";
break;
case STPBillingAddressFieldsName:
requiredBillingAddressFieldsDescription = @"STPBillingAddressFieldsName";
break;
}

NSString *requiredShippingAddressFieldsDescription = [self.requiredShippingAddressFields.allObjects componentsJoinedByString:@"|"];
Expand Down
23 changes: 21 additions & 2 deletions Tests/Tests/STPAddressTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ @interface STPAddressTests : XCTestCase
@implementation STPAddressTests

- (void)testInitWithPKContact_complete {

PKContact *contact = [PKContact new];
{
NSPersonNameComponents *name = [NSPersonNameComponents new];
Expand Down Expand Up @@ -196,7 +195,6 @@ - (void)testContainsRequiredFieldsNone {
XCTAssertTrue([address containsRequiredFields:STPBillingAddressFieldsNone]);
}


- (void)testContainsRequiredFieldsZip {
STPAddress *address = [STPAddress new];

Expand All @@ -215,6 +213,7 @@ - (void)testContainsRequiredFieldsZip {
address.country = nil; // nil treated as alphanumeric
XCTAssertTrue([address containsRequiredFields:STPBillingAddressFieldsZip]);
}

- (void)testContainsRequiredFieldsFull {
STPAddress *address = [STPAddress new];

Expand Down Expand Up @@ -268,13 +267,22 @@ - (void)testContainsRequiredFieldsFull {
XCTAssertTrue([address containsRequiredFields:STPBillingAddressFieldsFull]);
}

- (void)testContainsRequiredFieldsName {
STPAddress *address = [STPAddress new];

XCTAssertFalse([address containsRequiredFields:STPBillingAddressFieldsName]);
address.name = @"Jane Doe";
XCTAssertTrue([address containsRequiredFields:STPBillingAddressFieldsName]);
}

- (void)testContainsContentForBillingAddressFields {
STPAddress *address = [STPAddress new];

// Empty address should return false for everything
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsNone]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsZip]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsFull]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsName]);

// 1+ characters in postalCode will return true for .Zip && .Full
address.postalCode = @"0";
Expand All @@ -288,6 +296,14 @@ - (void)testContainsContentForBillingAddressFields {
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsFull]);
address.postalCode = nil;

// 1+ characters in name will return true for .Name
address.name = @"Jane Doe";
XCTAssertTrue([address containsContentForBillingAddressFields:STPBillingAddressFieldsName]);
// empty string returns false
address.name = @"";
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsName]);
address.name = nil;

// Test every other property that contributes to the full address, ensuring it returns True for .Full only
// This is *not* refactoring-safe, but I think it's better than a bunch of duplicated code
for (NSString *propertyName in @[@"line1", @"line2", @"city", @"state", @"country"]) {
Expand All @@ -296,6 +312,7 @@ - (void)testContainsContentForBillingAddressFields {
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsNone]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsZip]);
XCTAssertTrue([address containsContentForBillingAddressFields:STPBillingAddressFieldsFull]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsName]);
[address setValue:nil forKey:propertyName];
}

Expand All @@ -304,13 +321,15 @@ - (void)testContainsContentForBillingAddressFields {
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsNone]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsZip]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsFull]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsName]);
[address setValue:nil forKey:propertyName];
}

// ensure it still returns false for everything since it has been cleared
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsNone]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsZip]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsFull]);
XCTAssertFalse([address containsContentForBillingAddressFields:STPBillingAddressFieldsName]);
}

- (void)testContainsRequiredShippingAddressFields {
Expand Down
20 changes: 20 additions & 0 deletions Tests/Tests/STPAddressViewModelTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ - (void)testInitWithRequiredBillingFields {
for (NSUInteger i=0; i<[sut.addressCells count]; i++) {
XCTAssertEqual(sut.addressCells[i].type, [types[i] integerValue]);
}

sut = [[STPAddressViewModel alloc] initWithRequiredBillingFields:STPBillingAddressFieldsName];
XCTAssertTrue([sut.addressCells count] == 1);
XCTAssertEqual(sut.addressCells[0].type, STPAddressFieldTypeName);
}

- (void)testInitWithRequiredShippingFields {
Expand Down Expand Up @@ -163,4 +167,20 @@ - (void)testIsValid_Full {
XCTAssertTrue(sut.isValid);
}

- (void)testIsValid_Name {
STPAddressViewModel *sut = [[STPAddressViewModel alloc] initWithRequiredBillingFields:STPBillingAddressFieldsName];

STPAddress *address = [STPAddress new];

address.name = @"";
sut.address = address;
XCTAssertEqual(sut.addressCells.count, 1ul);
XCTAssertFalse(sut.isValid);

address.name = @"Jane Doe";
sut.address = address;
XCTAssertEqual(sut.addressCells.count, 1ul);
XCTAssertTrue(sut.isValid);
}

@end

0 comments on commit 45f176c

Please sign in to comment.