Skip to content

Commit a269524

Browse files
committed
fix bugs and add small checkout refactors
1 parent 7181583 commit a269524

13 files changed

Lines changed: 86 additions & 73 deletions

File tree

Example/OSXExample/ExampleWindowController.m

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,16 @@ - (IBAction)beginPayment:(id)sender {
4444

4545
- (void)checkoutController:(STPCheckoutViewController *)controller didCreateToken:(STPToken *)token completion:(STPTokenSubmissionHandler)completion {
4646
self.buyButton.enabled = YES;
47+
// Todo: post the token to our server and make a charge
4748
completion(STPBackendChargeResultSuccess, nil);
4849
}
4950

50-
- (void)checkoutController:(STPCheckoutViewController *)controller didFailWithError:(NSError *)error {
51-
self.buyButton.enabled = YES;
52-
[controller.view removeFromSuperview];
53-
}
54-
55-
- (void)checkoutControllerDidCancel:(STPCheckoutViewController *)controller {
56-
self.buyButton.enabled = YES;
57-
[controller.view removeFromSuperview];
58-
}
59-
60-
- (void)checkoutControllerDidFinish:(STPCheckoutViewController *)controller {
51+
- (void)checkoutController:(STPCheckoutViewController *)controller didFinishWithStatus:(STPPaymentStatus)status error:(NSError *)error {
6152
self.buyButton.enabled = YES;
6253
[controller.view removeFromSuperview];
54+
if (status == STPPaymentStatusSuccess) {
55+
// yay!
56+
}
6357
}
6458

6559
@end

Example/Stripe OSX Example.xcodeproj/project.pbxproj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@
262262
buildSettings = {
263263
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
264264
COMBINE_HIDPI_IMAGES = YES;
265-
FRAMEWORK_SEARCH_PATHS = (
266-
"$(inherited)",
267-
"$(USER_LIBRARY_DIR)/Developer/Xcode/DerivedData/Stripe-cxajdjnrspmiamhaiixwkuuqmmgg/Build/Products/Debug-iphoneos",
268-
);
265+
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
269266
HEADER_SEARCH_PATHS = (
270267
"$(inherited)",
271268
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
@@ -283,10 +280,7 @@
283280
buildSettings = {
284281
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
285282
COMBINE_HIDPI_IMAGES = YES;
286-
FRAMEWORK_SEARCH_PATHS = (
287-
"$(inherited)",
288-
"$(USER_LIBRARY_DIR)/Developer/Xcode/DerivedData/Stripe-cxajdjnrspmiamhaiixwkuuqmmgg/Build/Products/Debug-iphoneos",
289-
);
283+
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
290284
HEADER_SEARCH_PATHS = (
291285
"$(inherited)",
292286
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,

Example/Stripe iOS Example (Custom)/Constants.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#warning Replace these with your own values and then remove this warning. Make sure to replace the values in StripeExample/Parse/config/global.json as well if you want to use Parse.
1212

1313
// This can be found at https://dashboard.stripe.com/account/apikeys
14-
NSString *const StripePublishableKey = nil; // TODO: replace nil with your own value
14+
NSString *const StripePublishableKey = @"pk_test_vOo1umqsYxSrP5UXfOeL3ecm"; // TODO: replace nil with your own value
1515

1616
// These can be found at https://www.parse.com/apps/stripe-test/edit#app_keys
1717
NSString *const ParseApplicationId = nil; // TODO: replace nil with your own value

Example/Stripe iOS Example (Custom)/ViewController.m

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewC
165165

166166
- (IBAction)beginStripeCheckout:(id)sender {
167167
STPCheckoutOptions *options = [[STPCheckoutOptions alloc] init];
168+
options.publishableKey = [Stripe defaultPublishableKey];
169+
options.appleMerchantId = @"woo";
168170
options.purchaseDescription = @"Cool Shirt";
169171
options.purchaseAmount = @1000; // this is in cents
170172
options.logoColor = [UIColor purpleColor];
@@ -177,18 +179,21 @@ - (void)checkoutController:(STPCheckoutViewController *)controller didCreateToke
177179
[self createBackendChargeWithToken:token completion:completion];
178180
}
179181

180-
- (void)checkoutController:(STPCheckoutViewController *)controller didFailWithError:(NSError *)error {
181-
[self dismissViewControllerAnimated:YES completion:^{ [self presentError:error]; }];
182-
}
183-
184-
- (void)checkoutControllerDidCancel:(STPCheckoutViewController *)controller {
182+
- (void)checkoutController:(STPCheckoutViewController *)controller didFinishWithStatus:(STPPaymentStatus)status error:(NSError *)error {
183+
switch (status) {
184+
case STPPaymentStatusSuccess:
185+
[self paymentSucceeded];
186+
break;
187+
case STPPaymentStatusError:
188+
[self presentError:error];
189+
break;
190+
case STPPaymentStatusUserCanceled:
191+
// do nothing
192+
break;
193+
}
185194
[self dismissViewControllerAnimated:YES completion:nil];
186195
}
187196

188-
- (void)checkoutControllerDidFinish:(STPCheckoutViewController *)controller {
189-
[self dismissViewControllerAnimated:YES completion:^{ [self paymentSucceeded]; }];
190-
}
191-
192197
#pragma mark - Custom Credit Card Form
193198

194199
- (IBAction)beginCustomPayment:(id)sender {

Example/Stripe iOS Example (Simple).xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
isa = PBXGroup;
5454
children = (
5555
042CA4141A685E8D00D778E7 /* AppDelegate.swift */,
56+
042CA41C1A685E8D00D778E7 /* ViewController.swift */,
5657
042CA4171A685E8D00D778E7 /* Main.storyboard */,
5758
042CA4191A685E8D00D778E7 /* Images.xcassets */,
5859
042CA41A1A685E8D00D778E7 /* Info.plist */,
5960
042CA41B1A685E8D00D778E7 /* Stripe iOS Example (Simple)-Bridging-Header.h */,
60-
042CA41C1A685E8D00D778E7 /* ViewController.swift */,
6161
);
6262
path = "Stripe iOS Example (Simple)";
6363
sourceTree = "<group>";

Example/Stripe iOS Example (Simple)/AppDelegate.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
16-
// Override point for customization after application launch.
1716
return true
1817
}
1918

Example/Stripe iOS Example (Simple)/ViewController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Stripe
1212
class ViewController: UIViewController, STPPaymentPresenterDelegate {
1313

1414
// Replace these values with your application's keys
15-
let stripePublishableKey = ""
15+
let stripePublishableKey = "pk_test_vOo1umqsYxSrP5UXfOeL3ecm"
1616
let parseApplicationId = ""
1717
let parseClientKey = ""
1818

@@ -63,7 +63,6 @@ class ViewController: UIViewController, STPPaymentPresenterDelegate {
6363
self.dismissViewControllerAnimated(true, completion: { () -> Void in
6464
let alert = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
6565
if error != nil {
66-
alert.title = "Something went wrong."
6766
alert.message = error.localizedDescription
6867
}
6968
if status == STPPaymentStatus.Success {

Stripe/ApplePay/STPPaymentPresenter.m

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,16 @@ + (BOOL)isSimulatorBuild {
100100

101101
#pragma mark - STPCheckoutViewControllerDelegate
102102

103-
- (void)checkoutController:(__unused STPCheckoutViewController *)controller didFailWithError:(NSError *)error {
104-
[self finishWithStatus:STPPaymentStatusError error:error];
103+
- (void)checkoutController:(__unused STPCheckoutViewController *)controller didFinishWithStatus:(STPPaymentStatus)status error:(NSError *)error {
104+
[self finishWithStatus:status error:error];
105105
}
106106

107-
- (void)checkoutControllerDidCancel:(__unused STPCheckoutViewController *)controller {
108-
[self finishWithStatus:STPPaymentStatusUserCanceled error:nil];
109-
}
110-
111-
- (void)checkoutControllerDidFinish:(__unused STPCheckoutViewController *)controller {
112-
[self finishWithStatus:STPPaymentStatusSuccess error:nil];
113-
}
114-
115-
- (void)checkoutController:(__unused STPCheckoutViewController *)controller didCreateToken:(STPToken *)token completion:(STPTokenSubmissionHandler)completion {
107+
- (void)checkoutController:(__unused STPCheckoutViewController *)controller didCreateToken:(STPToken *)token completion:(STPTokenSubmissionHandler)checkoutCompletion {
108+
STPTokenSubmissionHandler completion = ^(STPBackendChargeResult status, NSError *backendError) {
109+
self.error = backendError;
110+
self.hasAuthorizedPayment = (status == STPBackendChargeResultSuccess);
111+
checkoutCompletion(status, backendError);
112+
};
116113
[self.delegate paymentPresenter:self didCreateStripeToken:token completion:completion];
117114
}
118115

Stripe/Checkout/STPCheckoutInternalUIWebViewController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
@property (nonatomic) NSURL *logoURL;
2828
@property (nonatomic) NSURL *url;
2929
@property (nonatomic, weak) id<STPCheckoutViewControllerDelegate> delegate;
30+
@property (nonatomic) BOOL backendChargeSuccessful;
31+
@property (nonatomic) NSError *backendChargeError;
3032

3133
@end
3234

Stripe/Checkout/STPCheckoutInternalUIWebViewController.m

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ - (void)viewDidLoad {
135135
}
136136

137137
- (void)cancel:(__unused id)sender {
138-
[self.delegate checkoutControllerDidCancel:self.checkoutController];
138+
[self.delegate checkoutController:self.checkoutController didFinishWithStatus:STPPaymentStatusUserCanceled error:nil];
139139
[self cleanup];
140140
}
141141

@@ -185,23 +185,36 @@ - (void)checkoutAdapter:(id<STPCheckoutWebViewAdapter>)adapter didTriggerEvent:(
185185
[self.delegate checkoutController:self.checkoutController
186186
didCreateToken:token
187187
completion:^(STPBackendChargeResult status, NSError *error) {
188+
self.backendChargeSuccessful = (status == STPBackendChargeResultSuccess);
189+
self.backendChargeError = error;
188190
if (status == STPBackendChargeResultSuccess) {
189191
[adapter evaluateJavaScript:payload[@"success"]];
190192
} else {
191193
NSString *failure = payload[@"failure"];
192-
NSString *script = [NSString stringWithFormat:failure, error.localizedDescription];
194+
NSString *encodedError = @"";
195+
if (error.localizedDescription) {
196+
encodedError = [[NSString alloc]
197+
initWithData:[NSJSONSerialization dataWithJSONObject:@[error.localizedDescription] options:0 error:nil]
198+
encoding:NSUTF8StringEncoding];
199+
encodedError = [encodedError substringWithRange:NSMakeRange(2, encodedError.length - 4)];
200+
}
201+
NSString *script = [NSString stringWithFormat:failure, encodedError];
193202
[adapter evaluateJavaScript:script];
194203
}
195204
}];
196205
} else if ([event isEqualToString:STPCheckoutEventFinish]) {
197-
[self.delegate checkoutControllerDidFinish:self.checkoutController];
206+
if (self.backendChargeSuccessful) {
207+
[self.delegate checkoutController:self.checkoutController didFinishWithStatus:STPPaymentStatusSuccess error:nil];
208+
} else {
209+
[self.delegate checkoutController:self.checkoutController didFinishWithStatus:STPPaymentStatusError error:self.backendChargeError];
210+
}
198211
[self cleanup];
199212
} else if ([event isEqualToString:STPCheckoutEventCancel]) {
200-
[self.delegate checkoutControllerDidCancel:self.checkoutController];
213+
[self.delegate checkoutController:self.checkoutController didFinishWithStatus:STPPaymentStatusUserCanceled error:nil];
201214
[self cleanup];
202215
} else if ([event isEqualToString:STPCheckoutEventError]) {
203216
NSError *error = [[NSError alloc] initWithDomain:StripeDomain code:STPCheckoutError userInfo:payload];
204-
[self.delegate checkoutController:self.checkoutController didFailWithError:error];
217+
[self.delegate checkoutController:self.checkoutController didFinishWithStatus:STPPaymentStatusError error:error];
205218
[self cleanup];
206219
}
207220
}
@@ -217,7 +230,7 @@ - (void)checkoutAdapterDidFinishLoad:(__unused id<STPCheckoutWebViewAdapter>)ada
217230

218231
- (void)checkoutAdapter:(__unused id<STPCheckoutWebViewAdapter>)adapter didError:(NSError *)error {
219232
[self.activityIndicator stopAnimating];
220-
[self.delegate checkoutController:self.checkoutController didFailWithError:error];
233+
[self.delegate checkoutController:self.checkoutController didFinishWithStatus:STPPaymentStatusError error:error];
221234
[self cleanup];
222235
}
223236

0 commit comments

Comments
 (0)