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_observer.h"
|
2018-01-10 05:45:42 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <vector>
|
|
|
|
|
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 "base/task/post_task.h"
|
|
|
|
#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>
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// InAppTransactionObserver
|
|
|
|
// ============================================================================
|
|
|
|
|
2018-01-10 07:55:49 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-04-05 06:33:13 +00:00
|
|
|
using InAppTransactionCallback = base::RepeatingCallback<void(
|
|
|
|
const std::vector<in_app_purchase::Transaction>&)>;
|
2018-01-10 07:55:49 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
@interface InAppTransactionObserver : NSObject <SKPaymentTransactionObserver> {
|
2017-11-30 13:27:33 +00:00
|
|
|
@private
|
2018-01-10 07:55:49 +00:00
|
|
|
InAppTransactionCallback callback_;
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 07:55:49 +00:00
|
|
|
- (id)initWithCallback:(const InAppTransactionCallback&)callback;
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation InAppTransactionObserver
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init with a callback.
|
|
|
|
*
|
|
|
|
* @param callback - The callback that will be called for each transaction
|
|
|
|
* update.
|
|
|
|
*/
|
2018-01-10 07:55:49 +00:00
|
|
|
- (id)initWithCallback:(const InAppTransactionCallback&)callback {
|
2017-11-30 13:27:33 +00:00
|
|
|
if ((self = [super init])) {
|
|
|
|
callback_ = callback;
|
|
|
|
|
|
|
|
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-01-10 08:30:30 +00:00
|
|
|
/**
|
|
|
|
* Cleanup.
|
|
|
|
*/
|
|
|
|
- (void)dealloc {
|
|
|
|
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
/**
|
|
|
|
* Run the callback in the browser thread.
|
|
|
|
*
|
|
|
|
* @param transaction - The transaction to pass to the callback.
|
|
|
|
*/
|
2018-01-10 08:39:16 +00:00
|
|
|
- (void)runCallback:(NSArray*)transactions {
|
2017-11-30 13:27:33 +00:00
|
|
|
// Convert the transaction.
|
2018-01-10 08:18:23 +00:00
|
|
|
std::vector<in_app_purchase::Transaction> converted;
|
|
|
|
converted.reserve([transactions count]);
|
|
|
|
for (SKPaymentTransaction* transaction in transactions) {
|
|
|
|
converted.push_back([self skPaymentTransactionToStruct:transaction]);
|
|
|
|
}
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
// Send the callback to the browser thread.
|
2019-09-18 19:58:00 +00:00
|
|
|
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce(callback_, converted));
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an NSDate to ISO String.
|
|
|
|
*
|
|
|
|
* @param date - The date to convert.
|
|
|
|
*/
|
|
|
|
- (NSString*)dateToISOString:(NSDate*)date {
|
|
|
|
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
|
NSLocale* enUSPOSIXLocale =
|
|
|
|
[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
|
|
|
|
[dateFormatter setLocale:enUSPOSIXLocale];
|
|
|
|
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
|
|
|
|
|
|
|
|
return [dateFormatter stringFromDate:date];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a SKPayment object to a Payment structure.
|
|
|
|
*
|
|
|
|
* @param payment - The SKPayment object to convert.
|
|
|
|
*/
|
|
|
|
- (in_app_purchase::Payment)skPaymentToStruct:(SKPayment*)payment {
|
|
|
|
in_app_purchase::Payment paymentStruct;
|
|
|
|
|
|
|
|
if (payment.productIdentifier != nil) {
|
|
|
|
paymentStruct.productIdentifier = [payment.productIdentifier UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payment.quantity >= 1) {
|
|
|
|
paymentStruct.quantity = (int)payment.quantity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return paymentStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a SKPaymentTransaction object to a Transaction structure.
|
|
|
|
*
|
|
|
|
* @param transaction - The SKPaymentTransaction object to convert.
|
|
|
|
*/
|
|
|
|
- (in_app_purchase::Transaction)skPaymentTransactionToStruct:
|
|
|
|
(SKPaymentTransaction*)transaction {
|
|
|
|
in_app_purchase::Transaction transactionStruct;
|
|
|
|
|
|
|
|
if (transaction.transactionIdentifier != nil) {
|
|
|
|
transactionStruct.transactionIdentifier =
|
|
|
|
[transaction.transactionIdentifier UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transaction.transactionDate != nil) {
|
|
|
|
transactionStruct.transactionDate =
|
|
|
|
[[self dateToISOString:transaction.transactionDate] UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transaction.originalTransaction != nil) {
|
|
|
|
transactionStruct.originalTransactionIdentifier =
|
|
|
|
[transaction.originalTransaction.transactionIdentifier UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transaction.error != nil) {
|
|
|
|
transactionStruct.errorCode = (int)transaction.error.code;
|
|
|
|
transactionStruct.errorMessage =
|
|
|
|
[[transaction.error localizedDescription] UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transaction.transactionState < 5) {
|
2018-04-05 06:33:13 +00:00
|
|
|
transactionStruct.transactionState =
|
|
|
|
[[@[ @"purchasing", @"purchased", @"failed", @"restored", @"deferred" ]
|
|
|
|
objectAtIndex:transaction.transactionState] UTF8String];
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 08:06:27 +00:00
|
|
|
if (transaction.payment != nil) {
|
|
|
|
transactionStruct.payment = [self skPaymentToStruct:transaction.payment];
|
|
|
|
}
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
return transactionStruct;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark SKPaymentTransactionObserver methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executed when a transaction is updated.
|
|
|
|
*
|
|
|
|
* @param queue - The payment queue.
|
|
|
|
* @param transactions - The list of transactions updated.
|
|
|
|
*/
|
|
|
|
- (void)paymentQueue:(SKPaymentQueue*)queue
|
|
|
|
updatedTransactions:(NSArray*)transactions {
|
2018-01-10 08:18:23 +00:00
|
|
|
[self runCallback:transactions];
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// C++ in_app_purchase
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
namespace in_app_purchase {
|
|
|
|
|
2018-04-17 23:37:22 +00:00
|
|
|
Transaction::Transaction() = default;
|
|
|
|
Transaction::Transaction(const Transaction&) = default;
|
|
|
|
Transaction::~Transaction() = default;
|
|
|
|
|
2018-01-10 07:55:49 +00:00
|
|
|
TransactionObserver::TransactionObserver() : weak_ptr_factory_(this) {
|
|
|
|
obeserver_ = [[InAppTransactionObserver alloc]
|
2019-05-03 19:08:41 +00:00
|
|
|
initWithCallback:base::BindRepeating(
|
|
|
|
&TransactionObserver::OnTransactionsUpdated,
|
|
|
|
weak_ptr_factory_.GetWeakPtr())];
|
2018-01-10 07:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TransactionObserver::~TransactionObserver() {
|
|
|
|
[obeserver_ release];
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace in_app_purchase
|