IAPAgnet.m 3.1 KB

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