-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·118 lines (109 loc) · 5.24 KB
/
index.js
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
var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
const exec = require('child-process-promise').exec;
const gpk = "xxxxxxxxxxxxx";
const iap = require('in-app-purchase');
const iosConfig = {
applePassword: "xxxxxxxxxxxxxxx"
}
const androidConfig = {
googlePublicKeyStrSandbox: gpk,
googlePublicKeyStrLive: gpk,
googleAccToken: "xxxxxxxxxxxxxxxxxxx",
googleRefToken: "xxxxxxxxxxxxxxxxxx",
googleClientID: "xxxxxxxxxxxxxxxxxxx",
googleClientSecret: "xxxxxxxxxxxxxxxxx"
}
iap.config(androidConfig);
const cors = require('cors')({ origin: true });
exports.validateReceipt = functions.https.onRequest((req, res) => {
let uid = req.query.uid;
let appStoreReceipt = req.query.receipt;
let transRef = admin.database().ref('users/' + uid + '/userPaidData/transaction');
let paidRef = admin.database().ref('users/' + uid + '/userPaidData/PaidUser');
let overridePaidRef = admin.database().ref('users/' + uid + '/userPaidData/overridePaid');
cors(req, res, () => {
overridePaidRef.once("value", function (overrideValue) {
if (overrideValue.val() && overrideValue.val() == true) {
paidRef.set(true);
console.log('valid');
res.status(200).send('valid');
} else {
transRef.once("value", function (data) {
if (data.val()) {
console.log(data.val());
let transaction = data.val();
let receipt;
switch (transaction.type) {
case "android-playstore":
receipt = {
"data": JSON.stringify(transaction.receipt),
"signature": transaction.signature
}
iap.config(androidConfig);
break;
case "ios-appstore":
receipt = transaction.appStoreReceipt;// transactionReceipt
iap.config(iosConfig);
break;
}
if (!receipt) {
paidRef.set(false);
console.log('no receipt');
res.status(200).send('no receipt');
}
iap.setup(function (error) {
if (error) {
// oops
console.log(error);
}
iap.validate(receipt, function (err, valRes) {
if (err) {
if (err == "Error: This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.") {
paidRef.set(false);
console.log('apple expired');
res.status(200).send('expired');
return;
}
res.status(500).send(JSON.stringify(err));
return console.error(err);
}
if (iap.isValidated(valRes)) {
let options = {
ignoreExpired: true
}
var purchaseDataList = iap.getPurchaseData(valRes, options);
console.log(purchaseDataList);
if (purchaseDataList.length == 0) {//all items expired
paidRef.set(false);
console.log('expired, not valid items');
res.status(200).send('expired');
} else {
paidRef.set(true);
console.log('valid');
res.status(200).send('valid');
}
} else {
paidRef.set(false);
console.log('invalid');
res.status(200).send('invalid');
}
});
})
} else {
paidRef.set(false);
console.log('expired');
res.status(200).send('expired');
}
});
}
});
});
});