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

STPAddress <-> STPPaymentMethod #1278

Merged
merged 4 commits into from
Aug 9, 2019
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
9 changes: 9 additions & 0 deletions Stripe/PublicHeaders/STPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "STPFormEncodable.h"

@class CNContact;
@class STPPaymentMethodBillingDetails;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions Stripe/PublicHeaders/STPUserInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <Foundation/Foundation.h>
#import "STPAddress.h"

@class STPPaymentMethodBillingDetails;

NS_ASSUME_NONNULL_BEGIN

/**
Expand All @@ -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;

Expand All @@ -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
19 changes: 19 additions & 0 deletions Stripe/STPAddress.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions Stripe/STPShippingAddressViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions Stripe/STPUserInformation.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ - (id)copyWithZone:(__unused NSZone *)zone {
return copy;
}

- (void)setBillingAddressWithBillingDetails:(STPPaymentMethodBillingDetails *)billingDetails {
self.billingAddress = [[STPAddress alloc] initWithPaymentMethodBillingDetails:billingDetails];
}

@end