IAPAgnet.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // IAPAgnet.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/31.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #if SDK_HAS_IAP_PAYMENT
  9. #import "IAPAgnet.h"
  10. #import "XSNetwork.h"
  11. #import "NSBundle+KODLocalizable.h"
  12. #import "KODCommonConst.h"
  13. @interface IAPAgnet ()
  14. @end
  15. @implementation IAPAgnet
  16. - (void)prepare {
  17. [SKPaymentQueue.defaultQueue addTransactionObserver: self];
  18. }
  19. - (void)clean {
  20. [SKPaymentQueue.defaultQueue removeTransactionObserver: self];
  21. }
  22. - (void)finishTransaction:(SKPaymentTransaction*)transaction {
  23. [SKPaymentQueue.defaultQueue finishTransaction:transaction];
  24. }
  25. - (void)failure:(SKPaymentTransaction*)transaction {
  26. [self finishTransaction:transaction];
  27. if (self.callback == nil) {
  28. return;
  29. }
  30. if (transaction.error != nil) {
  31. if (transaction.error.code == SKErrorPaymentCancelled) {
  32. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"取消购买"]);
  33. } else {
  34. self.callback(nil, transaction.error);
  35. }
  36. }
  37. }
  38. - (void)success:(SKPaymentTransaction*)transaction {
  39. [self finishTransaction:transaction];
  40. if (self.callback == nil) {
  41. return;
  42. }
  43. NSURL* url = NSBundle.mainBundle.appStoreReceiptURL;
  44. NSData* data = [NSData dataWithContentsOfURL:url];
  45. if (data == nil) {
  46. //票据丢失
  47. NSString *str1 = [NSBundle KOD_localizedStringForKey:BillLose];
  48. self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
  49. return;
  50. }
  51. if (![transaction.payment.applicationUsername isEqualToString:self.orderId]) {
  52. //订单未找到
  53. NSString *str1 = [NSBundle KOD_localizedStringForKey:OrderNoFind];
  54. self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
  55. return;
  56. }
  57. self.callback([data base64EncodedStringWithOptions:0], nil);
  58. }
  59. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
  60. if ([response.invalidProductIdentifiers containsObject:self.productId]) {
  61. //产品暂时不可用,请检查苹果后台
  62. NSString *str1 = [NSBundle KOD_localizedStringForKey:ProductNoUseCheckAppleDevelop];
  63. self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
  64. return;
  65. }
  66. for (SKProduct* product in response.products) {
  67. SKMutablePayment* payment = [SKMutablePayment paymentWithProduct:product];;
  68. payment.applicationUsername = self.orderId;
  69. [SKPaymentQueue.defaultQueue addPayment:payment];
  70. // Only one is allowed.
  71. return;
  72. }
  73. // No in-app purchase found, purchase failed.
  74. //产品未找到, 请查检查后台
  75. NSString *str1 = [NSBundle KOD_localizedStringForKey:ProductNoFindCheckDevelop];
  76. self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
  77. }
  78. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
  79. for (SKPaymentTransaction* transaction in transactions) {
  80. switch (transaction.transactionState) {
  81. case SKPaymentTransactionStatePurchased:
  82. [self success:transaction];
  83. break;
  84. case SKPaymentTransactionStateFailed:
  85. [self failure:transaction];
  86. break;
  87. case SKPaymentTransactionStateRestored:
  88. [self success:transaction];
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. }
  95. @end
  96. #endif