in_app_purchase_product.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_product.h"
  5. #include "base/bind.h"
  6. #include "base/strings/sys_string_conversions.h"
  7. #include "content/public/browser/browser_thread.h"
  8. #import <StoreKit/StoreKit.h>
  9. // ============================================================================
  10. // InAppPurchaseProduct
  11. // ============================================================================
  12. // --------------------------------- Interface --------------------------------
  13. @interface InAppPurchaseProduct : NSObject <SKProductsRequestDelegate> {
  14. @private
  15. in_app_purchase::InAppPurchaseProductsCallback callback_;
  16. }
  17. - (id)initWithCallback:
  18. (const in_app_purchase::InAppPurchaseProductsCallback&)callback;
  19. @end
  20. // ------------------------------- Implementation -----------------------------
  21. @implementation InAppPurchaseProduct
  22. /**
  23. * Init with a callback.
  24. *
  25. * @param callback - The callback that will be called to return the products.
  26. */
  27. - (id)initWithCallback:
  28. (const in_app_purchase::InAppPurchaseProductsCallback&)callback {
  29. if ((self = [super init])) {
  30. callback_ = callback;
  31. }
  32. return self;
  33. }
  34. /**
  35. * Return products.
  36. *
  37. * @param productIDs - The products' id to fetch.
  38. */
  39. - (void)getProducts:(NSSet*)productIDs {
  40. SKProductsRequest* productsRequest;
  41. productsRequest =
  42. [[SKProductsRequest alloc] initWithProductIdentifiers:productIDs];
  43. productsRequest.delegate = self;
  44. [productsRequest start];
  45. }
  46. /**
  47. * @see SKProductsRequestDelegate
  48. */
  49. - (void)productsRequest:(SKProductsRequest*)request
  50. didReceiveResponse:(SKProductsResponse*)response {
  51. // Release request object.
  52. [request release];
  53. // Get the products.
  54. NSArray* products = response.products;
  55. // Convert the products.
  56. std::vector<in_app_purchase::Product> converted;
  57. converted.reserve([products count]);
  58. for (SKProduct* product in products) {
  59. converted.push_back([self skProductToStruct:product]);
  60. }
  61. // Send the callback to the browser thread.
  62. content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
  63. base::Bind(callback_, converted));
  64. [self release];
  65. }
  66. /**
  67. * Format local price.
  68. *
  69. * @param price - The price to format.
  70. * @param priceLocal - The local format.
  71. */
  72. - (NSString*)formatPrice:(NSDecimalNumber*)price
  73. withLocal:(NSLocale*)priceLocal {
  74. NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
  75. [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
  76. [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
  77. [numberFormatter setLocale:priceLocal];
  78. return [numberFormatter stringFromNumber:price];
  79. }
  80. /**
  81. * Convert a skProduct object to Product structure.
  82. *
  83. * @param product - The SKProduct object to convert.
  84. */
  85. - (in_app_purchase::Product)skProductToStruct:(SKProduct*)product {
  86. in_app_purchase::Product productStruct;
  87. // Product Identifier
  88. if (product.productIdentifier != nil) {
  89. productStruct.productIdentifier = [product.productIdentifier UTF8String];
  90. }
  91. // Product Attributes
  92. if (product.localizedDescription != nil) {
  93. productStruct.localizedDescription =
  94. [product.localizedDescription UTF8String];
  95. }
  96. if (product.localizedTitle != nil) {
  97. productStruct.localizedTitle = [product.localizedTitle UTF8String];
  98. }
  99. if (product.contentVersion != nil) {
  100. productStruct.contentVersion = [product.contentVersion UTF8String];
  101. }
  102. if (product.contentLengths != nil) {
  103. productStruct.contentLengths.reserve([product.contentLengths count]);
  104. for (NSNumber* contentLength in product.contentLengths) {
  105. productStruct.contentLengths.push_back([contentLength longLongValue]);
  106. }
  107. }
  108. // Pricing Information
  109. if (product.price != nil) {
  110. productStruct.price = [product.price doubleValue];
  111. if (product.priceLocale != nil) {
  112. productStruct.formattedPrice =
  113. [[self formatPrice:product.price withLocal:product.priceLocale]
  114. UTF8String];
  115. }
  116. }
  117. // Downloadable Content Information
  118. if (product.downloadable == true) {
  119. productStruct.downloadable = true;
  120. }
  121. return productStruct;
  122. }
  123. @end
  124. // ============================================================================
  125. // C++ in_app_purchase
  126. // ============================================================================
  127. namespace in_app_purchase {
  128. Product::Product() = default;
  129. Product::Product(const Product&) = default;
  130. Product::~Product() = default;
  131. void GetProducts(const std::vector<std::string>& productIDs,
  132. const InAppPurchaseProductsCallback& callback) {
  133. auto* iapProduct = [[InAppPurchaseProduct alloc] initWithCallback:callback];
  134. // Convert the products' id to NSSet.
  135. NSMutableSet* productsIDSet =
  136. [NSMutableSet setWithCapacity:productIDs.size()];
  137. for (auto& productID : productIDs) {
  138. [productsIDSet addObject:base::SysUTF8ToNSString(productID)];
  139. }
  140. // Fetch the products.
  141. [iapProduct getProducts:productsIDSet];
  142. }
  143. } // namespace in_app_purchase