Migrate STPAddCardViewController to PaymentMethods

This commit is contained in:
Yuki Tokuhiro 2019-05-17 12:09:20 -07:00
parent e2f4440bec
commit a3247e365a
4 changed files with 40 additions and 92 deletions

View File

@ -128,7 +128,7 @@ class BrowseViewController: UITableViewController, STPAddCardViewControllerDeleg
dismiss(animated: true, completion: nil)
}
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: @escaping STPErrorBlock) {
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: @escaping STPErrorBlock) {
dismiss(animated: true, completion: nil)
}

View File

@ -319,7 +319,7 @@ static NSString * TitleForLocalizedScreen(LocalizedScreen screen) {
}
- (void)addCardViewController:(__unused STPAddCardViewController *)addCardViewController
didCreateToken:(__unused STPToken *)token
didCreatePaymentMethod:(__unused STPPaymentMethod *)paymentMethod
completion:(__unused STPErrorBlock)completion {
[self.navigationController popToRootViewControllerAnimated:NO];
}

View File

@ -50,11 +50,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, strong, nullable) STPUserInformation *prefilledInformation;
/**
If you're using the token generated from STPAddCardViewController to make a Managed Account, you should set this property to the currency that account will use. Otherwise, you should leave it empty. For more information, see https://stripe.com/docs/api#create_card_token-card-currency
*/
@property (nonatomic, copy, nullable) NSString *managedAccountCurrency;
/**
Provide this view controller with a footer view.
@ -98,44 +93,22 @@ NS_ASSUME_NONNULL_BEGIN
- (void)addCardViewControllerDidCancel:(STPAddCardViewController *)addCardViewController;
@optional
/**
This is called when the user successfully adds a card and Stripe returns a
card token.
Note: If `createsCardSource` is true, this method will not be called;
`addCardViewController:didCreateSource:` will be called instead.
You should send the token to your backend to store it on a customer, and then
call the provided `completion` block when that call is finished. If an error
occurs while talking to your backend, call `completion(error)`, otherwise,
dismiss (or pop) the view controller.
@param addCardViewController the view controller that successfully created a token
@param token the Stripe token that was created. @see STPToken
@param completion call this callback when you're done sending the token to your backend
*/
- (void)addCardViewController:(STPAddCardViewController *)addCardViewController
didCreateToken:(STPToken *)token
completion:(STPErrorBlock)completion;
/**
This is called when the user successfully adds a card and Stripe returns a
card source.
Note: If `createsCardSource` is false, this method will not be called;
`addCardViewController:didCreateToken:` will be called instead.
You should send the source to your backend to store it on a customer, and then
Payment Method.
You should send the PaymentMethod to your backend to store it on a customer, and then
call the provided `completion` block when that call is finished. If an error
occurs while talking to your backend, call `completion(error)`, otherwise,
dismiss (or pop) the view controller.
@param addCardViewController the view controller that successfully created a token
@param source the Stripe source that was created. @see STPSource
@param paymentMethod the Payment Method that was created. @see STPPaymentMethod
@param completion call this callback when you're done sending the token to your backend
*/
- (void)addCardViewController:(STPAddCardViewController *)addCardViewController
didCreateSource:(STPSource *)source
didCreatePaymentMethod:(STPPaymentMethod *)paymentMethod
completion:(STPErrorBlock)completion;
@end

View File

@ -28,8 +28,6 @@
#import "STPPaymentCardTextFieldCell.h"
#import "STPPromise.h"
#import "STPSectionHeaderView.h"
#import "STPSourceParams.h"
#import "STPToken.h"
#import "STPWeakStrongMacros.h"
#import "StripeError.h"
#import "UIBarButtonItem+Stripe.h"
@ -283,64 +281,41 @@ typedef NS_ENUM(NSUInteger, STPPaymentCardSection) {
- (void)nextPressed:(__unused id)sender {
self.loading = YES;
STPCardParams *cardParams = self.paymentCell.paymentField.cardParams;
cardParams.address = self.addressViewModel.address;
cardParams.currency = self.managedAccountCurrency;
if (cardParams) {
// Create and return a card source
if (self.configuration.createCardSources) {
STPSourceParams *sourceParams = [STPSourceParams cardParamsWithCard:cardParams];
[self.apiClient createSourceWithParams:sourceParams completion:^(STPSource * _Nullable source, NSError * _Nullable tokenizationError) {
if (tokenizationError) {
[self handleCardTokenizationError:tokenizationError];
}
else {
if ([self.delegate respondsToSelector:@selector(addCardViewController:didCreateSource:completion:)]) {
[self.delegate addCardViewController:self didCreateSource:source completion:^(NSError * _Nullable error) {
stpDispatchToMainThreadIfNecessary(^{
if (error) {
[self handleCardTokenizationError:error];
}
else {
self.loading = NO;
}
});
}];
}
else {
self.loading = NO;
}
}
}];
}
// Create and return a card token
else {
[self.apiClient createTokenWithCard:cardParams completion:^(STPToken *token, NSError *tokenizationError) {
if (tokenizationError) {
[self handleCardTokenizationError:tokenizationError];
}
else {
if ([self.delegate respondsToSelector:@selector(addCardViewController:didCreateToken:completion:)]) {
[self.delegate addCardViewController:self didCreateToken:token completion:^(NSError * _Nullable error) {
stpDispatchToMainThreadIfNecessary(^{
if (error) {
[self handleCardTokenizationError:error];
}
else {
self.loading = NO;
}
});
}];
}
else {
self.loading = NO;
}
}
}];
}
if (!cardParams) {
return;
}
// Create and return a Payment Method
STPPaymentMethodCardParams *paymentMethodCardParams = [[STPPaymentMethodCardParams alloc] initWithCardSourceParams:cardParams];
STPPaymentMethodBillingDetails *billingDetails = [[STPPaymentMethodBillingDetails alloc] init];
billingDetails.address = [[STPPaymentMethodAddress alloc] initWithAddress:self.addressViewModel.address];
billingDetails.email = self.addressViewModel.address.email;
billingDetails.name = self.addressViewModel.address.name;
billingDetails.phone = self.addressViewModel.address.phone;
STPPaymentMethodParams *paymentMethodParams = [STPPaymentMethodParams paramsWithCard:paymentMethodCardParams
billingDetails:billingDetails
metadata:nil];
[self.apiClient createPaymentMethodWithParams:paymentMethodParams completion:^(STPPaymentMethod * _Nullable paymentMethod, NSError * _Nullable createPaymentMethodError) {
if (createPaymentMethodError) {
[self handleError:createPaymentMethodError];
}
else {
if ([self.delegate respondsToSelector:@selector(addCardViewController:didCreatePaymentMethod:completion:)]) {
[self.delegate addCardViewController:self didCreatePaymentMethod:paymentMethod completion:^(NSError * _Nullable attachToCustomerError) {
stpDispatchToMainThreadIfNecessary(^{
if (attachToCustomerError) {
[self handleError:attachToCustomerError];
}
else {
self.loading = NO;
}
});
}];
}
}
}];
}
- (void)handleCardTokenizationError:(NSError *)error {
- (void)handleError:(NSError *)error {
self.loading = NO;
[[self firstEmptyField] becomeFirstResponder];