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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// PreCheckoutTableViewController.m
// Mobile Buy SDK Advanced Sample
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "PreCheckoutViewController.h"
#import "CheckoutViewController.h"
#import "SummaryItemsTableViewCell.h"
@import Buy;
@import PassKit;
typedef NS_ENUM(NSInteger, UITableViewSections) {
UITableViewSectionSummaryItems,
UITableViewSectionDiscountGiftCard,
UITableViewSectionContinue,
UITableViewSectionCount
};
typedef NS_ENUM(NSInteger, UITableViewDiscountGiftCardSection) {
UITableViewDiscountGiftCardSectionDiscount,
UITableViewDiscountGiftCardSectionGiftCard,
UITableViewDiscountGiftCardSectionCount
};
@interface PreCheckoutViewController ()
@property (nonatomic, strong) BUYCheckout *checkout;
@property (nonatomic, strong) BUYClient *client;
@property (nonatomic, strong) NSArray *summaryItems;
@end
@implementation PreCheckoutViewController
- (instancetype)initWithClient:(BUYClient *)client checkout:(BUYCheckout *)checkout;
{
NSParameterAssert(client);
NSParameterAssert(checkout);
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
self.checkout = checkout;
self.client = client;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Add Discount or Gift Card(s)";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.tableView registerClass:[SummaryItemsTableViewCell class] forCellReuseIdentifier:@"SummaryCell"];
}
- (void)setCheckout:(BUYCheckout *)checkout
{
_checkout = checkout;
// We can take advantage of the PKPaymentSummaryItems used for Apple Pay to display summary items natively in our own checkout
self.summaryItems = [checkout buy_summaryItems];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return UITableViewSectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case UITableViewSectionSummaryItems:
return [self.summaryItems count];
case UITableViewSectionDiscountGiftCard:
return UITableViewDiscountGiftCardSectionCount;
break;
default:
return 1;
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
switch (indexPath.section) {
case UITableViewSectionSummaryItems: {
cell = [tableView dequeueReusableCellWithIdentifier:@"SummaryCell" forIndexPath:indexPath];
PKPaymentSummaryItem *summaryItem = self.summaryItems[indexPath.row];
cell.textLabel.text = summaryItem.label;
cell.detailTextLabel.text = [self.currencyFormatter stringFromNumber:summaryItem.amount];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Only show a line above the last cell
if (indexPath.row != [self.summaryItems count] - 2) {
cell.separatorInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, cell.bounds.size.width);
}
}
break;
case UITableViewSectionDiscountGiftCard:
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.separatorInset = UIEdgeInsetsZero;
switch (indexPath.row) {
case UITableViewDiscountGiftCardSectionDiscount:
cell.textLabel.text = @"Add Discount";
break;
case UITableViewDiscountGiftCardSectionGiftCard:
cell.textLabel.text = @"Apply Gift Card";
break;
default:
break;
}
cell.textLabel.textAlignment = NSTextAlignmentCenter;
break;
case UITableViewSectionContinue:
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = @"Continue";
cell.textLabel.textAlignment = NSTextAlignmentCenter;
break;
default:
break;
}
cell.preservesSuperviewLayoutMargins = NO;
[cell setLayoutMargins:UIEdgeInsetsZero];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case UITableViewSectionDiscountGiftCard:
switch (indexPath.row) {
case UITableViewDiscountGiftCardSectionDiscount:
[self addDiscount];
break;
case UITableViewDiscountGiftCardSectionGiftCard:
[self applyGiftCard];
break;
default:
break;
}
break;
case UITableViewSectionContinue:
[self proceedToCheckout];
break;
default:
break;
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)addDiscount
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Enter Discount Code" message:nil preferredStyle:UIAlertControllerStyleAlert];;
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Discount Code";
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(@"Cancel action");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
BUYDiscount *discount = [[BUYDiscount alloc] initWithCode:[alertController.textFields[0] text]];
self.checkout.discount = discount;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.client updateCheckout:self.checkout completion:^(BUYCheckout *checkout, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (error == nil && checkout) {
NSLog(@"Successfully added discount");
self.checkout = checkout;
[self.tableView reloadData];
}
else {
NSLog(@"Error applying checkout: %@", error);
}
}];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)applyGiftCard
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Enter Gift Card Code" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Gift Card Code";
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(@"Cancel action");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.client applyGiftCardWithCode:[alertController.textFields[0] text] toCheckout:self.checkout completion:^(BUYCheckout *checkout, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (error == nil && checkout) {
NSLog(@"Successfully added gift card");
self.checkout = checkout;
[self.tableView reloadData];
}
else {
NSLog(@"Error applying gift card: %@", error);
}
}];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)proceedToCheckout
{
CheckoutViewController *checkoutController = [[CheckoutViewController alloc] initWithClient:self.client checkout:self.checkout];
checkoutController.currencyFormatter = self.currencyFormatter;
[self.navigationController pushViewController:checkoutController animated:YES];
}
@end