// // FKVipBuyPayController.m // FirstLink // // Created by jack on 15/9/14. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "FKVipPayController.h" #import "OrderPayWayCell.h" #import "FLThirdPayHelper.h" #import "OrderPayBottomView.h" #import "ShakeRedPacketController.h" #import "FKVipPayReform.h" #import "FKPaySuccessAnimView.h" @interface FKVipPayController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIView *headerView; @property (nonatomic, strong) OrderPayBottomView *bottomView; @end @implementation FKVipPayController - (void)viewDidLoad{ [super viewDidLoad]; [self addAllSubViews]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self]; self.navigationItem.title = @"付款方式"; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipaySuccess:) name:AlipaySuccessNotificaitonName object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipayFailed:) name:AlipayFailedNotificaitonName object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weixinPaySuccess:) name:WeixinPaySuccessNotificaitonName object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weixinPayFailed:) name:WeixinPayFailedNotificaitonName object:nil]; } - (void)addAllSubViews{ [self.view addSubview:self.tableView]; [self.view addSubview:self.bottomView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.view); make.bottom.equalTo(self.bottomView.mas_top); }]; [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -34 : 0); make.height.equalTo(@45); }]; } #pragma mark - tableView dataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [FLThirdPayHelper isWXAppInstalled] ? 2 : 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ OrderPayWayCell *wayCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderPayWayCell class])]; if (indexPath.row == 0 && [FLThirdPayHelper isWXAppInstalled]) { wayCell.iconImgView.image = [UIImage imageNamed:@"confirm_body_weixin_n"]; wayCell.titleLabel.text = @"微信支付"; wayCell.bottomLine.hidden = NO; wayCell.catchImgView.hidden = NO; return wayCell; }else{ wayCell.iconImgView.image = [UIImage imageNamed:@"confirm_body_zhifubao_n"]; wayCell.titleLabel.text = @"支付宝支付"; wayCell.bottomLine.hidden = YES; wayCell.catchImgView.hidden = [FLThirdPayHelper isWXAppInstalled] ? YES : NO; return wayCell; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) return 45; return 0; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if (section == 0) { return self.headerView; } return nil; } #pragma mark - tableView delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ for (OrderPayWayCell *cell in self.tableView.visibleCells) { if ([cell isKindOfClass:[OrderPayWayCell class]]) cell.catchImgView.hidden = YES; } OrderPayWayCell *wayCell = (OrderPayWayCell *)[tableView cellForRowAtIndexPath:indexPath]; if ([wayCell isKindOfClass:[OrderPayWayCell class]]) { wayCell.catchImgView.hidden = NO; } } #pragma mark - response - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{ if (header.code.intValue == RESPONSE_MSG_BUSINESS_EXCEPTION) { [FLProgressHUDHelper showText:header.msg inView:self.view]; }else if (header.code.intValue == RESPONSE_MSG_ACCESS_EXCEPTION){ [FLProgressHUDHelper showText:@"请求失败" inView:self.view]; } } - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header{ // [self.hudView hide:NO]; if (identify == WX_PREPARE_ID_REQ) { NSString *prepayId = [FKVipPayReform parserWxPrepayId:response]; if (prepayId.length > 0) [FLThirdPayHelper wxPayWithPrepayID:prepayId]; } } #pragma mark - action - (void)payButtonClick{ NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; // if (indexPath && indexPath.section == 0) return; if (![FLThirdPayHelper isWXAppInstalled] || indexPath.row == 1){ [FLThirdPayHelper alipayLaunchWithPaymentModel:self.payItem]; }else if (!indexPath || indexPath.row == 0){ [FLThirdPayHelper getWxPayPrepayIdWithOrderID:self.payItem.paymentNo delegate:self]; } } #pragma mark - alipay response - (void)setUserAsVIP { User *user = [FKUserManager sharedManager].user; user.level = @"2"; [[FKUserManager sharedManager] saveUser:user]; } - (void)alipaySuccess:(NSNotification *)notification{ [self setUserAsVIP]; WeakSelf(weakSelf); [FKPaySuccessAnimView showInView:self.view finish:^(BOOL done) { [weakSelf paySuccessDelegateAction]; }]; } - (void)alipayFailed:(NSNotificationCenter *)notification{ if (self.delegate && [self.delegate respondsToSelector:@selector(vipPayFail)]) { [self.delegate vipPayFail]; } } - (void)weixinPaySuccess:(NSNotification *)notification{ [self setUserAsVIP]; WeakSelf(weakSelf); [FKPaySuccessAnimView showInView:self.view finish:^(BOOL done) { [weakSelf paySuccessDelegateAction]; }]; } - (void)weixinPayFailed:(NSNotification *)notification{ if (self.delegate && [self.delegate respondsToSelector:@selector(vipPayFail)]) { [self.delegate vipPayFail]; } } #pragma mark - method - (void)paySuccessDelegateAction{ if (self.delegate && [self.delegate respondsToSelector:@selector(vipPaySuccess)]) { [self.delegate vipPaySuccess]; } } #pragma mark - property - (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.sectionFooterHeight = 0; _tableView.backgroundColor = COMMON_TABLEVIEW_BACKGROUND_COLOR; [_tableView registerClass:[OrderPayWayCell class] forCellReuseIdentifier:NSStringFromClass([OrderPayWayCell class])]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _tableView; } - (UIView *)headerView{ if (_headerView == nil) { _headerView = [[UIView alloc]init]; _headerView.backgroundColor = [UIColor clearColor]; UILabel *titleLabel = ({ UILabel *label = [[UILabel alloc]init]; label.font = [UIFont systemFontOfSize:14]; label.textColor = UIColorFromRGB(0x333333); label.text = @"选择支付方式"; label; }); [_headerView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_headerView).offset(15); make.bottom.equalTo(_headerView).offset(- 15); }]; } return _headerView; } - (OrderPayBottomView *)bottomView { if (_bottomView == nil) { _bottomView = [[OrderPayBottomView alloc]init]; [_bottomView.payButton addTarget:self action:@selector(payButtonClick) forControlEvents:UIControlEventTouchUpInside]; _bottomView.amountLabel.text = [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:self.payItem.totalFee]]; } return _bottomView; } @end