2017-11-30 13:27:33 +00:00
|
|
|
// Copyright (c) 2017 Amaplex Software, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/mac/in_app_purchase.h"
|
2018-01-10 05:45:42 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2017-11-30 13:27:33 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
|
|
|
#import <CommonCrypto/CommonCrypto.h>
|
|
|
|
#import <StoreKit/StoreKit.h>
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// InAppPurchase
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// --------------------------------- Interface --------------------------------
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
@interface InAppPurchase : NSObject <SKProductsRequestDelegate> {
|
2017-11-30 13:27:33 +00:00
|
|
|
@private
|
|
|
|
in_app_purchase::InAppPurchaseCallback callback_;
|
|
|
|
NSInteger quantity_;
|
2022-12-12 18:11:48 +00:00
|
|
|
NSString* username_;
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:56:01 +00:00
|
|
|
- (id)initWithCallback:(in_app_purchase::InAppPurchaseCallback)callback
|
2022-12-12 18:11:48 +00:00
|
|
|
quantity:(NSInteger)quantity
|
|
|
|
username:(NSString*)username;
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
- (void)purchaseProduct:(NSString*)productID;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// ------------------------------- Implementation -----------------------------
|
|
|
|
|
|
|
|
@implementation InAppPurchase
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init with a callback.
|
|
|
|
*
|
|
|
|
* @param callback - The callback that will be called when the payment is added
|
|
|
|
* to the queue.
|
|
|
|
*/
|
2019-03-13 20:56:01 +00:00
|
|
|
- (id)initWithCallback:(in_app_purchase::InAppPurchaseCallback)callback
|
2022-12-12 18:11:48 +00:00
|
|
|
quantity:(NSInteger)quantity
|
|
|
|
username:(NSString*)username {
|
2017-11-30 13:27:33 +00:00
|
|
|
if ((self = [super init])) {
|
2019-03-13 20:56:01 +00:00
|
|
|
callback_ = std::move(callback);
|
2017-11-30 13:27:33 +00:00
|
|
|
quantity_ = quantity;
|
2022-12-12 18:11:48 +00:00
|
|
|
username_ = [username copy];
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the in-app purchase process.
|
|
|
|
*
|
|
|
|
* @param productID - The id of the product to purchase (the id of
|
|
|
|
* com.example.app.product1 is product1).
|
|
|
|
*/
|
|
|
|
- (void)purchaseProduct:(NSString*)productID {
|
|
|
|
// Retrieve the product information. (The products request retrieves,
|
|
|
|
// information about valid products along with a list of the invalid product
|
|
|
|
// identifiers, and then calls its delegate to process the result).
|
|
|
|
SKProductsRequest* productsRequest;
|
|
|
|
productsRequest = [[SKProductsRequest alloc]
|
|
|
|
initWithProductIdentifiers:[NSSet setWithObject:productID]];
|
|
|
|
|
|
|
|
productsRequest.delegate = self;
|
|
|
|
[productsRequest start];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process product informations and start the payment.
|
|
|
|
*
|
|
|
|
* @param request - The product request.
|
|
|
|
* @param response - The informations about the list of products.
|
|
|
|
*/
|
|
|
|
- (void)productsRequest:(SKProductsRequest*)request
|
|
|
|
didReceiveResponse:(SKProductsResponse*)response {
|
2018-01-10 06:29:32 +00:00
|
|
|
// Release request object.
|
|
|
|
[request release];
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
// Get the first product.
|
|
|
|
NSArray* products = response.products;
|
|
|
|
SKProduct* product = [products count] == 1 ? [products firstObject] : nil;
|
|
|
|
|
|
|
|
// Return if the product is not found or invalid.
|
|
|
|
if (product == nil) {
|
|
|
|
[self runCallback:false];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the payment process.
|
|
|
|
[self checkout:product];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a payment request to the App Store.
|
|
|
|
*
|
|
|
|
* @param product - The product to purchase.
|
|
|
|
*/
|
|
|
|
- (void)checkout:(SKProduct*)product {
|
|
|
|
// Add the payment to the transaction queue. (The observer will be called
|
|
|
|
// when the transaction is finished).
|
|
|
|
SKMutablePayment* payment = [SKMutablePayment paymentWithProduct:product];
|
|
|
|
payment.quantity = quantity_;
|
2022-12-12 18:11:48 +00:00
|
|
|
payment.applicationUsername = username_;
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
[[SKPaymentQueue defaultQueue] addPayment:payment];
|
|
|
|
|
|
|
|
// Notify that the payment has been added to the queue with success.
|
|
|
|
[self runCallback:true];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a payment request to the App Store.
|
|
|
|
*
|
|
|
|
* @param product - The product to purchase.
|
|
|
|
*/
|
|
|
|
- (void)runCallback:(bool)isProductValid {
|
2017-12-02 09:59:13 +00:00
|
|
|
if (callback_) {
|
2022-05-17 16:48:40 +00:00
|
|
|
content::GetUIThreadTaskRunner({})->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(std::move(callback_), isProductValid));
|
2017-12-02 09:59:13 +00:00
|
|
|
}
|
2018-01-10 06:29:32 +00:00
|
|
|
// Release this delegate.
|
|
|
|
[self release];
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 18:11:48 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[username_ release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// C++ in_app_purchase
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
namespace in_app_purchase {
|
|
|
|
|
|
|
|
bool CanMakePayments() {
|
|
|
|
return [SKPaymentQueue canMakePayments];
|
|
|
|
}
|
|
|
|
|
2019-12-17 15:07:11 +00:00
|
|
|
void RestoreCompletedTransactions() {
|
|
|
|
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
|
|
|
|
}
|
|
|
|
|
2018-04-05 06:33:13 +00:00
|
|
|
void FinishAllTransactions() {
|
|
|
|
for (SKPaymentTransaction* transaction in SKPaymentQueue.defaultQueue
|
|
|
|
.transactions) {
|
|
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishTransactionByDate(const std::string& date) {
|
|
|
|
// Create the date formatter.
|
|
|
|
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
NSLocale* enUSPOSIXLocale =
|
|
|
|
[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
|
|
|
|
[dateFormatter setLocale:enUSPOSIXLocale];
|
|
|
|
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
|
|
|
|
|
|
|
|
// Remove the transaction.
|
|
|
|
NSString* transactionDate = base::SysUTF8ToNSString(date);
|
|
|
|
|
|
|
|
for (SKPaymentTransaction* transaction in SKPaymentQueue.defaultQueue
|
|
|
|
.transactions) {
|
|
|
|
if ([transactionDate
|
|
|
|
isEqualToString:[dateFormatter
|
|
|
|
stringFromDate:transaction.transactionDate]]) {
|
|
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
std::string GetReceiptURL() {
|
|
|
|
NSURL* receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
|
|
|
|
if (receiptURL != nil) {
|
2018-11-05 17:49:51 +00:00
|
|
|
return std::string([[receiptURL path] UTF8String]);
|
2017-11-30 13:27:33 +00:00
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PurchaseProduct(const std::string& productID,
|
2018-01-10 07:37:05 +00:00
|
|
|
int quantity,
|
2022-12-12 18:11:48 +00:00
|
|
|
const std::string& username,
|
2019-03-13 20:56:01 +00:00
|
|
|
InAppPurchaseCallback callback) {
|
2022-12-12 18:11:48 +00:00
|
|
|
auto* iap = [[InAppPurchase alloc]
|
|
|
|
initWithCallback:std::move(callback)
|
|
|
|
quantity:quantity
|
|
|
|
username:base::SysUTF8ToNSString(username)];
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
[iap purchaseProduct:base::SysUTF8ToNSString(productID)];
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace in_app_purchase
|