BogoRechargePopView.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // BogoRechargePopView.m
  3. // BuguLive
  4. //
  5. // Created by Mac on 2021/9/24.
  6. // Copyright © 2021 xfg. All rights reserved.
  7. //
  8. #import "BogoRechargePopView.h"
  9. #import "BogoRechargeScrollView.h"
  10. #import <StoreKit/StoreKit.h>//内购
  11. @interface BogoRechargePopView ()<BogoRechargeDelegate,SKProductsRequestDelegate,SKPaymentTransactionObserver>
  12. @property(nonatomic, strong) BogoRechargeScrollView *scrollView;
  13. @property (nonatomic, strong) SKProductsRequest * request;
  14. @end
  15. @implementation BogoRechargePopView
  16. - (instancetype)initWithFrame:(CGRect)frame{
  17. if (self = [super initWithFrame:frame]) {
  18. [self addSubview:self.scrollView];
  19. }
  20. return self;
  21. }
  22. - (void)dealloc{
  23. if (self.request)
  24. {
  25. [self.request cancel];
  26. }
  27. [[NSNotificationCenter defaultCenter] removeObserver:self];
  28. [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
  29. }
  30. -(void)alipay:(NSString *)payinfo{
  31. // [self alipay:payinfo block:nil];
  32. }
  33. //- (void)alipay:(NSString*)payinfo block:(void(^)(SResBase* resb))block
  34. //{
  35. // NSString *appScheme = AlipayScheme;
  36. //
  37. // [[AlipaySDK defaultService] payOrder:payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  38. //
  39. // SResBase* retobj = nil;
  40. //
  41. // if (resultDic)
  42. // {
  43. // if ( [[resultDic objectForKey:@"resultStatus"] intValue] == 9000 )
  44. // {
  45. // retobj = [[SResBase alloc]init];
  46. // retobj.msuccess = YES;
  47. // retobj.mmsg = ASLocalizedString(@"支付成功");
  48. // retobj.mcode = 1;
  49. // // block(retobj);
  50. // [FanweMessage alert:[NSString stringWithFormat:@"%@",retobj.mmsg]];
  51. //
  52. // }
  53. // else
  54. // {
  55. // retobj = [SResBase infoWithString: [resultDic objectForKey:@"memo" ]];
  56. // [FanweMessage alert:ASLocalizedString(@"支付失败")];
  57. // }
  58. // }
  59. // else
  60. // {
  61. // retobj = [SResBase infoWithString: ASLocalizedString(@"支付出现异常")];
  62. // [FanweMessage alert:ASLocalizedString(@"支付异常")];
  63. // }
  64. //
  65. // }];
  66. //}
  67. #pragma mark -- 苹果内购服务,下面的ProductId应该是事先在itunesConnect中添加好的,已存在的付费项目。否则查询会失败。
  68. -(void)checkProid:(NSString *)pro_id{
  69. // 监听购买结果
  70. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  71. //查询是否允许内付费
  72. if ([SKPaymentQueue canMakePayments])
  73. {
  74. // 执行下面提到的第5步:
  75. [self getProductInfowithprotectId:pro_id];
  76. }
  77. else
  78. {
  79. [FanweMessage alert:ASLocalizedString(@"您已禁止应用内付费购买商品")];
  80. }
  81. }
  82. - (void)getProductInfowithprotectId:(NSString *)proId
  83. {
  84. //这里填你的产品id,根据创建的名字
  85. //ProductIdofvip
  86. //ProductId
  87. NSMutableArray *proArr = [NSMutableArray new];
  88. [proArr addObject:proId];
  89. NSSet * set = [NSSet setWithArray:proArr];
  90. self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  91. self.request.delegate = self;
  92. [self.request start];
  93. NSLog(@"%@",set);
  94. NSLog(ASLocalizedString(@"请求开始请等待..."));
  95. }
  96. #pragma mark - 以上查询的回调函数,以上查询的回调函数
  97. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  98. {
  99. NSArray *myProduct = response.products;
  100. if (myProduct.count == 0)
  101. {
  102. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"无法获取产品信息,购买失败。")];
  103. [SVProgressHUD dismiss];
  104. return;
  105. }
  106. NSLog(@"productID:%@", response.invalidProductIdentifiers);
  107. NSLog(ASLocalizedString(@"产品付费数量:%lu"),(unsigned long)[myProduct count]);
  108. SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];
  109. [[SKPaymentQueue defaultQueue] addPayment:payment];
  110. }
  111. #pragma mark - others SKPaymentTransactionObserver
  112. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
  113. {
  114. for (SKPaymentTransaction *transaction in transactions)
  115. {
  116. switch (transaction.transactionState)
  117. {
  118. case SKPaymentTransactionStatePurchased://交易完成
  119. NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
  120. [SVProgressHUD dismiss];
  121. [self completeTransaction:transaction];
  122. //[queue finishTransaction:transaction];
  123. break;
  124. case SKPaymentTransactionStateFailed://交易失败
  125. NSLog(ASLocalizedString(@"交易失败"));
  126. [self failedTransaction:transaction];
  127. //[queue finishTransaction:transaction];
  128. break;
  129. case SKPaymentTransactionStateRestored://恢复已购买商品
  130. NSLog(ASLocalizedString(@"恢复已购买商品"));
  131. [self restoreTransaction:transaction];
  132. [queue finishTransaction:transaction];
  133. break;
  134. case SKPaymentTransactionStatePurchasing://商品添加进列表
  135. NSLog(ASLocalizedString(@"商品添加进列表"));
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. }
  142. - (void)completeTransaction:(SKPaymentTransaction *)transaction
  143. {
  144. // Your application should implement these two methods.
  145. NSLog(ASLocalizedString(@"---------进入了这里"));
  146. // NSString * productIdentifier = transaction.payment.productIdentifier;
  147. NSString * productIdentifier = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];
  148. NSData *data = [productIdentifier dataUsingEncoding:NSUTF8StringEncoding];
  149. NSString *base64String = [data base64EncodedStringWithOptions:0];
  150. if ([productIdentifier length] > 0) {
  151. // 向自己的服务器验证购买凭证
  152. [self shoppingValidation:base64String];
  153. }
  154. // Remove the transaction from the payment queue.
  155. [SVProgressHUD dismiss];
  156. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  157. }
  158. #pragma mark -- 向自己的服务器验证购买凭证
  159. - (void)shoppingValidation : (NSString *)base64Str
  160. {
  161. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
  162. // if (_is_vip)
  163. // {
  164. // [dict setObject:@"vip_pay" forKey:@"ctl"];
  165. // }
  166. // else
  167. // {
  168. [dict setObject:@"pay" forKey:@"ctl"];
  169. // }
  170. [dict setObject:@"iappay" forKey:@"act"];
  171. NSString *userid = [IMAPlatform sharedInstance].host.imUserId;
  172. [dict setObject:userid forKey:@"user_id"];
  173. [dict setObject:base64Str forKey:@"receipt-data"];
  174. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  175. // [FanweMessage alert:ASLocalizedString(@"充值成功")];
  176. } FailureBlock:^(NSError *error) {
  177. }];
  178. }
  179. - (void)failedTransaction:(SKPaymentTransaction *)transaction
  180. {
  181. [SVProgressHUD dismiss];
  182. if(transaction.error.code != SKErrorPaymentCancelled)
  183. {
  184. NSLog(ASLocalizedString(@"购买失败"));
  185. }
  186. else
  187. {
  188. NSLog(ASLocalizedString(@"用户取消交易"));
  189. //[FanweMessage alert:ASLocalizedString(@"您已经取消交易")];
  190. }
  191. [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  192. }
  193. - (void)restoreTransaction:(SKPaymentTransaction *)transaction
  194. {
  195. // 对于已购商品,处理恢复购买的逻辑
  196. //[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
  197. [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  198. }
  199. // 支付结果的通知:
  200. - (void)receiveResult:(NSString*)payid result:(BOOL)success message:(NSString*)message
  201. {
  202. if ( success == YES )
  203. {
  204. [FanweMessage alert:ASLocalizedString(@"支付成功")];
  205. }
  206. else
  207. {
  208. [FanweMessage alert:ASLocalizedString(@"支付失败")];
  209. }
  210. }
  211. - (void)rechargeScrollView:(BogoRechargeScrollView *)rechargeScrollView didClickCloseBtn:(UIButton *)sender{
  212. [self hide];
  213. [[self getTopMostController].navigationController popToRootViewControllerAnimated:YES];
  214. }
  215. -(BogoRechargeScrollView *)scrollView{
  216. if (!_scrollView) {
  217. _scrollView = [[BogoRechargeScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenW , kScreenH - kRealValue(180))];
  218. _scrollView.reDelegate = self;
  219. _scrollView.isRecharge = NO;
  220. }
  221. return _scrollView;
  222. }
  223. //获取当前最上层的控制器
  224. - (UIViewController *)getTopMostController {
  225. UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  226. //循环之前tempVC和topVC是一样的
  227. UIViewController *tempVC = topVC;
  228. while (1) {
  229. if ([topVC isKindOfClass:[UITabBarController class]]) {
  230. topVC = ((UITabBarController*)topVC).selectedViewController;
  231. }
  232. if ([topVC isKindOfClass:[UINavigationController class]]) {
  233. topVC = ((UINavigationController*)topVC).visibleViewController;
  234. }
  235. if (topVC.presentedViewController) {
  236. topVC = topVC.presentedViewController;
  237. }
  238. //如果两者一样,说明循环结束了
  239. if ([tempVC isEqual:topVC]) {
  240. break;
  241. } else {
  242. //如果两者不一样,继续循环
  243. tempVC = topVC;
  244. }
  245. }
  246. return topVC;
  247. }
  248. @end