Bez popisu

FKVipPayController.m 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // FKVipBuyPayController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/9/14.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKVipPayController.h"
  9. #import "OrderPayWayCell.h"
  10. #import "FLThirdPayHelper.h"
  11. #import "OrderPayBottomView.h"
  12. #import "ShakeRedPacketController.h"
  13. #import "FKVipPayReform.h"
  14. #import "FKPaySuccessAnimView.h"
  15. @interface FKVipPayController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate, UIAlertViewDelegate>
  16. @property (nonatomic, strong) UITableView *tableView;
  17. @property (nonatomic, strong) UIView *headerView;
  18. @property (nonatomic, strong) OrderPayBottomView *bottomView;
  19. @end
  20. @implementation FKVipPayController
  21. - (void)viewDidLoad{
  22. [super viewDidLoad];
  23. [self addAllSubViews];
  24. }
  25. - (void)viewWillAppear:(BOOL)animated
  26. {
  27. [super viewWillAppear:animated];
  28. [[NSNotificationCenter defaultCenter] removeObserver:self];
  29. self.navigationItem.title = @"付款方式";
  30. }
  31. - (void)viewDidAppear:(BOOL)animated
  32. {
  33. [super viewDidAppear:animated];
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipaySuccess:) name:AlipaySuccessNotificaitonName object:nil];
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alipayFailed:) name:AlipayFailedNotificaitonName object:nil];
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weixinPaySuccess:) name:WeixinPaySuccessNotificaitonName object:nil];
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weixinPayFailed:) name:WeixinPayFailedNotificaitonName object:nil];
  38. }
  39. - (void)addAllSubViews{
  40. [self.view addSubview:self.tableView];
  41. [self.view addSubview:self.bottomView];
  42. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.left.right.equalTo(self.view);
  44. make.bottom.equalTo(self.bottomView.mas_top);
  45. }];
  46. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.right.equalTo(self.view);
  48. make.bottom.equalTo(self.view).offset(IS_IPHONE_X ? -34 : 0);
  49. make.height.equalTo(@45);
  50. }];
  51. }
  52. #pragma mark - tableView dataSource
  53. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  54. return [FLThirdPayHelper isWXAppInstalled] ? 2 : 1;
  55. }
  56. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  57. OrderPayWayCell *wayCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderPayWayCell class])];
  58. if (indexPath.row == 0 && [FLThirdPayHelper isWXAppInstalled]) {
  59. wayCell.iconImgView.image = [UIImage imageNamed:@"confirm_body_weixin_n"];
  60. wayCell.titleLabel.text = @"微信支付";
  61. wayCell.bottomLine.hidden = NO;
  62. wayCell.catchImgView.hidden = NO;
  63. return wayCell;
  64. }else{
  65. wayCell.iconImgView.image = [UIImage imageNamed:@"confirm_body_zhifubao_n"];
  66. wayCell.titleLabel.text = @"支付宝支付";
  67. wayCell.bottomLine.hidden = YES;
  68. wayCell.catchImgView.hidden = [FLThirdPayHelper isWXAppInstalled] ? YES : NO;
  69. return wayCell;
  70. }
  71. }
  72. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  73. return 60;
  74. }
  75. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  76. if (section == 0) return 45;
  77. return 0;
  78. }
  79. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  80. if (section == 0) {
  81. return self.headerView;
  82. }
  83. return nil;
  84. }
  85. #pragma mark - tableView delegate
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  87. for (OrderPayWayCell *cell in self.tableView.visibleCells) {
  88. if ([cell isKindOfClass:[OrderPayWayCell class]]) cell.catchImgView.hidden = YES;
  89. }
  90. OrderPayWayCell *wayCell = (OrderPayWayCell *)[tableView cellForRowAtIndexPath:indexPath];
  91. if ([wayCell isKindOfClass:[OrderPayWayCell class]]) {
  92. wayCell.catchImgView.hidden = NO;
  93. }
  94. }
  95. #pragma mark - response
  96. - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{
  97. if (header.code.intValue == RESPONSE_MSG_BUSINESS_EXCEPTION) {
  98. [FLProgressHUDHelper showText:header.msg inView:self.view];
  99. }else if (header.code.intValue == RESPONSE_MSG_ACCESS_EXCEPTION){
  100. [FLProgressHUDHelper showText:@"请求失败" inView:self.view];
  101. }
  102. }
  103. - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header{
  104. // [self.hudView hide:NO];
  105. if (identify == WX_PREPARE_ID_REQ) {
  106. NSString *prepayId = [FKVipPayReform parserWxPrepayId:response];
  107. if (prepayId.length > 0) [FLThirdPayHelper wxPayWithPrepayID:prepayId];
  108. }
  109. }
  110. #pragma mark - action
  111. - (void)payButtonClick{
  112. NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  113. // if (indexPath && indexPath.section == 0) return;
  114. if (![FLThirdPayHelper isWXAppInstalled] || indexPath.row == 1){
  115. [FLThirdPayHelper alipayLaunchWithPaymentModel:self.payItem];
  116. }else if (!indexPath || indexPath.row == 0){
  117. [FLThirdPayHelper getWxPayPrepayIdWithOrderID:self.payItem.paymentNo delegate:self];
  118. }
  119. }
  120. #pragma mark - alipay response
  121. - (void)setUserAsVIP {
  122. User *user = [FKUserManager sharedManager].user;
  123. user.level = @"2";
  124. [[FKUserManager sharedManager] saveUser:user];
  125. }
  126. - (void)alipaySuccess:(NSNotification *)notification{
  127. [self setUserAsVIP];
  128. WeakSelf(weakSelf);
  129. [FKPaySuccessAnimView showInView:self.view finish:^(BOOL done) {
  130. [weakSelf paySuccessDelegateAction];
  131. }];
  132. }
  133. - (void)alipayFailed:(NSNotificationCenter *)notification{
  134. if (self.delegate && [self.delegate respondsToSelector:@selector(vipPayFail)]) {
  135. [self.delegate vipPayFail];
  136. }
  137. }
  138. - (void)weixinPaySuccess:(NSNotification *)notification{
  139. [self setUserAsVIP];
  140. WeakSelf(weakSelf);
  141. [FKPaySuccessAnimView showInView:self.view finish:^(BOOL done) {
  142. [weakSelf paySuccessDelegateAction];
  143. }];
  144. }
  145. - (void)weixinPayFailed:(NSNotification *)notification{
  146. if (self.delegate && [self.delegate respondsToSelector:@selector(vipPayFail)]) {
  147. [self.delegate vipPayFail];
  148. }
  149. }
  150. #pragma mark - method
  151. - (void)paySuccessDelegateAction{
  152. if (self.delegate && [self.delegate respondsToSelector:@selector(vipPaySuccess)]) {
  153. [self.delegate vipPaySuccess];
  154. }
  155. }
  156. #pragma mark - property
  157. - (UITableView *)tableView{
  158. if (_tableView == nil) {
  159. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  160. _tableView.dataSource = self;
  161. _tableView.delegate = self;
  162. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  163. _tableView.sectionFooterHeight = 0;
  164. _tableView.backgroundColor = COMMON_TABLEVIEW_BACKGROUND_COLOR;
  165. [_tableView registerClass:[OrderPayWayCell class] forCellReuseIdentifier:NSStringFromClass([OrderPayWayCell class])];
  166. if (@available(iOS 11.0, *)) {
  167. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  168. }
  169. }
  170. return _tableView;
  171. }
  172. - (UIView *)headerView{
  173. if (_headerView == nil) {
  174. _headerView = [[UIView alloc]init];
  175. _headerView.backgroundColor = [UIColor clearColor];
  176. UILabel *titleLabel = ({
  177. UILabel *label = [[UILabel alloc]init];
  178. label.font = [UIFont systemFontOfSize:14];
  179. label.textColor = UIColorFromRGB(0x333333);
  180. label.text = @"选择支付方式";
  181. label;
  182. });
  183. [_headerView addSubview:titleLabel];
  184. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  185. make.left.equalTo(_headerView).offset(15);
  186. make.bottom.equalTo(_headerView).offset(- 15);
  187. }];
  188. }
  189. return _headerView;
  190. }
  191. - (OrderPayBottomView *)bottomView
  192. {
  193. if (_bottomView == nil) {
  194. _bottomView = [[OrderPayBottomView alloc]init];
  195. [_bottomView.payButton addTarget:self action:@selector(payButtonClick) forControlEvents:UIControlEventTouchUpInside];
  196. _bottomView.amountLabel.text = [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:self.payItem.totalFee]];
  197. }
  198. return _bottomView;
  199. }
  200. @end