Skip to content

Commit

Permalink
add helper to present the controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jukiginanjar committed Oct 13, 2016
1 parent 810a4a6 commit 898be22
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Demo/VTDirectDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ - (IBAction)checkoutPressed:(UIBarButtonItem *)sender {
VTTransactionDetails *transactionDetails = [[VTTransactionDetails alloc] initWithOrderID:[NSString randomWithLength:20] andGrossAmount:[self grossAmountOfItemDetails:self.itemDetails]];

if (customerDetails) {

VTCardListController *vc = [[VTCardListController alloc] initWithCustomerDetails:customerDetails itemDetails:self.itemDetails transactionDetails:transactionDetails];
VTAddCardController *vc = [[VTAddCardController alloc] initWithCustomerDetails:customerDetails itemDetails:self.itemDetails transactionDetails:transactionDetails];
// VTCardListController *vc = [[VTCardListController alloc] initWithCustomerDetails:customerDetails itemDetails:self.itemDetails transactionDetails:transactionDetails];
[vc presentOnViewController:self];

// [[VTCardControllerConfig sharedInstance] setEnableOneClick:YES];
Expand Down
5 changes: 4 additions & 1 deletion MidtransKit/MidtransKit/VTAddCardController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

@interface VTAddCardController : VTPaymentController
@property (nonatomic, assign) id<VTAddCardControllerDelegate>delegate;

- (void)presentOnViewController:(UIViewController *)viewController;

@end

@protocol VTAddCardControllerDelegate <NSObject>

- (void)viewController:(VTAddCardController *)viewController didRegisterCard:(VTMaskedCreditCard *)registeredCard;

@end
@end
86 changes: 48 additions & 38 deletions MidtransKit/MidtransKit/VTAddCardController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
#import <MidtransCoreKit/MidtransCoreKit.h>

@interface VTAddCardController ()
@property (strong, nonatomic) IBOutlet VTTextField *cardNumber;
@property (strong, nonatomic) IBOutlet VTTextField *cardExpiryDate;
@property (strong, nonatomic) IBOutlet VTTextField *cardCvv;
@property (strong, nonatomic) IBOutlet UIScrollView *fieldScrollView;
@property (strong, nonatomic) IBOutlet VTCCFrontView *cardFrontView;
@property (strong, nonatomic) IBOutlet UILabel *amountLabel;
@property (strong, nonatomic) IBOutlet UISwitch *saveCardSwitch;
@property (strong, nonatomic) IBOutlet UIView *saveOptionView;
@end

@property (strong, nonatomic) IBOutlet VTTextField *cardNumber;
@property (strong, nonatomic) IBOutlet VTTextField *cardExpiryDate;
@property (strong, nonatomic) IBOutlet VTTextField *cardCvv;
@property (strong, nonatomic) IBOutlet UIScrollView *fieldScrollView;
@property (strong, nonatomic) IBOutlet VTCCFrontView *cardFrontView;
@property (strong, nonatomic) IBOutlet UILabel *amountLabel;
@property (strong, nonatomic) IBOutlet UISwitch *saveCardSwitch;
@property (strong, nonatomic) IBOutlet UIView *saveOptionView;

@end

@implementation VTAddCardController

- (void)viewDidLoad {
[super viewDidLoad];

Expand All @@ -54,70 +54,80 @@ - (void)viewDidLoad {
self.saveOptionView.hidden = YES;
}
}

- (void)dealloc {
[_cardExpiryDate removeObserver:self forKeyPath:@"text"];
}


- (void)presentOnViewController:(UIViewController *)viewController {
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:self];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(closePressed:)];
[viewController presentViewController:nvc animated:YES completion:nil];
}

- (void)closePressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)handleTransactionSuccess:(VTTransactionResult *)result {
[super handleTransactionSuccess:result];
[self hideLoadingHud];
}

- (void)handleTransactionError:(NSError *)error {
[super handleTransactionError:error];
[self hideLoadingHud];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"text"] &&
[object isEqual:_cardExpiryDate]) {
_cardFrontView.expiryLabel.text = _cardExpiryDate.text;
}
}

- (IBAction)textFieldChanged:(id)sender {
if ([sender isEqual:_cardNumber]) {
_cardFrontView.iconView.image = [self iconWithNumber:_cardNumber.text];
_cardFrontView.numberLabel.text = _cardNumber.text;
}
}

- (UIImage *)iconDarkWithNumber:(NSString *)number {
switch ([VTCreditCardHelper typeFromString:number]) {
case VTCreditCardTypeVisa:
return [UIImage imageNamed:@"VisaDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"VisaDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
case VTCreditCardTypeJCB:
return [UIImage imageNamed:@"JCBDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"JCBDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
case VTCreditCardTypeMasterCard:
return [UIImage imageNamed:@"MasterCardDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"MasterCardDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
case VTCreditCardTypeAmex:
return [UIImage imageNamed:@"AmexDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"AmexDark" inBundle:VTBundle compatibleWithTraitCollection:nil];
default:
return nil;
return nil;
}
}

- (UIImage *)iconWithNumber:(NSString *)number {
switch ([VTCreditCardHelper typeFromString:number]) {
case VTCreditCardTypeVisa:
return [UIImage imageNamed:@"Visa" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"Visa" inBundle:VTBundle compatibleWithTraitCollection:nil];
case VTCreditCardTypeJCB:
return [UIImage imageNamed:@"JCB" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"JCB" inBundle:VTBundle compatibleWithTraitCollection:nil];
case VTCreditCardTypeMasterCard:
return [UIImage imageNamed:@"MasterCard" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"MasterCard" inBundle:VTBundle compatibleWithTraitCollection:nil];
case VTCreditCardTypeAmex:
return [UIImage imageNamed:@"Amex" inBundle:VTBundle compatibleWithTraitCollection:nil];
return [UIImage imageNamed:@"Amex" inBundle:VTBundle compatibleWithTraitCollection:nil];
default:
return nil;
return nil;
}
}

- (IBAction)cvvInfoPressed:(UIButton *)sender {
VTCvvInfoController *guide = [[VTCvvInfoController alloc] init];
[self.navigationController presentCustomViewController:guide onViewController:self.navigationController completion:nil];
}

- (IBAction)registerPressed:(UIButton *)sender {
NSString *cardNumber = [_cardNumber.text stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *dates = [_cardExpiryDate.text componentsSeparatedByString:@"/"];
Expand Down Expand Up @@ -151,7 +161,7 @@ - (IBAction)registerPressed:(UIButton *)sender {
}
}];
}

- (void)handleRegisterCreditCardError:(NSError *)error {
[self hideLoadingHud];
if (error.code == -20) {
Expand All @@ -169,9 +179,9 @@ - (void)handleRegisterCreditCardError:(NSError *)error {
[alert show];
}
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isKindOfClass:[VTTextField class]]) {
((VTTextField *) textField).warning = nil;
Expand All @@ -198,9 +208,9 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
return YES;
}
}

#pragma mark - Helper

- (void)payWithToken:(NSString *)token {
VTPaymentCreditCard *paymentDetail = [[VTPaymentCreditCard alloc] initWithFeature:VTCreditCardPaymentFeatureNormal token:token];
paymentDetail.saveToken = _saveCardSwitch.on;
Expand All @@ -215,5 +225,5 @@ - (void)payWithToken:(NSString *)token {
}
}];
}
@end

@end

0 comments on commit 898be22

Please sign in to comment.