IAPAgnet.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. @interface IAPAgnet ()
  12. @end
  13. @implementation IAPAgnet
  14. - (void)prepare {
  15. [SKPaymentQueue.defaultQueue addTransactionObserver: self];
  16. }
  17. - (void)clean {
  18. [SKPaymentQueue.defaultQueue removeTransactionObserver: self];
  19. }
  20. - (void)finishTransaction:(SKPaymentTransaction*)transaction {
  21. [SKPaymentQueue.defaultQueue finishTransaction:transaction];
  22. }
  23. - (void)failure:(SKPaymentTransaction*)transaction {
  24. //...
  25. [self finishTransaction:transaction];
  26. if (self.callback == nil) {
  27. return;
  28. }
  29. if (transaction.error != nil) {
  30. if (transaction.error.code == SKErrorPaymentCancelled) {
  31. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"取消购买"]);
  32. } else {
  33. self.callback(nil, transaction.error);
  34. }
  35. }
  36. }
  37. - (void)success:(SKPaymentTransaction*)transaction {
  38. [self finishTransaction:transaction];
  39. if (self.callback == nil) {
  40. return;
  41. }
  42. NSURL* url = NSBundle.mainBundle.appStoreReceiptURL;
  43. NSData* data = [NSData dataWithContentsOfURL:url];
  44. if (data == nil) {
  45. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"票据丢失"]);
  46. return;
  47. }
  48. if (![transaction.payment.applicationUsername isEqualToString:self.orderId]) {
  49. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"订单未找到"]);
  50. return;
  51. }
  52. self.callback([data base64EncodedStringWithOptions:0], nil);
  53. }
  54. //查询成功后的回调
  55. - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
  56. if ([response.invalidProductIdentifiers containsObject:self.productId]) {
  57. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"产品暂时不可用,请检查苹果后台"]);
  58. return;
  59. }
  60. for (SKProduct* product in response.products) {
  61. SKMutablePayment* payment = [SKMutablePayment paymentWithProduct:product];;
  62. payment.applicationUsername = self.orderId;
  63. [SKPaymentQueue.defaultQueue addPayment:payment];
  64. // Only one is allowed.
  65. return;
  66. }
  67. // No in-app purchase found, purchase failed.
  68. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"产品未找到, 请查检查后台"]);
  69. }
  70. //回调
  71. - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
  72. //NSLog(@"调了几次这个方法~~");
  73. for (SKPaymentTransaction* transaction in transactions) {
  74. switch (transaction.transactionState) {
  75. case SKPaymentTransactionStatePurchased:
  76. [self success:transaction];
  77. break;
  78. case SKPaymentTransactionStateFailed:
  79. [self failure:transaction];
  80. break;
  81. case SKPaymentTransactionStateRestored:
  82. [self success:transaction];
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. }
  89. @end
  90. #endif