Files
peko-ios/YuMi/Modules/YMMine/View/Recharge/XPIAPRechargeViewController.m
2023-09-19 16:00:06 +08:00

524 lines
18 KiB
Objective-C

//
// YMIAPRechargeViewController.m
// YUMI
//
// Created by XY on 2023/2/21.
//
#import "XPIAPRechargeViewController.h"
///Third
#import <Masonry/Masonry.h>
#import <Base64/MF_Base64Additions.h>
///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"
#import "UIImage+Utils.h"
///Model
#import "RechargeListModel.h"
///View
#import "XPIAPRechargeHeaderView.h"
#import "XPIAPRechargeCollectionViewCell.h"
#import "XPMineRechargeNavView.h"
///P
#import "XPMineRechargePresenter.h"
#import "XPMineRechargeProtocol.h"
///VC
#import "XPWebViewController.h"
#import "YuMi-swift.h"
#define kHeaderViewHeight 220.0/375.0*KScreenWidth
@interface XPIAPRechargeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XPMineRechargeProtocol, XPMineRechargeNavViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
/// 头部
@property (nonatomic, strong) XPIAPRechargeHeaderView *headerView;
/// 充值按钮
@property (nonatomic, strong) UIButton *rechargeBtn;
///
@property (nonatomic,strong) UIStackView *stackView;
///同意
@property (nonatomic,strong) UILabel *agreeLabel;
///充值协议
@property (nonatomic,strong) UIButton *protcoloButton;
/// 联系
@property (nonatomic,strong) UILabel *contactLabel;
/// 返回
@property (nonatomic,strong) UIButton *backBtn;
/// 标题
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *tipslainiLable;
/// 数据源
@property (nonatomic, strong) NSArray<RechargeListModel *> *dataSource;
/// 选中下标
@property (nonatomic, assign) NSInteger selectedIndex;
/// 订单编号
@property (nonatomic,copy) NSString *orderId;
@end
@implementation XPIAPRechargeViewController
- (void)dealloc {
[XPIAPHelper shareHelper].delegate = nil;
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (XPMineRechargePresenter *)createPresenter {
return [[XPMineRechargePresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
[self initHttpData];
}
- (void)createUI {
[self.view addSubview:self.headerView];
[self.view addSubview:self.collectionView];
[self.view addSubview:self.tipslainiLable];
[self.view addSubview:self.rechargeBtn];
[self.view addSubview:self.stackView];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.backBtn];
[self.stackView addArrangedSubview:self.agreeLabel];
[self.stackView addArrangedSubview:self.protcoloButton];
[self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(0);
make.height.mas_equalTo(kHeaderViewHeight);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.headerView.mas_bottom);
make.left.right.mas_equalTo(0);
make.height.mas_equalTo(160);
}];
[self.tipslainiLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.collectionView.mas_bottom).offset(20);
make.left.mas_equalTo(40);
make.right.mas_equalTo(-40);
}];
[self.rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(16);
make.right.mas_equalTo(-16);
make.height.mas_equalTo(48);
make.bottom.mas_equalTo(-140);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view);
make.top.mas_equalTo(self.rechargeBtn.mas_bottom).offset(28);
}];
[self.tipslainiLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(40);
make.top.mas_equalTo(self.collectionView.mas_bottom).offset(20);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kStatusBarHeight);
make.height.mas_equalTo(44);
make.centerX.mas_equalTo(self.view);
}];
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0);
make.top.mas_equalTo(kStatusBarHeight);
make.width.height.mas_equalTo(44);
}];
}
- (void)initHttpData {
[self getRechargeList];
[self getUserWalletBalanceInfo];
[self checkTranscationIds];
}
/// 返回
- (void)backBtnAction {
if(self.isFairyPay){
[UIView animateWithDuration:0.5 animations:^{
self.view.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
}completion:^(BOOL finished) {
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
return;
}
[self.navigationController popViewControllerAnimated:YES];
}
/// 点击充值按钮
- (void)rechargeBtnAction {
RechargeListModel *model = [self.dataSource safeObjectAtIndex1:self.selectedIndex];
if (model.chargeProdId) {
[self showLoading];
[self.presenter requestIAPRechargeOrderWithChargeProdId:model.chargeProdId];
}
}
- (void)protcoloButtonAction:(UIButton *)sender {
if(self.isFairyPay){
XPWebViewController * webVC= [[XPWebViewController alloc] initWithCustomizeNav:YES];
webVC.url = URLWithType(kRechargePrivacyURL);
webVC.is_Pi_FairyPay = YES;
webVC.view.backgroundColor = [UIColor whiteColor];
[self addChildViewController:webVC];
[self.view addSubview:webVC.view];
webVC.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
return;
}
XPWebViewController * webVC= [[XPWebViewController alloc] init];
webVC.url = URLWithType(kRechargePrivacyURL);
[self.navigationController pushViewController:webVC animated:YES];
}
- (void)getRechargeList {
[self.presenter requestRechargeListWithChannel:@"8"];
}
- (void)getUserWalletBalanceInfo {
[self.presenter getUserWalletInfo];
}
///批量验证
- (void)checkTranscationIds {
NSString * uid = [AccountInfoStorage instance].getUid;
NSArray * array = [RechargeStorage getAllReciptsWithUid:uid];
// [self.presenter checkReceiptWithOrderId:self.orderId transcationId:transactionIdentifier];
// NSMutableArray *list = [NSMutableArray array];
// for (NSDictionary *transcationDic in array) {
// NSString *transcationId = transcationDic[@"transcationId"];
// if(transcationId){
// [list addObject:transcationId];
// }
// }
// if(list.count > 0){
// if (@available(iOS 15.0, *)) {
// PIIAPPayment *iap = [PIIAPPayment shared];
// [iap allTransactionWithList:list completionHandler:^{
//
// }];
//
// }
// }
for (NSDictionary *transcation in array) {
NSString *orderId = transcation[@"orderId"];
NSString *transcationId = transcation[@"transcationId"];
///二次验证
[self.presenter checkReceiptWithOrderId:orderId transcationId:transcationId errorToast:NO];
}
}
///充值成功之后保存订单到钥匙串
- (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 - XPMineRechargeNavViewDelegate
- (void)xPMineRechargeNavView:(XPMineRechargeNavView *)view didClickBackButton:(UIButton *)sender {
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - XPIAPHelperDelegate
///当前充值的状态
- (void)rechargeNewProcessStatus:(StoreState)status {
[self hideHUD];
if (status == StoreStatePay || status == StoreStateStart || status == StoreStateVerifiedServer) {
[self showLoading];
}else if (status == StoreStateUnowned) {
[self showErrorToast:YMLocalizedString(@"XPIAPRechargeViewController1")];
}else{
[self showErrorToast:YMLocalizedString(@"XPIAPRechargeViewController0")];
}
}
///充值成功回调id
- (void)rechargeSuccess:(NSString *)transactionIdentifier {
///保存唯一凭证
[self saveRechageReciptWithTransactionIdentifier:transactionIdentifier];
[self showLoading];
///二次验证
[self.presenter checkReceiptWithOrderId:self.orderId transcationId:transactionIdentifier errorToast:YES];
}
#pragma mark - XPMineRechargeProtocol
- (void)requestRechargeListSucccess:(NSArray *)list {
self.dataSource = list;
[self.collectionView reloadData];
}
- (void)requestIAPRechargeOrderSuccess:(NSString *)orderId chargeProdId:(NSString *)chargeProdId uuid:(nonnull NSString *)uuid{
if (orderId.length > 0) {
self.orderId = orderId;
if (@available(iOS 15.0, *)) {
PIIAPPayment *iap = [PIIAPPayment shared];
[iap requestBuyProductWithProductId:chargeProdId uuid:uuid completionHandler:^(NSError * _Nullable error) {
}];
@kWeakify(self);
iap.stateBlock = ^(enum StoreState state, NSDictionary<NSString *,id> * _Nullable result) {
@kStrongify(self);
[self rechargeNewProcessStatus:state];
switch (state) {
case StoreStateVerifiedServer:
{
NSString *transactionId = result[@"transactionId"];
[self rechargeSuccess:transactionId];
}
break;
default:
{
}
break;
}
};
} else {
[self hideHUD];
[self showErrorToast:YMLocalizedString(@"XPIAPRechargeViewController8")];
}
}else{
[self hideHUD];
}
}
- (void)requestIAPRechargeOrderFail {
///获取订单失败
[self hideHUD];
}
///二次验证成功
- (void)checkReceiptSuccess:(NSString *)transcationId {
[self hideHUD];
if (@available(iOS 15.0, *)) {
PIIAPPayment *iap = [PIIAPPayment shared];
[iap transactionFinishWithTransaction:[NSString stringWithFormat:@"%@",transcationId] completionHandler:^{
}];
}
[self deleteRechageReciptWithTransactionIdentifier:transcationId];
///刷新一下用户的💎
[self getUserWalletBalanceInfo];
self.orderId = nil;
[self hideHUD];
if(self.delegate && [self.delegate respondsToSelector:@selector(paySuccess)]){
[self.delegate paySuccess];
}
}
-(void)checkReceiptFailWithCode:(NSInteger)code transcationId:(NSString *)transcationId{
[self hideHUD];
if(code == 1444){
if (@available(iOS 15.0, *)) {
PIIAPPayment *iap = [PIIAPPayment shared];
[iap transactionFinishWithTransaction:[NSString stringWithFormat:@"%@",transcationId] completionHandler:^{
}];
}
[self deleteRechageReciptWithTransactionIdentifier:transcationId];
}
}
- (void)getUserWalletInfo:(WalletInfoModel *)balanceInfo {
self.headerView.walletInfo = balanceInfo;
}
- (void)checkTranscationIdsSuccess {
NSString * uid = [AccountInfoStorage instance].getUid;
[RechargeStorage delegateAllTranscationIdsWithUid:uid];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPIAPRechargeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(XPIAPRechargeCollectionViewCell.self) forIndexPath:indexPath];
cell.rechargeModel = [self.dataSource safeObjectAtIndex1:indexPath.item];
cell.selectedStyle = self.selectedIndex == indexPath.item;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndex = indexPath.item;
[collectionView reloadData];
}
#pragma mark - 懒加载
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
CGFloat width = 108;
CGFloat height = 65;
layout.itemSize = CGSizeMake(width, height);
layout.minimumLineSpacing = 10.0;
layout.minimumInteritemSpacing = 8.0;
layout.sectionInset = UIEdgeInsetsMake(20, 16, 16, 16);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = UIColor.clearColor;
_collectionView.alwaysBounceVertical = YES;
[_collectionView registerClass:[XPIAPRechargeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass(XPIAPRechargeCollectionViewCell.self)];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
// _collectionView.contentInset = UIEdgeInsetsMake(kHeaderViewHeight, 0, 0, 0);
}
return _collectionView;
}
- (XPIAPRechargeHeaderView *)headerView {
if (!_headerView) {
_headerView = [[XPIAPRechargeHeaderView alloc] init];
}
return _headerView;
}
- (UIButton *)rechargeBtn {
if (!_rechargeBtn) {
_rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_rechargeBtn setTitle:YMLocalizedString(@"XPIAPRechargeViewController2") forState:UIControlStateNormal];
[_rechargeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
_rechargeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
[_rechargeBtn setBackgroundImage:[UIImage gradientColorImageFromColors:@[[DJDKMIMOMColor confirmButtonGradientStartColor], [DJDKMIMOMColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
_rechargeBtn.layer.cornerRadius = 48/2;
_rechargeBtn.clipsToBounds = YES;
[_rechargeBtn addTarget:self action:@selector(rechargeBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _rechargeBtn;
}
- (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(@"XPIAPRechargeViewController4");
_agreeLabel.font = [UIFont systemFontOfSize:12];
_agreeLabel.textColor = [DJDKMIMOMColor textThirdColor];
}
return _agreeLabel;
}
- (UILabel *)contactLabel {
if (!_contactLabel) {
_contactLabel = [[UILabel alloc] init];
_contactLabel.text = [NSString stringWithFormat:@"%@: 88001",YMLocalizedString(@"XPIAPRechargeViewController5")];
_contactLabel.font = [UIFont systemFontOfSize:12];
_contactLabel.textColor = [DJDKMIMOMColor appMainColor];
}
return _contactLabel;
}
- (UIButton *)backBtn {
if (!_backBtn) {
_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_backBtn setImage:[UIImage imageNamed:@"room_info_back"] forState:UIControlStateNormal];
[_backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _backBtn;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = YMLocalizedString(@"XPIAPRechargeViewController6");
_titleLabel.textColor = UIColor.whiteColor;
_titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
}
return _titleLabel;
}
- (UILabel *)tipslainiLable {
if (!_tipslainiLable) {
_tipslainiLable = [[UILabel alloc] init];
_tipslainiLable.font = [UIFont systemFontOfSize:14];
_tipslainiLable.textColor = [DJDKMIMOMColor colorWithHexString:@"#878B9C"];
_tipslainiLable.text = YMLocalizedString(@"XPIAPRechargeViewController7");
_tipslainiLable.numberOfLines = 2;
_tipslainiLable.textAlignment = NSTextAlignmentCenter;
}
return _tipslainiLable;
}
@end