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

Create Explicit Test mode API calls #54

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions Example/StripeExample/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>

extern NSString * const StripePublishableKey;
extern BOOL const StripeTestMode;
extern NSString * const ParseApplicationId;
extern NSString * const ParseClientKey;

Expand Down
1 change: 1 addition & 0 deletions Example/StripeExample/Constants.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// This can be found at https://dashboard.stripe.com/account/apikeys
NSString * const StripePublishableKey = @"pk_YT1CEhhujd0bklb2KGQZiaL3iTzj3"; // TODO: replace nil with your own value
BOOL const StripeTestMode = TRUE; // TODO: replace with FALSE for production/live

// These can be found at https://www.parse.com/apps/stripe-test/edit#app_keys
NSString * const ParseApplicationId = nil; // TODO: replace nil with your own value
Expand Down
20 changes: 18 additions & 2 deletions Example/StripeExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#import "Stripe.h"
#import "Constants.h"

@interface ViewController()<PKPaymentAuthorizationViewControllerDelegate>
@interface ViewController()<PKPaymentAuthorizationViewControllerDelegate, STPTestPaymentAuthorizationViewControllerDelegate>
@end

@implementation ViewController
Expand All @@ -24,7 +24,13 @@ - (IBAction)beginPayment:(id)sender {
currency:@"USD"
description:@"Premium Llama Food"];
if ([Stripe canSubmitPaymentRequest:paymentRequest]) {
UIViewController *paymentController = [Stripe paymentControllerWithRequest:paymentRequest delegate:self];
UIViewController *paymentController;

if (StripeTestMode)
paymentController = [Stripe testPaymentControllerWithRequest:paymentRequest delegate:self];
else
paymentController = [Stripe paymentControllerWithRequest:paymentRequest delegate:self];

[self presentViewController:paymentController animated:YES completion:nil];
}
else {
Expand All @@ -38,6 +44,12 @@ - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController
[self handlePaymentAuthorizationWithPayment:payment completion:completion];
}

- (void)testPaymentAuthorizationViewController:(STPTestPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
[self handlePaymentAuthorizationWithPayment:payment completion:completion];
}

- (void)handlePaymentAuthorizationWithPayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
[Stripe createTokenWithPayment:payment
Expand Down Expand Up @@ -88,4 +100,8 @@ - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewC
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)testPaymentAuthorizationViewControllerDidFinish:(UIViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}

@end
13 changes: 12 additions & 1 deletion Stripe/STPTestPaymentAuthorizationViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@
#import <UIKit/UIKit.h>
#import <PassKit/PassKit.h>

@protocol STPTestPaymentAuthorizationViewControllerDelegate <NSObject>

@required
-(void)testPaymentAuthorizationViewController:(UIViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion;

-(void)testPaymentAuthorizationViewControllerDidFinish:(UIViewController *)controller;
@end


@interface STPTestPaymentAuthorizationViewController : UIViewController

@property(nonatomic, assign)id<PKPaymentAuthorizationViewControllerDelegate>delegate;
@property(nonatomic, assign)id<STPTestPaymentAuthorizationViewControllerDelegate>delegate;

@end

Expand Down
6 changes: 3 additions & 3 deletions Stripe/STPTestPaymentAuthorizationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ - (void)makePaymentWithCardNumber:(NSString *)cardNumber {
PKPaymentAuthorizationViewController *auth = (PKPaymentAuthorizationViewController *)self;

[self.activityIndicator startAnimating];
[self.delegate paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)auth
[self.delegate testPaymentAuthorizationViewController:(STPTestPaymentAuthorizationViewController *)auth
didAuthorizePayment:payment
completion:^(PKPaymentAuthorizationStatus status) {
[self.activityIndicator stopAnimating];
[self.delegate paymentAuthorizationViewControllerDidFinish:auth];
[self.delegate testPaymentAuthorizationViewControllerDidFinish:auth];
}];
}

Expand All @@ -67,7 +67,7 @@ - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {
[self.delegate paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)self];
[self.delegate testPaymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)self];
}

@end
Expand Down
3 changes: 3 additions & 0 deletions Stripe/Stripe.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "StripeError.h"
#import "STPCard.h"
#import "STPToken.h"
#import "STPTestPaymentAuthorizationViewController.h"

FOUNDATION_EXPORT NSString *const kStripeiOSVersion; // Version of this library.

Expand Down Expand Up @@ -51,6 +52,8 @@ typedef void (^STPCompletionBlock)(STPToken *token, NSError *error);
currency:(NSString *)currency
description:(NSString *)description;

+ (UIViewController *)testPaymentControllerWithRequest:(PKPaymentRequest *)request
delegate:(id<STPTestPaymentAuthorizationViewControllerDelegate>)delegate;
+ (UIViewController *)paymentControllerWithRequest:(PKPaymentRequest *)request
delegate:(id<PKPaymentAuthorizationViewControllerDelegate>)delegate;

Expand Down
13 changes: 8 additions & 5 deletions Stripe/Stripe.m
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,16 @@ + (PKPaymentRequest *)paymentRequestWithMerchantIdentifier:(NSString *)merchantI
return paymentRequest;
}


+ (UIViewController *)testPaymentControllerWithRequest:(PKPaymentRequest *)request
delegate:(id<STPTestPaymentAuthorizationViewControllerDelegate>)delegate {
STPTestPaymentAuthorizationViewController *test = [STPTestPaymentAuthorizationViewController new];
test.delegate = delegate;
return test;
}

+ (UIViewController *)paymentControllerWithRequest:(PKPaymentRequest *)request
delegate:(id<PKPaymentAuthorizationViewControllerDelegate>)delegate {
if ([self isSimulatorBuild]) {
STPTestPaymentAuthorizationViewController *test = [STPTestPaymentAuthorizationViewController new];
test.delegate = delegate;
return test;
}
PKPaymentAuthorizationViewController *auth = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
auth.delegate = delegate;
return auth;
Expand Down