forked from jeremybarbet/iap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
133 lines (116 loc) · 3.38 KB
/
index.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
import {
type AppleVerifyResponse,
type GoogleVerifyResponse,
AppleEnvironment,
verifyAppleReceipt,
verifyGoogleReceipt,
} from '../index';
import { loadDataFile } from './load-data-file';
const { apple, google } = loadDataFile();
const responseLog = (name: string, response: AppleVerifyResponse | GoogleVerifyResponse) => {
console.log(name);
console.log('├ valid: ', response.valid);
console.log('├ status: ', response.status);
console.log('├ message: ', response.message);
console.log('└ data: ', response.data);
console.log('\n');
};
const errorLog = (name: string, error: unknown) => {
console.log(name);
console.log('└ error', error);
console.log('\n');
};
const appleTests = async () => {
if (apple.credentials.password === '' || apple.payload.transactionReceipt === '') {
throw new Error('Missing credentials and/or payload');
}
try {
const response = await verifyAppleReceipt(
{
transactionReceipt: apple.payload.transactionReceipt,
},
{
environment: AppleEnvironment.SANDBOX,
password: apple.credentials.password,
},
);
responseLog('verifyAppleReceipt', response);
} catch (error) {
errorLog('verifyAppleReceipt', error);
}
};
const googleTests = async () => {
if (
google.credentials.clientEmail === '' ||
google.credentials.privateKey === '' ||
google.payload.packageNameAndroid === '' ||
google.payload.productId === '' ||
google.payload.purchaseToken === ''
) {
throw new Error('Missing credentials and/or payload');
}
try {
const response = await verifyGoogleReceipt(
{
packageName: google.payload.packageNameAndroid,
token: google.payload.purchaseToken,
productId: google.payload.productId,
},
{
clientEmail: google.credentials.clientEmail,
privateKey: google.credentials.privateKey,
},
);
responseLog('verifyGoogleReceipt: `acknowledge: false``', response);
} catch (error) {
errorLog('verifyGoogleReceipt: `acknowledge: false``', error);
}
try {
const response = await verifyGoogleReceipt(
{
packageName: google.payload.packageNameAndroid,
token: google.payload.purchaseToken,
productId: google.payload.productId,
acknowledge: true,
},
{
clientEmail: google.credentials.clientEmail,
privateKey: google.credentials.privateKey,
},
);
responseLog('verifyGoogleReceipt: `acknowledge: true`', response);
} catch (error) {
errorLog('verifyGoogleReceipt: `acknowledge: true`', error);
}
try {
const response = await verifyGoogleReceipt(
{
packageName: google.payload.packageNameAndroid,
token: google.payload.purchaseToken,
productId: google.payload.productId,
acknowledge: true,
fetchResource: true,
},
{
clientEmail: google.credentials.clientEmail,
privateKey: google.credentials.privateKey,
},
);
responseLog('verifyGoogleReceipt: `acknowledge: true`, `fetchResource: true`', response);
} catch (error) {
errorLog('verifyGoogleReceipt: `acknowledge: true`, `fetchResource: true`', error);
}
};
const main = async () => {
try {
await appleTests();
} catch (error) {
console.error(error);
}
try {
await googleTests();
} catch (error) {
console.error(error);
}
};
void main();