in_app_purchase.mm 5.4 KB

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