Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thuycom205/c96e5a87e40ce6085d19b7854de5a2af to your computer and use it in GitHub Desktop.
Save thuycom205/c96e5a87e40ce6085d19b7854de5a2af to your computer and use it in GitHub Desktop.
//
// MNSelectPaymentMethodViewController.m
// OpenShop
//
// Created by Thuy Luu on 2/26/18.
// Copyright © 2018 Business-Factory. All rights reserved.
//
#import "MNSelectPaymentMethodViewController.h"
#import "MNSavedCardPaymentTableViewCell.h"
#import "MNPaymentMethodTableViewCell.h"
#import "MNMasterCardInputTableViewCell.h"
@interface MNSelectPaymentMethodViewController ()
@end
@implementation MNSelectPaymentMethodViewController
- (void)mockData {
for (int i = 0 ; i < 5; i++) {
MNPaymentMethod *paymentMethod = [[MNPaymentMethod alloc] init];
paymentMethod.code = @"co";
paymentMethod.title = @"Cash On Delivery";
[self.paymentMethods addObject:paymentMethod];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.step == 0) {
NSUInteger savedCardCount = [self.savedCards count];
return [self.paymentMethods count] + savedCardCount;
} else if (self.step == 1) {
return 1;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.step == 0) {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedIndex = indexPath.row;
NSUInteger savedCardCount = [self.savedCards count];
if (indexPath.row < savedCardCount) {
MNPaymentMethod *vaultMethod = [MNPaymentMethod new];
vaultMethod.title = @"Credit Card";
vaultMethod.code = @"tns_hpf_vault";
self.delegate.selectedPaymentMethod = vaultMethod;
self.delegate.selectedsavedCard = [self.savedCards objectAtIndex:indexPath.row];
[self dismissViewControllerAnimated:YES completion:^() {
NSLog(@"dismiss the selected payment");
[self.delegate removeOverlay];
}
];
}
else {
NSUInteger paymentIndex = indexPath.row - savedCardCount;
self.delegate.selectedPaymentMethod = [self.paymentMethods objectAtIndex:paymentIndex];
self.selectedPaymentMethod = [self.paymentMethods objectAtIndex:paymentIndex];
if ( [self.selectedPaymentMethod.code isEqualToString:@"tns_hpf"] ) {
self.step = 1 ;
[self.tableView reloadData];
} else {
[self dismissViewControllerAnimated:YES completion:^() {
NSLog(@"dismiss the selected payment");
[self.delegate removeOverlay];
}
];
}
}
} else if (self.step == 1) {
NSLog(@"we dont process logic here");
}
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = indexPath.row;
if (self.step == 0) {
NSUInteger savedCardCount = [self.savedCards count];
if (savedCardCount == 0) {
MNPaymentMethod *paymentMethod = [self.paymentMethods objectAtIndex:indexPath.row];
if ([paymentMethod.code isEqualToString:@"tns_hpf_no_exist"]) {
MNMasterCardInputTableViewCell *cell = (MNMasterCardInputTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"masterCardCell"];
cell.paymentMethodLabel.text = paymentMethod.title;
return cell;
} else if ([paymentMethod.code isEqualToString:@"paypal"]) {
} else {
MNPaymentMethodTableViewCell *cell = (MNPaymentMethodTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"normalPaymentCell"];
if(!cell) {
cell = [[MNPaymentMethodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"normalPaymentCell"];
}
cell.methodTitleLabel.text = paymentMethod.title;
return cell;
}
} else {
if (row < savedCardCount ){
MNSavedCard *savedCardItem = [self.savedCards objectAtIndex:row];
MNSavedCardPaymentTableViewCell *cell = (MNSavedCardPaymentTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"saveCard"];
cell.tokenLabel.text = savedCardItem.card_code;
return cell;
} else {
NSUInteger paymentIndex = indexPath.row - savedCardCount;
MNPaymentMethod *paymentMethod = [self.paymentMethods objectAtIndex:paymentIndex];
MNPaymentMethodTableViewCell *cell = (MNPaymentMethodTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"normalPaymentCell"];
if(!cell) {
cell = [[MNPaymentMethodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"normalPaymentCell"];
}
cell.methodTitleLabel.text = paymentMethod.title;
return cell;
}
}
} else if (self.step == 1) {
MNMasterCardInputTableViewCell *cell = (MNMasterCardInputTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"masterCardCell"];
cell.paymentMethodLabel.text = @"Credit Card Information";
cell.cvvTextField.keyboardType = UIKeyboardTypeNumberPad;
cell.expiredDateField.keyboardType = UIKeyboardTypeNumberPad;
cell.expiredYearField.keyboardType = UIKeyboardTypeNumberPad;
cell.cardNoTextField.keyboardType = UIKeyboardTypeNumberPad;
// cell.datePicker=[[UIDatePicker alloc]init];
//cell.datePicker.datePickerMode=UIDatePickerModeDate;
// [cell.expiredDateField setInputView: cell.datePicker];
// UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
// [toolBar setTintColor:[UIColor grayColor]];
// UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:cell action:@selector(ShowSelectedDate)];
// UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
//cell.expiredDateField setInputAccessoryView:toolBar];
[cell.continueBtn addTarget:self action:@selector(continue:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
// return nil;
}
-(void)SetTextFieldBorder :(UITextField *)textField red:(BOOL) isRed{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 1;
if (isRed) {
border.borderColor = [UIColor redColor].CGColor;
} else {
border.borderColor = [UIColor clearColor].CGColor;
}
border.cornerRadius = 1.0;
border.frame = CGRectMake(0, 0, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
}
-(void)continue: (id)sender {
UIButton *btn = (UIButton*)sender;
MNMasterCardInputTableViewCell *cell = (MNMasterCardInputTableViewCell*)[[btn superview] superview];
NSString *creditCardNo = cell.cardNoTextField.text;
NSString *expiryMonth = cell.expiredDateField.text;
NSString *expiryYear = cell.expiredYearField.text;
NSString *securityCode = cell.cvvTextField.text;
BOOL isValid = YES;
[self.requiredValidator validate:creditCardNo];
if (self.requiredValidator.state == AJWValidatorValidationStateInvalid) {
isValid= NO;
[self SetTextFieldBorder:cell.cardNoTextField red:YES];
} else {
[self SetTextFieldBorder:cell.cardNoTextField red:NO];
}
[self.requiredValidator validate:expiryMonth];
if (self.requiredValidator.state == AJWValidatorValidationStateInvalid) {
isValid= NO;
[self SetTextFieldBorder:cell.expiredDateField red:YES];
} else {
[self SetTextFieldBorder:cell.expiredDateField red:NO];
}
[self.requiredValidator validate:expiryYear];
if (self.requiredValidator.state == AJWValidatorValidationStateInvalid) {
isValid= NO;
[self SetTextFieldBorder:cell.expiredYearField red:YES];
} else {
[self SetTextFieldBorder:cell.expiredYearField red:NO];
}
[self.requiredValidator validate:securityCode];
if (self.requiredValidator.state == AJWValidatorValidationStateInvalid) {
isValid= NO;
[self SetTextFieldBorder:cell.cvvTextField red:YES];
} else {
[self SetTextFieldBorder:cell.cvvTextField red:NO];
}
if (!isValid) {
return;
}
NSLog(creditCardNo);
//todo:
if (creditCardNo && expiryMonth && expiryYear && securityCode) {
NSDictionary *creditCardInfo = @{ @"cardNumber":@"4111111111111111",
@"expiryMonth": expiryMonth,
@"expiryYear": expiryYear,
@"securityCode" :securityCode
};
self.delegate.creditCardInfo = creditCardInfo;
[self dismissViewControllerAnimated:YES completion:^() {
NSLog(@"dismiss the selected payment");
self.step = 0;
[self.delegate requestPaymentSession];
}
];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Field is required"
message:@"Please fill all required fields."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 68;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.requiredValidator = [AJWValidator validatorWithType:AJWValidatorTypeString];
[self.requiredValidator addValidationToEnsurePresenceWithInvalidMessage:@"required"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment