This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
purchase.android.ts
178 lines (150 loc) · 6.57 KB
/
purchase.android.ts
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
/*! *****************************************************************************
Copyright (c) 2019 Tangra Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************** */
import * as common from "./purchase-common";
import * as application from "application";
import * as types from "utils/types";
import { Product } from "nativescript-purchase/product";
import { Transaction, TransactionState } from "nativescript-purchase/transaction";
export * from "./purchase-common";
let helper: com.tangrainc.inappbilling.InAppBillingHelper;
let currentBuyPayload: string;
let currentBuyProductIdentifier: string;
export function init(productIdentifiers: Array<string>): Promise<any> {
return new Promise((resolve, reject) => {
const nativeArray = Array.create(java.lang.String, productIdentifiers.length);
for (let loop = 0; loop < productIdentifiers.length; loop++) {
nativeArray[loop] = productIdentifiers[loop].toLowerCase(); // Android product IDs are all lower case
}
ensureApplicationContext().then(() => {
helper = new com.tangrainc.inappbilling.InAppBillingHelper(application.android.context, nativeArray);
resolve();
});
application.android.on(application.AndroidApplication.activityResultEvent, (args: application.AndroidActivityResultEventData) => {
if (args.requestCode === com.tangrainc.inappbilling.InAppBillingHelper.BUY_INTENT_REQUEST_CODE) {
const intent = args.intent as android.content.Intent;
let tran: Transaction;
if (intent) {
const responseCode = intent.getIntExtra("RESPONSE_CODE", 0);
const purchaseData = intent.getStringExtra("INAPP_PURCHASE_DATA");
const dataSignature = intent.getStringExtra("INAPP_DATA_SIGNATURE");
if (args.resultCode === android.app.Activity.RESULT_OK && responseCode === 0 && !types.isNullOrUndefined(purchaseData)) {
const nativeValue = new org.json.JSONObject(purchaseData);
nativeValue.put("signature", dataSignature);
tran = new Transaction(nativeValue);
}
else {
tran = getFailedTransaction();
}
}
else {
tran = getFailedTransaction();
}
common._notify(common.transactionUpdatedEvent, tran);
}
});
});
}
export function getProducts(): Promise<Array<Product>> {
return new Promise<Array<Product>>((resolve, reject) => {
Promise.all([
futureToPromise(helper.getProducts("inapp")),
futureToPromise(helper.getProducts("subs")),
])
.then((result: Array<Array<org.json.JSONObject>>) => {
const productArray: Array<Product> = [];
for (let type = 0; type <= 1; type++) {
for (const item of result[type]) {
productArray.push(new Product(item, (type === 0 ? "inapp" : "subs")));
}
}
resolve(productArray);
})
.catch(reject);
});
}
export function buyProduct(product: Product, developerPayload?: string) {
const tran = new Transaction(null);
tran.transactionState = TransactionState.Purchasing;
tran.productIdentifier = product.productIdentifier;
tran.developerPayload = developerPayload;
common._notify(common.transactionUpdatedEvent, tran);
currentBuyProductIdentifier = product.productIdentifier;
currentBuyPayload = developerPayload;
helper.startBuyIntent(application.android.foregroundActivity,
product.productIdentifier,
product.productType,
developerPayload || "");
}
export function consumePurchase(token: string): Promise<number> {
return new Promise<number>((resolve, reject) => {
futureToPromise(helper.consumePurchase(token)).then(resolve, reject);
});
}
export function restorePurchases(): Promise<void> {
return Promise.all([
futureToPromise(helper.getPurchases(null, "inapp")),
futureToPromise(helper.getPurchases(null, "subs"))
]).then((result: Array<Array<org.json.JSONObject>>) => {
for (let type = 0; type <= 1; type++) {
for (const item of result[type]) {
const tran = new Transaction(null);
tran.originalTransaction = new Transaction(item);
tran.transactionState = TransactionState.Restored;
common._notify(common.transactionUpdatedEvent, tran);
}
}
});
}
export function canMakePayments(): boolean {
return true;
}
export function getStoreReceipt(): string {
return null;
}
export function refreshStoreReceipt(): Promise<void> {
return Promise.resolve();
}
function getFailedTransaction() {
const tran = new Transaction(null);
tran.transactionState = TransactionState.Failed;
tran.productIdentifier = currentBuyProductIdentifier;
tran.developerPayload = currentBuyPayload;
return tran;
}
function futureToPromise(future: any /* ListenableFuture */): Promise<any> {
return new Promise((resolve, reject) => {
com.google.common.util.concurrent.Futures.addCallback(
future,
new com.google.common.util.concurrent.FutureCallback({
onSuccess: (result) => {
resolve(result);
}
, onFailure: (t /* Throwable */) => {
reject(new Error(t.getMessage()));
}
}),
com.google.common.util.concurrent.MoreExecutors.directExecutor(),
);
});
}
function ensureApplicationContext(): Promise<any> {
return new Promise<any>((resolve, reject) => {
if (application.android && application.android.context) {
resolve();
return;
}
application.on(application.launchEvent, (args: application.LaunchEventData) => {
resolve();
});
});
}