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

Adding STPAddress containsContentFor...Fields: methods to check for any useful data #834

Merged
merged 6 commits into from
Nov 9, 2017

Conversation

danj-stripe
Copy link
Contributor

@danj-stripe danj-stripe commented Nov 8, 2017

Summary

These are like an isEmpty check for the address, but they check the fields needed
for a particular billing or shipping address setting.

Motivation

This addresses #831, which was introduced when
STPShippingAddressViewController.billingAddress started receiving an empty STPAddress
from STPCard instead of a nil one.

Testing

Unit tests, and manual testing through the standard integration app.

Other Notes

I'm not in love with the method names.

I had trouble deciding on the semantics of containsContentFor...Fields: .None (should it
return true or false?). I chose to always return NO. If the question is "Does this address
have useful data for me, and I'm interested in none of the data from this address?", then
false/NO makes sense. It also parallels with the behavior when messaging nil.

… any useful data

These are like an `isEmpty` check for the address, but they check the fields needed
for a particular billing or shipping address setting.

This addresses #831, which was introduced when
`STPShippingAddressViewController.billingAddress` started receiving an empty STPAddress
from STPCard instead of a nil one.

I had trouble deciding on the semantics of `containsContentForFields: .None` (should it
return true or false?). I chose to always return NO. If the question is "Does this address
have useful data for me, and I'm interested in none of the data from this address?", then
false/NO makes sense. It also parallels with the behavior when messaging nil.

Unit tests to come, and I'm not in love with the method names.
BOOL buttonVisible = (needsAddress && self.shippingAddress != nil && !self.hasUsedShippingAddress);
BOOL requiredFields = self.configuration.requiredBillingAddressFields;
BOOL needsAddress = requiredFields != STPBillingAddressFieldsNone && !self.addressViewModel.isValid;
BOOL buttonVisible = (needsAddress &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There wasn't actually a bug here (shippingAddress was still nil), but I chose to make a parallel change to keep things consistent and guard against future changes.

Copy link
Contributor

@bdorfman-stripe bdorfman-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good I think. Left a few comments but otherwise I think this all makes sense.

I think we should possibly also revisit our overwriting logic for these autofills and hide the button if the user has manually entered text into the fields, but that can be a separate task.

@parameter desiredFields The billing address information the caller is interested in.
@return YES if there is any data in this STPAddress that's relevant for those fields.
*/
- (BOOL)containsContentForFields:(STPBillingAddressFields)desiredFields;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be billingAddressFields. It doesn't match the other one (which was written before billing address existed) but I think new methods going forward should make clear whether they are doing billing or shipping fields and at some point we can deprecate/rename the old ones. This is what we did for the newer Apple Pay billing address related method further down.

BOOL needsAddress = self.configuration.requiredShippingAddressFields & PKAddressFieldPostalAddress && !self.addressViewModel.isValid;
BOOL buttonVisible = (needsAddress && self.billingAddress != nil && !self.hasUsedBillingAddress);
PKAddressField requiredFields = self.configuration.requiredShippingAddressFields;
BOOL needsAddress = requiredFields & PKAddressFieldPostalAddress && !self.addressViewModel.isValid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity I think we should put parens around the expression that is a bitwise AND here because it's easy to misinterpret what's happening.

@danj-stripe danj-stripe changed the title [WIP] Adding STPAddress containsContent...ForFields: methods to check for any useful data Adding STPAddress containsContent...ForFields: methods to check for any useful data Nov 8, 2017
@danj-stripe danj-stripe changed the title Adding STPAddress containsContent...ForFields: methods to check for any useful data Adding STPAddress containsContentFor...Fields: methods to check for any useful data Nov 8, 2017

// Test every property that contributes to the full address
// This is *not* refactoring-safe, but I think it's better than a bunch more duplicated code
for (NSString *propertyName in @[@"line1", @"line2", @"city", @"state", @"postalCode", @"country"]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do something like NSStringFromSelector(@selector(line1)) instead of @"line1" for some more compiler safety?

This matches `testContainsRequiredShippingAddressFields`
@stripe-ci stripe-ci removed the approved label Nov 9, 2017
I made a dumb mistake in STPAddCardViewController, declared the wrong type for the
`requiredFields` variable.

The snapshot tests for Add Card and Shipping Address were apparently relying on the
using a non-nil & empty Address to display the "Use <> address" button. Put a piece of
data into the address to keep triggering the button.
@danj-stripe danj-stripe merged commit ab37217 into master Nov 9, 2017
@danj-stripe danj-stripe deleted the danj-stripe/bugfix/831-billing-button branch November 9, 2017 17:15
davidme-stripe pushed a commit that referenced this pull request Mar 7, 2022
* Sync strings

* Add snapshot tests for localized mandate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants