in_app_purchase.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2017 Amaplex Software, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/mac/in_app_purchase.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/functional/bind.h"
  8. #include "base/strings/sys_string_conversions.h"
  9. #include "content/public/browser/browser_task_traits.h"
  10. #include "content/public/browser/browser_thread.h"
  11. #import <CommonCrypto/CommonCrypto.h>
  12. #import <StoreKit/StoreKit.h>
  13. // ============================================================================
  14. // InAppPurchase
  15. // ============================================================================
  16. // --------------------------------- Interface --------------------------------
  17. @interface InAppPurchase : NSObject <SKProductsRequestDelegate> {
  18. @private
  19. in_app_purchase::InAppPurchaseCallback callback_;
  20. NSInteger quantity_;
  21. NSString* username_;
  22. InAppPurchase __strong* self_;
  23. }
  24. - (id)initWithCallback:(in_app_purchase::InAppPurchaseCallback)callback
  25. quantity:(NSInteger)quantity
  26. username:(NSString*)username;
  27. - (void)purchaseProduct:(NSString*)productID;
  28. @end
  29. // ------------------------------- Implementation -----------------------------
  30. @implementation InAppPurchase
  31. /**
  32. * Init with a callback.
  33. *
  34. * @param callback - The callback that will be called when the payment is added
  35. * to the queue.
  36. */
  37. - (id)initWithCallback:(in_app_purchase::InAppPurchaseCallback)callback
  38. quantity:(NSInteger)quantity
  39. username:(NSString*)username {
  40. if ((self = [super init])) {
  41. callback_ = std::move(callback);
  42. quantity_ = quantity;
  43. username_ = [username copy];
  44. self_ = self;
  45. }
  46. return self;
  47. }
  48. /**
  49. * Start the in-app purchase process.
  50. *
  51. * @param productID - The id of the product to purchase (the id of
  52. * com.example.app.product1 is product1).
  53. */
  54. - (void)purchaseProduct:(NSString*)productID {
  55. // Retrieve the product information. (The products request retrieves,
  56. // information about valid products along with a list of the invalid product
  57. // identifiers, and then calls its delegate to process the result).
  58. SKProductsRequest* productsRequest;
  59. productsRequest = [[SKProductsRequest alloc]
  60. initWithProductIdentifiers:[NSSet setWithObject:productID]];
  61. productsRequest.delegate = self;
  62. [productsRequest start];
  63. }
  64. /**
  65. * Process product informations and start the payment.
  66. *
  67. * @param request - The product request.
  68. * @param response - The informations about the list of products.
  69. */
  70. - (void)productsRequest:(SKProductsRequest*)request
  71. didReceiveResponse:(SKProductsResponse*)response {
  72. // Get the first product.
  73. NSArray* products = response.products;
  74. SKProduct* product = [products count] == 1 ? [products firstObject] : nil;
  75. // Return if the product is not found or invalid.
  76. if (product == nil) {
  77. [self runCallback:false];
  78. self_ = nil;
  79. return;
  80. }
  81. // Start the payment process.
  82. [self checkout:product];
  83. }
  84. /**
  85. * Submit a payment request to the App Store.
  86. *
  87. * @param product - The product to purchase.
  88. */
  89. - (void)checkout:(SKProduct*)product {
  90. // Add the payment to the transaction queue. (The observer will be called
  91. // when the transaction is finished).
  92. SKMutablePayment* payment = [SKMutablePayment paymentWithProduct:product];
  93. payment.quantity = quantity_;
  94. payment.applicationUsername = username_;
  95. [[SKPaymentQueue defaultQueue] addPayment:payment];
  96. // Notify that the payment has been added to the queue with success.
  97. [self runCallback:true];
  98. self_ = nil;
  99. }
  100. /**
  101. * Submit a payment request to the App Store.
  102. *
  103. * @param product - The product to purchase.
  104. */
  105. - (void)runCallback:(bool)isProductValid {
  106. if (callback_) {
  107. content::GetUIThreadTaskRunner({})->PostTask(
  108. FROM_HERE, base::BindOnce(std::move(callback_), isProductValid));
  109. }
  110. }
  111. @end
  112. // ============================================================================
  113. // C++ in_app_purchase
  114. // ============================================================================
  115. namespace in_app_purchase {
  116. bool CanMakePayments() {
  117. return [SKPaymentQueue canMakePayments];
  118. }
  119. void RestoreCompletedTransactions() {
  120. [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  121. }
  122. void FinishAllTransactions() {
  123. for (SKPaymentTransaction* transaction in SKPaymentQueue.defaultQueue
  124. .transactions) {
  125. [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  126. }
  127. }
  128. void FinishTransactionByDate(const std::string& date) {
  129. // Create the date formatter.
  130. NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
  131. NSLocale* enUSPOSIXLocale =
  132. [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  133. [dateFormatter setLocale:enUSPOSIXLocale];
  134. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  135. // Remove the transaction.
  136. NSString* transactionDate = base::SysUTF8ToNSString(date);
  137. for (SKPaymentTransaction* transaction in SKPaymentQueue.defaultQueue
  138. .transactions) {
  139. if ([transactionDate
  140. isEqualToString:[dateFormatter
  141. stringFromDate:transaction.transactionDate]]) {
  142. [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  143. }
  144. }
  145. }
  146. std::string GetReceiptURL() {
  147. NSURL* receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
  148. if (receiptURL != nil) {
  149. return std::string([[receiptURL path] UTF8String]);
  150. } else {
  151. return "";
  152. }
  153. }
  154. void PurchaseProduct(const std::string& productID,
  155. int quantity,
  156. const std::string& username,
  157. InAppPurchaseCallback callback) {
  158. auto* iap = [[InAppPurchase alloc]
  159. initWithCallback:std::move(callback)
  160. quantity:quantity
  161. username:base::SysUTF8ToNSString(username)];
  162. [iap purchaseProduct:base::SysUTF8ToNSString(productID)];
  163. }
  164. } // namespace in_app_purchase