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/electron_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"
|
2020-07-23 21:55:41 +00:00
|
|
|
#include "shell/common/gin_helper/promise.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
|
|
|
|
2022-01-24 16:55:18 +00:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::PaymentDiscount> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::PaymentDiscount& paymentDiscount) {
|
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
|
|
|
dict.Set("identifier", paymentDiscount.identifier);
|
|
|
|
dict.Set("keyIdentifier", paymentDiscount.keyIdentifier);
|
|
|
|
dict.Set("nonce", paymentDiscount.nonce);
|
|
|
|
dict.Set("signature", paymentDiscount.signature);
|
|
|
|
dict.Set("timestamp", paymentDiscount.timestamp);
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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);
|
2022-01-24 16:55:18 +00:00
|
|
|
dict.Set("applicationUsername", payment.applicationUsername);
|
|
|
|
if (payment.paymentDiscount.has_value()) {
|
|
|
|
dict.Set("paymentDiscount", payment.paymentDiscount.value());
|
|
|
|
}
|
2018-01-10 07:37:05 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-24 16:55:18 +00:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::ProductSubscriptionPeriod> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::ProductSubscriptionPeriod&
|
|
|
|
productSubscriptionPeriod) {
|
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
|
|
|
dict.Set("numberOfUnits", productSubscriptionPeriod.numberOfUnits);
|
|
|
|
dict.Set("unit", productSubscriptionPeriod.unit);
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::ProductDiscount> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::ProductDiscount& productDiscount) {
|
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.SetHidden("simple", true);
|
|
|
|
dict.Set("identifier", productDiscount.identifier);
|
|
|
|
dict.Set("type", productDiscount.type);
|
|
|
|
dict.Set("price", productDiscount.price);
|
|
|
|
dict.Set("priceLocale", productDiscount.priceLocale);
|
|
|
|
dict.Set("paymentMode", productDiscount.paymentMode);
|
|
|
|
dict.Set("numberOfPeriods", productDiscount.numberOfPeriods);
|
|
|
|
if (productDiscount.subscriptionPeriod.has_value()) {
|
|
|
|
dict.Set("subscriptionPeriod",
|
|
|
|
productDiscount.subscriptionPeriod.value());
|
|
|
|
}
|
|
|
|
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);
|
2022-01-24 16:55:18 +00:00
|
|
|
dict.Set("contentVersion", val.contentVersion);
|
2018-04-05 06:33:13 +00:00
|
|
|
dict.Set("contentLengths", val.contentLengths);
|
|
|
|
|
|
|
|
// Pricing Information
|
|
|
|
dict.Set("price", val.price);
|
|
|
|
dict.Set("formattedPrice", val.formattedPrice);
|
2020-08-21 17:41:40 +00:00
|
|
|
dict.Set("currencyCode", val.currencyCode);
|
2022-01-24 16:55:18 +00:00
|
|
|
if (val.introductoryPrice.has_value()) {
|
|
|
|
dict.Set("introductoryPrice", val.introductoryPrice.value());
|
|
|
|
}
|
|
|
|
dict.Set("discounts", val.discounts);
|
|
|
|
dict.Set("subscriptionGroupIdentifier", val.subscriptionGroupIdentifier);
|
|
|
|
if (val.subscriptionPeriod.has_value()) {
|
|
|
|
dict.Set("subscriptionPeriod", val.subscriptionPeriod.value());
|
|
|
|
}
|
2018-04-05 06:33:13 +00:00
|
|
|
// Downloadable Content Information
|
2019-06-12 17:54:14 +00:00
|
|
|
dict.Set("isDownloadable", val.isDownloadable);
|
2022-01-24 16:55:18 +00:00
|
|
|
dict.Set("downloadContentVersion", val.downloadContentVersion);
|
|
|
|
dict.Set("downloadContentLengths", val.downloadContentLengths);
|
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
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2020-07-23 21:55:41 +00:00
|
|
|
gin::WrapperInfo InAppPurchase::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
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) {
|
2020-07-23 21:55:41 +00:00
|
|
|
return gin::CreateHandle(isolate, new InAppPurchase());
|
2017-11-30 13:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 21:55:41 +00:00
|
|
|
gin::ObjectTemplateBuilder InAppPurchase::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin_helper::EventEmitterMixin<InAppPurchase>::GetObjectTemplateBuilder(
|
|
|
|
isolate)
|
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
|
|
|
}
|
|
|
|
|
2020-07-23 21:55:41 +00:00
|
|
|
const char* InAppPurchase::GetTypeName() {
|
|
|
|
return "InAppPurchase";
|
2018-01-10 07:37:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 04:16:13 +00:00
|
|
|
InAppPurchase::InAppPurchase() = default;
|
2020-07-23 21:55:41 +00:00
|
|
|
|
2021-06-04 04:16:13 +00:00
|
|
|
InAppPurchase::~InAppPurchase() = default;
|
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
|
|
|
|
|
|
|
|
} // namespace electron
|
2017-11-30 13:27:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-01-10 07:37:05 +00:00
|
|
|
using electron::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) {
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
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));
|
2017-11-30 13:27:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_in_app_purchase, Initialize)
|