Skip to content

Commit

Permalink
Add ability to read/write shipping to a PaymentIntent (#1558)
Browse files Browse the repository at this point in the history
* Add PaymentIntent shipping details

* Add shipping to STPPaymentIntent

* Add shipping to STPPaymentIntent confirm

* PR feedback

* Undo NS_UNAVAILABLE new

* Fix broken test b/c bad merge conflict
  • Loading branch information
yuki-stripe committed Apr 28, 2020
1 parent 9320104 commit a8fba70
Show file tree
Hide file tree
Showing 18 changed files with 665 additions and 5 deletions.
4 changes: 2 additions & 2 deletions STYLEGUIDE.md
Expand Up @@ -231,9 +231,9 @@ NS_ASSUME_NON_NULL_END
- Properties should be defined using this syntax:

```
@property (<nonatomic / atomic>, <weak / copy / strong>, <nullable / _>, <readonly / readwrite>) <class> *<name>;
@property (<nonatomic / atomic>, <weak / copy / _>, <nullable / _>, <readonly / _>) <class> *<name>;
@property (<nonatomic / atomic>, <assign>, <readonly / readwrite>) <type> <name>;
@property (<nonatomic / atomic>, <readonly / _>) <type> <name>;
```

- Omit default properties (`assign`, `readwrite`, `strong`)
Expand Down
48 changes: 48 additions & 0 deletions Stripe.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Stripe/PublicHeaders/STPPaymentIntent.h
Expand Up @@ -14,7 +14,7 @@

NS_ASSUME_NONNULL_BEGIN

@class STPIntentAction, STPPaymentIntentLastPaymentError;
@class STPIntentAction, STPPaymentIntentLastPaymentError, STPPaymentIntentShippingDetails;

/**
A PaymentIntent tracks the process of collecting a payment from your customer.
Expand Down Expand Up @@ -123,6 +123,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, nullable, readonly) STPPaymentIntentLastPaymentError *lastPaymentError;

/**
Shipping information for this PaymentIntent.
*/
@property (nonatomic, nullable, readonly) STPPaymentIntentShippingDetails *shipping;

#pragma mark - Deprecated

/**
Expand Down
8 changes: 7 additions & 1 deletion Stripe/PublicHeaders/STPPaymentIntentParams.h
Expand Up @@ -16,7 +16,8 @@ NS_ASSUME_NONNULL_BEGIN
STPMandateDataParams,
STPSourceParams,
STPPaymentMethodParams,
STPPaymentResult;
STPPaymentResult,
STPPaymentIntentShippingDetailsParams;

/**
An object representing parameters used to confirm a PaymentIntent object.
Expand Down Expand Up @@ -141,6 +142,11 @@ STPPaymentResult;
*/
@property (nonatomic, nullable) STPConfirmPaymentMethodOptions *paymentMethodOptions;

/**
Shipping information.
*/
@property (nonatomic, nullable) STPPaymentIntentShippingDetailsParams *shipping;

/**
The URL to redirect your customer back to after they authenticate or cancel
their payment on the payment method’s app or site.
Expand Down
57 changes: 57 additions & 0 deletions Stripe/PublicHeaders/STPPaymentIntentShippingDetails.h
@@ -0,0 +1,57 @@
//
// STPPaymentIntentShippingDetails.h
// Stripe
//
// Created by Yuki Tokuhiro on 4/27/20.
// Copyright © 2020 Stripe, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "STPAPIResponseDecodable.h"

@class STPPaymentIntentShippingDetailsAddress;

NS_ASSUME_NONNULL_BEGIN

/**
Shipping information for a PaymentIntent
@see https://stripe.com/docs/api/payment_intents/object#payment_intent_object-shipping
*/
@interface STPPaymentIntentShippingDetails : NSObject <STPAPIResponseDecodable>

/**
Shipping address.
*/
@property (nonatomic, nullable, readonly) STPPaymentIntentShippingDetailsAddress *address;

/**
Recipient name.
*/
@property (nonatomic, nullable, copy, readonly) NSString *name;

/**
The delivery service that shipped a physical product, such as Fedex, UPS, USPS, etc.
*/
@property (nonatomic, nullable, copy, readonly) NSString *carrier;

/**
Recipient phone (including extension).
*/
@property (nonatomic, nullable, copy, readonly) NSString *phone;

/**
The tracking number for a physical product, obtained from the delivery service. If multiple tracking numbers were generated for this purchase, please separate them with commas.
*/
@property (nonatomic, nullable, copy, readonly) NSString *trackingNumber;

/**
You cannot directly instantiate an `STPPaymentIntentShippingDetails`.
You should only use one that is part of an existing `STPPaymentMethod` object.
*/
- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
60 changes: 60 additions & 0 deletions Stripe/PublicHeaders/STPPaymentIntentShippingDetailsAddress.h
@@ -0,0 +1,60 @@
//
// STPPaymentIntentShippingDetailsAddress.h
// Stripe
//
// Created by Yuki Tokuhiro on 4/27/20.
// Copyright © 2020 Stripe, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "STPAPIResponseDecodable.h"

NS_ASSUME_NONNULL_BEGIN

/**
Shipping address for a PaymentIntent's shipping details.
@see https://stripe.com/docs/api/payment_intents/object#payment_intent_object-shipping
*/
@interface STPPaymentIntentShippingDetailsAddress : NSObject <STPAPIResponseDecodable>

/**
City/District/Suburb/Town/Village.
*/
@property (nonatomic, copy, nullable, readonly) NSString *city;

/**
Two-letter country code (ISO 3166-1 alpha-2).
*/
@property (nonatomic, copy, nullable, readonly) NSString *country;

/**
Address line 1 (Street address/PO Box/Company name).
*/
@property (nonatomic, copy, nullable, readonly) NSString *line1;

/**
Address line 2 (Apartment/Suite/Unit/Building).
*/
@property (nonatomic, copy, nullable, readonly) NSString *line2;

/**
ZIP or postal code.
*/
@property (nonatomic, copy, nullable, readonly) NSString *postalCode;

/**
State/County/Province/Region.
*/
@property (nonatomic, copy, nullable, readonly) NSString *state;

/**
You cannot directly instantiate an `STPPaymentIntentShippingDetailsAddress`.
You should only use one that is part of an existing `STPPaymentMethod` object.
*/
- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
@@ -0,0 +1,64 @@
//
// STPPaymentIntentShippingDetailsAddressParams.h
// Stripe
//
// Created by Yuki Tokuhiro on 4/27/20.
// Copyright © 2020 Stripe, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "STPFormEncodable.h"

NS_ASSUME_NONNULL_BEGIN

/**
Shipping address for a PaymentIntent's shipping details.
@see https://stripe.com/docs/api/payment_intents/confirm#confirm_payment_intent-shipping-address
*/
@interface STPPaymentIntentShippingDetailsAddressParams : NSObject <NSCopying, STPFormEncodable>

/**
City/District/Suburb/Town/Village.
*/
@property (nonatomic, copy, nullable) NSString *city;

/**
Two-letter country code (ISO 3166-1 alpha-2).
*/
@property (nonatomic, copy, nullable) NSString *country;

/**
Address line 1 (Street address/PO Box/Company name).
*/
@property (nonatomic, copy) NSString *line1;

/**
Address line 2 (Apartment/Suite/Unit/Building).
*/
@property (nonatomic, copy, nullable) NSString *line2;

/**
ZIP or postal code.
*/
@property (nonatomic, copy, nullable) NSString *postalCode;

/**
State/County/Province/Region.
*/
@property (nonatomic, copy, nullable) NSString *state;

/**
Initialize an `STPPaymentIntentShippingDetailsAddressParams` instance with required properties.
*/
- (instancetype)initWithLine1:(NSString *)line1;

/**
Use `initWithLine1:` instead.
*/
- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
61 changes: 61 additions & 0 deletions Stripe/PublicHeaders/STPPaymentIntentShippingDetailsParams.h
@@ -0,0 +1,61 @@
//
// STPPaymentIntentShippingDetailsParams.h
// Stripe
//
// Created by Yuki Tokuhiro on 4/27/20.
// Copyright © 2020 Stripe, Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "STPFormEncodable.h"

@class STPPaymentIntentShippingDetailsAddressParams;

NS_ASSUME_NONNULL_BEGIN

/**
Shipping information for a PaymentIntent
@see https://stripe.com/docs/api/payment_intents/confirm#confirm_payment_intent-shipping
*/
@interface STPPaymentIntentShippingDetailsParams : NSObject <NSCopying, STPFormEncodable>

/**
Shipping address.
*/
@property (nonatomic) STPPaymentIntentShippingDetailsAddressParams *address;

/**
Recipient name.
*/
@property (nonatomic, copy) NSString *name;

/**
The delivery service that shipped a physical product, such as Fedex, UPS, USPS, etc.
*/
@property (nonatomic, nullable, copy) NSString *carrier;

/**
Recipient phone (including extension).
*/
@property (nonatomic, nullable, copy) NSString *phone;

/**
The tracking number for a physical product, obtained from the delivery service. If multiple tracking numbers were generated for this purchase, please separate them with commas.
*/
@property (nonatomic, nullable, copy) NSString *trackingNumber;

/**
Initialize an `STPPaymentIntentShippingDetailsParams` with required properties.
*/
- (instancetype)initWithAddress:(STPPaymentIntentShippingDetailsAddressParams *)address name:(NSString *)name;

/**
Use `initWithAddress:name:` instead.
*/
- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions Stripe/PublicHeaders/Stripe.h
Expand Up @@ -65,6 +65,10 @@
#import "STPPaymentIntentLastPaymentError.h"
#import "STPPaymentIntentParams.h"
#import "STPPaymentIntentParams+Utilities.h"
#import "STPPaymentIntentShippingDetails.h"
#import "STPPaymentIntentShippingDetailsAddress.h"
#import "STPPaymentIntentShippingDetailsAddressParams.h"
#import "STPPaymentIntentShippingDetailsParams.h"
#import "STPPaymentIntentSourceAction.h"
#import "STPPaymentIntentSourceActionAuthorizeWithURL.h"
#import "STPPaymentMethod.h"
Expand Down
5 changes: 4 additions & 1 deletion Stripe/STPPaymentIntent.m
Expand Up @@ -12,6 +12,7 @@
#import "STPPaymentIntentAction.h"
#import "STPPaymentIntentLastPaymentError.h"
#import "STPPaymentMethod+Private.h"
#import "STPPaymentIntentShippingDetails.h"

#import "NSDictionary+Stripe.h"
#import "NSArray+Stripe.h"
Expand All @@ -36,6 +37,7 @@ @interface STPPaymentIntent ()
@property (nonatomic, copy, nullable, readwrite) NSArray<NSNumber *> *paymentMethodTypes;
@property (nonatomic) STPPaymentIntentSetupFutureUsage setupFutureUsage;
@property (nonatomic, nullable, readwrite) STPPaymentIntentLastPaymentError *lastPaymentError;
@property (nonatomic, nullable, readwrite) STPPaymentIntentShippingDetails *shipping;

@property (nonatomic, copy, nonnull, readwrite) NSDictionary *allResponseFields;
@end
Expand Down Expand Up @@ -67,7 +69,7 @@ - (NSString *)description {
[NSString stringWithFormat:@"paymentMethodTypes = %@", [self.allResponseFields stp_arrayForKey:@"payment_method_types"]],
[NSString stringWithFormat:@"receiptEmail = %@", self.receiptEmail],
[NSString stringWithFormat:@"setupFutureUsage = %@", self.allResponseFields[@"setup_future_usage"]],
[NSString stringWithFormat:@"shipping = %@", self.allResponseFields[@"shipping"]],
[NSString stringWithFormat:@"shipping = %@", self.shipping],
[NSString stringWithFormat:@"sourceId = %@", self.sourceId],
[NSString stringWithFormat:@"status = %@", [self.allResponseFields stp_stringForKey:@"status"]],
];
Expand Down Expand Up @@ -199,6 +201,7 @@ + (nullable instancetype)decodedObjectFromAPIResponse:(nullable NSDictionary *)r
NSString *rawSetupFutureUsage = [dict stp_stringForKey:@"setup_future_usage"];
paymentIntent.setupFutureUsage = rawSetupFutureUsage ? [[self class] setupFutureUsageFromString:rawSetupFutureUsage] : STPPaymentIntentSetupFutureUsageNone;
paymentIntent.lastPaymentError = [STPPaymentIntentLastPaymentError decodedObjectFromAPIResponse:[dict stp_dictionaryForKey:@"last_payment_error"]];
paymentIntent.shipping = [STPPaymentIntentShippingDetails decodedObjectFromAPIResponse:[dict stp_dictionaryForKey:@"shipping"]];

paymentIntent.allResponseFields = dict;

Expand Down
4 changes: 4 additions & 0 deletions Stripe/STPPaymentIntentParams.m
Expand Up @@ -13,6 +13,7 @@
#import "STPMandateCustomerAcceptanceParams.h"
#import "STPMandateOnlineParams+Private.h"
#import "STPMandateDataParams.h"
#import "STPPaymentIntentShippingDetailsParams.h"
#import "STPPaymentIntent+Private.h"
#import "STPPaymentMethod.h"
#import "STPPaymentMethodParams.h"
Expand Down Expand Up @@ -58,6 +59,7 @@ - (NSString *)description {
[NSString stringWithFormat:@"returnURL = %@", self.returnURL],
[NSString stringWithFormat:@"savePaymentMethod = %@", (self.savePaymentMethod.boolValue) ? @"YES" : @"NO"],
[NSString stringWithFormat:@"setupFutureUsage = %@", self.setupFutureUsage],
[NSString stringWithFormat:@"shipping = %@", self.shipping],
[NSString stringWithFormat:@"useStripeSDK = %@", (self.useStripeSDK.boolValue) ? @"YES" : @"NO"],

// Source
Expand Down Expand Up @@ -163,6 +165,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
copy.mandateData = self.mandateData;
copy.mandate = self.mandate;
copy.paymentMethodOptions = self.paymentMethodOptions;
copy.shipping = self.shipping;
copy.additionalAPIParameters = self.additionalAPIParameters;

return copy;
Expand All @@ -189,6 +192,7 @@ + (nonnull NSDictionary *)propertyNamesToFormFieldNamesMapping {
NSStringFromSelector(@selector(mandateData)) : @"mandate_data",
NSStringFromSelector(@selector(mandate)) : @"mandate",
NSStringFromSelector(@selector(paymentMethodOptions)) : @"payment_method_options",
NSStringFromSelector(@selector(shipping)) : @"shipping",
};
}

Expand Down

0 comments on commit a8fba70

Please sign in to comment.