| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- //
- // BogoRechargeViewController.m
- // BuguLive
- //
- // Created by 宋晨光 on 2021/4/7.
- // Copyright © 2021 xfg. All rights reserved.
- //
- #import "BogoRechargeViewController.h"
- #import "BogoRechargeScrollView.h"
- #import <StoreKit/StoreKit.h>//内购
- #import "BogoRechargeRecordController.h"//收支记录
- @interface BogoRechargeViewController ()<BogoRechargeDelegate,SKProductsRequestDelegate,SKPaymentTransactionObserver>
- @property(nonatomic, strong) BogoRechargeScrollView *scrollView;
- @property (nonatomic, strong) SKProductsRequest * request;
- @end
- @implementation BogoRechargeViewController
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- self.navigationController.navigationBar.hidden = YES;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self setUpView];
-
- // self.title =ASLocalizedString( @"充值");
- // self.view.backgroundColor = kWhiteColor;
-
- self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
-
- UIImageView * navView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationHeight)];
- navView.image = [UIImage imageNamed:@"mine_navbg"];
- navView.userInteractionEnabled = YES;
- [self.view addSubview:navView];
-
- UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [backBtn setImage:[UIImage imageNamed:@"com_arrow_vc_back"] forState:UIControlStateNormal];
- backBtn.frame = CGRectMake(10, StatusBarHeight, 44, 44);
- [backBtn addTarget:self action:@selector(backLastVC) forControlEvents:UIControlEventTouchUpInside];
- [navView addSubview:backBtn];
-
- UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
- titleLabel.centerX = SCREEN_WIDTH/2;
- titleLabel.centerY = backBtn.centerY;
- titleLabel.text = ASLocalizedString(@"充值");
- titleLabel.textColor = UIColor.blackColor;
- titleLabel.font = [UIFont boldSystemFontOfSize:18];
- titleLabel.textAlignment = NSTextAlignmentCenter;
- [navView addSubview:titleLabel];
-
- UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- // [backBtn setImage:[UIImage imageNamed:@"com_arrow_vc_back"] forState:UIControlStateNormal];
- rightBtn.frame = CGRectMake(SCREEN_WIDTH - 60 - 10, StatusBarHeight, 60, 44);
- [rightBtn addTarget:self action:@selector(clickRightItem) forControlEvents:UIControlEventTouchUpInside];
- [rightBtn setTitle:ASLocalizedString(@"收支记录") forState:UIControlStateNormal];
- rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
- [rightBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
- [navView addSubview:rightBtn];
-
- // UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:ASLocalizedString(@"收支记录") style:UIBarButtonItemStylePlain target:self action:@selector(clickRightItem:)];
- // self.navigationItem.rightBarButtonItem = rightItem;
- // [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"],NSFontAttributeName: [UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
- // [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"],NSFontAttributeName: [UIFont systemFontOfSize:14]} forState:UIControlStateHighlighted];
-
- [self setupBackBtnWithBlock:nil];
-
-
- }
- - (void)backLastVC {
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(void)clickRightItem {
- BogoRechargeRecordController *vc = [BogoRechargeRecordController new];
- [[AppDelegate sharedAppDelegate]pushViewController:vc animated:YES];
- }
- -(void)setUpView{
-
- // [self.scrollView addSubview:self.topView];
- self.scrollView.contentSize = CGSizeMake(0, kScreenH - SafeAreaBottomHeight - NavigationHeight);
- [self.view addSubview:self.scrollView];
- }
- #pragma marlk 刷新账户
- - (void)reloadAcount
- {
- NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
- // if (_is_vip)
- // {
- // [parmDict setObject:@"vip_pay" forKey:@"ctl"];
- // [parmDict setObject:@"purchase" forKey:@"act"];
- // }
- // else
- // {
- [parmDict setObject:@"pay" forKey:@"ctl"];
- [parmDict setObject:@"recharge" forKey:@"act"];
- // }
-
- WeakSelf
- [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
- {
- NSInteger diamonds = [[responseJson objectForKey:@"diamonds"] integerValue];
- [weakSelf.scrollView updateDiamonds:diamonds];
- } FailureBlock:^(NSError *error)
- {
-
- }];
- }
- #pragma marlk 支付宝支付
- -(void)alipay:(NSString *)payinfo{
-
- // [self alipay:payinfo block:nil];
- }
- //- (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
- //{
- // NSString *appScheme = AlipayScheme;
- //
- // [[AlipaySDK defaultService] payOrder:payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
- //
- // SResBase* retobj = nil;
- //
- // if (resultDic)
- // {
- // if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
- // {
- // retobj = [[SResBase alloc]init];
- // retobj.msuccess = YES;
- // retobj.mmsg = ASLocalizedString(@"支付成功");
- // retobj.mcode = 1;
- // // block(retobj);
- // [FanweMessage alert:[NSString stringWithFormat:@"%@",retobj.mmsg]];
- //
- // [self reloadAcount];
- // }
- // else
- // {
- // retobj = [SResBase infoWithString: [resultDic objectForKey:@"memo" ]];
- // [FanweMessage alert:ASLocalizedString(@"支付失败")];
- // }
- // }
- // else
- // {
- // retobj = [SResBase infoWithString: ASLocalizedString(@"支付出现异常")];
- // [FanweMessage alert:ASLocalizedString(@"支付异常")];
- // }
- //
- // }];
- //}
- #pragma mark -- 苹果内购服务,下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
- -(void)checkProid:(NSString *)pro_id{
- // 监听购买结果
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
-
- //查询是否允许内付费
- if ([SKPaymentQueue canMakePayments])
- {
- // 执行下面提到的第5步:
- [self getProductInfowithprotectId:pro_id];
- }
- else
- {
- [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
- }
- }
- - (void)getProductInfowithprotectId:(NSString *)proId
- {
- //这里填你的产品id,根据创建的名字
- //ProductIdofvip
- //ProductId
- NSMutableArray *proArr = [NSMutableArray new];
- [proArr addObject:proId];
- NSSet * set = [NSSet setWithArray:proArr];
-
- self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
- self.request.delegate = self;
- [self.request start];
-
- NSLog(@"%@",set);
- NSLog(ASLocalizedString(@"请求开始请等待..."));
- }
- #pragma mark - 以上查询的回调函数,以上查询的回调函数
- - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
- {
- NSArray *myProduct = response.products;
- if (myProduct.count == 0)
- {
- [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"无法获取产品信息,购买失败。")];
- [SVProgressHUD dismiss];
- return;
- }
- NSLog(@"productID:%@", response.invalidProductIdentifiers);
- NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
- SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
- [[SKPaymentQueue defaultQueue] addPayment:payment];
- }
- #pragma mark - others SKPaymentTransactionObserver
- - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
- {
- for (SKPaymentTransaction *transaction in transactions)
- {
- switch (transaction.transactionState)
- {
- case SKPaymentTransactionStatePurchased://交易完成
- NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
- [SVProgressHUD dismiss];
- [self completeTransaction:transaction];
- //[queue finishTransaction:transaction];
- break;
- case SKPaymentTransactionStateFailed://交易失败
- NSLog(ASLocalizedString(@"交易失败"));
- [self failedTransaction:transaction];
- //[queue finishTransaction:transaction];
- break;
- case SKPaymentTransactionStateRestored://恢复已购买商品
- NSLog(ASLocalizedString(@"恢复已购买商品"));
- [self restoreTransaction:transaction];
- [queue finishTransaction:transaction];
- break;
- case SKPaymentTransactionStatePurchasing://商品添加进列表
- NSLog(ASLocalizedString(@"商品添加进列表"));
- break;
- default:
- break;
- }
- }
- }
- - (void)completeTransaction:(SKPaymentTransaction *)transaction
- {
- // Your application should implement these two methods.
- NSLog(ASLocalizedString(@"---------进入了这里"));
- // NSString * productIdentifier = transaction.payment.productIdentifier;
- NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
- NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
- NSString *base64String = [data base64EncodedStringWithOptions:0];
- if ([productIdentifier length] > 0) {
- // 向自己的服务器验证购买凭证
- [self shoppingValidation:base64String retryCount:3];
- }
- // Remove the transaction from the payment queue.
- [SVProgressHUD dismiss];
- [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
- }
- #pragma mark -- 向自己的服务器验证购买凭证
- - (void)shoppingValidation : (NSString *)base64Str retryCount:(NSInteger)retryCount
- {
- retryCount--;
- if (retryCount <= 0) {
- [FanweMessage alert:ASLocalizedString(@"支付失败")];
- return;
- }
- NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
- // if (_is_vip)
- // {
- // [dict setObject:@"vip_pay" forKey:@"ctl"];
- // }
- // else
- // {
- [dict setObject:@"pay" forKey:@"ctl"];
- // }
- [dict setObject:@"iappay" forKey:@"act"];
- NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
- [dict setObject:userid forKey:@"user_id"];
- [dict setObject:base64Str forKey:@"receipt-data"];
- [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
- NSInteger diamonds = [[responseJson objectForKey:@"total_diamonds"] integerValue];
- [self.scrollView updateDiamonds:diamonds];
- [self reloadAcount];
- [FanweMessage alert:ASLocalizedString(@"充值成功")];
-
- } FailureBlock:^(NSError *error) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self shoppingValidation:base64Str retryCount:retryCount];
- });
- }];
- }
- - (void)failedTransaction:(SKPaymentTransaction *)transaction
- {
- [SVProgressHUD dismiss];
- if(transaction.error.code != SKErrorPaymentCancelled)
- {
- NSLog(ASLocalizedString(@"购买失败"));
- }
- else
- {
- NSLog(ASLocalizedString(@"用户取消交易"));
- //[FanweMessage alert:ASLocalizedString(@"您已经取消交易")];
- }
- [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
- }
- - (void)restoreTransaction:(SKPaymentTransaction *)transaction
- {
- // 对于已购商品,处理恢复购买的逻辑
- //[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
- [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
- }
- // 支付结果的通知:
- - (void)receiveResult:(NSString*)payid result:(BOOL)success message:(NSString*)message
- {
- if ( success == YES )
- {
- [self reloadAcount];
- [FanweMessage alert:ASLocalizedString(@"支付成功")];
- }
- else
- {
- [FanweMessage alert:ASLocalizedString(@"支付失败")];
- }
- }
- - (void)receiveChannelTypes:(NSArray<NSNumber *>*)types
- {
- // [BGInterface selectChannel:_juBaoModel.withType];
- }
- - (BOOL)shouldAutorotate
- {
- return YES;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- }
- - (void)dealloc
- {
- NSLog(ASLocalizedString(@"释放充值"));
- if (self.request)
- {
- [self.request cancel];
- }
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
- }
- -(BogoRechargeScrollView *)scrollView{
- if (!_scrollView) {
- _scrollView = [[BogoRechargeScrollView alloc]initWithFrame:CGRectMake(0, NavigationHeight, kScreenW , kScreenH - kTopHeight - MG_BOTTOM_MARGIN)];
- _scrollView.reDelegate = self;
- _scrollView.isRecharge = YES;
- }
- return _scrollView;
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|