BogoRechargeViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // BogoRechargeViewController.m
  3. // BuguLive
  4. //
  5. // Created by 宋晨光 on 2021/4/7.
  6. // Copyright © 2021 xfg. All rights reserved.
  7. //
  8. #import "BogoRechargeViewController.h"
  9. #import "BogoRechargeScrollView.h"
  10. #import <StoreKit/StoreKit.h>//内购
  11. #import "BogoRechargeRecordController.h"//收支记录
  12. @interface BogoRechargeViewController ()<BogoRechargeDelegate,SKProductsRequestDelegate,SKPaymentTransactionObserver>
  13. @property(nonatomic, strong) BogoRechargeScrollView *scrollView;
  14. @property (nonatomic, strong) SKProductsRequest * request;
  15. @end
  16. @implementation BogoRechargeViewController
  17. - (void)viewWillAppear:(BOOL)animated {
  18. [super viewWillAppear:animated];
  19. self.navigationController.navigationBar.hidden = YES;
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. [self setUpView];
  25. // self.title =ASLocalizedString( @"充值");
  26. // self.view.backgroundColor = kWhiteColor;
  27. self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
  28. UIImageView * navView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationHeight)];
  29. navView.image = [UIImage imageNamed:@"mine_navbg"];
  30. navView.userInteractionEnabled = YES;
  31. [self.view addSubview:navView];
  32. UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  33. [backBtn setImage:[UIImage imageNamed:@"com_arrow_vc_back"] forState:UIControlStateNormal];
  34. backBtn.frame = CGRectMake(10, StatusBarHeight, 44, 44);
  35. [backBtn addTarget:self action:@selector(backLastVC) forControlEvents:UIControlEventTouchUpInside];
  36. [navView addSubview:backBtn];
  37. UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
  38. titleLabel.centerX = SCREEN_WIDTH/2;
  39. titleLabel.centerY = backBtn.centerY;
  40. titleLabel.text = ASLocalizedString(@"充值");
  41. titleLabel.textColor = UIColor.blackColor;
  42. titleLabel.font = [UIFont boldSystemFontOfSize:18];
  43. titleLabel.textAlignment = NSTextAlignmentCenter;
  44. [navView addSubview:titleLabel];
  45. UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  46. // [backBtn setImage:[UIImage imageNamed:@"com_arrow_vc_back"] forState:UIControlStateNormal];
  47. rightBtn.frame = CGRectMake(SCREEN_WIDTH - 60 - 10, StatusBarHeight, 60, 44);
  48. [rightBtn addTarget:self action:@selector(clickRightItem) forControlEvents:UIControlEventTouchUpInside];
  49. [rightBtn setTitle:ASLocalizedString(@"收支记录") forState:UIControlStateNormal];
  50. rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  51. [rightBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
  52. [navView addSubview:rightBtn];
  53. // UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:ASLocalizedString(@"收支记录") style:UIBarButtonItemStylePlain target:self action:@selector(clickRightItem:)];
  54. // self.navigationItem.rightBarButtonItem = rightItem;
  55. // [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"],NSFontAttributeName: [UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
  56. // [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"],NSFontAttributeName: [UIFont systemFontOfSize:14]} forState:UIControlStateHighlighted];
  57. [self setupBackBtnWithBlock:nil];
  58. }
  59. - (void)backLastVC {
  60. [self.navigationController popViewControllerAnimated:YES];
  61. }
  62. -(void)clickRightItem {
  63. BogoRechargeRecordController *vc = [BogoRechargeRecordController new];
  64. [[AppDelegate sharedAppDelegate]pushViewController:vc animated:YES];
  65. }
  66. -(void)setUpView{
  67. // [self.scrollView addSubview:self.topView];
  68. self.scrollView.contentSize = CGSizeMake(0, kScreenH - SafeAreaBottomHeight - NavigationHeight);
  69. [self.view addSubview:self.scrollView];
  70. }
  71. #pragma marlk 刷新账户
  72. - (void)reloadAcount
  73. {
  74. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  75. // if (_is_vip)
  76. // {
  77. // [parmDict setObject:@"vip_pay" forKey:@"ctl"];
  78. // [parmDict setObject:@"purchase" forKey:@"act"];
  79. // }
  80. // else
  81. // {
  82. [parmDict setObject:@"pay" forKey:@"ctl"];
  83. [parmDict setObject:@"recharge" forKey:@"act"];
  84. // }
  85. WeakSelf
  86. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  87. {
  88. NSInteger diamonds = [[responseJson objectForKey:@"diamonds"] integerValue];
  89. [weakSelf.scrollView updateDiamonds:diamonds];
  90. } FailureBlock:^(NSError *error)
  91. {
  92. }];
  93. }
  94. #pragma marlk 支付宝支付
  95. -(void)alipay:(NSString *)payinfo{
  96. // [self alipay:payinfo block:nil];
  97. }
  98. //- (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
  99. //{
  100. // NSString *appScheme = AlipayScheme;
  101. //
  102. // [[AlipaySDK defaultService] payOrder:payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  103. //
  104. // SResBase* retobj = nil;
  105. //
  106. // if (resultDic)
  107. // {
  108. // if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
  109. // {
  110. // retobj = [[SResBase alloc]init];
  111. // retobj.msuccess = YES;
  112. // retobj.mmsg = ASLocalizedString(@"支付成功");
  113. // retobj.mcode = 1;
  114. // // block(retobj);
  115. // [FanweMessage alert:[NSString stringWithFormat:@"%@",retobj.mmsg]];
  116. //
  117. // [self reloadAcount];
  118. // }
  119. // else
  120. // {
  121. // retobj = [SResBase infoWithString: [resultDic objectForKey:@"memo" ]];
  122. // [FanweMessage alert:ASLocalizedString(@"支付失败")];
  123. // }
  124. // }
  125. // else
  126. // {
  127. // retobj = [SResBase infoWithString: ASLocalizedString(@"支付出现异常")];
  128. // [FanweMessage alert:ASLocalizedString(@"支付异常")];
  129. // }
  130. //
  131. // }];
  132. //}
  133. #pragma mark -- 苹果内购服务,下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
  134. -(void)checkProid:(NSString *)pro_id{
  135. // 监听购买结果
  136. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  137. //查询是否允许内付费
  138. if ([SKPaymentQueue canMakePayments])
  139. {
  140. // 执行下面提到的第5步:
  141. [self getProductInfowithprotectId:pro_id];
  142. }
  143. else
  144. {
  145. [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
  146. }
  147. }
  148. - (void)getProductInfowithprotectId:(NSString *)proId
  149. {
  150. //这里填你的产品id,根据创建的名字
  151. //ProductIdofvip
  152. //ProductId
  153. NSMutableArray *proArr = [NSMutableArray new];
  154. [proArr addObject:proId];
  155. NSSet * set = [NSSet setWithArray:proArr];
  156. self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  157. self.request.delegate = self;
  158. [self.request start];
  159. NSLog(@"%@",set);
  160. NSLog(ASLocalizedString(@"请求开始请等待..."));
  161. }
  162. #pragma mark - 以上查询的回调函数,以上查询的回调函数
  163. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  164. {
  165. NSArray *myProduct = response.products;
  166. if (myProduct.count == 0)
  167. {
  168. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"无法获取产品信息,购买失败。")];
  169. [SVProgressHUD dismiss];
  170. return;
  171. }
  172. NSLog(@"productID:%@", response.invalidProductIdentifiers);
  173. NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
  174. SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
  175. [[SKPaymentQueue defaultQueue] addPayment:payment];
  176. }
  177. #pragma mark - others SKPaymentTransactionObserver
  178. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
  179. {
  180. for (SKPaymentTransaction *transaction in transactions)
  181. {
  182. switch (transaction.transactionState)
  183. {
  184. case SKPaymentTransactionStatePurchased://交易完成
  185. NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
  186. [SVProgressHUD dismiss];
  187. [self completeTransaction:transaction];
  188. //[queue finishTransaction:transaction];
  189. break;
  190. case SKPaymentTransactionStateFailed://交易失败
  191. NSLog(ASLocalizedString(@"交易失败"));
  192. [self failedTransaction:transaction];
  193. //[queue finishTransaction:transaction];
  194. break;
  195. case SKPaymentTransactionStateRestored://恢复已购买商品
  196. NSLog(ASLocalizedString(@"恢复已购买商品"));
  197. [self restoreTransaction:transaction];
  198. [queue finishTransaction:transaction];
  199. break;
  200. case SKPaymentTransactionStatePurchasing://商品添加进列表
  201. NSLog(ASLocalizedString(@"商品添加进列表"));
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. }
  208. - (void)completeTransaction:(SKPaymentTransaction *)transaction
  209. {
  210. // Your application should implement these two methods.
  211. NSLog(ASLocalizedString(@"---------进入了这里"));
  212. // NSString * productIdentifier = transaction.payment.productIdentifier;
  213. NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
  214. NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
  215. NSString *base64String = [data base64EncodedStringWithOptions:0];
  216. if ([productIdentifier length] > 0) {
  217. // 向自己的服务器验证购买凭证
  218. [self shoppingValidation:base64String retryCount:3];
  219. }
  220. // Remove the transaction from the payment queue.
  221. [SVProgressHUD dismiss];
  222. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  223. }
  224. #pragma mark -- 向自己的服务器验证购买凭证
  225. - (void)shoppingValidation : (NSString *)base64Str retryCount:(NSInteger)retryCount
  226. {
  227. retryCount--;
  228. if (retryCount <= 0) {
  229. [FanweMessage alert:ASLocalizedString(@"支付失败")];
  230. return;
  231. }
  232. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
  233. // if (_is_vip)
  234. // {
  235. // [dict setObject:@"vip_pay" forKey:@"ctl"];
  236. // }
  237. // else
  238. // {
  239. [dict setObject:@"pay" forKey:@"ctl"];
  240. // }
  241. [dict setObject:@"iappay" forKey:@"act"];
  242. NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
  243. [dict setObject:userid forKey:@"user_id"];
  244. [dict setObject:base64Str forKey:@"receipt-data"];
  245. [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  246. NSInteger diamonds = [[responseJson objectForKey:@"total_diamonds"] integerValue];
  247. [self.scrollView updateDiamonds:diamonds];
  248. [self reloadAcount];
  249. [FanweMessage alert:ASLocalizedString(@"充值成功")];
  250. } FailureBlock:^(NSError *error) {
  251. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  252. [self shoppingValidation:base64Str retryCount:retryCount];
  253. });
  254. }];
  255. }
  256. - (void)failedTransaction:(SKPaymentTransaction *)transaction
  257. {
  258. [SVProgressHUD dismiss];
  259. if(transaction.error.code != SKErrorPaymentCancelled)
  260. {
  261. NSLog(ASLocalizedString(@"购买失败"));
  262. }
  263. else
  264. {
  265. NSLog(ASLocalizedString(@"用户取消交易"));
  266. //[FanweMessage alert:ASLocalizedString(@"您已经取消交易")];
  267. }
  268. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  269. }
  270. - (void)restoreTransaction:(SKPaymentTransaction *)transaction
  271. {
  272. // 对于已购商品,处理恢复购买的逻辑
  273. //[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  274. [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  275. }
  276. // 支付结果的通知:
  277. - (void)receiveResult:(NSString*)payid result:(BOOL)success message:(NSString*)message
  278. {
  279. if ( success == YES )
  280. {
  281. [self reloadAcount];
  282. [FanweMessage alert:ASLocalizedString(@"支付成功")];
  283. }
  284. else
  285. {
  286. [FanweMessage alert:ASLocalizedString(@"支付失败")];
  287. }
  288. }
  289. - (void)receiveChannelTypes:(NSArray<NSNumber *>*)types
  290. {
  291. // [BGInterface selectChannel:_juBaoModel.withType];
  292. }
  293. - (BOOL)shouldAutorotate
  294. {
  295. return YES;
  296. }
  297. - (void)didReceiveMemoryWarning
  298. {
  299. [super didReceiveMemoryWarning];
  300. }
  301. - (void)dealloc
  302. {
  303. NSLog(ASLocalizedString(@"释放充值"));
  304. if (self.request)
  305. {
  306. [self.request cancel];
  307. }
  308. [[NSNotificationCenter defaultCenter] removeObserver:self];
  309. [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
  310. }
  311. -(BogoRechargeScrollView *)scrollView{
  312. if (!_scrollView) {
  313. _scrollView = [[BogoRechargeScrollView alloc]initWithFrame:CGRectMake(0, NavigationHeight, kScreenW , kScreenH - kTopHeight - MG_BOTTOM_MARGIN)];
  314. _scrollView.reDelegate = self;
  315. _scrollView.isRecharge = YES;
  316. }
  317. return _scrollView;
  318. }
  319. /*
  320. #pragma mark - Navigation
  321. // In a storyboard-based application, you will often want to do a little preparation before navigation
  322. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  323. // Get the new view controller using [segue destinationViewController].
  324. // Pass the selected object to the new view controller.
  325. }
  326. */
  327. @end