KDIAPManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // KDIAPManager.m
  3. // KDChat
  4. //
  5. // Created by JYJ on 2019/6/14.
  6. // Copyright © 2019 dcjf. All rights reserved.
  7. //
  8. #import "KDIAPManager.h"
  9. #import <StoreKit/StoreKit.h>
  10. //#import "KDMainPresenter.h"
  11. #import "XSNetwork.h"
  12. #import <Adjust/Adjust.h>
  13. @interface KDIAPManager() <SKPaymentTransactionObserver,SKProductsRequestDelegate> {
  14. IAPCompletionHandle _handle;
  15. }
  16. @property (nonatomic, strong) NSString* productId;
  17. @property (nonatomic, strong) NSString* orderId;
  18. /** presenter */
  19. //请求网络的.
  20. //@property (nonatomic, strong) KDMainPresenter *presenter;
  21. @end
  22. @implementation KDIAPManager
  23. /**
  24. 单例模式
  25. @return HZIAPManager
  26. */
  27. + (instancetype)shareIAPManager {
  28. static KDIAPManager *IAPManager = nil;
  29. static dispatch_once_t onceToken;
  30. dispatch_once(&onceToken, ^{
  31. IAPManager = [[KDIAPManager alloc] init];
  32. });
  33. return IAPManager;
  34. }
  35. - (instancetype)init {
  36. if (self = [super init]) {
  37. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  38. }
  39. return self;
  40. }
  41. - (void)dealloc {
  42. [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
  43. }
  44. #pragma mark -- Method
  45. /** 检测客户端与服务器漏单情况处理*/
  46. + (void)checkOrderStatus {
  47. NSDictionary *orderInfo = [KDIAPManager getReceiptData];
  48. if (orderInfo != nil) {
  49. NSString *orderId = orderInfo[@"orderId"];
  50. id receipt = orderInfo[@"receipt"];
  51. [[KDIAPManager shareIAPManager] verifyPurchaseForServiceWithOrderId:orderId receipt:receipt];
  52. }
  53. }
  54. #pragma mark -- 结束上次未完成的交易
  55. - (void)removeAllUncompleteTransactionsBeforeNewPurchase {
  56. NSArray* transactions = [SKPaymentQueue defaultQueue].transactions;
  57. if (transactions.count >= 1) {
  58. for (SKPaymentTransaction* transaction in transactions) {
  59. if (transaction.transactionState == SKPaymentTransactionStatePurchased ||
  60. transaction.transactionState == SKPaymentTransactionStateRestored) {
  61. [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
  62. }
  63. }
  64. } else {
  65. NSLog(@"没有历史未消耗订单");
  66. }
  67. }
  68. - (void)startIAPWithOrderId:(NSString *)orderId productID:(NSString *)productID completeHandle: (IAPCompletionHandle)handle {
  69. _handle = handle;
  70. if(productID && productID.length > 0) {
  71. if ([SKPaymentQueue canMakePayments]) {
  72. [self removeAllUncompleteTransactionsBeforeNewPurchase];
  73. self.orderId = orderId;
  74. // 允许内购
  75. self.productId = productID;
  76. NSSet *set = [NSSet setWithObjects:productID, nil];
  77. SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
  78. request.delegate = self;
  79. // 获取内购项目信息
  80. [request start];
  81. } else {
  82. // 不允许内购
  83. [self handleActionWithType:IAPResultNotArrow data:nil];
  84. }
  85. } else {
  86. NSLog(@"内购项目ID错误");
  87. [XSNetwork errorWithCode:-3 message:@"产品暂时不可用,请检查苹果后台"];
  88. [self handleActionWithType:IAPResultIDError data:nil];
  89. }
  90. }
  91. - (void)handleActionWithType:(IAPResultType)type data:(NSData *)data{
  92. switch (type) {
  93. case IAPResultSuccess:
  94. NSLog(@"购买成功");
  95. break;
  96. case IAPResultFailed:
  97. NSLog(@"购买失败");
  98. break;
  99. case IAPResultCancle:
  100. NSLog(@"用户取消购买");
  101. break;
  102. case IAPResultVerFailed:
  103. NSLog(@"订单校验失败");
  104. break;
  105. case IAPResultVerSuccess:
  106. NSLog(@"订单校验成功");
  107. break;
  108. case IAPResultNotArrow:
  109. NSLog(@"不允许程序内付费");
  110. break;
  111. default:
  112. break;
  113. }
  114. if(_handle){
  115. _handle(type, data);
  116. }
  117. }
  118. #pragma mark -- SKProductsRequestDelegate
  119. /**
  120. 收到产品信息的回调
  121. @param request 请求的信息
  122. @param response 返回的产品信息
  123. */
  124. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
  125. // 商品所在数组
  126. NSArray *productArr = response.products;
  127. if (productArr.count > 0) {
  128. // SKProduct *product = nil;
  129. for (SKProduct *product in productArr) {
  130. if ([product.productIdentifier isEqualToString:self.productId]) {
  131. // product = p;
  132. SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
  133. payment.quantity = 1;
  134. // payment.applicationUsername
  135. // payment.applicationUsername = _orderId;
  136. // 发起内购
  137. [[SKPaymentQueue defaultQueue] addPayment:payment];
  138. break;
  139. }
  140. }
  141. } else {
  142. [self handleActionWithType:IAPResultIDError data:nil];
  143. }
  144. }
  145. #pragma mark -- SKPaymentTransactionObserver
  146. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
  147. // 获取结果
  148. // 验证成功与否都注销交易,否则会出现虚假凭证信息一直验证不通过,每次进程序都得输入苹果账号
  149. for (SKPaymentTransaction *trans in transactions) {
  150. switch (trans.transactionState) {
  151. case SKPaymentTransactionStatePurchasing:
  152. NSLog(@"商品添加进列表");
  153. break;
  154. case SKPaymentTransactionStatePurchased:
  155. {
  156. NSLog(@"交易完成");
  157. [self completeTransaction:trans];
  158. [[SKPaymentQueue defaultQueue] finishTransaction:trans];
  159. //1. adjust统计内购支付事件
  160. NSString *kRechargeEventToken = [NSBundle mainBundle].infoDictionary[@"kRechargeEventToken"];
  161. ADJEvent *event = [ADJEvent eventWithEventToken:kRechargeEventToken];
  162. //[event setRevenue:1.0 currency:@"EUR"];
  163. [event setTransactionId:trans.transactionIdentifier]; //避免重复
  164. [Adjust trackEvent:event];
  165. }
  166. break;
  167. case SKPaymentTransactionStateFailed:
  168. NSLog(@"交易失败");
  169. [self failedTransaction:trans];
  170. [[SKPaymentQueue defaultQueue] finishTransaction:trans];
  171. break;
  172. case SKPaymentTransactionStateRestored:
  173. NSLog(@"已经购买过商品");
  174. [[SKPaymentQueue defaultQueue] finishTransaction:trans]; //消耗型商品不用写
  175. break;
  176. case SKPaymentTransactionStateDeferred:
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. /**
  184. 内购完成
  185. @param transaction 内购项目体
  186. */
  187. - (void)completeTransaction:(SKPaymentTransaction *)transaction {
  188. NSString *productIdentifier = transaction.payment.productIdentifier;
  189. NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
  190. NSData *data = [NSData dataWithContentsOfURL:recepitURL];
  191. id receiptString = [data base64EncodedStringWithOptions:0];
  192. if ([productIdentifier length] > 0 && data) {
  193. if (receiptString && self.orderId.length > 0) {
  194. //1.保存订单号和报文到本地.
  195. [self saveReceiptData:@{@"receipt":receiptString,
  196. @"orderId":self.orderId ?:@""}];
  197. //2.向自己的服务器验证购买凭证.
  198. [self verifyPurchaseForServiceWithOrderId:self.orderId
  199. receipt:receiptString];
  200. }
  201. } else {
  202. [self handleActionWithType:IAPResultVerFailed data:nil];
  203. }
  204. // // 自己向苹果发送验证
  205. // [self verifyPurchaseWithPaymentTransaction:transaction isTestServer:NO];
  206. }
  207. ///服务器验证购买凭证.
  208. - (void)verifyPurchaseForServiceWithOrderId:(NSString *)orderId
  209. receipt:(id)receiptString {
  210. if (orderId == nil && !receiptString) {
  211. [self handleActionWithType:IAPResultIDError data:nil];
  212. return;
  213. }
  214. [XSNetwork payWithReceipt:receiptString orderId:orderId complete:^(id object, NSError *error) {
  215. //TTOO:网络异常,服务器校验receiptString失败,就会出现掉单问题.需要保存订单号到本地,下次启动app,继续校验服务器.
  216. if (error != nil) {
  217. //订单校验失败.
  218. [self handleActionWithType:IAPResultVerFailed data:nil];
  219. }else{
  220. //订单校验成功,删除本地保存的订单号和苹果返回的data凭证.
  221. [self handleActionWithType:IAPResultSuccess data:nil];
  222. [self removeLocReceiptData];
  223. }
  224. }];
  225. }
  226. /**
  227. 交易失败
  228. @param transaction 内购项目体
  229. */
  230. - (void)failedTransaction:(SKPaymentTransaction *)transaction {
  231. if (transaction.error.code != SKErrorPaymentCancelled) {
  232. [self handleActionWithType:IAPResultFailed data:nil];
  233. } else {
  234. [self handleActionWithType:IAPResultCancle data:nil];
  235. }
  236. }
  237. #pragma mark -- 本地保存一次支付凭证
  238. static NSString *const kSaveReceiptData = @"kSaveReceiptData";
  239. - (void)saveReceiptData:(NSDictionary *)receiptData {
  240. [[NSUserDefaults standardUserDefaults] setValue:receiptData forKey:kSaveReceiptData];
  241. [[NSUserDefaults standardUserDefaults]synchronize];
  242. }
  243. + (NSDictionary *)getReceiptData {
  244. return [[NSUserDefaults standardUserDefaults] valueForKey:kSaveReceiptData];
  245. }
  246. - (void)removeLocReceiptData {
  247. [[NSUserDefaults standardUserDefaults] removeObjectForKey:kSaveReceiptData];
  248. [[NSUserDefaults standardUserDefaults] synchronize];
  249. }
  250. /**
  251. - (void)verifyPurchaseWithPaymentTransaction:(SKPaymentTransaction *)transaction isTestServer:(BOOL)flag {
  252. //交易验证
  253. NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
  254. NSData *receipt = [NSData dataWithContentsOfURL:recepitURL];
  255. NSError *error;
  256. NSDictionary *requestContents = @{@"receipt-data": [receipt base64EncodedStringWithOptions:0]};
  257. NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents options:0 error:&error];
  258. if (!requestData) { // 交易凭证为空验证失败
  259. [self handleActionWithType:IAPResultVerFailed data:nil];
  260. return;
  261. }
  262. //In the test environment, use https://sandbox.itunes.apple.com/verifyReceipt
  263. //In the real environment, use https://buy.itunes.apple.com/verifyReceipt
  264. NSString *serverString = @"https://buy.itunes.apple.com/verifyReceipt";
  265. if (flag) {
  266. serverString = @"https://sandbox.itunes.apple.com/verifyReceipt";
  267. }
  268. NSURL *storeURL = [NSURL URLWithString:serverString];
  269. NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
  270. [storeRequest setHTTPMethod:@"POST"];
  271. [storeRequest setHTTPBody:requestData];
  272. storeRequest.timeoutInterval = 20;
  273. [storeRequest addValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  274. NSURLSession *session = [NSURLSession sharedSession];
  275. NSURLSessionDataTask *task = [session dataTaskWithRequest:storeRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  276. if (error) {
  277. // 无法连接服务器,购买校验失败
  278. [self handleActionWithType:IAPResultVerFailed data:nil];
  279. } else {
  280. NSError *error;
  281. NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  282. if (!jsonResponse) {
  283. // 苹果服务器校验数据返回为空校验失败
  284. [self handleActionWithType:IAPResultVerFailed data:nil];
  285. }
  286. // 先验证正式服务器,如果正式服务器返回21007再去苹果测试服务器验证,沙盒测试环境苹果用的是测试服务器
  287. NSString *status = [NSString stringWithFormat:@"%@",jsonResponse[@"status"]];
  288. if (status && [status isEqualToString:@"21007"]) {
  289. [self verifyPurchaseWithPaymentTransaction:transaction isTestServer:YES];
  290. } else if(status && [status isEqualToString:@"0"]){
  291. [self handleActionWithType:IAPResultVerSuccess data:nil];
  292. }
  293. }
  294. }];
  295. [task resume];
  296. }
  297. */
  298. //- (KDMainPresenter *)presenter {
  299. // if (!_presenter) {
  300. // self.presenter = [[KDMainPresenter alloc] init];
  301. // }
  302. // return _presenter;
  303. //}
  304. @end