This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
electron_api_in_app_purchase.cc
233 lines (199 loc) · 8.02 KB
/
electron_api_in_app_purchase.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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.
#include "shell/browser/api/electron_api_in_app_purchase.h"
#include <string>
#include <utility>
#include <vector>
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/node_includes.h"
namespace gin {
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();
}
};
template <>
struct Converter<in_app_purchase::Payment> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const in_app_purchase::Payment& payment) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("productIdentifier", payment.productIdentifier);
dict.Set("quantity", payment.quantity);
dict.Set("applicationUsername", payment.applicationUsername);
if (payment.paymentDiscount.has_value()) {
dict.Set("paymentDiscount", payment.paymentDiscount.value());
}
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) {
gin_helper::Dictionary dict = gin::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);
dict.Set("payment", val.payment);
return dict.GetHandle();
}
};
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();
}
};
template <>
struct Converter<in_app_purchase::Product> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const in_app_purchase::Product& val) {
gin_helper::Dictionary dict = gin::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.contentVersion);
dict.Set("contentLengths", val.contentLengths);
// Pricing Information
dict.Set("price", val.price);
dict.Set("formattedPrice", val.formattedPrice);
dict.Set("currencyCode", val.currencyCode);
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());
}
// Downloadable Content Information
dict.Set("isDownloadable", val.isDownloadable);
dict.Set("downloadContentVersion", val.downloadContentVersion);
dict.Set("downloadContentLengths", val.downloadContentLengths);
return dict.GetHandle();
}
};
} // namespace gin
namespace electron::api {
gin::WrapperInfo InAppPurchase::kWrapperInfo = {gin::kEmbedderNativeGin};
#if BUILDFLAG(IS_MAC)
// static
gin::Handle<InAppPurchase> InAppPurchase::Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new InAppPurchase());
}
gin::ObjectTemplateBuilder InAppPurchase::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<InAppPurchase>::GetObjectTemplateBuilder(
isolate)
.SetMethod("canMakePayments", &in_app_purchase::CanMakePayments)
.SetMethod("restoreCompletedTransactions",
&in_app_purchase::RestoreCompletedTransactions)
.SetMethod("getReceiptURL", &in_app_purchase::GetReceiptURL)
.SetMethod("purchaseProduct", &InAppPurchase::PurchaseProduct)
.SetMethod("finishAllTransactions",
&in_app_purchase::FinishAllTransactions)
.SetMethod("finishTransactionByDate",
&in_app_purchase::FinishTransactionByDate)
.SetMethod("getProducts", &InAppPurchase::GetProducts);
}
const char* InAppPurchase::GetTypeName() {
return "InAppPurchase";
}
InAppPurchase::InAppPurchase() = default;
InAppPurchase::~InAppPurchase() = default;
v8::Local<v8::Promise> InAppPurchase::PurchaseProduct(
const std::string& product_id,
gin::Arguments* args) {
v8::Isolate* isolate = args->isolate();
gin_helper::Promise<bool> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
int quantity = 1;
args->GetNext(&quantity);
std::string username = "";
args->GetNext(&username);
in_app_purchase::PurchaseProduct(
product_id, quantity, username,
base::BindOnce(gin_helper::Promise<bool>::ResolvePromise,
std::move(promise)));
return handle;
}
v8::Local<v8::Promise> InAppPurchase::GetProducts(
const std::vector<std::string>& productIDs,
gin::Arguments* args) {
v8::Isolate* isolate = args->isolate();
gin_helper::Promise<std::vector<in_app_purchase::Product>> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
in_app_purchase::GetProducts(
productIDs,
base::BindOnce(gin_helper::Promise<
std::vector<in_app_purchase::Product>>::ResolvePromise,
std::move(promise)));
return handle;
}
void InAppPurchase::OnTransactionsUpdated(
const std::vector<in_app_purchase::Transaction>& transactions) {
Emit("transactions-updated", transactions);
}
#endif
} // namespace electron::api
namespace {
using electron::api::InAppPurchase;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
#if BUILDFLAG(IS_MAC)
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("inAppPurchase", InAppPurchase::Create(isolate));
#endif
}
} // namespace
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_in_app_purchase, Initialize)