BogoRechargeViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. if ((NSNull *)responseJson != [NSNull null])
  89. {
  90. weakSelf.scrollView.diamondStr = [NSString stringWithFormat:@"%@",[responseJson objectForKey:@"diamonds"]];
  91. }
  92. } FailureBlock:^(NSError *error)
  93. {
  94. }];
  95. }
  96. #pragma marlk 支付宝支付
  97. -(void)alipay:(NSString *)payinfo{
  98. // [self alipay:payinfo block:nil];
  99. }
  100. //- (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
  101. //{
  102. // NSString *appScheme = AlipayScheme;
  103. //
  104. // [[AlipaySDK defaultService] payOrder:payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  105. //
  106. // SResBase* retobj = nil;
  107. //
  108. // if (resultDic)
  109. // {
  110. // if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
  111. // {
  112. // retobj = [[SResBase alloc]init];
  113. // retobj.msuccess = YES;
  114. // retobj.mmsg = ASLocalizedString(@"支付成功");
  115. // retobj.mcode = 1;
  116. // // block(retobj);
  117. // [FanweMessage alert:[NSString stringWithFormat:@"%@",retobj.mmsg]];
  118. //
  119. // [self reloadAcount];
  120. // }
  121. // else
  122. // {
  123. // retobj = [SResBase infoWithString: [resultDic objectForKey:@"memo" ]];
  124. // [FanweMessage alert:ASLocalizedString(@"支付失败")];
  125. // }
  126. // }
  127. // else
  128. // {
  129. // retobj = [SResBase infoWithString: ASLocalizedString(@"支付出现异常")];
  130. // [FanweMessage alert:ASLocalizedString(@"支付异常")];
  131. // }
  132. //
  133. // }];
  134. //}
  135. #pragma mark -- 苹果内购服务,下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
  136. -(void)checkProid:(NSString *)pro_id{
  137. // 监听购买结果
  138. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  139. //查询是否允许内付费
  140. if ([SKPaymentQueue canMakePayments])
  141. {
  142. // 执行下面提到的第5步:
  143. [self getProductInfowithprotectId:pro_id];
  144. }
  145. else
  146. {
  147. [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
  148. }
  149. }
  150. - (void)getProductInfowithprotectId:(NSString *)proId
  151. {
  152. //这里填你的产品id,根据创建的名字
  153. //ProductIdofvip
  154. //ProductId
  155. NSMutableArray *proArr = [NSMutableArray new];
  156. [proArr addObject:proId];
  157. NSSet * set = [NSSet setWithArray:proArr];
  158. self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  159. self.request.delegate = self;
  160. [self.request start];
  161. NSLog(@"%@",set);
  162. NSLog(ASLocalizedString(@"请求开始请等待..."));
  163. }
  164. #pragma mark - 以上查询的回调函数,以上查询的回调函数
  165. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  166. {
  167. NSArray *myProduct = response.products;
  168. if (myProduct.count == 0)
  169. {
  170. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"无法获取产品信息,购买失败。")];
  171. [SVProgressHUD dismiss];
  172. return;
  173. }
  174. NSLog(@"productID:%@", response.invalidProductIdentifiers);
  175. NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
  176. SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
  177. [[SKPaymentQueue defaultQueue] addPayment:payment];
  178. }
  179. #pragma mark - others SKPaymentTransactionObserver
  180. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
  181. {
  182. for (SKPaymentTransaction *transaction in transactions)
  183. {
  184. switch (transaction.transactionState)
  185. {
  186. case SKPaymentTransactionStatePurchased://交易完成
  187. NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
  188. [SVProgressHUD dismiss];
  189. [self completeTransaction:transaction];
  190. //[queue finishTransaction:transaction];
  191. break;
  192. case SKPaymentTransactionStateFailed://交易失败
  193. NSLog(ASLocalizedString(@"交易失败"));
  194. [self failedTransaction:transaction];
  195. //[queue finishTransaction:transaction];
  196. break;
  197. case SKPaymentTransactionStateRestored://恢复已购买商品
  198. NSLog(ASLocalizedString(@"恢复已购买商品"));
  199. [self restoreTransaction:transaction];
  200. [queue finishTransaction:transaction];
  201. break;
  202. case SKPaymentTransactionStatePurchasing://商品添加进列表
  203. NSLog(ASLocalizedString(@"商品添加进列表"));
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. }
  210. - (void)completeTransaction:(SKPaymentTransaction *)transaction
  211. {
  212. // Your application should implement these two methods.
  213. NSLog(ASLocalizedString(@"---------进入了这里"));
  214. // NSString * productIdentifier = transaction.payment.productIdentifier;
  215. NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
  216. NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
  217. NSString *base64String = [data base64EncodedStringWithOptions:0];
  218. if ([productIdentifier length] > 0) {
  219. // 向自己的服务器验证购买凭证
  220. [self shoppingValidation:base64String];
  221. }
  222. // Remove the transaction from the payment queue.
  223. [SVProgressHUD dismiss];
  224. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  225. }
  226. #pragma mark -- 向自己的服务器验证购买凭证
  227. - (void)shoppingValidation : (NSString *)base64Str
  228. {
  229. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
  230. // if (_is_vip)
  231. // {
  232. // [dict setObject:@"vip_pay" forKey:@"ctl"];
  233. // }
  234. // else
  235. // {
  236. [dict setObject:@"pay" forKey:@"ctl"];
  237. // }
  238. [dict setObject:@"iappay" forKey:@"act"];
  239. NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
  240. [dict setObject:userid forKey:@"user_id"];
  241. [dict setObject:base64Str forKey:@"receipt-data"];
  242. [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  243. [self reloadAcount];
  244. // [FanweMessage alert:ASLocalizedString(@"充值成功")];
  245. } FailureBlock:^(NSError *error) {
  246. }];
  247. }
  248. - (void)failedTransaction:(SKPaymentTransaction *)transaction
  249. {
  250. [SVProgressHUD dismiss];
  251. if(transaction.error.code != SKErrorPaymentCancelled)
  252. {
  253. NSLog(ASLocalizedString(@"购买失败"));
  254. }
  255. else
  256. {
  257. NSLog(ASLocalizedString(@"用户取消交易"));
  258. //[FanweMessage alert:ASLocalizedString(@"您已经取消交易")];
  259. }
  260. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  261. }
  262. - (void)restoreTransaction:(SKPaymentTransaction *)transaction
  263. {
  264. // 对于已购商品,处理恢复购买的逻辑
  265. //[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  266. [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  267. }
  268. // 支付结果的通知:
  269. - (void)receiveResult:(NSString*)payid result:(BOOL)success message:(NSString*)message
  270. {
  271. if ( success == YES )
  272. {
  273. [self reloadAcount];
  274. [FanweMessage alert:ASLocalizedString(@"支付成功")];
  275. }
  276. else
  277. {
  278. [FanweMessage alert:ASLocalizedString(@"支付失败")];
  279. }
  280. }
  281. - (void)receiveChannelTypes:(NSArray<NSNumber *>*)types
  282. {
  283. // [BGInterface selectChannel:_juBaoModel.withType];
  284. }
  285. - (BOOL)shouldAutorotate
  286. {
  287. return YES;
  288. }
  289. - (void)didReceiveMemoryWarning
  290. {
  291. [super didReceiveMemoryWarning];
  292. }
  293. - (void)dealloc
  294. {
  295. NSLog(ASLocalizedString(@"释放充值"));
  296. if (self.request)
  297. {
  298. [self.request cancel];
  299. }
  300. [[NSNotificationCenter defaultCenter] removeObserver:self];
  301. [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
  302. }
  303. -(BogoRechargeScrollView *)scrollView{
  304. if (!_scrollView) {
  305. _scrollView = [[BogoRechargeScrollView alloc]initWithFrame:CGRectMake(0, NavigationHeight, kScreenW , kScreenH - kTopHeight - MG_BOTTOM_MARGIN)];
  306. _scrollView.reDelegate = self;
  307. _scrollView.isRecharge = YES;
  308. }
  309. return _scrollView;
  310. }
  311. /*
  312. #pragma mark - Navigation
  313. // In a storyboard-based application, you will often want to do a little preparation before navigation
  314. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  315. // Get the new view controller using [segue destinationViewController].
  316. // Pass the selected object to the new view controller.
  317. }
  318. */
  319. @end