Pass multiple transactions at the same time

This follows the design of SKPayment API.
This commit is contained in:
Cheng Zhao 2018-01-10 17:18:23 +09:00
parent 2dd545ebda
commit e77ddd3221
6 changed files with 35 additions and 33 deletions

View file

@ -82,9 +82,9 @@ void InAppPurchase::PurchaseProduct(const std::string& product_id,
in_app_purchase::PurchaseProduct(product_id, quantity, callback); in_app_purchase::PurchaseProduct(product_id, quantity, callback);
} }
void InAppPurchase::OnTransactionUpdated( void InAppPurchase::OnTransactionsUpdated(
const in_app_purchase::Transaction& transaction) { const std::vector<in_app_purchase::Transaction>& transactions) {
Emit("transaction-updated", transaction); Emit("transactions-updated", transactions);
} }
} // namespace api } // namespace api

View file

@ -6,6 +6,7 @@
#define ATOM_BROWSER_API_ATOM_API_IN_APP_PURCHASE_H_ #define ATOM_BROWSER_API_ATOM_API_IN_APP_PURCHASE_H_
#include <string> #include <string>
#include <vector>
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/event_emitter.h"
#include "atom/browser/mac/in_app_purchase.h" #include "atom/browser/mac/in_app_purchase.h"
@ -31,8 +32,8 @@ class InAppPurchase: public mate::EventEmitter<InAppPurchase>,
void PurchaseProduct(const std::string& product_id, mate::Arguments* args); void PurchaseProduct(const std::string& product_id, mate::Arguments* args);
// TransactionObserver: // TransactionObserver:
void OnTransactionUpdated( void OnTransactionsUpdated(
const in_app_purchase::Transaction& transaction) override; const std::vector<in_app_purchase::Transaction>& transactions) override;
private: private:
DISALLOW_COPY_AND_ASSIGN(InAppPurchase); DISALLOW_COPY_AND_ASSIGN(InAppPurchase);

View file

@ -6,6 +6,7 @@
#define ATOM_BROWSER_MAC_IN_APP_PURCHASE_OBSERVER_H_ #define ATOM_BROWSER_MAC_IN_APP_PURCHASE_OBSERVER_H_
#include <string> #include <string>
#include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
@ -14,7 +15,7 @@
@class InAppTransactionObserver; @class InAppTransactionObserver;
#else // __OBJC__ #else // __OBJC__
class InAppTransactionObserver; class InAppTransactionObserver;
#endif // __OBJC__ #endif // __OBJC__
namespace in_app_purchase { namespace in_app_purchase {
@ -42,7 +43,8 @@ class TransactionObserver {
TransactionObserver(); TransactionObserver();
virtual ~TransactionObserver(); virtual ~TransactionObserver();
virtual void OnTransactionUpdated(const Transaction& transaction) = 0; virtual void OnTransactionsUpdated(
const std::vector<Transaction>& transactions) = 0;
private: private:
InAppTransactionObserver* obeserver_; InAppTransactionObserver* obeserver_;

View file

@ -18,7 +18,8 @@
namespace { namespace {
using InAppTransactionCallback = using InAppTransactionCallback =
base::RepeatingCallback<void(const in_app_purchase::Transaction&)>; base::RepeatingCallback<
void(const std::vector<in_app_purchase::Transaction>&)>;
} // namespace } // namespace
@ -54,19 +55,17 @@ using InAppTransactionCallback =
* *
* @param transaction - The transaction to pass to the callback. * @param transaction - The transaction to pass to the callback.
*/ */
- (void)runCallback:(SKPaymentTransaction*)transaction { - (void)runCallback:(NSArray<SKPaymentTransaction*>*)transactions {
if (transaction == nil) {
return;
}
// Convert the transaction. // Convert the transaction.
in_app_purchase::Transaction transactionStruct; std::vector<in_app_purchase::Transaction> converted;
transactionStruct = [self skPaymentTransactionToStruct:transaction]; converted.reserve([transactions count]);
for (SKPaymentTransaction* transaction in transactions) {
converted.push_back([self skPaymentTransactionToStruct:transaction]);
}
// Send the callback to the browser thread. // Send the callback to the browser thread.
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE, base::Bind(callback_, converted));
base::Bind(callback_, transactionStruct));
} }
/** /**
@ -157,9 +156,7 @@ using InAppTransactionCallback =
*/ */
- (void)paymentQueue:(SKPaymentQueue*)queue - (void)paymentQueue:(SKPaymentQueue*)queue
updatedTransactions:(NSArray*)transactions { updatedTransactions:(NSArray*)transactions {
for (SKPaymentTransaction* transaction in transactions) { [self runCallback:transactions];
[self runCallback:transaction];
}
} }
@end @end
@ -172,7 +169,7 @@ namespace in_app_purchase {
TransactionObserver::TransactionObserver() : weak_ptr_factory_(this) { TransactionObserver::TransactionObserver() : weak_ptr_factory_(this) {
obeserver_ = [[InAppTransactionObserver alloc] obeserver_ = [[InAppTransactionObserver alloc]
initWithCallback:base::Bind(&TransactionObserver::OnTransactionUpdated, initWithCallback:base::Bind(&TransactionObserver::OnTransactionsUpdated,
weak_ptr_factory_.GetWeakPtr())]; weak_ptr_factory_.GetWeakPtr())];
} }

View file

@ -8,23 +8,14 @@ Process: [Main](../glossary.md#main-process)
The `inAppPurchase` module emits the following events: The `inAppPurchase` module emits the following events:
### Event: 'transaction-updated' ### Event: 'transactions-updated'
Emitted when a transaction has been updated. Emitted when one or more transactions have been updated.
Returns: Returns:
* `event` Event * `event` Event
* `transaction` Object * `transactions` ([Transaction[]](structures/transaction.md) - Array of transactions.
* `transactionIdentifier` String
* `transactionDate` String
* `originalTransactionIdentifier` String
* `transactionState` String - The transaction sate (`"purchasing"`, `"purchased"`, `"failed"`, `"restored"`, or `"deferred"`)
* `errorCode` Integer
* `errorMessage` String
* `payment` Object
* `productIdentifier` String
* `quantity` Integer
## Methods ## Methods

View file

@ -0,0 +1,11 @@
# Transaction Object
* `transactionIdentifier` String
* `transactionDate` String
* `originalTransactionIdentifier` String
* `transactionState` String - The transaction sate (`"purchasing"`, `"purchased"`, `"failed"`, `"restored"`, or `"deferred"`)
* `errorCode` Integer
* `errorMessage` String
* `payment` Object
* `productIdentifier` String
* `quantity` Integer