Pass multiple transactions at the same time
This follows the design of SKPayment API.
This commit is contained in:
parent
2dd545ebda
commit
e77ddd3221
6 changed files with 35 additions and 33 deletions
|
@ -82,9 +82,9 @@ void InAppPurchase::PurchaseProduct(const std::string& product_id,
|
|||
in_app_purchase::PurchaseProduct(product_id, quantity, callback);
|
||||
}
|
||||
|
||||
void InAppPurchase::OnTransactionUpdated(
|
||||
const in_app_purchase::Transaction& transaction) {
|
||||
Emit("transaction-updated", transaction);
|
||||
void InAppPurchase::OnTransactionsUpdated(
|
||||
const std::vector<in_app_purchase::Transaction>& transactions) {
|
||||
Emit("transactions-updated", transactions);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#define ATOM_BROWSER_API_ATOM_API_IN_APP_PURCHASE_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/browser/api/event_emitter.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);
|
||||
|
||||
// TransactionObserver:
|
||||
void OnTransactionUpdated(
|
||||
const in_app_purchase::Transaction& transaction) override;
|
||||
void OnTransactionsUpdated(
|
||||
const std::vector<in_app_purchase::Transaction>& transactions) override;
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(InAppPurchase);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#define ATOM_BROWSER_MAC_IN_APP_PURCHASE_OBSERVER_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
|
@ -14,7 +15,7 @@
|
|||
@class InAppTransactionObserver;
|
||||
#else // __OBJC__
|
||||
class InAppTransactionObserver;
|
||||
#endif // __OBJC__
|
||||
#endif // __OBJC__
|
||||
|
||||
namespace in_app_purchase {
|
||||
|
||||
|
@ -42,7 +43,8 @@ class TransactionObserver {
|
|||
TransactionObserver();
|
||||
virtual ~TransactionObserver();
|
||||
|
||||
virtual void OnTransactionUpdated(const Transaction& transaction) = 0;
|
||||
virtual void OnTransactionsUpdated(
|
||||
const std::vector<Transaction>& transactions) = 0;
|
||||
|
||||
private:
|
||||
InAppTransactionObserver* obeserver_;
|
||||
|
|
|
@ -18,7 +18,8 @@
|
|||
namespace {
|
||||
|
||||
using InAppTransactionCallback =
|
||||
base::RepeatingCallback<void(const in_app_purchase::Transaction&)>;
|
||||
base::RepeatingCallback<
|
||||
void(const std::vector<in_app_purchase::Transaction>&)>;
|
||||
|
||||
} // namespace
|
||||
|
||||
|
@ -54,19 +55,17 @@ using InAppTransactionCallback =
|
|||
*
|
||||
* @param transaction - The transaction to pass to the callback.
|
||||
*/
|
||||
- (void)runCallback:(SKPaymentTransaction*)transaction {
|
||||
if (transaction == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
- (void)runCallback:(NSArray<SKPaymentTransaction*>*)transactions {
|
||||
// Convert the transaction.
|
||||
in_app_purchase::Transaction transactionStruct;
|
||||
transactionStruct = [self skPaymentTransactionToStruct:transaction];
|
||||
std::vector<in_app_purchase::Transaction> converted;
|
||||
converted.reserve([transactions count]);
|
||||
for (SKPaymentTransaction* transaction in transactions) {
|
||||
converted.push_back([self skPaymentTransactionToStruct:transaction]);
|
||||
}
|
||||
|
||||
// Send the callback to the browser thread.
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(callback_, transactionStruct));
|
||||
content::BrowserThread::UI, FROM_HERE, base::Bind(callback_, converted));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -157,9 +156,7 @@ using InAppTransactionCallback =
|
|||
*/
|
||||
- (void)paymentQueue:(SKPaymentQueue*)queue
|
||||
updatedTransactions:(NSArray*)transactions {
|
||||
for (SKPaymentTransaction* transaction in transactions) {
|
||||
[self runCallback:transaction];
|
||||
}
|
||||
[self runCallback:transactions];
|
||||
}
|
||||
|
||||
@end
|
||||
|
@ -172,7 +169,7 @@ namespace in_app_purchase {
|
|||
|
||||
TransactionObserver::TransactionObserver() : weak_ptr_factory_(this) {
|
||||
obeserver_ = [[InAppTransactionObserver alloc]
|
||||
initWithCallback:base::Bind(&TransactionObserver::OnTransactionUpdated,
|
||||
initWithCallback:base::Bind(&TransactionObserver::OnTransactionsUpdated,
|
||||
weak_ptr_factory_.GetWeakPtr())];
|
||||
}
|
||||
|
||||
|
|
|
@ -8,23 +8,14 @@ Process: [Main](../glossary.md#main-process)
|
|||
|
||||
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:
|
||||
|
||||
* `event` Event
|
||||
* `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
|
||||
* `transactions` ([Transaction[]](structures/transaction.md) - Array of transactions.
|
||||
|
||||
## Methods
|
||||
|
||||
|
|
11
docs/api/structures/transaction.md
Normal file
11
docs/api/structures/transaction.md
Normal 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
|
Loading…
Reference in a new issue