// // YMMineRechargeViewController.m // YUMI // // Created by YUMI on 2021/9/24. // #import "XPMineRechargeViewController.h" ///Third #import #import ///Tool #import "DJDKMIMOMColor.h" #import "YUMIMacroUitls.h" #import "YUMIHtmlUrl.h" #import "XPIAPHelper.h" #import "RechargeStorage.h" #import "AccountInfoStorage.h" #import "NSObject+MJExtension.h" #import "NSArray+Safe.h" ///Model #import "RechargeListModel.h" ///View #import "XPMineRechageHeadView.h" #import "XPMineRechargeTableViewCell.h" #import "XPMineRechargeNavView.h" ///P #import "XPMineRechargePresenter.h" #import "XPMineRechargeProtocol.h" ///VC #import "XPWebViewController.h" @interface XPMineRechargeViewController () ///数据源 @property (nonatomic,strong) NSArray *datasource; ///列表 @property (nonatomic,strong) UITableView *tableView; /// @property (nonatomic,strong) XPMineRechageHeadView *headeView; ///订单编号 @property (nonatomic,copy) NSString *orderId; ///导航栏 @property (nonatomic,strong) XPMineRechargeNavView *navView; /// @property (nonatomic,strong) UIStackView *stackView; ///同意 @property (nonatomic,strong) UILabel *agreeLabel; ///充值协议 @property (nonatomic,strong) UIButton *protcoloButton; @end @implementation XPMineRechargeViewController - (void)dealloc { [XPIAPHelper shareHelper].delegate = nil; } - (BOOL)isHiddenNavBar { return YES; } - (XPMineRechargePresenter *)createPresenter { return [[XPMineRechargePresenter alloc] init]; } - (void)viewDidLoad { [super viewDidLoad]; [self initHttpData]; [self initSubViews]; [self initSubViewConstraints]; } #pragma mark - Response - (void)protcoloButtonAction:(UIButton *)sender { XPWebViewController * webVC= [[XPWebViewController alloc] init]; webVC.url = URLWithType(kRechargePrivacyURL); [self.navigationController pushViewController:webVC animated:YES]; } #pragma mark - Private Method - (void)initSubViews { [self.view addSubview:self.tableView]; [self.view addSubview:self.navView]; [self.view addSubview:self.stackView]; [self.stackView addArrangedSubview:self.agreeLabel]; [self.stackView addArrangedSubview:self.protcoloButton]; self.tableView.tableHeaderView = self.headeView; [XPIAPHelper shareHelper].delegate = self; self.automaticallyAdjustsScrollViewInsets = NO; } - (void)initSubViewConstraints { [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view); make.bottom.mas_equalTo(self.view).offset(-20 - kSafeAreaBottomHeight); }]; [self.navView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(self.view); make.height.mas_equalTo(kNavigationHeight); }]; } - (void)initHttpData { [self getRechargeList]; [self getUserWalletBalanceInfo]; [self checkTranscationIds]; } - (void)getRechargeList { [self.presenter requestRechargeListWithChannel:@"8"]; } - (void)getUserWalletBalanceInfo { [self.presenter getUserWalletInfo]; } ///批量验证 - (void)checkTranscationIds { NSString * uid = [AccountInfoStorage instance].getUid; NSArray * array = [RechargeStorage getAllReciptsWithUid:uid]; if (array.count > 0) { [self.presenter checkTranscationIds:array]; } } ///充值成功之后保存订单到钥匙串 - (void)saveRechageReciptWithTransactionIdentifier:(NSString *)transactionIdentifier { NSData *receipt = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]; NSString *encodeStr = [receipt base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:transactionIdentifier forKey:@"transcationId"]; [dictionary setObject:encodeStr forKey:@"recipt"]; [dictionary setObject:self.orderId forKey:@"orderId"]; NSString *reciptJson = [dictionary toJSONString]; NSString * uid = [AccountInfoStorage instance].getUid; BOOL saveSuccess = [RechargeStorage saveTranscationId:transactionIdentifier recipt:reciptJson uid:uid]; if (!saveSuccess) { #warning to do 保存失败 需要埋点 } } ///删除本地保存的 - (void)deleteRechageReciptWithTransactionIdentifier:(NSString *)transactionIdentifier { NSString * uid = [AccountInfoStorage instance].getUid; BOOL deleteSuccess = [RechargeStorage delegateTranscationId:transactionIdentifier uid:uid]; if (deleteSuccess) { #warning to do 保存失败 需要埋点 } } #pragma mark - UITableViewDelegate And UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.datasource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { XPMineRechargeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMineRechargeTableViewCell class]) forIndexPath:indexPath]; cell.listModel = [self.datasource safeObjectAtIndex1:indexPath.row]; cell.delegate = self; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:true]; } #pragma mark - XPMineRechargeTableViewCellDelegate - (void)xPMineRechargeTableViewCell:(XPMineRechargeTableViewCell *)cell didSelectItem:(RechargeListModel *)listModel { if (listModel.chargeProdId) { [self showLoading]; [self.presenter requestIAPRechargeOrderWithChargeProdId:listModel.chargeProdId]; } } #pragma mark - XPMineRechargeNavViewDelegate - (void)xPMineRechargeNavView:(XPMineRechargeNavView *)view didClickBackButton:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - XPIAPHelperDelegate ///当前充值的状态 - (void)rechargeProcessStatus:(PaymentStatus)status { [self hideHUD]; if (status == PaymentStatus_Purchased) { [self showLoading]; } else if (status == PaymentStatus_Purchasing) { [self showLoading]; } else if (status == PaymentStatus_Failed) { [self showErrorToast:YMLocalizedString(@"XPMineRechargeViewController0")]; } else if (status == PaymentStatus_Deferred) { [self showErrorToast:YMLocalizedString(@"XPMineRechargeViewController1")]; } } ///充值成功回调id - (void)rechargeSuccess:(NSString *)transactionIdentifier { ///保存唯一凭证 [self saveRechageReciptWithTransactionIdentifier:transactionIdentifier]; NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]; NSString * receipt = [MF_Base64Codec base64StringFromData:receiptData]; ///二次验证 [self.presenter checkReceiptWithData:receipt orderId:self.orderId transcationId:transactionIdentifier]; } #pragma mark - XPMineRechargeProtocol - (void)requestRechargeListSucccess:(NSArray *)list { self.datasource = list; [self.tableView reloadData]; } - (void)requestIAPRechargeOrderSuccess:(NSString *)orderId chargeProdId:(NSString *)chargeProdId { if (orderId.length > 0) { self.orderId = orderId; [[XPIAPHelper shareHelper] buyAppProductWithAppProductId:chargeProdId]; } } - (void)requestIAPRechargeOrderFail { ///获取订单失败 } ///二次验证成功 - (void)checkReceiptSuccess:(NSString *)transcationId { [self deleteRechageReciptWithTransactionIdentifier:transcationId]; ///刷新一下用户的💎 [self getUserWalletBalanceInfo]; self.orderId = nil; } - (void)getUserWalletInfo:(WalletInfoModel *)balanceInfo { self.headeView.walletInfo = balanceInfo; } - (void)checkTranscationIdsSuccess { NSString * uid = [AccountInfoStorage instance].getUid; [RechargeStorage delegateAllTranscationIdsWithUid:uid]; } #pragma mark - Getters And Setters - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [UIView new]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor clearColor]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [_tableView registerClass:[XPMineRechargeTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMineRechargeTableViewCell class])]; } return _tableView; } - (XPMineRechageHeadView *)headeView { if (!_headeView) { _headeView = [[XPMineRechageHeadView alloc] init]; } return _headeView; } - (UIStackView *)stackView { if (!_stackView) { _stackView = [[UIStackView alloc] init]; _stackView.axis = UILayoutConstraintAxisHorizontal; _stackView.distribution = UIStackViewDistributionFill; _stackView.alignment = UIStackViewAlignmentFill; _stackView.spacing = 2; } return _stackView; } - (UIButton *)protcoloButton { if (!_protcoloButton) { _protcoloButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_protcoloButton setTitle:YMLocalizedString(@"XPIAPRechargeViewController3") forState:UIControlStateNormal]; [_protcoloButton setTitleColor:[DJDKMIMOMColor appMainColor] forState:UIControlStateNormal]; _protcoloButton.titleLabel.font = [UIFont systemFontOfSize:12]; [_protcoloButton addTarget:self action:@selector(protcoloButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _protcoloButton; } - (UILabel *)agreeLabel { if (!_agreeLabel) { _agreeLabel = [[UILabel alloc] init]; _agreeLabel.text = YMLocalizedString(@"XPMineRechargeViewController3"); _agreeLabel.font = [UIFont systemFontOfSize:12]; _agreeLabel.textColor = [DJDKMIMOMColor textThirdColor]; } return _agreeLabel; } - (XPMineRechargeNavView *)navView { if (!_navView) { _navView = [[XPMineRechargeNavView alloc] init]; _navView.delegate = self; } return _navView; } @end