Skip to content

Instantly share code, notes, and snippets.

@thuycom205
Last active July 7, 2018 07:21
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/eb48fc0aede985a7ce9b59fcabebb54d to your computer and use it in GitHub Desktop.
Save thuycom205/eb48fc0aede985a7ce9b59fcabebb54d to your computer and use it in GitHub Desktop.
#pragma mark - Present Payment Method Controller
- (void)presentLoginFormSheetFromController:(BFFormSheetViewController *)controller {
MNSelectPaymentMethodViewController *selectedCartController = (MNSelectPaymentMethodViewController*)controller;
selectedCartController.delegate = self;
if (self.selectedsavedCard) {
selectedCartController.selectedSavedCard = self.selectedsavedCard;
}
if (self.selectedPaymentMethod) {
selectedCartController.selectedPaymentMethod = self.selectedPaymentMethod;
}
//if it is virtual cart and it is guest cart
BOOL isVirtual = [self.cartInfo.is_virtual boolValue];
if (isVirtual && !self.isCustomer) {
if (self.availablePaymentMethods) {
//place holder
selectedCartController.paymentMethods = [self.availablePaymentMethods mutableCopy];
// present form sheet
[self showPaymentFormSheet:controller];
return;
} else {
//request available payment Method
if (self.selectedBillingAddress) {
[self getPaymentInformationForVirtualQuote:YES controller:controller];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self removeOverlay];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Billing address is required"
message:@"Please fill in billing address to continue to process checkout."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
return;;
}
}
// NSDictionary *params = [self formParamToSetShippingInformation];
} else if (!isVirtual && !self.isCustomer) {
//need billing adddress and shipping address and selected shipping method
NSString *message;
BOOL missingRequiredField = NO;
if ( !self.selectedShippingAddress) {
dispatch_async(dispatch_get_main_queue(), ^{
[self removeOverlay];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Shipping address is required"
message:@"Please fill in shipping address to continue to process checkout."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
return;
}
//billing address
if ( !self.selectedBillingAddress) {
dispatch_async(dispatch_get_main_queue(), ^{
[self removeOverlay];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Billing address is required"
message:@"Please fill in billing address to continue to process checkout."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
return;
}
if (self.availablePaymentMethods) {
[self showPaymentFormSheet:controller];
} else {
[self formShippingMethodPostsRequest];
NSDictionary *payload = [self.setShippingMethodRequestParam copy];
MagentoGuestCart *guestCart = [MagentoGuestCart sharedGuestCart];
NSString *quoteId = guestCart.quoteData.quoteId;
NSString *url = [BFAPIPath setShippingInformationForGuest:quoteId];
[[BFAPIManager sharedManager] setShippingInformationForGuest: url param:payload completionBlock:^(NSArray * _Nullable records, id _Nullable customResponse, NSError * _Nullable error) {
if (error) {
MagentoGuestCart *guestCart = [MagentoGuestCart sharedGuestCart];
NSString *quoteId = guestCart.quoteData.quoteId;
NSInteger code = error.code;
NSArray *keys = [error.userInfo allKeys];
NSLog(@"error");
NSDictionary *errorDic = [error userInfo];
id data = [errorDic objectForKey:@"com.alamofire.serialization.response.error.data"];
NSString * convertedStr =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(convertedStr);
[self removeOverlay];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error happen"
message:convertedStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
} else {
self.availablePaymentMethods = records;
dispatch_async(dispatch_get_main_queue(), ^ {
[self displaySelectPaymentController:controller];
});
}
}];
}
}
else if (isVirtual && self.isCustomer) {
if (!self.selectedBillingAddress) {
dispatch_async(dispatch_get_main_queue(), ^{
[self removeOverlay];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Billing address is required"
message:@"Please fill in billing address to continue to process checkout."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
} else {
if (self.availablePaymentMethods) {
//placeholder
[self showPaymentFormSheet:controller];
} else {
//form the payment-information request
NSMutableDictionary *payload = [[NSMutableDictionary alloc] init];
if (self.selectedBillingAddress) {
NSDictionary *shippingAdd = [self formDictionaryFromAddress: self.selectedBillingAddress];
NSDictionary *billingAddress = @{@"billing_address": shippingAdd};
[payload setValue:billingAddress forKey:@"addressInformation"];
}
NSDictionary *param = [payload copy];
//request lis of payment method from server
[[BFAPIManager sharedManager] getPaymentInformation:param completionBlock:^(NSArray * _Nullable records, id _Nullable customResponse, NSError * _Nullable error) {
self.availablePaymentMethods = records;
//place holder
[self showPaymentFormSheet:controller];
}];
}
}
} else
{
if (self.availablePaymentMethods) {
//placeholder
[self showPaymentFormSheet:controller];
} else {
[self formShippingMethodPostsRequest];
NSDictionary *params = [self.setShippingMethodRequestParam copy];
[[BFAPIManager sharedManager] setShippingInformation:params completionBlock:^(NSArray * _Nullable records, id _Nullable customResponse, NSError * _Nullable error) {
if (error) {
NSInteger code = error.code;
NSArray *keys = [error.userInfo allKeys];
NSLog(@"error");
NSDictionary *errorDic = [error userInfo];
id data = [errorDic objectForKey:@"com.alamofire.serialization.response.error.data"];
NSString * convertedStr =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(convertedStr);
dispatch_async(dispatch_get_main_queue(), ^{
[self removeOverlay];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error happened"
message:convertedStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
});
} else {
self.availablePaymentMethods = records;
[self showPaymentFormSheet:controller];
}
}];
}
}
}
-(void)displaySelectPaymentController:(BFFormSheetViewController *)controller {
MNSelectPaymentMethodViewController *selectedCartController = (MNSelectPaymentMethodViewController*)controller;
selectedCartController.paymentMethods = [[NSMutableArray alloc]init];
[selectedCartController.paymentMethods addObjectsFromArray:self.availablePaymentMethods];
[selectedCartController presentFormSheetWithSize:CGSizeMake(self.view.frame.size.width, contentHeight) optionsHandler:^(MZFormSheetPresentationViewController *formSheetController) {
formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyleSlideFromBottom;
// disable default MZFormSheet background color
formSheetController.presentationController.backgroundColor = [UIColor clearColor];
// custom presentation options with handlers
__weak __typeof__(self) weakSelf = self;
formSheetController.presentationController.dismissalTransitionWillBeginCompletionHandler = ^(UIViewController *presentedFSViewController) {
[weakSelf setContentViewHidden:NO animated:YES];
};
formSheetController.presentationController.dismissalTransitionDidEndCompletionHandler = ^(UIViewController *presentedFSViewController, BOOL completed) {
NSLog(@"complete");
[self removeOverlay];
for (int i = 0; i < self.numOfExtensions ; i++) {
id extension = [self extensionAtIndex:i];
if ( [extension isKindOfClass:[MNPaymentCellExtension class]] ) {
MNPaymentCellExtension *paymentExt = (MNPaymentCellExtension*) extension;
paymentExt.mode = 0;
[self reloadExtensions:@[paymentExt]];
}
}
for (UIView *subView in weakSelf.view.subviews) {
if ([subView.restorationIdentifier isEqualToString:@"overlay"]) {
[subView removeFromSuperview];
}
}
for (UIView *subview in self.navigationController.view.subviews) {
if ([subview.restorationIdentifier isEqualToString:@"overlay"]) {
[subview removeFromSuperview];
}
}
};
formSheetController.presentationController.presentationTransitionWillBeginCompletionHandler = ^(UIViewController *presentedFSViewController) {
[weakSelf setContentViewHidden:YES animated:YES];
[weakSelf.overLayWindow removeFromSuperview];
};
formSheetController.presentationController.frameConfigurationHandler = ^(UIView *presentedView, CGRect currentFrame, BOOL isKeyboardVisible) {
if (isKeyboardVisible) {
UIView *overLayWindow = [[UIView alloc]init];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//[window addSubview:overLayWindow];
overLayWindow.frame = self.navigationController.view.bounds;
overLayWindow.accessibilityHint = @"dim View";
overLayWindow.restorationIdentifier = @"overlay";
// [self.view addSubview:overLayWindow];
[self.navigationController.view addSubview:overLayWindow];
//self.navigationController.navigationBar.layer.zPosition = -1;
overLayWindow.backgroundColor = [UIColor BFN_dimColor];
// self.overLayWindow = overLayWindow;
// weakSelf.view.backgroundColor = [UIColor BFN_darkerDimColor];
return CGRectMake(currentFrame.origin.x,
weakSelf.view.frame.size.height - currentFrame.size.height - 8,
currentFrame.size.width,
currentFrame.size.height);
}
else {
UIView *overLayWindow = [[UIView alloc]init];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
// [window addSubview:overLayWindow];
overLayWindow.frame = self.navigationController.view.bounds;
[self.view addSubview:overLayWindow];
self.navigationController.navigationBar.layer.zPosition = -1;
overLayWindow.accessibilityHint = @"dim View";
overLayWindow.restorationIdentifier = @"overlay";
overLayWindow.backgroundColor = [UIColor BFN_lighterDimColor];
[self.navigationController.view addSubview:overLayWindow];
// self.overLayWindow = overLayWindow;
// weakSelf.view.backgroundColor = [UIColor BFN_dimColor];
return CGRectMake(currentFrame.origin.x,
weakSelf.view.bounds.size.height - currentFrame.size.height + 60,
currentFrame.size.width,
currentFrame.size.height);
}
};
} animated:YES fromSender:self];
}
- (void)presentSelectShippingMethodFormSheetFromController:(id)sender {
MNShippingMethodEachVendorCollectionViewCell *cell = (MNShippingMethodEachVendorCollectionViewCell*)sender;
MNVendorCartItem *vendorSection = (MNVendorCartItem*)cell.data;
MNSelectShippingMethodViewController *selectedCartController =(MNSelectShippingMethodViewController*) [self BFN_mainStoryboardClassInstanceWithIdentifier:@"MNSelectShippingMethodViewController"];
selectedCartController.delegate = self;
selectedCartController.selectedShippingMethod = vendorSection.selectedShippingMethod;
selectedCartController.shippingMethods = [[NSMutableArray alloc] init];
[selectedCartController.shippingMethods addObjectsFromArray:[vendorSection.shippingMethods copy]];
selectedCartController.vendorSection = vendorSection;
selectedCartController.delegate = self;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// present form sheet
[selectedCartController presentFormSheetWithSize:CGSizeMake(self.view.frame.size.width, contentHeight) optionsHandler:^(MZFormSheetPresentationViewController *formSheetController) {
formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyleSlideFromBottom;
// disable default MZFormSheet background color
formSheetController.presentationController.backgroundColor = [UIColor clearColor];
// custom presentation options with handlers
__weak __typeof__(self) weakSelf = self;
formSheetController.presentationController.dismissalTransitionWillBeginCompletionHandler = ^(UIViewController *presentedFSViewController) {
[weakSelf setContentViewHidden:NO animated:YES];
[weakSelf.overLayWindow removeFromSuperview];
};
formSheetController.presentationController.dismissalTransitionDidEndCompletionHandler = ^(UIViewController *presentedFSViewController, BOOL completed) {
NSLog(@"complete2");
/** */
[weakSelf.overLayWindow removeFromSuperview];
for (UIView *subView in weakSelf.view.subviews) {
if ([subView.restorationIdentifier isEqualToString:@"overlay"]) {
[subView removeFromSuperview];
}
}
//self.navigationController.navigationBar.layer.zPosition = 0;
for (UIView *subview in self.navigationController.view.subviews) {
if ([subview.restorationIdentifier isEqualToString:@"overlay"]) {
[subview removeFromSuperview];
}
}
};
formSheetController.presentationController.presentationTransitionWillBeginCompletionHandler = ^(UIViewController *presentedFSViewController) {
[weakSelf setContentViewHidden:YES animated:YES];
[weakSelf.overLayWindow removeFromSuperview];
};
formSheetController.presentationController.frameConfigurationHandler = ^(UIView *presentedView, CGRect currentFrame, BOOL isKeyboardVisible) {
if (isKeyboardVisible) {
UIView *overLayWindow = [[UIView alloc]init];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//[window addSubview:overLayWindow];
overLayWindow.frame = self.navigationController.view.bounds;
overLayWindow.accessibilityHint = @"dim View";
overLayWindow.restorationIdentifier = @"overlay";
// [self.view addSubview:overLayWindow];
[self.navigationController.view addSubview:overLayWindow];
//self.navigationController.navigationBar.layer.zPosition = -1;
overLayWindow.backgroundColor = [UIColor BFN_dimColor];
// self.overLayWindow = overLayWindow;
// weakSelf.view.backgroundColor = [UIColor BFN_darkerDimColor];
return CGRectMake(currentFrame.origin.x,
weakSelf.view.frame.size.height - currentFrame.size.height - 8,
currentFrame.size.width,
currentFrame.size.height);
}
else {
UIView *overLayWindow = [[UIView alloc]init];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
// [window addSubview:overLayWindow];
overLayWindow.frame = self.navigationController.view.bounds;
[self.view addSubview:overLayWindow];
self.navigationController.navigationBar.layer.zPosition = -1;
overLayWindow.accessibilityHint = @"dim View";
overLayWindow.restorationIdentifier = @"overlay";
overLayWindow.backgroundColor = [UIColor BFN_lighterDimColor];
[self.navigationController.view addSubview:overLayWindow];
// self.overLayWindow = overLayWindow;
// weakSelf.view.backgroundColor = [UIColor BFN_dimColor];
return CGRectMake(currentFrame.origin.x,
weakSelf.view.bounds.size.height - currentFrame.size.height -30,
currentFrame.size.width,
currentFrame.size.height);
}
};
} animated:YES fromSender:self];
////////////////////////////////////////////////////////////////////////////////////////////////////////
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedIndex = indexPath.row;
MNShippingInfo *method = [self.shippingMethods objectAtIndex:indexPath.row];
self.delegate.neeUpdateVendorShippingMehtod = YES;
NSDictionary *updateInfo = @{@"shipping_method" :method, @"vendor_id":self.vendorSection.vendor_id };
self.delegate.updateShippingMethodInfo = updateInfo;
[self dismissViewControllerAnimated:YES completion:^() {
NSLog(@"dismiss the selected shipping method 1");
[self.delegate updateShippingMethodForVendorServerSide];
}
];
}
-(void) updateShippingMethodForVendorServerSide {
if (self.neeUpdateVendorShippingMehtod) {
// NSDictionary *updateInfo = @{@"shipping_method" :method, @"vendor_id":self.vendorSection.vendor_id };
//
if ( self.updateShippingMethodInfo) {
NSString *vendor_id = [self.updateShippingMethodInfo objectForKey:@"vendor_id"];
for (MNVendorCartItem *vendor in self.vendors ) {
if ([vendor.vendor_id isEqualToString:vendor_id]) {
vendor.selectedShippingMethod = [self.updateShippingMethodInfo objectForKey:@"shipping_method"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
}
}
self.neeUpdateVendorShippingMehtod = NO;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment