2017-11-30 06:27:33 -07: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.
|
|
|
|
|
2020-02-04 13:19:40 -07:00
|
|
|
#include "shell/browser/api/electron_api_in_app_purchase.h"
|
2018-01-09 22:45:42 -07:00
|
|
|
|
2017-11-30 06:27:33 -07:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-09-08 09:10:18 -06:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-10-21 01:05:40 -06:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2020-07-23 15:55:41 -06:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/common/node_includes.h"
|
2017-11-30 06:27:33 -07:00
|
|
|
|
2019-10-21 01:05:40 -06:00
|
|
|
namespace gin {
|
2017-11-30 06:27:33 -07:00
|
|
|
|
2022-01-24 09:55:18 -07:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::PaymentDiscount> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::PaymentDiscount& paymentDiscount) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2022-01-24 09:55:18 -07:00
|
|
|
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 00:37:05 -07:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::Payment> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::Payment& payment) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2018-01-10 00:37:05 -07:00
|
|
|
dict.Set("productIdentifier", payment.productIdentifier);
|
|
|
|
dict.Set("quantity", payment.quantity);
|
2022-01-24 09:55:18 -07:00
|
|
|
dict.Set("applicationUsername", payment.applicationUsername);
|
|
|
|
if (payment.paymentDiscount.has_value()) {
|
|
|
|
dict.Set("paymentDiscount", payment.paymentDiscount.value());
|
|
|
|
}
|
2018-01-10 00:37:05 -07: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) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2018-01-10 00:37:05 -07: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 01:06:27 -07:00
|
|
|
dict.Set("payment", val.payment);
|
2018-01-10 00:37:05 -07:00
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-24 09:55:18 -07:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::ProductSubscriptionPeriod> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::ProductSubscriptionPeriod&
|
|
|
|
productSubscriptionPeriod) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2022-01-24 09:55:18 -07:00
|
|
|
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) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2022-01-24 09:55:18 -07:00
|
|
|
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 00:33:13 -06:00
|
|
|
template <>
|
|
|
|
struct Converter<in_app_purchase::Product> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const in_app_purchase::Product& val) {
|
2023-08-20 19:43:41 -06:00
|
|
|
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
|
2018-04-05 00:33:13 -06:00
|
|
|
dict.Set("productIdentifier", val.productIdentifier);
|
|
|
|
dict.Set("localizedDescription", val.localizedDescription);
|
|
|
|
dict.Set("localizedTitle", val.localizedTitle);
|
|
|
|
|
|
|
|
// Pricing Information
|
|
|
|
dict.Set("price", val.price);
|
|
|
|
dict.Set("formattedPrice", val.formattedPrice);
|
2020-08-21 11:41:40 -06:00
|
|
|
dict.Set("currencyCode", val.currencyCode);
|
2022-01-24 09:55:18 -07: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 00:33:13 -06:00
|
|
|
// Downloadable Content Information
|
2019-06-12 11:54:14 -06:00
|
|
|
dict.Set("isDownloadable", val.isDownloadable);
|
2022-01-24 09:55:18 -07:00
|
|
|
dict.Set("downloadContentVersion", val.downloadContentVersion);
|
|
|
|
dict.Set("downloadContentLengths", val.downloadContentLengths);
|
2018-04-05 00:33:13 -06:00
|
|
|
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-21 01:05:40 -06:00
|
|
|
} // namespace gin
|
2018-01-10 00:37:05 -07:00
|
|
|
|
2022-06-29 13:55:47 -06:00
|
|
|
namespace electron::api {
|
2018-01-10 00:37:05 -07:00
|
|
|
|
2020-07-23 15:55:41 -06:00
|
|
|
gin::WrapperInfo InAppPurchase::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2018-01-10 00:37:05 -07:00
|
|
|
// static
|
2019-10-21 01:05:40 -06:00
|
|
|
gin::Handle<InAppPurchase> InAppPurchase::Create(v8::Isolate* isolate) {
|
2020-07-23 15:55:41 -06:00
|
|
|
return gin::CreateHandle(isolate, new InAppPurchase());
|
2017-11-30 06:27:33 -07:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:55:41 -06:00
|
|
|
gin::ObjectTemplateBuilder InAppPurchase::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin_helper::EventEmitterMixin<InAppPurchase>::GetObjectTemplateBuilder(
|
|
|
|
isolate)
|
2018-01-10 00:37:05 -07:00
|
|
|
.SetMethod("canMakePayments", &in_app_purchase::CanMakePayments)
|
2019-12-17 08:07:11 -07:00
|
|
|
.SetMethod("restoreCompletedTransactions",
|
|
|
|
&in_app_purchase::RestoreCompletedTransactions)
|
2018-01-10 00:37:05 -07:00
|
|
|
.SetMethod("getReceiptURL", &in_app_purchase::GetReceiptURL)
|
2018-04-05 00:33:13 -06:00
|
|
|
.SetMethod("purchaseProduct", &InAppPurchase::PurchaseProduct)
|
|
|
|
.SetMethod("finishAllTransactions",
|
|
|
|
&in_app_purchase::FinishAllTransactions)
|
|
|
|
.SetMethod("finishTransactionByDate",
|
|
|
|
&in_app_purchase::FinishTransactionByDate)
|
2019-03-13 14:56:01 -06:00
|
|
|
.SetMethod("getProducts", &InAppPurchase::GetProducts);
|
2018-01-10 00:37:05 -07:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:55:41 -06:00
|
|
|
const char* InAppPurchase::GetTypeName() {
|
|
|
|
return "InAppPurchase";
|
2018-01-10 00:37:05 -07:00
|
|
|
}
|
|
|
|
|
2021-06-03 22:16:13 -06:00
|
|
|
InAppPurchase::InAppPurchase() = default;
|
2020-07-23 15:55:41 -06:00
|
|
|
|
2021-06-03 22:16:13 -06:00
|
|
|
InAppPurchase::~InAppPurchase() = default;
|
2018-01-10 00:37:05 -07:00
|
|
|
|
2019-03-13 14:56:01 -06:00
|
|
|
v8::Local<v8::Promise> InAppPurchase::PurchaseProduct(
|
|
|
|
const std::string& product_id,
|
2019-10-21 01:05:40 -06:00
|
|
|
gin::Arguments* args) {
|
2019-03-13 14:56:01 -06:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 00:10:32 -06:00
|
|
|
gin_helper::Promise<bool> promise(isolate);
|
2019-03-13 14:56:01 -06:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2018-01-10 00:37:05 -07:00
|
|
|
int quantity = 1;
|
|
|
|
args->GetNext(&quantity);
|
2022-12-12 11:11:48 -07:00
|
|
|
std::string username = "";
|
|
|
|
args->GetNext(&username);
|
2019-03-13 14:56:01 -06:00
|
|
|
|
|
|
|
in_app_purchase::PurchaseProduct(
|
2022-12-12 11:11:48 -07:00
|
|
|
product_id, quantity, username,
|
2019-11-01 00:10:32 -06:00
|
|
|
base::BindOnce(gin_helper::Promise<bool>::ResolvePromise,
|
|
|
|
std::move(promise)));
|
2019-03-13 14:56:01 -06:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> InAppPurchase::GetProducts(
|
|
|
|
const std::vector<std::string>& productIDs,
|
2019-10-21 01:05:40 -06:00
|
|
|
gin::Arguments* args) {
|
2019-03-13 14:56:01 -06:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 00:10:32 -06:00
|
|
|
gin_helper::Promise<std::vector<in_app_purchase::Product>> promise(isolate);
|
2019-03-13 14:56:01 -06:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
in_app_purchase::GetProducts(
|
2019-08-22 18:03:28 -06:00
|
|
|
productIDs,
|
2019-11-01 00:10:32 -06:00
|
|
|
base::BindOnce(gin_helper::Promise<
|
|
|
|
std::vector<in_app_purchase::Product>>::ResolvePromise,
|
|
|
|
std::move(promise)));
|
2019-03-13 14:56:01 -06:00
|
|
|
|
|
|
|
return handle;
|
2017-11-30 06:27:33 -07:00
|
|
|
}
|
2018-01-10 00:37:05 -07:00
|
|
|
|
2018-01-10 01:18:23 -07:00
|
|
|
void InAppPurchase::OnTransactionsUpdated(
|
|
|
|
const std::vector<in_app_purchase::Transaction>& transactions) {
|
|
|
|
Emit("transactions-updated", transactions);
|
2018-01-10 00:55:49 -07:00
|
|
|
}
|
2018-01-10 01:39:16 -07:00
|
|
|
#endif
|
2018-01-10 00:55:49 -07:00
|
|
|
|
2022-06-29 13:55:47 -06:00
|
|
|
} // namespace electron::api
|
2017-11-30 06:27:33 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
using electron::api::InAppPurchase;
|
2018-01-10 00:37:05 -07:00
|
|
|
|
2017-11-30 06:27:33 -07:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2018-01-10 00:37:05 -07:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-21 01:05:40 -06:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2018-01-10 00:37:05 -07:00
|
|
|
dict.Set("inAppPurchase", InAppPurchase::Create(isolate));
|
2017-11-30 06:27:33 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-02-08 18:31:38 -07:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_in_app_purchase, Initialize)
|