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