IAPSK1.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #import <StoreKit/StoreKit.h>
  19. #import "IAPSK1.h"
  20. @interface IAPSK1() <SKRequestDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver>
  21. @property (nonatomic, strong) NSArray<SKProduct *> *products;
  22. @property (nonatomic, strong) NSMutableDictionary<NSString *, SKProduct *> *productOfferings;
  23. @end
  24. @implementation IAPSK1
  25. #pragma mark - Public APIs
  26. + (IAPSK1 *)sharedInstance
  27. {
  28. static dispatch_once_t pred;
  29. static IAPSK1 *shared = nil;
  30. dispatch_once(&pred, ^{
  31. shared = [[IAPSK1 alloc] init];
  32. });
  33. return shared;
  34. }
  35. - (void)initialize
  36. {
  37. static dispatch_once_t pred;
  38. dispatch_once(&pred, ^{
  39. NSLog(@"Initializing SK1 IAP...");
  40. [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  41. self.productOfferings = [[NSMutableDictionary alloc] initWithDictionary:@{}];
  42. [self fetchProducts:@[@"com.fbsdk.unity.consumable", @"com.fbsdk.unity.nonconsumable", @"com.fbsdk.unity.subscription"]];
  43. });
  44. }
  45. - (void)purchaseConsumable
  46. {
  47. [self purchaseProductWithID:@"com.fbsdk.unity.consumable"];
  48. }
  49. - (void)purchaseNonConsumable
  50. {
  51. [self purchaseProductWithID:@"com.fbsdk.unity.nonconsumable"];
  52. }
  53. - (void)purchaseSubscription
  54. {
  55. [self purchaseProductWithID:@"com.fbsdk.unity.subscription"];
  56. }
  57. #pragma mark - Internal APIs
  58. - (void)purchaseProductWithID:(NSString *)identifier
  59. {
  60. if (self.products.count == 0) {
  61. return;
  62. }
  63. SKProduct *product = [self.productOfferings valueForKey:identifier];
  64. if (product == nil) {
  65. return;
  66. }
  67. SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
  68. [[SKPaymentQueue defaultQueue] addPayment:payment];
  69. }
  70. - (void)fetchProducts:(NSArray *)productIds
  71. {
  72. SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIds]];
  73. request.delegate = self;
  74. [request start];
  75. }
  76. #pragma mark - SKProductsRequestDelegate
  77. - (void)productsRequest:(nonnull SKProductsRequest *)request didReceiveResponse:(nonnull SKProductsResponse *)response {
  78. self.products = [[NSArray alloc] initWithArray:response.products];
  79. for (SKProduct* product in self.products) {
  80. [self.productOfferings setValue:product forKey:product.productIdentifier];
  81. }
  82. NSLog(@"Fetched Product Offerings: %@", self.productOfferings);
  83. }
  84. #pragma mark - SKPaymentTransactionObserver
  85. - (void)paymentQueue:(nonnull SKPaymentQueue *)queue updatedTransactions:(nonnull NSArray<SKPaymentTransaction *> *)transactions {
  86. for (SKPaymentTransaction* transaction in transactions) {
  87. switch (transaction.transactionState) {
  88. case SKPaymentTransactionStatePurchasing:
  89. break;
  90. case SKPaymentTransactionStateDeferred:
  91. break;
  92. case SKPaymentTransactionStatePurchased:
  93. NSLog(@"Successfully purchased %@ with id %@", transaction.payment.productIdentifier, transaction.transactionIdentifier);
  94. [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  95. break;
  96. case SKPaymentTransactionStateRestored:
  97. [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  98. break;
  99. case SKPaymentTransactionStateFailed:
  100. [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  101. break;
  102. }
  103. }
  104. }
  105. @end