forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment-token.js
47 lines (38 loc) · 1.03 KB
/
payment-token.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
var base64url = require('base64url');
var MsPayEmulatedStripeTokenSource = 'tok_18yWDMKVgMv7trmwyE21VqO';
function parse(tokenString) {
if (!tokenString) {
throw new Error('PaymentToken string expected');
}
var tokenParts = tokenString.split('.');
if (tokenParts.length !== 3) {
throw new Error('Invalid PaymentToken');
}
var header = parseHeader(tokenParts[0]);
var source = parseSource(tokenParts[1]);
var signature = parseSignature(tokenParts[2]);
return {
header,
source,
signature,
isEmulated: MsPayEmulatedStripeTokenSource === source
};
}
function parseHeader(headerString) {
var json = base64url.decode(headerString);
return JSON.parse(json);
}
function parseSource(sourceString) {
return base64url.decode(sourceString);
}
function parseSignature(signatureString) {
return base64url.toBuffer(signatureString);
}
module.exports = {
parse: parse,
tokenFormat: {
Invalid: 0,
Error: 1,
Stripe: 2
}
};