-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchecksumService.ts
More file actions
94 lines (86 loc) · 2.2 KB
/
checksumService.ts
File metadata and controls
94 lines (86 loc) · 2.2 KB
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
import { PlanProlongationPayload } from '@hawk.so/types';
import jwt, { Secret } from 'jsonwebtoken';
export type ChecksumData = PlanPurchaseChecksumData | CardLinkChecksumData;
interface PlanPurchaseChecksumData {
/**
* Workspace Identifier
*/
workspaceId: string;
/**
* Id of the user making the payment
*/
userId: string;
/**
* Workspace current plan id or plan id to change
*/
tariffPlanId: string;
/**
* If true, we will save user card
*/
shouldSaveCard: boolean;
/**
* Next payment date
*/
nextPaymentDate: string;
}
interface CardLinkChecksumData {
/**
* Workspace Identifier
*/
workspaceId: string;
/**
* Id of the user making the payment
*/
userId: string;
/**
* True if this is card linking operation – charging minimal amount of money to validate card info
*/
isCardLinkOperation: boolean;
/**
* Next payment date
*/
nextPaymentDate: string;
}
/**
* Helper class for working with checksums
*/
class ChecksumService {
/**
* Generates checksum for processing billing requests
*
* @param data - data for processing billing request
*/
public async generateChecksum(data: ChecksumData): Promise<string> {
return jwt.sign(
data,
process.env.JWT_SECRET_BILLING_CHECKSUM as Secret,
{ expiresIn: '30m' }
);
}
/**
* Parses checksum from request data and returns data from it
*
* @param checksum - checksum to parse
*/
public parseAndVerifyChecksum(checksum: string): ChecksumData {
const payload = jwt.verify(checksum, process.env.JWT_SECRET_BILLING_CHECKSUM as Secret) as ChecksumData;
if ('isCardLinkOperation' in payload) {
return {
workspaceId: payload.workspaceId,
userId: payload.userId,
isCardLinkOperation: payload.isCardLinkOperation,
nextPaymentDate: payload.nextPaymentDate,
};
} else {
return {
workspaceId: payload.workspaceId,
userId: payload.userId,
tariffPlanId: payload.tariffPlanId,
shouldSaveCard: payload.shouldSaveCard,
nextPaymentDate: payload.nextPaymentDate,
};
}
}
}
const checksumService = new ChecksumService();
export default checksumService;