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/api/atom_api_in_app_purchase.h"
|
2018-01-10 05:45:42 +00:00
|
|
|
|
2017-11-30 13:27:33 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-09-08 15:10:18 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2017-11-30 13:27:33 +00:00
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
namespace gin {
|
2017-11-30 13:27:33 +00:00
|
|
|
|
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) {
|
2019-10-21 07:05:40 +00:00
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
2018-01-10 07:37:05 +00:00
|
|
|
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) {
|
2019-10-21 07:05:40 +00:00
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
2018-01-10 07:37:05 +00:00
|
|
|
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) {
|
2019-10-21 07:05:40 +00:00
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
2018-04-05 06:33:13 +00:00
|
|
|
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
|
2019-06-12 17:54:14 +00:00
|
|
|
dict.Set("isDownloadable", val.isDownloadable);
|
2018-04-05 06:33:13 +00:00
|
|
|
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
} // namespace gin
|
2018-01-10 07:37:05 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-01-10 07:37:05 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2018-01-10 08:39:16 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-01-10 07:37:05 +00:00
|
|
|
// static
|
2019-10-21 07:05:40 +00:00
|
|
|
gin::Handle<InAppPurchase> InAppPurchase::Create(v8::Isolate* isolate) {
|
|
|
|
return gin::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) {
|
2019-10-21 07:05:40 +00:00
|
|
|
prototype->SetClassName(gin::StringToV8(isolate, "InAppPurchase"));
|
|
|
|
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2018-01-10 07:37:05 +00:00
|
|
|
.SetMethod("canMakePayments", &in_app_purchase::CanMakePayments)
|
2019-12-17 15:07:11 +00:00
|
|
|
.SetMethod("restoreCompletedTransactions",
|
|
|
|
&in_app_purchase::RestoreCompletedTransactions)
|
2018-01-10 07:37:05 +00:00
|
|
|
.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)
|
2019-03-13 20:56:01 +00:00
|
|
|
.SetMethod("getProducts", &InAppPurchase::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
|
|
|
|
2019-03-13 20:56:01 +00:00
|
|
|
v8::Local<v8::Promise> InAppPurchase::PurchaseProduct(
|
|
|
|
const std::string& product_id,
|
2019-10-21 07:05:40 +00:00
|
|
|
gin::Arguments* args) {
|
2019-03-13 20:56:01 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<bool> promise(isolate);
|
2019-03-13 20:56:01 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
int quantity = 1;
|
|
|
|
args->GetNext(&quantity);
|
2019-03-13 20:56:01 +00:00
|
|
|
|
|
|
|
in_app_purchase::PurchaseProduct(
|
|
|
|
product_id, quantity,
|
2019-11-01 06:10:32 +00:00
|
|
|
base::BindOnce(gin_helper::Promise<bool>::ResolvePromise,
|
|
|
|
std::move(promise)));
|
2019-03-13 20:56:01 +00:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> InAppPurchase::GetProducts(
|
|
|
|
const std::vector<std::string>& productIDs,
|
2019-10-21 07:05:40 +00:00
|
|
|
gin::Arguments* args) {
|
2019-03-13 20:56:01 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<std::vector<in_app_purchase::Product>> promise(isolate);
|
2019-03-13 20:56:01 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
in_app_purchase::GetProducts(
|
2019-08-23 00:03:28 +00:00
|
|
|
productIDs,
|
2019-11-01 06:10:32 +00:00
|
|
|
base::BindOnce(gin_helper::Promise<
|
|
|
|
std::vector<in_app_purchase::Product>>::ResolvePromise,
|
|
|
|
std::move(promise)));
|
2019-03-13 20:56:01 +00:00
|
|
|
|
|
|
|
return handle;
|
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
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
using electron::api::InAppPurchase;
|
2018-01-10 07:37:05 +00:00
|
|
|
|
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();
|
2019-10-21 07:05:40 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2018-01-10 07:37:05 +00:00
|
|
|
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
|
|
|
|
|
2019-03-08 18:29:52 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_in_app_purchase, Initialize)
|