Reorgnize the platform-specific files

This commit is contained in:
Cheng Zhao 2018-01-10 14:45:42 +09:00
parent b2542a27c3
commit 5f1c76c688
7 changed files with 18 additions and 24 deletions

View file

@ -0,0 +1,30 @@
// 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.
#ifndef ATOM_BROWSER_MAC_IN_APP_PURCHASE_H_
#define ATOM_BROWSER_MAC_IN_APP_PURCHASE_H_
#include <string>
#include "base/callback.h"
namespace in_app_purchase {
// --------------------------- Typedefs ---------------------------
typedef base::Callback<void(bool isProductValid)> InAppPurchaseCallback;
// --------------------------- Functions ---------------------------
bool CanMakePayments(void);
std::string GetReceiptURL(void);
void PurchaseProduct(const std::string& productID,
const int quantity,
const InAppPurchaseCallback& callback);
} // namespace in_app_purchase
#endif // ATOM_BROWSER_MAC_IN_APP_PURCHASE_H_

View file

@ -0,0 +1,152 @@
// 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.
#include "atom/browser/mac/in_app_purchase.h"
#include "base/bind.h"
#include "base/strings/sys_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#import <CommonCrypto/CommonCrypto.h>
#import <StoreKit/StoreKit.h>
// ============================================================================
// InAppPurchase
// ============================================================================
// --------------------------------- Interface --------------------------------
@interface InAppPurchase : NSObject<SKProductsRequestDelegate> {
@private
in_app_purchase::InAppPurchaseCallback callback_;
NSInteger quantity_;
}
- (id)initWithCallback:(const in_app_purchase::InAppPurchaseCallback&)callback
quantity:(NSInteger)quantity;
- (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.
*/
- (id)initWithCallback:(const in_app_purchase::InAppPurchaseCallback&)callback
quantity:(NSInteger)quantity {
if ((self = [super init])) {
callback_ = callback;
quantity_ = quantity;
}
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 {
// 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_;
[[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 {
if (callback_) {
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(callback_, isProductValid));
}
}
@end
// ============================================================================
// C++ in_app_purchase
// ============================================================================
namespace in_app_purchase {
bool CanMakePayments() {
return [SKPaymentQueue canMakePayments];
}
std::string GetReceiptURL() {
NSURL* receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
if (receiptURL != nil) {
return [[receiptURL absoluteString] UTF8String];
} else {
return "";
}
}
void PurchaseProduct(const std::string& productID,
const int quantity,
const InAppPurchaseCallback& callback) {
auto iap =
[[InAppPurchase alloc] initWithCallback:callback quantity:quantity];
[iap purchaseProduct:base::SysUTF8ToNSString(productID)];
}
} // namespace in_app_purchase

View file

@ -0,0 +1,41 @@
// 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.
#ifndef ATOM_BROWSER_MAC_IN_APP_PURCHASE_OBSERVER_H_
#define ATOM_BROWSER_MAC_IN_APP_PURCHASE_OBSERVER_H_
#include <string>
#include "base/callback.h"
namespace in_app_purchase {
// --------------------------- Structures ---------------------------
struct Payment {
std::string productIdentifier = "";
int quantity = 1;
};
struct Transaction {
std::string transactionIdentifier = "";
std::string transactionDate = "";
std::string originalTransactionIdentifier = "";
int errorCode = 0;
std::string errorMessage = "";
std::string transactionState = "";
};
// --------------------------- Typedefs ---------------------------
typedef base::RepeatingCallback<void(const Payment, const Transaction)>
InAppTransactionCallback;
// --------------------------- Functions ---------------------------
void AddTransactionObserver(const InAppTransactionCallback& callback);
} // namespace in_app_purchase
#endif // ATOM_BROWSER_MAC_IN_APP_PURCHASE_OBSERVER_H_

View file

@ -0,0 +1,183 @@
// 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.
#include "atom/browser/mac/in_app_purchase_observer.h"
#include "base/bind.h"
#include "base/strings/sys_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#import <CommonCrypto/CommonCrypto.h>
#import <StoreKit/StoreKit.h>
// ============================================================================
// InAppTransactionObserver
// ============================================================================
@interface InAppTransactionObserver : NSObject<SKPaymentTransactionObserver> {
@private
in_app_purchase::InAppTransactionCallback callback_;
}
- (id)initWithCallback:
(const in_app_purchase::InAppTransactionCallback&)callback;
@end
@implementation InAppTransactionObserver
/**
* Init with a callback.
*
* @param callback - The callback that will be called for each transaction
* update.
*/
- (id)initWithCallback:
(const in_app_purchase::InAppTransactionCallback&)callback {
if ((self = [super init])) {
callback_ = callback;
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
return self;
}
/**
* Run the callback in the browser thread.
*
* @param transaction - The transaction to pass to the callback.
*/
- (void)runCallback:(SKPaymentTransaction*)transaction {
if (transaction == nil) {
return;
}
// Convert the payment.
in_app_purchase::Payment paymentStruct;
paymentStruct = [self skPaymentToStruct:transaction.payment];
// Convert the transaction.
in_app_purchase::Transaction transactionStruct;
transactionStruct = [self skPaymentTransactionToStruct:transaction];
// Send the callback to the browser thread.
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(callback_, paymentStruct, transactionStruct));
}
/**
* 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 == nil) {
return 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 == nil) {
return 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) {
transactionStruct.transactionState = [[@[
@"SKPaymentTransactionStatePurchasing",
@"SKPaymentTransactionStatePurchased", @"SKPaymentTransactionStateFailed",
@"SKPaymentTransactionStateRestored",
@"SKPaymentTransactionStateDeferred"
] objectAtIndex:transaction.transactionState] UTF8String];
}
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 {
for (SKPaymentTransaction* transaction in transactions) {
[self runCallback:transaction];
}
}
@end
// ============================================================================
// C++ in_app_purchase
// ============================================================================
namespace in_app_purchase {
void AddTransactionObserver(const InAppTransactionCallback& callback) {
[[InAppTransactionObserver alloc] initWithCallback:callback];
}
} // namespace in_app_purchase