forked from stripe/stripe-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPXExampleViewController.m
153 lines (134 loc) · 6.82 KB
/
FPXExampleViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// FPXExampleViewController.m
// Non-Card Payment Examples
//
// Created by David Estes on 8/26/19.
// Copyright © 2019 Stripe. All rights reserved.
//
#import <Stripe/Stripe.h>
#import "FPXExampleViewController.h"
#import "BrowseExamplesViewController.h"
#import "MyAPIClient.h"
/**
This example demonstrates using PaymentIntents to accept payments using FPX, a popular
payment method in Malaysia.
First, we ask our server to set up a PaymentIntent. We create a PaymentMethodParams with the
details of our selected FPX-supporting bank, then call STPPaymentHandler to confirm the PaymentIntent
using the Stripe API.
*/
@interface FPXExampleViewController () <STPAuthenticationContext, STPBankSelectionViewControllerDelegate>
@property (nonatomic, weak) UIButton *payButton;
@property (nonatomic, weak) UILabel *waitingLabel;
@property (nonatomic, weak) UIActivityIndicatorView *activityIndicator;
@end
@implementation FPXExampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
#ifdef __IPHONE_13_0
if (@available(iOS 13.0, *)) {
self.view.backgroundColor = [UIColor systemBackgroundColor];
}
#endif
self.title = @"FPX";
self.edgesForExtendedLayout = UIRectEdgeNone;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Pay RM2.34 with Bank Account (FPX)" forState:UIControlStateNormal];
[button sizeToFit];
[button addTarget:self action:@selector(selectBank) forControlEvents:UIControlEventTouchUpInside];
self.payButton = button;
[self.view addSubview:button];
UILabel *label = [UILabel new];
label.text = @"Waiting for payment authorization";
[label sizeToFit];
label.textColor = [UIColor grayColor];
#ifdef __IPHONE_13_0
if (@available(iOS 13.0, *)) {
label.textColor = [UIColor secondaryLabelColor];
}
#endif
label.alpha = 0;
[self.view addSubview:label];
self.waitingLabel = label;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.hidesWhenStopped = YES;
self.activityIndicator = activityIndicator;
[self.view addSubview:activityIndicator];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat padding = 15;
CGRect bounds = self.view.bounds;
self.payButton.center = CGPointMake(CGRectGetMidX(bounds), 100);
self.activityIndicator.center = CGPointMake(CGRectGetMidX(bounds),
CGRectGetMaxY(self.payButton.frame) + padding*2);
self.waitingLabel.center = CGPointMake(CGRectGetMidX(bounds),
CGRectGetMaxY(self.activityIndicator.frame) + padding*2);
}
- (void)updateUIForPaymentInProgress:(BOOL)paymentInProgress {
self.navigationController.navigationBar.userInteractionEnabled = !paymentInProgress;
self.payButton.enabled = !paymentInProgress;
[UIView animateWithDuration:0.2 animations:^{
self.waitingLabel.alpha = paymentInProgress ? 1 : 0;
}];
if (paymentInProgress) {
[self.activityIndicator startAnimating];
} else {
[self.activityIndicator stopAnimating];
}
}
- (void)selectBank {
STPBankSelectionViewController *vc = [[STPBankSelectionViewController alloc] initWithBankMethod:STPBankSelectionMethodFPX];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)payWithBankAccount:(STPPaymentMethodParams *)paymentMethodParams {
if (![Stripe defaultPublishableKey]) {
[self.delegate exampleViewController:self didFinishWithMessage:@"Please set a Stripe Publishable Key in Constants.m"];
return;
}
[self updateUIForPaymentInProgress:YES];
[[MyAPIClient sharedClient] createPaymentIntentWithCompletion:^(MyAPIClientResult status, NSString *clientSecret, NSError *error) {
if (status == MyAPIClientResultFailure || clientSecret == nil) {
[self.delegate exampleViewController:self didFinishWithError:error];
return;
}
STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:clientSecret];
paymentIntentParams.paymentMethodParams = paymentMethodParams;
paymentIntentParams.returnURL = @"payments-example://stripe-redirect";
[[STPPaymentHandler sharedHandler] confirmPayment:paymentIntentParams
withAuthenticationContext:self
completion:^(STPPaymentHandlerActionStatus handlerStatus, STPPaymentIntent * handledIntent, NSError * _Nullable handlerError) {
switch (handlerStatus) {
case STPPaymentHandlerActionStatusFailed:
[self.delegate exampleViewController:self didFinishWithError:handlerError];
break;
case STPPaymentHandlerActionStatusCanceled:
[self.delegate exampleViewController:self didFinishWithMessage:@"Canceled"];
break;
case STPPaymentHandlerActionStatusSucceeded:
[self.delegate exampleViewController:self didFinishWithMessage:@"Payment successfully created"];
break;
}
}];
} additionalParameters:@"country=my"];
}
- (void)bankSelectionViewController:(nonnull STPBankSelectionViewController *)bankViewController didCreatePaymentMethodParams:(STPPaymentMethodParams *)paymentMethodParams {
[self payWithBankAccount:paymentMethodParams];
}
#pragma mark - STPAuthenticationContext
- (UIViewController *)authenticationPresentingViewController {
return self.navigationController.topViewController;
}
- (void)authenticationContextWillDismissViewController:(UIViewController *)viewController {
// Remove the bank selector from the view controller stack so that we pop directly
// back to FPXExampleViewController. This provides a better experience vs sending the user back to the bank selector list.
NSMutableArray <UIViewController *> *vcs = [self.navigationController.viewControllers mutableCopy];
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[STPBankSelectionViewController class]]) {
[vcs removeObject:vc];
}
}
self.navigationController.viewControllers = vcs;
}
@end