MGLiveRechargeView.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. //
  2. // MGLiveRechargeView.m
  3. // BuguLive
  4. //
  5. // Created by 宋晨光 on 2020/8/5.
  6. // Copyright © 2020 xfg. All rights reserved.
  7. //
  8. #import "MGLiveRechargeView.h"
  9. #import "MGLiveRechargeCell.h"
  10. @implementation MGLiveRechargeView
  11. - (instancetype)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. self.backgroundColor = kClearColor;
  16. self.nowMainColor = [UIColor colorWithRed:166/255 green:96/255 blue:255/255 alpha:1];
  17. self.isSelectAgree = NO;
  18. self.money = @"0";
  19. self.listArr = [NSMutableArray array];
  20. [self setUpView];
  21. [self requestModel];
  22. }
  23. return self;
  24. }
  25. -(void)setUpView{
  26. UIView *bgTopView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kRealValue(50))];
  27. bgTopView.backgroundColor = kWhiteColor;
  28. UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:bgTopView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(6,6)];//圆角大小
  29. CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
  30. maskLayer1.frame = bgTopView.bounds;
  31. maskLayer1.path = maskPath1.CGPath;
  32. bgTopView.layer.mask = maskLayer1;
  33. // bgTopView.layer.masksToBounds = YES;
  34. // bgTopView.layer.cornerRadius = kRealValue(4);
  35. _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, bgTopView.bottom,kScreenW, self.height - bgTopView.height)];
  36. _bgView.backgroundColor = kWhiteColor;
  37. UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW / 2, kRealValue(44))];
  38. titleL.centerX = kScreenW / 2;
  39. titleL.text = ASLocalizedString(@"钻石充值");
  40. titleL.textAlignment = NSTextAlignmentCenter;
  41. titleL.font = [UIFont boldSystemFontOfSize:16];
  42. _titleL = titleL;
  43. UIButton *payTitleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  44. payTitleBtn.frame = CGRectMake(0, bgTopView.bottom, kScreenW, kRealValue(40));
  45. [payTitleBtn setTitle:ASLocalizedString(@"充值")forState:UIControlStateNormal];
  46. payTitleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  47. [payTitleBtn setTitleColor:self.nowMainColor forState:UIControlStateNormal];
  48. UILabel *balanceL = [[UILabel alloc]init];
  49. balanceL.frame = CGRectMake(kRealValue(15), payTitleBtn.bottom + kRealValue(5), kScreenW * 0.8, kRealValue(30));
  50. balanceL.font = [UIFont boldSystemFontOfSize:18];
  51. balanceL.textColor = [UIColor colorWithHexString:@"#333333"];
  52. _balanceL = balanceL;
  53. [self addSubview:bgTopView];
  54. [self addSubview:self.bgView];
  55. [self addSubview:payTitleBtn];
  56. [self addSubview:titleL];
  57. [self addSubview:_balanceL];
  58. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  59. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  60. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, balanceL.bottom, kScreenW, kRealValue(200)) collectionViewLayout:layout];
  61. [_collectionView registerClass:[MGLiveRechargeCell class] forCellWithReuseIdentifier:NSStringFromClass([MGLiveRechargeCell class])];
  62. _collectionView.backgroundColor = kWhiteColor;
  63. _collectionView.dataSource =self;
  64. _collectionView.delegate = self;
  65. _collectionView.showsHorizontalScrollIndicator = NO;
  66. [self addSubview:_collectionView];
  67. _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _collectionView.bottom + kRealValue(15), kScreenW, kRealValue(50))];;
  68. _scrollView.backgroundColor = kWhiteColor;;
  69. _scrollView.contentSize = CGSizeMake(kScreenW * 1.2, 0);
  70. [self addSubview:self.scrollView];
  71. UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  72. selectBtn.frame = CGRectMake(_balanceL.left, _scrollView.bottom + kRealValue(10), kRealValue(20), kRealValue(20));
  73. [selectBtn addTarget:self action:@selector(clickSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
  74. [selectBtn setImage:[UIImage imageNamed:@"com_radio_selected_1"] forState:UIControlStateNormal];
  75. [selectBtn setImage:[UIImage imageNamed:@"com_radio_selected_2"] forState:UIControlStateSelected];
  76. UILabel *agreeLabel = [[UILabel alloc]initWithFrame:CGRectMake(selectBtn.right + kRealValue(10), 0, kScreenW *0.7, kRealValue(20))];
  77. agreeLabel.centerY = selectBtn.centerY;
  78. agreeLabel.userInteractionEnabled = YES;
  79. NSString *firstStr = ASLocalizedString(@"同意");
  80. NSString *secondStr = ASLocalizedString(@" 用户充值免责协议");
  81. NSString *contentStr = [NSString stringWithFormat:@"%@%@",firstStr,secondStr];
  82. NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:contentStr];
  83. [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#999999"] range:NSMakeRange(0, firstStr.length)];
  84. [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#9D63FF"] range:NSMakeRange(firstStr.length, secondStr.length)];
  85. agreeLabel.attributedText = attributeString;
  86. agreeLabel.font = [UIFont systemFontOfSize:12];
  87. UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAgreement:)];
  88. [agreeLabel addGestureRecognizer:tapLabel];
  89. [self addSubview:selectBtn];
  90. [self addSubview:agreeLabel];
  91. }
  92. -(void)clickSelectBtn:(UIButton *)sender{
  93. sender.selected = !sender.selected;
  94. self.isSelectAgree = sender.selected;
  95. }
  96. -(void)clickAgreement:(UITapGestureRecognizer *)sender{
  97. NSString *tmpUrlStr = [GlobalVariables sharedInstance].appModel.h5_url.url_recharge_agreement;
  98. BGMainWebViewController *tmpController = [BGMainWebViewController webControlerWithUrlStr:tmpUrlStr isShowIndicator:YES isShowNavBar:YES isShowBackBtn:YES isShowCloseBtn:NO];
  99. [[AppDelegate sharedAppDelegate] pushViewController:tmpController animated:YES];
  100. }
  101. -(void)requestModel{
  102. self.ruleListArr = [NSMutableArray array];
  103. NSMutableDictionary * parmDict = [NSMutableDictionary dictionary];
  104. [parmDict setObject:@"pay" forKey:@"ctl"];
  105. [parmDict setObject:@"recharge" forKey:@"act"];
  106. FWWeakify(self)
  107. [[NetHttpsManager manager]POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  108. FWStrongify(self)
  109. self.model = [AccountRechargeModel mj_objectWithKeyValues:responseJson];
  110. self.balanceL.text = [NSString stringWithFormat:ASLocalizedString(@"余额:%ld"),self.model.diamonds];
  111. if (self.model.pay_list.count)
  112. {
  113. PayTypeModel *model = [self.model.pay_list firstObject];
  114. if (model)
  115. {
  116. model.isSelect = YES;
  117. }
  118. // self.row = 1;
  119. // self.ruleListArr = [NSMutableArray arrayWithArray:self.model.pay_list];
  120. // self.ruleListArr = [NSMutableArray arrayWithArray:self.model.pay_list];
  121. self.ruleListArr = model.rule_list.count>0 ? model.rule_list : self.model.rule_list;
  122. // model.rule_list.count>0 ? model.rule_list : self.model.rule_list;
  123. [self resetScrollView];
  124. CGFloat viewWidth = kRealValue(200);
  125. _scrollView.contentSize = CGSizeMake(viewWidth * 3 * 1.1, 0);
  126. }
  127. if (self.model.rule_list.count) {
  128. self.listArr = [NSMutableArray arrayWithArray:self.model.rule_list];
  129. // self.ruleListArr = model.rule_list.count>0 ? model.rule_list : self.model.rule_list;
  130. [self.collectionView reloadData];
  131. }
  132. } FailureBlock:^(NSError *error) {
  133. }];
  134. }
  135. -(void)resetScrollView{
  136. [self.scrollView removeAllSubViews];
  137. CGFloat viewWidth = kRealValue(200);
  138. self.payBtnArray = [NSMutableArray array];
  139. for (int i = 0; i < self.model.pay_list.count; i ++ ) {
  140. PayTypeModel *model = [self.model.pay_list objectAtIndex:i];
  141. UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(kRealValue(15) + (viewWidth + 10) * i, 0, viewWidth, kRealValue(30))];
  142. [btn setTitle:model.name forState:UIControlStateNormal];
  143. btn.tag = 1000 + i;
  144. [btn setTitleColor:kWhiteColor forState:UIControlStateSelected];
  145. [btn setTitleColor:self.nowMainColor forState:UIControlStateNormal];
  146. btn.backgroundColor = self.nowMainColor;
  147. btn.layer.cornerRadius = kRealValue(30 / 2);
  148. btn.layer.masksToBounds = YES;
  149. btn.layer.borderColor = self.nowMainColor.CGColor;
  150. btn.layer.borderWidth = 0.5f;
  151. if (i == 0) {
  152. btn.selected = YES;
  153. btn.backgroundColor = self.nowMainColor;
  154. }else{
  155. btn.selected = NO;
  156. btn.backgroundColor = kWhiteColor;
  157. }
  158. [btn addTarget:self action:@selector(clickPayTypeBtn:) forControlEvents:UIControlEventTouchUpInside];
  159. btn.titleLabel.font = [UIFont systemFontOfSize:14];
  160. [self.payBtnArray addObject:btn];
  161. [self.scrollView addSubview:btn];
  162. }
  163. }
  164. -(void)clickPayTypeBtn:(UIButton *)sender{
  165. PayTypeModel *model = [self.model.pay_list objectAtIndex:sender.tag - 1000];
  166. for (PayTypeModel *model in self.model.pay_list)
  167. {
  168. model.isSelect = NO;
  169. }
  170. model.isSelect = YES;
  171. for (UIButton *btn in self.payBtnArray) {
  172. if (btn == sender) {
  173. sender.selected = YES;
  174. btn.backgroundColor = self.nowMainColor;
  175. }else{
  176. btn.selected = NO;
  177. btn.backgroundColor = kWhiteColor;
  178. }
  179. }
  180. }
  181. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  182. return 1;
  183. }
  184. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  185. return self.listArr.count;
  186. }
  187. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  188. MGLiveRechargeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MGLiveRechargeCell" forIndexPath:indexPath];
  189. if (indexPath.item < self.listArr.count) {
  190. PayMoneyModel *model = self.listArr[indexPath.item];
  191. [cell resetViewWithModel:model];
  192. cell.control.userInteractionEnabled = NO;
  193. // [cell.control addTarget:self action:@selector(clickControl:) forControlEvents:UIControlEventTouchUpInside];
  194. return cell;
  195. }
  196. return cell;
  197. }
  198. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  199. if (!self.isSelectAgree) {
  200. [FanweMessage alertHUD:ASLocalizedString(@"请先勾选是否同意用户充值免责协议")];
  201. return;
  202. }
  203. PayMoneyModel *model = self.listArr[indexPath.item];
  204. self.money = model.money_name;
  205. MGLiveRechargeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MGLiveRechargeCell" forIndexPath:indexPath];
  206. cell.selected = !cell.isSelected;
  207. [self payRequestNet:(int)indexPath.row wxPayNet:1];
  208. // __weak MGLiveRechargeView *weakSelf = self;
  209. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  210. //// weakSelf.hsClick = YES;
  211. // });
  212. }
  213. -(void)clickControl:(UIControl *)sender{
  214. }
  215. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  216. CGFloat viewWidth = (kScreenW - kRealValue(15 * 4)) / 3;
  217. return CGSizeMake(viewWidth, kRealValue(70));
  218. }
  219. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  220. {
  221. return UIEdgeInsetsMake(10, 10, 10, 10);
  222. }
  223. #pragma mark 支付请求
  224. - (void)payRequestNet:(int)indicate wxPayNet:(int)wxIndicate
  225. {
  226. NSString *payID = @"";
  227. for (PayTypeModel *model in self.model.pay_list)
  228. {
  229. if (model.isSelect)
  230. {
  231. payID = [NSString stringWithFormat:@"%ld",(long)model.payWayID];
  232. }
  233. }
  234. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  235. [parmDict setObject:@"pay" forKey:@"ctl"];
  236. [parmDict setObject:@"pay" forKey:@"act"];
  237. [parmDict setObject:payID forKey:@"pay_id"];
  238. if (wxIndicate == 1 && indicate < self.ruleListArr.count)
  239. {
  240. PayMoneyModel *model = self.ruleListArr[indicate];
  241. [parmDict setObject:[NSString stringWithFormat:@"%ld",(long)model.payID] forKey:@"rule_id"];
  242. [parmDict setObject:[NSString stringWithFormat:@"%@",model.money] forKey:@"money"];
  243. }
  244. else
  245. {
  246. [parmDict setObject:self.money forKey:@"money"];
  247. }
  248. [SVProgressHUD show];
  249. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson){
  250. [SVProgressHUD dismiss];
  251. if ([responseJson toInt:@"status"]==1)
  252. {
  253. NSDictionary *payDic =[responseJson objectForKey:@"pay"];
  254. NSDictionary *sdkDic =[payDic objectForKey:@"sdk_code"];
  255. NSString *sdkType =[sdkDic objectForKey:@"pay_sdk_type"];
  256. if ([sdkType isEqualToString:@"alipay"])
  257. {
  258. //支付宝支付
  259. NSDictionary *configDic =[sdkDic objectForKey:@"config"];
  260. Pay_Model * model2 = [Pay_Model mj_objectWithKeyValues: configDic];
  261. NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",model2.order_spec, model2.sign, model2.sign_type];
  262. // [self alipay:orderString block:nil];
  263. }
  264. // else if ([sdkType isEqualToString:@"wxpay"])
  265. // {
  266. // //微信支付
  267. // NSDictionary *configDic =[payDic objectForKey:@"config"];
  268. // NSDictionary *iosDic =[configDic objectForKey:@"ios"];
  269. // Mwxpay * wxmodel =[Mwxpay mj_objectWithKeyValues: iosDic];
  270. // PayReq* req = [[PayReq alloc] init];
  271. // req.openID = wxmodel.appid;
  272. // req.partnerId = wxmodel.partnerid;
  273. // req.prepayId = wxmodel.prepayid;
  274. // req.nonceStr = wxmodel.noncestr;
  275. // req.timeStamp = [wxmodel.timestamp intValue];
  276. // req.package = wxmodel.package;
  277. // req.sign = wxmodel.sign;
  278. //
  279. // [WXApi sendReq:req completion:^(BOOL success) {
  280. //
  281. // }];
  282. //
  283. // }
  284. else if ([sdkType isEqualToString:@"JubaoWxsdk"] || [sdkType isEqualToString:@"JubaoAlisdk"])
  285. {
  286. // NSDictionary *configDic =[sdkDic objectForKey:@"config"];
  287. // _juBaoModel = [JuBaoModel mj_objectWithKeyValues: configDic];
  288. // BGParam *param = [[BGParam alloc] init];
  289. // // playerid:用户在第三方平台上的用户名
  290. // param.playerid = _juBaoModel.playerid;
  291. // // goodsname:购买商品名称
  292. // param.goodsname = _juBaoModel.goodsname;
  293. // // amount:购买商品价格,单位是元
  294. // param.amount = _juBaoModel.amount;
  295. // // payid:第三方平台上的订单号,请传真实订单号,方便后续对账,例子里采用随机数,
  296. // param.payid = _juBaoModel.payid;
  297. //
  298. // [BGInterface start:self withParams:param withDelegate:self];
  299. //[BGInterface start:self withParams:param withType:model.withType withDelegate:self];
  300. // 凡伟支付 end
  301. }
  302. else if ([sdkType isEqualToString:@"iappay"])
  303. {
  304. [SVProgressHUD showWithStatus:ASLocalizedString(@"正在提交iTunes Store,请等待...")];
  305. // 监听购买结果
  306. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  307. NSMutableDictionary *configDic = [NSMutableDictionary new];
  308. configDic = sdkDic[@"config"];
  309. self.pro_id = configDic[@"product_id"];
  310. //查询是否允许内付费
  311. if ([SKPaymentQueue canMakePayments])
  312. {
  313. // 执行下面提到的第5步:
  314. [self getProductInfowithprotectId:self.pro_id];
  315. }
  316. else
  317. {
  318. [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
  319. }
  320. }
  321. else if ([payDic toInt:@"is_wap"] == 1)
  322. {
  323. if ([payDic toInt:@"is_without"] == 1) // 跳转外部浏览器
  324. {
  325. NSURL *url=[NSURL URLWithString:[payDic stringForKey:@"url"]];
  326. [[UIApplication sharedApplication] openURL:url];
  327. }
  328. else
  329. {
  330. BGMainWebViewController *vc = [BGMainWebViewController webControlerWithUrlStr:[payDic stringForKey:@"url"] isShowIndicator:YES isShowNavBar:YES isShowBackBtn:YES];
  331. [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
  332. }
  333. }
  334. else
  335. {
  336. NSLog(ASLocalizedString(@"错误"));
  337. }
  338. }
  339. else
  340. {
  341. NSLog(ASLocalizedString(@"请求失败"));
  342. }
  343. }FailureBlock:^(NSError *error){
  344. [SVProgressHUD dismiss];
  345. }];
  346. }
  347. #pragma marlk 支付宝支付
  348. //- (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
  349. //{
  350. // NSString *appScheme = AlipayScheme;
  351. //
  352. // [[AlipaySDK defaultService] payOrder:payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  353. //
  354. // SResBase* retobj = nil;
  355. //
  356. // if ( resultDic )
  357. // {
  358. // if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
  359. // {
  360. // retobj = [[SResBase alloc]init];
  361. // retobj.msuccess = YES;
  362. // retobj.mmsg = ASLocalizedString(@"支付成功");
  363. // retobj.mcode = 1;
  364. // // block(retobj);
  365. // [FanweMessage alert:[NSString stringWithFormat:@"%@",retobj.mmsg]];
  366. //
  367. // [self reloadAcount];
  368. // }
  369. // else
  370. // {
  371. // retobj = [SResBase infoWithString: [resultDic objectForKey:@"memo" ]];
  372. // [FanweMessage alert:ASLocalizedString(@"支付失败")];
  373. // }
  374. // }
  375. // else
  376. // {
  377. // retobj = [SResBase infoWithString: ASLocalizedString(@"支付出现异常")];
  378. // [FanweMessage alert:ASLocalizedString(@"支付异常")];
  379. // }
  380. //
  381. // }];
  382. //}
  383. #pragma mark -- 苹果内购服务,下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
  384. - (void)getProductInfowithprotectId:(NSString *)proId
  385. {
  386. //这里填你的产品id,根据创建的名字
  387. //ProductIdofvip
  388. //ProductId
  389. NSMutableArray *proArr = [NSMutableArray new];
  390. [proArr addObject:proId];
  391. NSSet * set = [NSSet setWithArray:proArr];
  392. self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  393. self.request.delegate = self;
  394. [self.request start];
  395. NSLog(@"%@",set);
  396. NSLog(ASLocalizedString(@"请求开始请等待..."));
  397. }
  398. #pragma mark - 以上查询的回调函数,以上查询的回调函数
  399. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  400. {
  401. NSArray *myProduct = response.products;
  402. if (myProduct.count == 0)
  403. {
  404. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"无法获取产品信息,购买失败。")];
  405. [SVProgressHUD dismiss];
  406. return;
  407. }
  408. NSLog(@"productID:%@", response.invalidProductIdentifiers);
  409. NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
  410. SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
  411. [[SKPaymentQueue defaultQueue] addPayment:payment];
  412. }
  413. #pragma mark - others SKPaymentTransactionObserver
  414. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
  415. {
  416. for (SKPaymentTransaction *transaction in transactions)
  417. {
  418. switch (transaction.transactionState)
  419. {
  420. case SKPaymentTransactionStatePurchased://交易完成
  421. NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
  422. [SVProgressHUD dismiss];
  423. [self completeTransaction:transaction];
  424. //[queue finishTransaction:transaction];
  425. break;
  426. case SKPaymentTransactionStateFailed://交易失败
  427. NSLog(ASLocalizedString(@"交易失败"));
  428. [self failedTransaction:transaction];
  429. //[queue finishTransaction:transaction];
  430. break;
  431. case SKPaymentTransactionStateRestored://恢复已购买商品
  432. NSLog(ASLocalizedString(@"恢复已购买商品"));
  433. [self restoreTransaction:transaction];
  434. [queue finishTransaction:transaction];
  435. break;
  436. case SKPaymentTransactionStatePurchasing://商品添加进列表
  437. NSLog(ASLocalizedString(@"商品添加进列表"));
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. }
  444. - (void)completeTransaction:(SKPaymentTransaction *)transaction
  445. {
  446. // Your application should implement these two methods.
  447. NSLog(ASLocalizedString(@"---------进入了这里"));
  448. // NSString * productIdentifier = transaction.payment.productIdentifier;
  449. NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
  450. NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
  451. NSString *base64String = [data base64EncodedStringWithOptions:0];
  452. if ([productIdentifier length] > 0) {
  453. // 向自己的服务器验证购买凭证
  454. [self shoppingValidation:base64String];
  455. }
  456. // Remove the transaction from the payment queue.
  457. [SVProgressHUD dismiss];
  458. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  459. }
  460. #pragma mark -- 向自己的服务器验证购买凭证
  461. - (void)shoppingValidation : (NSString *)base64Str
  462. {
  463. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
  464. [dict setObject:@"pay" forKey:@"ctl"];
  465. [dict setObject:@"iappay" forKey:@"act"];
  466. NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
  467. [dict setObject:userid forKey:@"user_id"];
  468. [dict setObject:base64Str forKey:@"receipt-data"];
  469. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  470. [self reloadAcount];
  471. // [FanweMessage alert:ASLocalizedString(@"充值成功")];
  472. } FailureBlock:^(NSError *error) {
  473. }];
  474. }
  475. - (void)failedTransaction:(SKPaymentTransaction *)transaction
  476. {
  477. [SVProgressHUD dismiss];
  478. if(transaction.error.code != SKErrorPaymentCancelled)
  479. {
  480. NSLog(ASLocalizedString(@"购买失败"));
  481. }
  482. else
  483. {
  484. NSLog(ASLocalizedString(@"用户取消交易"));
  485. //[FanweMessage alert:ASLocalizedString(@"您已经取消交易")];
  486. }
  487. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  488. }
  489. - (void)restoreTransaction:(SKPaymentTransaction *)transaction
  490. {
  491. // 对于已购商品,处理恢复购买的逻辑
  492. //[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  493. [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  494. }
  495. // 支付结果的通知:
  496. - (void)receiveResult:(NSString*)payid result:(BOOL)success message:(NSString*)message
  497. {
  498. if ( success == YES )
  499. {
  500. [self reloadAcount];
  501. [FanweMessage alert:ASLocalizedString(@"支付成功")];
  502. }
  503. else
  504. {
  505. [FanweMessage alert:ASLocalizedString(@"支付失败")];
  506. }
  507. }
  508. //支付成功刷新账户
  509. - (void)paySuccess
  510. {
  511. [self reloadAcount];
  512. }
  513. #pragma marlk 刷新账户
  514. - (void)reloadAcount
  515. {
  516. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  517. // if (_is_vip)
  518. // {
  519. // [parmDict setObject:@"vip_pay" forKey:@"ctl"];
  520. // [parmDict setObject:@"purchase" forKey:@"act"];
  521. // }
  522. // else
  523. // {
  524. [parmDict setObject:@"pay" forKey:@"ctl"];
  525. [parmDict setObject:@"recharge" forKey:@"act"];
  526. // }
  527. __weak MGLiveRechargeView *weakSelf = self;
  528. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  529. {
  530. if ((NSNull *)responseJson != [NSNull null])
  531. {
  532. // if (_is_vip)
  533. // {
  534. // weakSelf.model.vip_expire_time = [responseJson objectForKey:@"vip_expire_time"];
  535. // weakSelf.model.is_vip = [responseJson integerForKey:@"is_vip"];
  536. // }
  537. // else
  538. // {
  539. weakSelf.model.diamonds = [[responseJson objectForKey:@"diamonds"] doubleValue];
  540. // }
  541. self.balanceL.text = [NSString stringWithFormat:@"%@",[responseJson objectForKey:@"diamonds"]];
  542. // [weakSelf.tableView reloadData];
  543. }
  544. } FailureBlock:^(NSError *error)
  545. {
  546. }];
  547. }
  548. - (void)receiveChannelTypes:(NSArray<NSNumber *>*)types
  549. {
  550. // [BGInterface selectChannel:_juBaoModel.withType];
  551. }
  552. - (BOOL)shouldAutorotate
  553. {
  554. return YES;
  555. }
  556. #pragma mark - Show And Hide
  557. - (void)show:(UIView *)superView{
  558. [self requestModel];
  559. [superView addSubview:self.shadowView];
  560. [superView addSubview:self];
  561. [UIView animateWithDuration:0.25 animations:^{
  562. self.shadowView.alpha = 1;
  563. self.y = kScreenH - self.height;
  564. }];
  565. }
  566. - (void)hide{
  567. // if (self.clickHideLiveWishBlock) {
  568. // self.clickHideLiveWishBlock();
  569. // }
  570. [UIView animateWithDuration:0.25 animations:^{
  571. self.shadowView.alpha = 1;
  572. self.y = kScreenH;
  573. } completion:^(BOOL finished) {
  574. [self.shadowView removeFromSuperview];
  575. [self removeFromSuperview];
  576. }];
  577. }
  578. - (UIView *)shadowView{
  579. if (!_shadowView) {
  580. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  581. _shadowView.backgroundColor = kClearColor;
  582. _shadowView.userInteractionEnabled = YES;
  583. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  584. [_shadowView addGestureRecognizer:tap];
  585. }
  586. return _shadowView;
  587. }
  588. @end