-
Notifications
You must be signed in to change notification settings - Fork 5
/
HPFTokenizableCardPaymentProductViewController.m
812 lines (604 loc) · 29.8 KB
/
HPFTokenizableCardPaymentProductViewController.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//
// HPFTokenizableCardPaymentProductViewController.m
// Pods
//
// Created by HiPay on 02/11/2015.
//
//
#import "HPFTokenizableCardPaymentProductViewController.h"
#import "HPFOrderRequest.h"
#import "HPFAbstractPaymentProductViewController_Protected.h"
#import "NSString+HPFValidation.h"
#import "HPFPaymentScreenUtils.h"
#import "HPFSecureVaultClient.h"
#import "HPFCardNumberTextField.h"
#import "HPFExpiryDateTextField.h"
#import "HPFSecurityCodeInputTableViewCell.h"
#import "HPFSecurityCodeTableViewFooterView.h"
#import "HPFSecurityCodeTextField.h"
#import "HPFCardNumberInputTableViewCell.h"
#import "HPFPaymentCardSwitchTableHeaderView.h"
#import "HPFPaymentCardTokenDatabase.h"
#import "HPFExpiryDateFormatter.h"
#import <LocalAuthentication/LAContext.h>
#import <AVFoundation/AVFoundation.h>
@interface HPFTokenizableCardPaymentProductViewController ()
@property (nonatomic, getter=isSwitchOn) BOOL switchOn;
@property (nonatomic, getter=isTouchIDOn) BOOL touchIDOn;
@property (nonatomic, strong) HPFPaymentCardToken *paymentCardToken;
@end
@implementation HPFTokenizableCardPaymentProductViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"HPFScanCardTableViewCell" bundle:HPFPaymentScreenViewsBundle()] forCellReuseIdentifier:@"ScanCard"];
[self.tableView registerNib:[UINib nibWithNibName:@"HPFSecurityCodeTableViewFooterView" bundle:HPFPaymentScreenViewsBundle()] forHeaderFooterViewReuseIdentifier:@"SecurityCode"];
[self.tableView registerNib:[UINib nibWithNibName:@"HPFPaymentCardSwitchTableHeaderView" bundle:HPFPaymentScreenViewsBundle()] forHeaderFooterViewReuseIdentifier:@"PaymentCardSwitch"];
self.switchOn = NO;
self.touchIDOn = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.isCameraScanDisplayed)
{
[CardIOUtilities preloadCardIO];
}
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (!defaultFormValuesDefined) {
UITextField *cardHolderTextField = [self textFieldForIdentifier:@"holder"];
cardHolderTextField.text = self.paymentPageRequest.customer.displayName;
defaultFormValuesDefined = YES;
}
((HPFSecurityCodeTableViewFooterView *) [self.tableView footerViewForSection:[self formSection]]).separatorInset = self.tableView.separatorInset;
}
- (BOOL)submitButtonEnabled
{
HPFCardNumberTextField *cardNumberTextField = (HPFCardNumberTextField *) [self textFieldForIdentifier:@"number"];
HPFExpiryDateTextField *expiryDateTextField = (HPFExpiryDateTextField *) [self textFieldForIdentifier:@"expiry_date"];
BOOL validation = [[self textForIdentifier:@"holder"] isDefined] && cardNumberTextField.completed && expiryDateTextField.completed && cardNumberTextField.valid && expiryDateTextField.valid;
HPFSecurityCodeTextField *securityCodeTextField = (HPFSecurityCodeTextField *) [self textFieldForIdentifier:@"security_code"];
if ([self securityCodeSectionEnabled]) {
validation = validation && securityCodeTextField.completed && securityCodeTextField.valid;
}
if (validation) {
[self.view endEditing:YES];
}
return validation;
}
#pragma mark - Form edition
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[super textFieldDidBeginEditing:textField];
HPFSecurityCodeTextField *securityCodeTextField = (HPFSecurityCodeTextField *) [self textFieldForIdentifier:@"security_code"];
if (textField == securityCodeTextField) {
[self.tableView beginUpdates];
footerHeight = 56.0;
[self.tableView endUpdates];
[self securityCodeFooter].hidden = NO;
[self securityCodeFooter].alpha = 0.0;
[UIView animateWithDuration:0.2 animations:^{
[self securityCodeFooter].alpha = 1.0;
}];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[super textFieldDidEndEditing:textField];
HPFSecurityCodeTextField *securityCodeTextField = (HPFSecurityCodeTextField *) [self textFieldForIdentifier:@"security_code"];
HPFCardNumberTextField *cardNumberTextField = (HPFCardNumberTextField *) [self textFieldForIdentifier:@"number"];
HPFExpiryDateTextField *expiryDateTextField = (HPFExpiryDateTextField *) [self textFieldForIdentifier:@"expiry_date"];
if (textField == securityCodeTextField) {
[self.tableView beginUpdates];
footerHeight = 0.0;
[self.tableView endUpdates];
[UIView animateWithDuration:0.2 animations:^{
[self securityCodeFooter].alpha = 0.0;
} completion:^(BOOL finished) {
[self securityCodeFooter].hidden = YES;
}];
if ([securityCodeTextField.text isDefined] && !securityCodeTextField.completed) {
[self cellWithTextField:securityCodeTextField].incorrectInput = YES;
}
}
else if (textField == cardNumberTextField) {
if ([cardNumberTextField.text isDefined] && !cardNumberTextField.completed) {
[self cellWithTextField:cardNumberTextField].incorrectInput = YES;
}
}
else if (textField == expiryDateTextField) {
if ([expiryDateTextField.text isDefined] && !expiryDateTextField.completed) {
[self cellWithTextField:expiryDateTextField].incorrectInput = YES;
}
}
}
- (void)textFieldDidChange:(UITextField *)textField
{
[super textFieldDidChange:textField];
HPFCardNumberTextField *cardNumberTextField = (HPFCardNumberTextField *) [self textFieldForIdentifier:@"number"];
HPFExpiryDateTextField *expiryDateTextField = (HPFExpiryDateTextField *) [self textFieldForIdentifier:@"expiry_date"];
HPFSecurityCodeTextField *securityCodeTextField = (HPFSecurityCodeTextField *) [self textFieldForIdentifier:@"security_code"];
[self inferPaymentProductCode];
if (textField == cardNumberTextField) {
BOOL valid = cardNumberTextField.valid && !paymentProductDisallowed;
[self cellWithTextField:cardNumberTextField].incorrectInput = !valid;
if (valid && (cardNumberTextField.isCompleted)) {
[expiryDateTextField becomeFirstResponder];
}
}
else if (textField == expiryDateTextField) {
BOOL valid = expiryDateTextField.valid;
[self cellWithTextField:expiryDateTextField].incorrectInput = !valid;
if (valid && (expiryDateTextField.isCompleted)) {
[securityCodeTextField becomeFirstResponder];
}
}
else if (textField == expiryDateTextField) {
BOOL valid = expiryDateTextField.valid;
[self cellWithTextField:expiryDateTextField].incorrectInput = !valid;
if (valid && (expiryDateTextField.isCompleted)) {
[securityCodeTextField becomeFirstResponder];
}
}
else if (textField == securityCodeTextField) {
BOOL valid = securityCodeTextField.valid;
[self cellWithTextField:securityCodeTextField].incorrectInput = !valid;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == [self textFieldForIdentifier:@"holder"]) {
[[self textFieldForIdentifier:@"number"] becomeFirstResponder];
return YES;
}
else if (textField == [self textFieldForIdentifier:@"number"]) {
[[self textFieldForIdentifier:@"expiry_date"] becomeFirstResponder];
return YES;
}
else if (textField == [self textFieldForIdentifier:@"expiry_date"]) {
[[self textFieldForIdentifier:@"security_code"] becomeFirstResponder];
return YES;
}
return [super textFieldShouldReturn:textField];
}
#pragma mark - Security code fields behavior
- (void)updateTitleHeader
{
[self.tableView headerViewForSection:[self formSection]].textLabel.text = [[self tableView:self.tableView titleForHeaderInSection:[self formSection]] uppercaseString];
[[self.tableView headerViewForSection:[self formSection]] layoutSubviews];
}
- (NSString *)currentPaymentProductCode
{
if (inferedPaymentProductCode != nil) {
return inferedPaymentProductCode;
}
return self.paymentProduct.code;
}
- (HPFSecurityCodeTableViewFooterView *)securityCodeFooter
{
return (HPFSecurityCodeTableViewFooterView *) [self.tableView footerViewForSection:[self formSection]];
}
- (HPFPaymentProduct *) getPaymentProductFromInferedCode:(NSString *)inferedCode
{
return [self.delegate paymentProductViewController:self paymentProductForInferredPaymentProductCode:inferedCode];
}
- (void)inferPaymentProductCode
{
HPFCardNumberTextField *cardNumberTextField = (HPFCardNumberTextField *) [self textFieldForIdentifier:@"number"];
HPFSecurityCodeTextField *securityCodeTextField = (HPFSecurityCodeTextField *) [self textFieldForIdentifier:@"security_code"];
BOOL wasPaymentProductDisallowed = paymentProductDisallowed;
BOOL securityCodeSectionEnabled = [self securityCodeSectionEnabled];
HPFSecurityCodeType currentSecurityCodeType = [self currentSecurityCodeType];
BOOL isCardStorageEnabled = [self paymentCardStorageConfigEnabled];
if ((cardNumberTextField.paymentProductCodes.count == 1) && [[HPFCardNumberFormatter sharedFormatter] plainTextNumber:cardNumberTextField.text isInRangeForPaymentProductCode:cardNumberTextField.paymentProductCodes.anyObject]) {
inferedPaymentProductCode = cardNumberTextField.paymentProductCodes.anyObject;
HPFPaymentProduct *newInferredPaymentProduct = [self getPaymentProductFromInferedCode:inferedPaymentProductCode];
if (newInferredPaymentProduct != inferedPaymentProduct) {
inferedPaymentProduct = newInferredPaymentProduct;
if (isCardStorageEnabled) {
UITableViewHeaderFooterView *headerView = [self.tableView headerViewForSection:[self paySection]];
if (headerView != nil) {
self.touchIDOn = NO;
self.switchOn = NO;
}
}
[self updateTitleHeader];
[self.delegate paymentProductViewController:self changeSelectedPaymentProduct:inferedPaymentProduct];
}
if (inferedPaymentProduct == nil) {
paymentProductDisallowed = YES;
} else {
paymentProductDisallowed = NO;
}
}
else if (inferedPaymentProductCode != nil) {
inferedPaymentProductCode = nil;
inferedPaymentProduct = nil;
paymentProductDisallowed = NO;
[self updateTitleHeader];
[self.delegate paymentProductViewController:self changeSelectedPaymentProduct:self.paymentProduct];
if (isCardStorageEnabled) {
self.touchIDOn = NO;
self.switchOn = NO;
}
}
if (!paymentProductDisallowed) {
if ([self securityCodeSectionEnabled]) {
NSString *paymentProductCode = [self currentPaymentProductCode];
((HPFSecurityCodeInputTableViewCell *)[self cellWithTextField:securityCodeTextField]).paymentProductCode = paymentProductCode;
[self securityCodeFooter].paymentProductCode = paymentProductCode;
}
if (!wasPaymentProductDisallowed) {
if (securityCodeSectionEnabled != [self securityCodeSectionEnabled]) {
if ([self securityCodeSectionEnabled]) {
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:[self formSection]]] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[self paySection]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
else {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:[self formSection]]] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[self paySection]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
else {
if (currentSecurityCodeType != [self currentSecurityCodeType]) {
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:[self formSection]]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[self paySection]] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
}
}
}
- (HPFSecurityCodeType)currentSecurityCodeType
{
if (inferedPaymentProductCode != nil) {
return [HPFPaymentProduct securityCodeTypeForPaymentProductCode:inferedPaymentProductCode];
}
// Default behavior for selected payment product
return [HPFPaymentProduct securityCodeTypeForPaymentProductCode:self.paymentProduct.code];
}
- (BOOL)isTouchIDEnabled
{
return [HPFClientConfig.sharedClientConfig isTouchIDEnabled] && [self canEvaluatePolicy];
}
- (BOOL)canEvaluatePolicy {
LAContext *context = [[LAContext alloc] init];
NSError *error;
// test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled
return [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
}
- (BOOL)paymentCardStorageEnabled
{
if (inferedPaymentProductCode != nil) {
return [HPFPaymentProduct isPaymentCardStorageEnabledForPaymentProductCode:inferedPaymentProductCode];
}
return [HPFPaymentProduct isPaymentCardStorageEnabledForPaymentProductCode:self.paymentProduct.code];
}
- (BOOL)paymentCardStorageConfigEnabled
{
BOOL paymentCardEnabled = [HPFClientConfig.sharedClientConfig isPaymentCardStorageEnabled];
BOOL paymentPageRequestECI = self.paymentPageRequest.eci == HPFECISecureECommerce ? YES : NO;
return paymentCardEnabled && paymentPageRequestECI;
}
- (BOOL)securityCodeSectionEnabled
{
return [self currentSecurityCodeType] != HPFSecurityCodeTypeNone;
}
- (NSString *)securityCodePlaceholderForPaymentProductCode:(NSString *)paymentProductCode
{
if ([paymentProductCode isEqualToString:HPFPaymentProductCodeAmericanExpress]) {
return @"CARD_SECURITY_CODE_PLACEHOLDER_CID";
} else {
return @"CARD_SECURITY_CODE_PLACEHOLDER_CVV";
}
}
- (BOOL)isCameraFeatureAllowed
{
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSCameraUsageDescription"] != nil)
{
return YES;
}
return NO;
}
- (BOOL)isScanConfigEnabled
{
return [HPFClientConfig.sharedClientConfig isPaymentCardScanEnabled];
}
- (BOOL) canReadCardWithCamera
{
return [CardIOUtilities canReadCardWithCamera];
}
- (BOOL) isCameraScanDisplayed {
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
BOOL canReadCard = YES;
if (self.canReadCardWithCamera == NO) {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]
&& authStatus == AVAuthorizationStatusDenied) {
canReadCard = YES;
} else {
canReadCard = NO;
}
}
return self.isScanConfigEnabled && self.isCameraFeatureAllowed && canReadCard;
}
#pragma mark - Table View delegate and data source
- (NSInteger) formSection
{
return self.isCameraScanDisplayed ? 1 : 0;
}
- (NSInteger) paySection
{
return self.isCameraScanDisplayed ? 2 : 1;
}
- (NSInteger) scanSection
{
return self.isCameraScanDisplayed ? 0 : -1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == [self formSection]) {
NSString *description = self.paymentProduct.paymentProductDescription;
if ((inferedPaymentProduct != nil) && ![HPFPaymentProduct isPaymentProductCode:self.paymentProduct.code domesticNetworkOfPaymentProductCode:inferedPaymentProduct.code]) {
description = inferedPaymentProduct.paymentProductDescription;
}
return [NSString stringWithFormat:HPFLocalizedString(@"HPF_PAY_WITH_THIS_METHOD"), description];
}
return [super tableView:tableView titleForHeaderInSection:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (self.isCameraScanDisplayed) {
return 3;
} else {
return 2;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == [self formSection]) {
if ([self securityCodeSectionEnabled]) {
return 4;
} else {
return 3;
}
}
// pay section and scan section are 1 line
return 1;
}
- (void)submit
{
[self setPaymentButtonLoadingMode:YES];
NSString *securityCode = nil;
if ([self securityCodeSectionEnabled]) {
securityCode = [self textForIdentifier:@"security_code"];
}
HPFExpiryDateTextField *expiryDateTextField = (HPFExpiryDateTextField *)[self textFieldForIdentifier:@"expiry_date"];
NSString *year = [NSString stringWithFormat: @"%ld", (long)expiryDateTextField.dateComponents.year];
NSString *month = [NSString stringWithFormat: @"%02ld", (long)expiryDateTextField.dateComponents.month];
BOOL paymentCardEnabled = [self paymentCardStorageConfigEnabled];
if (paymentCardEnabled && [self isSwitchOn]) {
self.paymentPageRequest.multiUse = YES;
}
transactionLoadingRequest = [[HPFSecureVaultClient sharedClient] generateTokenWithCardNumber:[self textForIdentifier:@"number"] cardExpiryMonth:month cardExpiryYear:year cardHolder:[self textForIdentifier:@"holder"] securityCode:securityCode multiUse:self.paymentPageRequest.multiUse andCompletionHandler:^(HPFPaymentCardToken *cardToken, NSError *error) {
[self setPaymentButtonLoadingMode:NO];
self->transactionLoadingRequest = nil;
if (cardToken != nil) {
if (paymentCardEnabled && [self isSwitchOn]) {
self.paymentCardToken = cardToken;
}
HPFOrderRequest *orderRequest = [self createOrderRequest];
NSString *paymentProductCode = (cardToken.domesticNetwork) ? cardToken.domesticNetwork : cardToken.brand;
paymentProductCode = [paymentProductCode stringByReplacingOccurrencesOfString:@" " withString:@"-"];
orderRequest.paymentProductCode = paymentProductCode;
orderRequest.paymentMethod = [HPFCardTokenPaymentMethodRequest cardTokenPaymentMethodRequestWithToken:cardToken.token eci:self.paymentPageRequest.eci authenticationIndicator:self.paymentPageRequest.authenticationIndicator];
[self performOrderRequest:orderRequest signature:self.signature];
} else {
[self checkTransactionError:error];
}
}];
}
- (void) savePaymentMethod:(HPFPaymentMethod *)paymentMethod {
if ([paymentMethod isMemberOfClass:[HPFPaymentCardToken class]]) {
HPFPaymentCardToken *cardToken = (HPFPaymentCardToken *)paymentMethod;
if (self.paymentCardToken != nil) {
if ([cardToken isEqualToPaymentCardToken:[self paymentCardToken]]) {
if ([self paymentCardStorageConfigEnabled] && [self isSwitchOn]) {
[HPFPaymentCardTokenDatabase save:[self paymentCardToken] forCurrency:self.paymentPageRequest.currency withTouchID:[self isTouchIDOn]];
}
}
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == [self scanSection])
{
return [self dequeueScanCardCell];
}
else if (indexPath.section == [self paySection])
{
return [super dequeuePaymentButtonCell];
}
// next is for FORM SECTION
HPFInputTableViewCell *cell;
switch (indexPath.row) {
case 0:
cell = [self dequeueInputCellWithIdentifier:@"Input" fieldIdentifier:@"holder"];
cell.inputLabel.text = HPFLocalizedString(@"HPF_CARD_HOLDER_LABEL");
cell.textField.placeholder = HPFLocalizedString(@"HPF_CARD_HOLDER_PLACEHOLDER");
cell.textField.keyboardType = UIKeyboardTypeAlphabet;
cell.textField.autocorrectionType = UITextAutocorrectionTypeNo;
cell.textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
cell.textField.returnKeyType = UIReturnKeyNext;
if (@available(iOS 10.0, *)) {
cell.textField.textContentType = UITextContentTypeName;
}
break;
case 1:
cell = [self dequeueInputCellWithIdentifier:@"CardNumberInput" fieldIdentifier:@"number"];
((HPFCardNumberInputTableViewCell *)cell).defaultPaymentProductCode = [self currentPaymentProductCode];
cell.textField.returnKeyType = UIReturnKeyNext;
if (@available(iOS 10.0, *)) {
cell.textField.textContentType = UITextContentTypeCreditCardNumber;
}
break;
case 2:
cell = [self dequeueInputCellWithIdentifier:@"ExpiryDateInput" fieldIdentifier:@"expiry_date"];
cell.inputLabel.text = HPFLocalizedString(@"HPF_CARD_EXPIRATION_LABEL");
cell.textField.placeholder = HPFLocalizedString(@"HPF_CARD_EXPIRATION_PLACEHOLDER");
cell.textField.returnKeyType = UIReturnKeyNext;
break;
case 3:
cell = [self dequeueInputCellWithIdentifier:@"SecurityCodeInput" fieldIdentifier:@"security_code"];
((HPFSecurityCodeInputTableViewCell *)cell).paymentProductCode = [self currentPaymentProductCode];
cell.textField.returnKeyType = UIReturnKeyDone;
break;
}
if (cell) {
return cell;
}
else {
NSLog(@"Unexpected tableView error");
abort();
}
}
- (HPFScanCardTableViewCell *)dequeueScanCardCell
{
HPFScanCardTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ScanCard"];
cell.delegate = self;
return cell;
}
- (void)scanCardTableViewCellDidTouchButton:(HPFScanCardTableViewCell *)cell {
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
// this method checks if the user has the appropriate permission
if (!self.canReadCardWithCamera) {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]
&& authStatus == AVAuthorizationStatusDenied) {
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:HPFLocalizedString(@"HPF_CARD_SCAN")
message:HPFLocalizedString(@"HPF_CARD_SCAN_PERMISSION")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelButton = [UIAlertAction
actionWithTitle:HPFLocalizedString(@"HPF_CANCEL")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
UIAlertAction* settingsButton = [UIAlertAction
actionWithTitle:HPFLocalizedString(@"HPF_SETTINGS")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: UIApplicationOpenSettingsURLString]];
}];
[alertViewController addAction:cancelButton];
[alertViewController addAction:settingsButton];
[self presentViewController:alertViewController animated:YES completion:nil];
} else {
// should not happen
}
} else {
CardIOPaymentViewController *scanViewController = [[CardIOPaymentViewController alloc] initWithPaymentDelegate:self];
scanViewController.hideCardIOLogo = YES;
scanViewController.suppressScanConfirmation = YES;
scanViewController.disableManualEntryButtons = YES;
[self presentViewController:scanViewController animated:YES completion:nil];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == [self formSection]) {
HPFSecurityCodeTableViewFooterView *footer = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SecurityCode"];
footer.paymentProductCode = [self currentPaymentProductCode];
footer.hidden = YES;
return footer;
}
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
BOOL paymentCardEnabled = [self paymentCardStorageConfigEnabled];
if (section == [self paySection] && paymentCardEnabled) {
if ([self paymentCardStorageEnabled]) {
HPFPaymentCardSwitchTableHeaderView *header = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PaymentCardSwitch"];
[[header saveSwitch] addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
return header;
}
}
return nil;
}
- (void) switchChanged:(UISwitch *)sender {
self.switchOn = sender.isOn;
// if touchID is not enabled, don't ask for it
if (self.isSwitchOn && self.isTouchIDEnabled) {
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:HPFLocalizedString(@"HPF_CARD_SWITCH_TOUCHID_TITLE")
message:HPFLocalizedString(@"HPF_CARD_SWITCH_TOUCHID_DESCRIPTION")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelButton = [UIAlertAction
actionWithTitle:HPFLocalizedString(@"HPF_NO")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
self.touchIDOn = NO;
}];
UIAlertAction* settingsButton = [UIAlertAction
actionWithTitle:HPFLocalizedString(@"HPF_YES")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
self.touchIDOn = YES;
}];
[alertViewController addAction:cancelButton];
[alertViewController addAction:settingsButton];
[self presentViewController:alertViewController animated:YES completion:nil];
} else {
self.touchIDOn = NO;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == [self formSection]) {
return footerHeight;
}
return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == self.formSection) {
return 32.f;
} else if (section == self.paySection) {
BOOL paymentCardEnabled = [self paymentCardStorageConfigEnabled];
if ([self paymentCardStorageEnabled] && paymentCardEnabled) {
return 48.f;
}
} else {
return 7.0f;
}
return 0.f;
}
- (void)userDidCancelPaymentViewController:(CardIOPaymentViewController *)scanViewController {
NSLog(@"User canceled payment info");
// Handle user cancellation here...
[scanViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)userDidProvideCreditCardInfo:(CardIOCreditCardInfo *)info inPaymentViewController:(CardIOPaymentViewController *)scanViewController {
HPFCardNumberTextField *cardNumberTextField = (HPFCardNumberTextField *) [self textFieldForIdentifier:@"number"];
cardNumberTextField.text = info.cardNumber;
[cardNumberTextField textFieldDidChange:nil];
[self textFieldDidChange:cardNumberTextField];
if (info.expiryMonth != 0 && info.expiryYear != 0) {
HPFExpiryDateTextField *expiryDateTextField = (HPFExpiryDateTextField *) [self textFieldForIdentifier:@"expiry_date"];
NSString *expiryYear = [NSString stringWithFormat:@"%lu", (unsigned long)info.expiryYear];
expiryYear = [NSString stringWithFormat:@"%02lu%@", (unsigned long)info.expiryMonth, [expiryYear substringFromIndex: [expiryYear length] - 2]];
NSAttributedString *expiryYearAttributed = [[HPFExpiryDateFormatter sharedFormatter] formattedDateWithPlainText:expiryYear];
expiryDateTextField.attributedText = expiryYearAttributed;
[self textFieldDidChange:expiryDateTextField];
}
if (info.cvv != nil && info.cvv.length > 0) {
HPFSecurityCodeTextField *securityCodeTextField = (HPFSecurityCodeTextField *) [self textFieldForIdentifier:@"security_code"];
securityCodeTextField.text = info.cvv;
[self textFieldDidChange:securityCodeTextField];
}
[scanViewController dismissViewControllerAnimated:YES completion:nil];
}
@end