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.
|
|
|
|
|
2018-01-10 05:45:42 +00:00
|
|
|
#include "atom/browser/api/atom_api_in_app_purchase.h"
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::Payment> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::Payment& payment) {
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
|
|
|
dict.Set("productIdentifier", payment.productIdentifier);
|
|
|
|
dict.Set("quantity", payment.quantity);
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::Transaction> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::Transaction& val) {
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
|
|
|
dict.Set("transactionIdentifier", val.transactionIdentifier);
|
|
|
|
dict.Set("transactionDate", val.transactionDate);
|
|
|
|
dict.Set("originalTransactionIdentifier",
|
|
|
|
val.originalTransactionIdentifier);
|
|
|
|
dict.Set("transactionState", val.transactionState);
|
|
|
|
dict.Set("errorCode", val.errorCode);
|
|
|
|
dict.Set("errorMessage", val.errorMessage);
|
2018-01-10 08:06:27 +00:00
|
|
|
dict.Set("payment", val.payment);
|
2018-01-10 07:37:05 +00:00
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-05 06:33:13 +00:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::Product> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::Product& val) {
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
|
|
|
dict.Set("productIdentifier", val.productIdentifier);
|
|
|
|
dict.Set("localizedDescription", val.localizedDescription);
|
|
|
|
dict.Set("localizedTitle", val.localizedTitle);
|
|
|
|
dict.Set("contentVersion", val.localizedTitle);
|
|
|
|
dict.Set("contentLengths", val.contentLengths);
|
|
|
|
|
|
|
|
// Pricing Information
|
|
|
|
dict.Set("price", val.price);
|
|
|
|
dict.Set("formattedPrice", val.formattedPrice);
|
|
|
|
|
|
|
|
// Downloadable Content Information
|
|
|
|
dict.Set("isDownloadable", val.downloadable);
|
|
|
|
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2018-01-10 08:39:16 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-01-10 07:37:05 +00:00
|
|
|
// static
|
|
|
|
mate::Handle<InAppPurchase> InAppPurchase::Create(v8::Isolate* isolate) {
|
|
|
|
return mate::CreateHandle(isolate, new InAppPurchase(isolate));
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
// static
|
|
|
|
void InAppPurchase::BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "InAppPurchase"));
|
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
|
|
|
.SetMethod("canMakePayments", &in_app_purchase::CanMakePayments)
|
|
|
|
.SetMethod("getReceiptURL", &in_app_purchase::GetReceiptURL)
|
2018-04-05 06:33:13 +00:00
|
|
|
.SetMethod("purchaseProduct", &InAppPurchase::PurchaseProduct)
|
|
|
|
.SetMethod("finishAllTransactions",
|
|
|
|
&in_app_purchase::FinishAllTransactions)
|
|
|
|
.SetMethod("finishTransactionByDate",
|
|
|
|
&in_app_purchase::FinishTransactionByDate)
|
|
|
|
.SetMethod("getProducts", &in_app_purchase::GetProducts);
|
2018-01-10 07:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InAppPurchase::InAppPurchase(v8::Isolate* isolate) {
|
|
|
|
Init(isolate);
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
InAppPurchase::~InAppPurchase() {}
|
2018-01-10 07:37:05 +00:00
|
|
|
|
|
|
|
void InAppPurchase::PurchaseProduct(const std::string& product_id,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
int quantity = 1;
|
|
|
|
in_app_purchase::InAppPurchaseCallback callback;
|
|
|
|
args->GetNext(&quantity);
|
|
|
|
args->GetNext(&callback);
|
|
|
|
in_app_purchase::PurchaseProduct(product_id, quantity, callback);
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
2018-01-10 07:37:05 +00:00
|
|
|
|
2018-01-10 08:18:23 +00:00
|
|
|
void InAppPurchase::OnTransactionsUpdated(
|
|
|
|
const std::vector<in_app_purchase::Transaction>& transactions) {
|
|
|
|
Emit("transactions-updated", transactions);
|
2018-01-10 07:55:49 +00:00
|
|
|
}
|
2018-01-10 08:39:16 +00:00
|
|
|
#endif
|
2018-01-10 07:55:49 +00:00
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
using atom::api::InAppPurchase;
|
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
|
|
|
#if defined(OS_MACOSX)
|
2018-01-10 07:37:05 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("inAppPurchase", InAppPurchase::Create(isolate));
|
2019-01-09 19:17:05 +00:00
|
|
|
dict.Set("InAppPurchase", InAppPurchase::GetConstructor(isolate)
|
|
|
|
->GetFunction(context)
|
|
|
|
.ToLocalChecked());
|
2017-11-30 13:27:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-31 07:09:13 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_in_app_purchase, Initialize)
|