RechargeView.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. //
  2. // RechargeView.m
  3. // BuguLive
  4. //
  5. // Created by 王珂 on 17/3/13.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "RechargeView.h"
  9. #import "RechargeDesCell.h"
  10. #import "Pay_Model.h"
  11. #import "Mwxpay.h"
  12. #import "BGInterface.h"
  13. #import "JuBaoModel.h"
  14. #import <StoreKit/StoreKit.h>
  15. @interface RechargeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,RechargeWayViewDelegate,SKPaymentTransactionObserver,SKProductsRequestDelegate,RechargeDesCellDelegate,FWInterfaceDelegate>
  16. @property (nonatomic, strong) UIButton * rechargeBtn; //充值按钮
  17. @property (nonatomic, strong) UIButton * exchangeBtn; //兑换按钮
  18. @property (nonatomic, strong) UIButton * closeBtn; //关闭按钮
  19. @property (nonatomic, strong) UIView *lineView; //充值与兑换之间的分割线
  20. @property (nonatomic, strong) RechargeWayView * rechargeWayView;
  21. @property (nonatomic, strong) UIView * separateView;
  22. @property (nonatomic, strong) UICollectionView * listCollectionView;
  23. @property (nonatomic, strong) UIView * balanceView;//余额视图
  24. @property (nonatomic, strong) UILabel * balanceLabel;
  25. @property (nonatomic, strong) UIImageView * diamondsImgView;
  26. @property (nonatomic, strong) NSMutableArray * listArray;
  27. @property (nonatomic, strong) NetHttpsManager * httpsManager;
  28. @property (nonatomic, strong) GlobalVariables * BuguLive;
  29. @property (nonatomic, strong) UIButton * oldButton;
  30. @property (nonatomic, strong) NSMutableArray * aliPayArr;//支付宝支付项目
  31. @property (nonatomic, strong) NSMutableArray * wxPayArr;//微信支付项目
  32. @property (nonatomic, assign) BOOL hadClick;//是否已经点击支付,限制频繁点击
  33. @property (nonatomic, strong) NSString *pro_id;
  34. @property (nonatomic, strong) NSMutableArray * otherPayArr;//除苹果支付外的其它第三方支付(此数组用于存有第三方支付,且show_other字段为1但支付列表只有最外面的一个统一支付列表,并没有每个不同支付方式对应不同类别时的情况)
  35. @property (nonatomic, strong) JuBaoModel * juBaoModel;
  36. @property (nonatomic, strong) UIView * selectLineView; //选中的分割线
  37. @end
  38. @implementation RechargeView
  39. - (void)dealloc
  40. {
  41. NSLog(ASLocalizedString(@"释放充值"));
  42. [[NSNotificationCenter defaultCenter] removeObserver:self];
  43. [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
  44. }
  45. - (instancetype)initWithFrame:(CGRect)frame andUIViewController:(UIViewController *)viewController
  46. {
  47. if (self = [super initWithFrame:frame])
  48. {
  49. self.backgroundColor = kAppRechargeSelectColor;
  50. _BuguLive = [GlobalVariables sharedInstance];
  51. CGFloat bthOrignx = (self.width/2-80)/2;
  52. _rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  53. _rechargeBtn.titleLabel.font = kAppLargeTextFont;
  54. _rechargeBtn.frame = CGRectMake(bthOrignx, 30, 80, 18);
  55. [_rechargeBtn setTitle:ASLocalizedString(@"充值")forState:UIControlStateNormal];
  56. [_rechargeBtn setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  57. [_rechargeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  58. [self addSubview:_rechargeBtn];
  59. _lineView = [[UIView alloc] initWithFrame:CGRectMake(self.width/2-0.25, 30, 0.5, 18)];
  60. //_lineView.backgroundColor = [BGUtils colorWithHexString:@"4dffffff"];
  61. _lineView.backgroundColor = kAppSpaceColor;
  62. [self addSubview:_lineView];
  63. _exchangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  64. _exchangeBtn.titleLabel.font = kAppLargeTextFont;
  65. _exchangeBtn.frame = CGRectMake(self.width/2+bthOrignx, 30, 80, 18);
  66. [_exchangeBtn setTitle:ASLocalizedString(@"兑换")forState:UIControlStateNormal];
  67. //[_exchangeBtn setTitleColor:[BGUtils colorWithHexString:@"4dffffff"] forState:UIControlStateNormal];
  68. [_exchangeBtn setTitleColor:kAppGrayColor3 forState:UIControlStateNormal];
  69. [_exchangeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  70. [self addSubview:_exchangeBtn];
  71. _selectLineView= [[UIView alloc] initWithFrame:CGRectMake((self.width/2-50)/2, CGRectGetMaxY(_rechargeBtn.frame)+10, 50, 2)];
  72. _selectLineView.backgroundColor = kAppGrayColor1;
  73. [self addSubview:_selectLineView];
  74. if (_BuguLive.appModel.open_game_module == 0 || _BuguLive.appModel.open_diamond_game_module == 1)
  75. {
  76. _rechargeBtn.frame = CGRectMake(self.width/2-40, 30, 80, 18);
  77. _lineView.hidden = YES;
  78. _exchangeBtn.hidden = YES;
  79. _selectLineView.hidden = YES;
  80. }
  81. _oldButton = _rechargeBtn;
  82. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  83. _closeBtn.frame = CGRectMake(self.width-30, 10, 20, 20);
  84. [_closeBtn setImage:[UIImage imageNamed:@"com_close_2"] forState:UIControlStateNormal];
  85. [_closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  86. [_closeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  87. [self addSubview:_closeBtn];
  88. _rechargeWayView = [[RechargeWayView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_selectLineView.frame) - kRealValue(5), self.width, 40)];
  89. _rechargeWayView.delegate = self;
  90. [self addSubview:_rechargeWayView];
  91. self.agreeBtn.frame = CGRectMake(kRealValue(20), _rechargeWayView.bottom + kRealValue(5), self.width, kRealValue(30));
  92. [self addSubview:self.agreeBtn];
  93. [self addSubview:self.selectAgreeBtn];
  94. _separateView = [[UIView alloc] initWithFrame:CGRectMake(10, _rechargeWayView.bottom + 15, self.width-20, 0.5)];
  95. _separateView.backgroundColor = kAppSpaceColor;
  96. [self addSubview:_separateView];
  97. UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init] ;
  98. CGFloat itemW = (self.width-20*3)/2 ;
  99. CGFloat itemH = 60;
  100. //设置cell的大小
  101. flow.itemSize = CGSizeMake(itemW, itemH);
  102. //设置分组距离collectionView四周的边距
  103. flow.minimumLineSpacing = 20;
  104. flow.minimumInteritemSpacing = 20;
  105. flow.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20) ;
  106. _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_separateView.frame), self.width, self.height-CGRectGetMaxY(_separateView.frame)-45) collectionViewLayout:flow];
  107. _listCollectionView.backgroundColor = kAppRechargeSelectColor;
  108. _listCollectionView.delegate = self;
  109. _listCollectionView.dataSource = self;
  110. [_listCollectionView registerClass:[RechargeDesCell class] forCellWithReuseIdentifier:@"rechargeCell"];
  111. [self addSubview:_listCollectionView];
  112. _balanceView = [[UIView alloc] init];
  113. //_balanceView.frame = CGRectMake(0, self.height-44, 100, 15);
  114. [self addSubview:_balanceView];
  115. _balanceLabel = [[UILabel alloc] init];
  116. _balanceLabel.textColor = kAppGrayColor1;
  117. _balanceLabel.font = kAppSmallTextFont;
  118. [_balanceView addSubview:_balanceLabel];
  119. _diamondsImgView = [[UIImageView alloc] init];
  120. _diamondsImgView.contentMode = UIViewContentModeScaleAspectFit;
  121. [_diamondsImgView setImage:[UIImage imageNamed:@"com_diamond_1"]];
  122. [_balanceView addSubview:_diamondsImgView];
  123. _httpsManager = [NetHttpsManager manager];
  124. _listArray = [NSMutableArray array];
  125. _aliPayArr = [NSMutableArray array];
  126. _wxPayArr = [NSMutableArray array];
  127. _otherPayArr = [NSMutableArray array];
  128. [self loadRechargeData];
  129. _indexPayWay = 0;
  130. self.layer.cornerRadius = 5;
  131. self.layer.masksToBounds = YES;
  132. _viewController = viewController;
  133. }
  134. return self;
  135. }
  136. - (void)loadRechargeData
  137. {
  138. NSMutableDictionary * parmDict = [NSMutableDictionary dictionary];
  139. [parmDict setObject:@"pay" forKey:@"ctl"];
  140. [parmDict setObject:@"recharge" forKey:@"act"];
  141. FWWeakify(self)
  142. [_httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  143. FWStrongify(self)
  144. [self.listArray removeAllObjects];
  145. self.model = [AccountRechargeModel mj_objectWithKeyValues:responseJson];
  146. //如果有其它金额充值方式,要在除苹果支付外的其它支付方式里面的每个支付方式列表里添加一个对应的model,添加的model用于表示其它金额支付
  147. if ([self.model.show_other integerValue] == 1)
  148. {
  149. for (int i=0; i<self.model.pay_list.count; ++i)
  150. {
  151. PayTypeModel * model = self.model.pay_list[i];
  152. if (![model.class_name isEqual:@"Iappay"])
  153. {
  154. PayMoneyModel * otherModel = [[PayMoneyModel alloc] init];
  155. otherModel.hasOtherPay = YES;
  156. if (model.rule_list.count>0)
  157. {
  158. [model.rule_list addObject:otherModel];
  159. self.model.pay_list[i] = model;
  160. }
  161. else
  162. {
  163. NSMutableArray * otherArray = [NSMutableArray arrayWithArray:self.model.rule_list];
  164. [otherArray addObject:otherModel];
  165. self.otherPayArr = otherArray;
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. PayTypeModel * model = self.model.pay_list.firstObject;
  172. if ([model.class_name isEqual:@"Iappay"] && self.model.pay_list.count == 1)
  173. {
  174. self.rechargeWayView.frame = CGRectMake(0, CGRectGetMaxY(self.rechargeBtn.frame), self.width, 0);
  175. self.rechargeWayView.hidden = YES;
  176. self.separateView.frame = CGRectMake(10, CGRectGetMaxY(self.rechargeWayView.frame)+20, self.width-20, 0.5);
  177. }
  178. else
  179. {
  180. self.rechargeWayView.frame = CGRectMake(0, CGRectGetMaxY(_selectLineView.frame) - kRealValue(5), self.width, 40);
  181. self.selectAgreeBtn.frame = CGRectMake(kRealValue(15), self.rechargeWayView.bottom + kRealValue(15), kRealValue(20), kRealValue(20));
  182. self.agreeBtn.centerY = self.selectAgreeBtn.centerY;
  183. self.agreeBtn.left = self.selectAgreeBtn.right;
  184. self.rechargeWayView.hidden = NO;
  185. self.separateView.frame = CGRectMake(10, _rechargeWayView.bottom + 35, self.width-20, 0.5);
  186. }
  187. self.listCollectionView.frame = CGRectMake(0, CGRectGetMaxY(self.separateView.frame), self.width, self.height-CGRectGetMaxY(_separateView.frame)-45);
  188. if (self.model.pay_list.count>0)
  189. {
  190. PayTypeModel *model = [self.model.pay_list firstObject];
  191. if ([self.model.show_other integerValue] == 1)
  192. {
  193. if (![model.class_name isEqual:@"Iappay"])
  194. {
  195. if (model.rule_list.count>0)
  196. {
  197. self.listArray = model.rule_list;
  198. }
  199. else
  200. {
  201. self.listArray = self.otherPayArr;
  202. }
  203. }
  204. else
  205. {
  206. self.listArray = model.rule_list.count>0 ? model.rule_list: self.model.rule_list;
  207. }
  208. }
  209. else
  210. {
  211. self.listArray = model.rule_list.count>0 ? model.rule_list: self.model.rule_list;
  212. }
  213. }
  214. self.rechargeWayView.rechargeWayArr =self.model.pay_list;
  215. [self.listCollectionView reloadData];
  216. [self reloadView];
  217. } FailureBlock:^(NSError *error) {
  218. }];
  219. }
  220. - (void)setModel:(AccountRechargeModel *)model
  221. {
  222. _model = model;
  223. [self reloadView];
  224. }
  225. - (void)reloadView
  226. {
  227. NSString * str = [NSString stringWithFormat:ASLocalizedString(@"余额:%zd"),self.model.diamonds];
  228. CGSize priceSize = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, self.width) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kAppSmallTextFont} context:nil].size;
  229. CGFloat length = priceSize.width+17;
  230. //_balanceView.frame = CGRectMake(0, self.height-44, 100, 15);
  231. _balanceView.frame = CGRectMake((self.width-length)/2, self.height-44, length, 30);
  232. _balanceLabel.frame = CGRectMake(0, 5, priceSize.width, 15);
  233. _diamondsImgView.frame = CGRectMake(CGRectGetMaxX(_balanceLabel.frame)+2,5, 15, 15);
  234. _balanceLabel.text = str;
  235. }
  236. - (void)clickBtn:(UIButton *) button
  237. {
  238. if (button == _rechargeBtn)
  239. {
  240. _oldButton = _rechargeBtn;
  241. }
  242. else if (button == _exchangeBtn)
  243. {
  244. _oldButton = _exchangeBtn;
  245. //点击兑换按钮
  246. self.hidden = YES;
  247. self.transform = CGAffineTransformIdentity;
  248. if (_delegate && [_delegate respondsToSelector:@selector(choseRecharge:orExchange:)])
  249. {
  250. [_delegate choseRecharge:NO orExchange:YES];
  251. }
  252. }
  253. else if (button == _closeBtn)
  254. {
  255. if (_delegate && [_delegate respondsToSelector:@selector(closeRechargeWithRechargeView:)])
  256. {
  257. [_delegate closeRechargeWithRechargeView:self];
  258. }
  259. // [UIView animateWithDuration:0.5 animations:^{
  260. // self.transform = CGAffineTransformIdentity;
  261. // } completion:^(BOOL finished) {
  262. // self.hidden = YES;
  263. // }];
  264. }
  265. }
  266. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  267. {
  268. return self.listArray.count;
  269. }
  270. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. RechargeDesCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rechargeCell" forIndexPath:indexPath] ;
  273. cell.model = self.listArray[indexPath.row];
  274. cell.delegate = self;
  275. return cell ;
  276. }
  277. //点击购买
  278. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  279. {
  280. if (self.selectAgreeBtn.selected) {
  281. PayMoneyModel * model = self.listArray[indexPath.row];
  282. [self goToPayWithPayModel:model];
  283. }else{
  284. [FanweMessage alertHUD:ASLocalizedString(@"请先勾选是否同意用户充值免责协议")];
  285. }
  286. }
  287. - (void)choseRechargeWayWithIndex:(NSInteger) index
  288. {
  289. _indexPayWay = index;
  290. PayTypeModel * model = self.model.pay_list[index];
  291. if ([self.model.show_other integerValue] == 1)
  292. {
  293. if (![model.class_name isEqual:@"Iappay"])
  294. {
  295. if (model.rule_list.count>0)
  296. {
  297. self.listArray = model.rule_list;
  298. }
  299. else
  300. {
  301. self.listArray = _otherPayArr;
  302. }
  303. }
  304. else
  305. {
  306. self.listArray = model.rule_list.count>0 ? model.rule_list : self.model.rule_list;
  307. }
  308. }
  309. else
  310. {
  311. self.listArray = model.rule_list.count>0 ? model.rule_list : self.model.rule_list;
  312. }
  313. [self.listCollectionView reloadData];
  314. }
  315. - (void)goToPayWithPayModel:(PayMoneyModel *)model
  316. {
  317. if (_hadClick)
  318. {
  319. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"正在提交中")];
  320. return;
  321. }
  322. else
  323. {
  324. _hadClick = YES;
  325. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  326. _hadClick = NO;
  327. });
  328. //选择其它支付,弹出其它支付的窗口
  329. if (model.hasOtherPay)
  330. {
  331. self.hidden = YES;
  332. //self.frame = CGRectMake(10, kScreenH, kScreenW-20, kScreenH-180);
  333. self.transform = CGAffineTransformIdentity;
  334. if (_delegate && [_delegate respondsToSelector:@selector(choseOtherRechargeWithRechargeView:)])
  335. {
  336. [_delegate choseOtherRechargeWithRechargeView:self];
  337. }
  338. }
  339. else
  340. {
  341. [self payRequestWithModel:model];
  342. }
  343. }
  344. }
  345. - (void)payRequestWithModel:(PayMoneyModel *)payModel withPayWayName:(NSString *)name
  346. {
  347. for (int i=0; i<self.model.pay_list.count; ++i)
  348. {
  349. PayTypeModel * model = self.model.pay_list[i];
  350. if ([model.class_name isEqual:name])
  351. {
  352. _indexPayWay = i;
  353. [self payRequestWithModel:payModel];
  354. }
  355. }
  356. }
  357. - (void)payRequestWithModel:(PayMoneyModel *)payModel withPayWayIndex:(NSInteger )index
  358. {
  359. _indexPayWay = index;
  360. self.rechargeWayView.selectIndex = _indexPayWay;
  361. [self payRequestWithModel:payModel];
  362. }
  363. //支付请求
  364. - (void)payRequestWithModel:(PayMoneyModel *)payModel
  365. {
  366. PayTypeModel * model = self.model.pay_list[_indexPayWay];
  367. NSString * payID = [NSString stringWithFormat:@"%zd",model.payWayID];
  368. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  369. [parmDict setObject:@"pay" forKey:@"ctl"];
  370. [parmDict setObject:@"pay" forKey:@"act"];
  371. [parmDict setObject:payID forKey:@"pay_id"];
  372. if (payModel.hasOtherPay == NO)
  373. {
  374. [parmDict setObject:[NSString stringWithFormat:@"%zd",payModel.payID] forKey:@"rule_id"];
  375. [parmDict setObject:[NSString stringWithFormat:@"%@",payModel.money] forKey:@"money"];
  376. }
  377. else
  378. {
  379. //选择其它支付
  380. [parmDict setObject:self.money forKey:@"money"];
  381. }
  382. [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"支付请求中,请稍后")inView:self];
  383. [_httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  384. if ([responseJson toInt:@"status"]==1)
  385. {
  386. NSDictionary *payDic =[responseJson objectForKey:@"pay"];
  387. NSDictionary *sdkDic =[payDic objectForKey:@"sdk_code"];
  388. NSString *sdkType =[sdkDic objectForKey:@"pay_sdk_type"];
  389. if ([sdkType isEqualToString:@"alipay"])
  390. {
  391. //支付宝支付
  392. NSDictionary *configDic =[sdkDic objectForKey:@"config"];
  393. Pay_Model * model2 =[Pay_Model mj_objectWithKeyValues: configDic];
  394. NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",model2.order_spec, model2.sign, model2.sign_type];
  395. [self alipay:orderString block:nil];
  396. }
  397. // else if ([sdkType isEqualToString:@"wxpay"])
  398. // {
  399. // //微信支付
  400. // NSDictionary *configDic =[payDic objectForKey:@"config"];
  401. // NSDictionary *iosDic =[configDic objectForKey:@"ios"];
  402. // Mwxpay * wxmodel =[Mwxpay mj_objectWithKeyValues: iosDic];
  403. // PayReq* req = [[PayReq alloc] init];
  404. // req.openID = wxmodel.appid;
  405. // req.partnerId = wxmodel.partnerid;
  406. // req.prepayId = wxmodel.prepayid;
  407. // req.nonceStr = wxmodel.noncestr;
  408. // req.timeStamp = [wxmodel.timestamp intValue];
  409. // req.package = wxmodel.package;
  410. // req.sign = wxmodel.sign;
  411. //
  412. // [WXApi sendReq:req completion:nil];
  413. //
  414. //
  415. // }
  416. else if ([sdkType isEqualToString:@"iappay"])
  417. {
  418. [SVProgressHUD showWithStatus:ASLocalizedString(@"正在提交iTunes Store,请等待...")];
  419. // 监听购买结果
  420. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  421. NSMutableDictionary *configDic = [NSMutableDictionary new];
  422. configDic = sdkDic[@"config"];
  423. self.pro_id = configDic[@"product_id"];
  424. //查询是否允许内付费
  425. if ([SKPaymentQueue canMakePayments])
  426. {
  427. // 执行下面提到的第5步:
  428. [self getProductInfowithprotectId:self.pro_id];
  429. }
  430. else
  431. {
  432. [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
  433. }
  434. }
  435. else if ([sdkType isEqualToString:@"JubaoWxsdk"] || [sdkType isEqualToString:@"JubaoAlisdk"])
  436. {
  437. // NSDictionary *configDic =[sdkDic objectForKey:@"config"];
  438. // _juBaoModel =[JuBaoModel mj_objectWithKeyValues: configDic];
  439. // BGParam *param = [[BGParam alloc] init];
  440. // // playerid:用户在第三方平台上的用户名
  441. // param.playerid = _juBaoModel.playerid;
  442. // // goodsname:购买商品名称
  443. // param.goodsname = _juBaoModel.goodsname;
  444. // // amount:购买商品价格,单位是元
  445. // param.amount = _juBaoModel.amount;
  446. // // payid:第三方平台上的订单号,请传真实订单号,方便后续对账,例子里采用随机数,
  447. // param.payid = _juBaoModel.payid;
  448. // [BGInterface start:_viewController withParams:param withDelegate:self];
  449. //[BGInterface start:_viewController withParams:param withType:model.withType withDelegate:self];
  450. // 凡伟支付 end
  451. }
  452. else if ([payDic toInt:@"is_wap"] == 1)
  453. {
  454. if ([payDic toInt:@"is_without"] == 1) // 跳转外部浏览器
  455. {
  456. NSURL *url=[NSURL URLWithString:[payDic stringForKey:@"url"]];
  457. [[UIApplication sharedApplication] openURL:url];
  458. }
  459. else
  460. {
  461. BGMainWebViewController *vc = [BGMainWebViewController webControlerWithUrlStr:[payDic stringForKey:@"url"] isShowIndicator:YES isShowNavBar:YES];
  462. [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
  463. }
  464. }
  465. else
  466. {
  467. NSLog(ASLocalizedString(@"错误"));
  468. }
  469. }
  470. else
  471. {
  472. NSLog(ASLocalizedString(@"请求失败"));
  473. }
  474. [[BGHUDHelper sharedInstance] syncStopLoading];
  475. }FailureBlock:^(NSError *error)
  476. {
  477. [[BGHUDHelper sharedInstance] syncStopLoading];
  478. [SVProgressHUD dismiss];
  479. }];
  480. }
  481. #pragma marlk 支付宝支付
  482. - (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
  483. {
  484. NSString *appScheme = AlipayScheme;
  485. // [[AlipaySDK defaultService] payOrder:payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  486. //
  487. // SResBase* retobj = nil;
  488. //
  489. // if ( resultDic )
  490. // {
  491. // if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
  492. // {
  493. // retobj = [[SResBase alloc]init];
  494. // retobj.msuccess = YES;
  495. // retobj.mmsg = ASLocalizedString(@"支付成功");
  496. // retobj.mcode = 1;
  497. // [FanweMessage alert:[NSString stringWithFormat:@"%@",retobj.mmsg]];
  498. //
  499. // [self reloadAcount];
  500. // if (_delegate && [_delegate respondsToSelector:@selector(rechargeSuccessWithRechargeView:)])
  501. // {
  502. // [_delegate rechargeSuccessWithRechargeView:self];
  503. // }
  504. // }
  505. // else
  506. // {
  507. // retobj = [SResBase infoWithString: [resultDic objectForKey:@"memo" ]];
  508. // [FanweMessage alert:ASLocalizedString(@"支付失败")];
  509. // }
  510. // }
  511. // else
  512. // {
  513. // retobj = [SResBase infoWithString: ASLocalizedString(@"支付出现异常")];
  514. // [FanweMessage alert:ASLocalizedString(@"支付异常")];
  515. // }
  516. //
  517. // }];
  518. }
  519. #pragma mark -- 苹果内购服务
  520. // 下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
  521. - (void)getProductInfowithprotectId:(NSString *)proId
  522. {
  523. //这里填你的产品id,根据创建的名字
  524. //ProductIdofvip
  525. //ProductId
  526. NSMutableArray *proArr = [NSMutableArray new];
  527. [proArr addObject:proId];
  528. NSSet * set = [NSSet setWithArray:proArr];
  529. SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  530. request.delegate = self;
  531. [request start];
  532. NSLog(@"%@",set);
  533. NSLog(ASLocalizedString(@"请求开始请等待..."));
  534. }
  535. #pragma mark - 以上查询的回调函数-------
  536. // 以上查询的回调函数
  537. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  538. {
  539. NSArray *myProduct = response.products;
  540. if (myProduct.count == 0)
  541. {
  542. [FanweMessage alert:ASLocalizedString(@"无法获取产品信息,购买失败。")];
  543. [SVProgressHUD dismiss];
  544. return;
  545. }
  546. NSLog(@"productID:%@", response.invalidProductIdentifiers);
  547. NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
  548. SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
  549. [[SKPaymentQueue defaultQueue] addPayment:payment];
  550. }
  551. #pragma mark - others SKPaymentTransactionObserver
  552. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
  553. {
  554. for (SKPaymentTransaction *transaction in transactions)
  555. {
  556. switch (transaction.transactionState)
  557. {
  558. case SKPaymentTransactionStatePurchased://交易完成
  559. NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
  560. [SVProgressHUD dismiss];
  561. [self completeTransaction:transaction];
  562. //[queue finishTransaction:transaction];
  563. break;
  564. case SKPaymentTransactionStateFailed://交易失败
  565. NSLog(ASLocalizedString(@"交易失败"));
  566. [self failedTransaction:transaction];
  567. //[queue finishTransaction:transaction];
  568. break;
  569. case SKPaymentTransactionStateRestored://恢复已购买的商品
  570. NSLog(ASLocalizedString(@"已经购买过该产品"));
  571. [self restoreTransaction:transaction];
  572. [queue finishTransaction:transaction];
  573. break;
  574. case SKPaymentTransactionStatePurchasing://商品添加进列表
  575. NSLog(ASLocalizedString(@"商品添加进列表"));
  576. break;
  577. default:
  578. break;
  579. }
  580. }
  581. }
  582. - (void)completeTransaction:(SKPaymentTransaction *)transaction
  583. {
  584. // Your application should implement these two methods.
  585. NSLog(ASLocalizedString(@"---------进入了这里"));
  586. // NSString * productIdentifier = transaction.payment.productIdentifier;
  587. NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
  588. NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
  589. NSString *base64String = [data base64EncodedStringWithOptions:0];
  590. if ([productIdentifier length] > 0) {
  591. // 向自己的服务器验证购买凭证
  592. [self shoppingValidation:base64String];
  593. }
  594. // Remove the transaction from the payment queue.
  595. [SVProgressHUD dismiss];
  596. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  597. }
  598. #pragma mark -- 向自己的服务器验证购买凭证
  599. - (void)shoppingValidation : (NSString *)base64Str
  600. {
  601. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
  602. [dict setObject:@"pay" forKey:@"ctl"];
  603. [dict setObject:@"iappay" forKey:@"act"];
  604. NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
  605. [dict setObject:userid forKey:@"user_id"];
  606. [dict setObject:base64Str forKey:@"receipt-data"];
  607. [_httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  608. [self reloadAcount];
  609. if (_delegate && [_delegate respondsToSelector:@selector(rechargeSuccessWithRechargeView:)])
  610. {
  611. [_delegate rechargeSuccessWithRechargeView:self];
  612. }
  613. } FailureBlock:^(NSError *error) {
  614. }];
  615. }
  616. - (void)failedTransaction:(SKPaymentTransaction *)transaction
  617. {
  618. [SVProgressHUD dismiss];
  619. if(transaction.error.code != SKErrorPaymentCancelled)
  620. {
  621. NSLog(ASLocalizedString(@"购买失败"));
  622. } else {
  623. NSLog(ASLocalizedString(@"用户取消交易"));
  624. //[FanweMessage alert:ASLocalizedString(@"您已经取消交易")];
  625. }
  626. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  627. }
  628. - (void)restoreTransaction:(SKPaymentTransaction *)transaction
  629. {
  630. // 对于已购商品,处理恢复购买的逻辑
  631. [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  632. }
  633. #pragma marlk 刷新账户
  634. - (void)reloadAcount
  635. {
  636. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  637. [parmDict setObject:@"pay" forKey:@"ctl"];
  638. [parmDict setObject:@"recharge" forKey:@"act"];
  639. __weak typeof(self) ws = self;
  640. [_httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  641. if ([responseJson toInt:@"status"] == 1)
  642. {
  643. ws.model.diamonds = [[responseJson toString:@"diamonds"] doubleValue];
  644. [ws reloadView];
  645. }
  646. } FailureBlock:^(NSError *error) {
  647. }];
  648. }
  649. - (void)clickWithRechargeDesCell:(RechargeDesCell *)cell
  650. {
  651. if (self.selectAgreeBtn.selected) {
  652. NSIndexPath * indexPath = [_listCollectionView indexPathForCell:cell];
  653. PayMoneyModel * model = self.listArray[indexPath.row];
  654. [self goToPayWithPayModel:model];
  655. }else{
  656. [FanweMessage alertHUD:ASLocalizedString(@"请先勾选是否同意用户充值免责协议")];
  657. }
  658. }
  659. // 支付结果的通知:
  660. - (void)receiveResult:(NSString*)payid result:(BOOL)success message:(NSString*)message
  661. {
  662. if ( success == YES )
  663. {
  664. [self reloadAcount];
  665. [FanweMessage alert:ASLocalizedString(@"支付成功")];
  666. }
  667. else
  668. {
  669. [FanweMessage alert:ASLocalizedString(@"支付失败")];
  670. }
  671. }
  672. - (void)receiveChannelTypes:(NSArray<NSNumber *>*)types
  673. {
  674. // [BGInterface selectChannel:_juBaoModel.withType];
  675. }
  676. - (BOOL)shouldAutorotate
  677. {
  678. return YES;
  679. }
  680. -(void)clickAgreenBtn:(UIButton *)sender{
  681. NSString *tmpUrlStr = [GlobalVariables sharedInstance].appModel.h5_url.url_recharge_agreement;
  682. BGMainWebViewController *tmpController = [BGMainWebViewController webControlerWithUrlStr:tmpUrlStr isShowIndicator:YES isShowNavBar:YES isShowBackBtn:YES isShowCloseBtn:NO];
  683. [[AppDelegate sharedAppDelegate] pushViewController:tmpController animated:YES];
  684. }
  685. -(void)clickSelectAgreenBtn:(UIButton *)sender{
  686. sender.selected = !sender.selected;
  687. }
  688. -(QMUIButton *)agreeBtn{
  689. if (!_agreeBtn) {
  690. NSString *firstStr = ASLocalizedString(@"同意");
  691. NSString *secondStr = ASLocalizedString(@" 用户充值免责协议");
  692. NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",firstStr,secondStr]];
  693. [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:NSMakeRange(0, firstStr.length)];
  694. [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#DE88FF"] range:NSMakeRange(firstStr.length, secondStr.length)];
  695. _agreeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  696. _agreeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  697. [_agreeBtn setAttributedTitle:attributeString forState:UIControlStateNormal];
  698. [_agreeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
  699. _agreeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  700. [_agreeBtn addTarget:self action:@selector(clickAgreenBtn:) forControlEvents:UIControlEventTouchUpInside];
  701. }
  702. return _agreeBtn;
  703. }
  704. -(QMUIButton *)selectAgreeBtn{
  705. if (!_selectAgreeBtn) {
  706. _selectAgreeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  707. _selectAgreeBtn.selected = YES;
  708. [_selectAgreeBtn setImage:[UIImage imageNamed:@"com_radio_selected_1"] forState:UIControlStateNormal];
  709. [_selectAgreeBtn setImage:[UIImage imageNamed:@"com_radio_selected_2"] forState:UIControlStateSelected];
  710. [_selectAgreeBtn addTarget:self action:@selector(clickSelectAgreenBtn:) forControlEvents:UIControlEventTouchUpInside];
  711. }
  712. return _selectAgreeBtn;
  713. }
  714. @end