diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eaaae5a1f3..c5953c84b8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## ?? +* Add STPAddress initializer that takes STPPaymentMethodBillingDetails. [#1278](https://github.com/stripe/stripe-ios/pull/1278) +* Add convenience method to populate STPUserInformation with STPPaymentMethodBillingDetails. [#1278](https://github.com/stripe/stripe-ios/pull/1278) +* STPShippingAddressViewController prefills billing address for PaymentMethods too now, not just Card. [#1278](https://github.com/stripe/stripe-ios/pull/1278) + ## 16.0.5 2019-08-09 * Fixed an compatibility issue when building with certain Cocoapods configurations. [#1288](https://github.com/stripe/stripe-ios/issues/1288) diff --git a/Stripe/PublicHeaders/STPAddress.h b/Stripe/PublicHeaders/STPAddress.h index 03452a7f785..c1567b6a5e6 100644 --- a/Stripe/PublicHeaders/STPAddress.h +++ b/Stripe/PublicHeaders/STPAddress.h @@ -13,6 +13,7 @@ #import "STPFormEncodable.h" @class CNContact; +@class STPPaymentMethodBillingDetails; NS_ASSUME_NONNULL_BEGIN @@ -132,6 +133,14 @@ extern STPContactField const STPContactFieldName; + (nullable NSDictionary *)shippingInfoForChargeWithAddress:(nullable STPAddress *)address shippingMethod:(nullable PKShippingMethod *)method; +/** + Initializes a new STPAddress with data from STPPaymentMethodBillingDetails. + + @param billingDetails The STPPaymentMethodBillingDetails instance you want to populate the STPAddress from. + @return A new STPAddress instance with data copied from the passed in billing details. + */ +- (instancetype)initWithPaymentMethodBillingDetails:(STPPaymentMethodBillingDetails *)billingDetails; + /** Initializes a new STPAddress with data from an PassKit contact. diff --git a/Stripe/PublicHeaders/STPUserInformation.h b/Stripe/PublicHeaders/STPUserInformation.h index d365d321dd3..367a62e9a20 100644 --- a/Stripe/PublicHeaders/STPUserInformation.h +++ b/Stripe/PublicHeaders/STPUserInformation.h @@ -9,6 +9,8 @@ #import #import "STPAddress.h" +@class STPPaymentMethodBillingDetails; + NS_ASSUME_NONNULL_BEGIN /** @@ -23,6 +25,9 @@ NS_ASSUME_NONNULL_BEGIN The user's billing address. When set, the add card form will be filled with this address. The user will also have the option to fill their shipping address using this address. + + @note Set this using `setBillingAddressWithBillingDetails:` to use the billing + details from an `STPPaymentMethod` or `STPPaymentMethodParams` instance. */ @property (nonatomic, strong, nullable) STPAddress *billingAddress; @@ -33,6 +38,14 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, strong, nullable) STPAddress *shippingAddress; +/** + A convenience method to populate `billingAddress` with a PaymentMethod's billing details. + + @note Calling this overwrites the value of `billingAddress`. + */ +- (void)setBillingAddressWithBillingDetails:(STPPaymentMethodBillingDetails *)billingDetails +NS_SWIFT_NAME(setBillingAddress(with:)); + @end NS_ASSUME_NONNULL_END diff --git a/Stripe/STPAddress.m b/Stripe/STPAddress.m index 8247aed8390..abc80765f03 100644 --- a/Stripe/STPAddress.m +++ b/Stripe/STPAddress.m @@ -14,6 +14,8 @@ #import "STPCardValidator.h" #import "STPEmailAddressValidator.h" #import "STPFormEncoder.h" +#import "STPPaymentMethodAddress.h" +#import "STPPaymentMethodBillingDetails.h" #import "STPPhoneNumberValidator.h" #import "STPPostalCodeValidator.h" @@ -58,6 +60,23 @@ - (NSString *)sanitizedPhoneStringFromCNPhoneNumber:(CNPhoneNumber *)phoneNumber return stringIfHasContentsElseNil(phone); } +- (instancetype)initWithPaymentMethodBillingDetails:(STPPaymentMethodBillingDetails *)billingDetails { + self = [super init]; + if (self) { + _name = [billingDetails.name copy]; + _phone = [billingDetails.phone copy]; + _email = [billingDetails.email copy]; + STPPaymentMethodAddress *pmAddress = billingDetails.address; + _line1 = [pmAddress.line1 copy]; + _line2 = [pmAddress.line2 copy]; + _city = [pmAddress.city copy]; + _state = [pmAddress.state copy]; + _postalCode = [pmAddress.postalCode copy]; + _country = [pmAddress.country copy]; + } + return self; +} + - (instancetype)initWithCNContact:(CNContact *)contact { self = [super init]; if (self) { diff --git a/Stripe/STPShippingAddressViewController.m b/Stripe/STPShippingAddressViewController.m index 8f2d60293a4..ff328fe20ff 100644 --- a/Stripe/STPShippingAddressViewController.m +++ b/Stripe/STPShippingAddressViewController.m @@ -53,6 +53,9 @@ - (instancetype)initWithPaymentContext:(STPPaymentContext *)paymentContext { if ([paymentOption isKindOfClass:[STPCard class]]) { STPCard *card = (STPCard *)paymentOption; billingAddress = [card address]; + } else if ([paymentOption isKindOfClass:[STPPaymentMethod class]]) { + STPPaymentMethod *paymentMethod = (STPPaymentMethod *)paymentOption; + billingAddress = [[STPAddress alloc] initWithPaymentMethodBillingDetails:paymentMethod.billingDetails]; } STPUserInformation *prefilledInformation; if (paymentContext.prefilledInformation != nil) { diff --git a/Stripe/STPUserInformation.m b/Stripe/STPUserInformation.m index 6db66898c34..6b979a9ff08 100644 --- a/Stripe/STPUserInformation.m +++ b/Stripe/STPUserInformation.m @@ -19,4 +19,8 @@ - (id)copyWithZone:(__unused NSZone *)zone { return copy; } +- (void)setBillingAddressWithBillingDetails:(STPPaymentMethodBillingDetails *)billingDetails { + self.billingAddress = [[STPAddress alloc] initWithPaymentMethodBillingDetails:billingDetails]; +} + @end