@misterhomer1992/miit-bot-payment 1.1.7 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/dist/config/ConfigurationManager.d.ts +64 -0
  2. package/dist/config/ConfigurationManager.d.ts.map +1 -0
  3. package/dist/config/ConfigurationManager.js +144 -0
  4. package/dist/config/ConfigurationManager.js.map +1 -0
  5. package/dist/config/defaults.d.ts +18 -0
  6. package/dist/config/defaults.d.ts.map +1 -0
  7. package/dist/config/defaults.js +26 -0
  8. package/dist/config/defaults.js.map +1 -0
  9. package/dist/config/environment.d.ts +38 -0
  10. package/dist/config/environment.d.ts.map +1 -0
  11. package/dist/config/environment.js +91 -0
  12. package/dist/config/environment.js.map +1 -0
  13. package/dist/config/index.d.ts +5 -0
  14. package/dist/config/index.d.ts.map +1 -0
  15. package/dist/config/index.js +18 -0
  16. package/dist/config/index.js.map +1 -0
  17. package/dist/config/types.d.ts +53 -0
  18. package/dist/config/types.d.ts.map +1 -0
  19. package/dist/config/types.js +3 -0
  20. package/dist/config/types.js.map +1 -0
  21. package/dist/index.d.ts +21 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +23 -0
  24. package/dist/index.js.map +1 -1
  25. package/dist/modules/cache/InMemoryCache.d.ts +17 -0
  26. package/dist/modules/cache/InMemoryCache.d.ts.map +1 -0
  27. package/dist/modules/cache/InMemoryCache.js +77 -0
  28. package/dist/modules/cache/InMemoryCache.js.map +1 -0
  29. package/dist/modules/cache/index.d.ts +3 -0
  30. package/dist/modules/cache/index.d.ts.map +1 -0
  31. package/dist/modules/cache/index.js +19 -0
  32. package/dist/modules/cache/index.js.map +1 -0
  33. package/dist/modules/cache/types.d.ts +52 -0
  34. package/dist/modules/cache/types.d.ts.map +1 -0
  35. package/dist/modules/cache/types.js +3 -0
  36. package/dist/modules/cache/types.js.map +1 -0
  37. package/dist/modules/errors/index.d.ts +2 -0
  38. package/dist/modules/errors/index.d.ts.map +1 -0
  39. package/dist/modules/errors/index.js +19 -0
  40. package/dist/modules/errors/index.js.map +1 -0
  41. package/dist/modules/errors/types.d.ts +112 -0
  42. package/dist/modules/errors/types.d.ts.map +1 -0
  43. package/dist/modules/errors/types.js +174 -0
  44. package/dist/modules/errors/types.js.map +1 -0
  45. package/dist/modules/payments/api.d.ts +63 -1
  46. package/dist/modules/payments/api.d.ts.map +1 -1
  47. package/dist/modules/payments/api.js +103 -1
  48. package/dist/modules/payments/api.js.map +1 -1
  49. package/dist/modules/payments/const.d.ts.map +1 -1
  50. package/dist/modules/payments/const.js +1 -0
  51. package/dist/modules/payments/const.js.map +1 -1
  52. package/dist/modules/payments/index.d.ts +8 -0
  53. package/dist/modules/payments/index.d.ts.map +1 -1
  54. package/dist/modules/payments/index.js +8 -0
  55. package/dist/modules/payments/index.js.map +1 -1
  56. package/dist/modules/payments/service.d.ts +42 -2
  57. package/dist/modules/payments/service.d.ts.map +1 -1
  58. package/dist/modules/payments/service.js +132 -3
  59. package/dist/modules/payments/service.js.map +1 -1
  60. package/dist/modules/payments/subscription-check-webhook.handler.d.ts +85 -0
  61. package/dist/modules/payments/subscription-check-webhook.handler.d.ts.map +1 -0
  62. package/dist/modules/payments/subscription-check-webhook.handler.js +155 -0
  63. package/dist/modules/payments/subscription-check-webhook.handler.js.map +1 -0
  64. package/dist/modules/payments/subscription-check-webhook.service.d.ts +59 -0
  65. package/dist/modules/payments/subscription-check-webhook.service.d.ts.map +1 -0
  66. package/dist/modules/payments/subscription-check-webhook.service.js +330 -0
  67. package/dist/modules/payments/subscription-check-webhook.service.js.map +1 -0
  68. package/dist/modules/payments/subscription-check-webhook.types.d.ts +25 -0
  69. package/dist/modules/payments/subscription-check-webhook.types.d.ts.map +1 -0
  70. package/dist/modules/payments/subscription-check-webhook.types.js +3 -0
  71. package/dist/modules/payments/subscription-check-webhook.types.js.map +1 -0
  72. package/dist/modules/payments/types.d.ts +69 -2
  73. package/dist/modules/payments/types.d.ts.map +1 -1
  74. package/dist/modules/payments/utils.d.ts +151 -5
  75. package/dist/modules/payments/utils.d.ts.map +1 -1
  76. package/dist/modules/payments/utils.js +253 -9
  77. package/dist/modules/payments/utils.js.map +1 -1
  78. package/dist/modules/payments/wayforpay.service.d.ts +39 -0
  79. package/dist/modules/payments/wayforpay.service.d.ts.map +1 -0
  80. package/dist/modules/payments/wayforpay.service.js +217 -0
  81. package/dist/modules/payments/wayforpay.service.js.map +1 -0
  82. package/dist/modules/payments/wayforpay.types.d.ts +115 -0
  83. package/dist/modules/payments/wayforpay.types.d.ts.map +1 -0
  84. package/dist/modules/payments/wayforpay.types.js +3 -0
  85. package/dist/modules/payments/wayforpay.types.js.map +1 -0
  86. package/dist/modules/payments/webhook.handler.d.ts +98 -0
  87. package/dist/modules/payments/webhook.handler.d.ts.map +1 -0
  88. package/dist/modules/payments/webhook.handler.js +153 -0
  89. package/dist/modules/payments/webhook.handler.js.map +1 -0
  90. package/dist/modules/payments/webhook.service.d.ts +99 -0
  91. package/dist/modules/payments/webhook.service.d.ts.map +1 -0
  92. package/dist/modules/payments/webhook.service.js +672 -0
  93. package/dist/modules/payments/webhook.service.js.map +1 -0
  94. package/dist/modules/payments/webhook.types.d.ts +35 -0
  95. package/dist/modules/payments/webhook.types.d.ts.map +1 -0
  96. package/dist/modules/payments/webhook.types.js +3 -0
  97. package/dist/modules/payments/webhook.types.js.map +1 -0
  98. package/dist/modules/subscription/change.service.d.ts +80 -0
  99. package/dist/modules/subscription/change.service.d.ts.map +1 -0
  100. package/dist/modules/subscription/change.service.js +226 -0
  101. package/dist/modules/subscription/change.service.js.map +1 -0
  102. package/dist/modules/subscription/index.d.ts +2 -0
  103. package/dist/modules/subscription/index.d.ts.map +1 -1
  104. package/dist/modules/subscription/index.js +2 -0
  105. package/dist/modules/subscription/index.js.map +1 -1
  106. package/dist/modules/subscription/service.d.ts +8 -1
  107. package/dist/modules/subscription/service.d.ts.map +1 -1
  108. package/dist/modules/subscription/service.js +58 -1
  109. package/dist/modules/subscription/service.js.map +1 -1
  110. package/dist/modules/subscription/status-check.handler.d.ts +117 -0
  111. package/dist/modules/subscription/status-check.handler.d.ts.map +1 -0
  112. package/dist/modules/subscription/status-check.handler.js +164 -0
  113. package/dist/modules/subscription/status-check.handler.js.map +1 -0
  114. package/dist/modules/subscription/types.d.ts +37 -1
  115. package/dist/modules/subscription/types.d.ts.map +1 -1
  116. package/dist/modules/subscriptionPlan/const.d.ts +5 -0
  117. package/dist/modules/subscriptionPlan/const.d.ts.map +1 -0
  118. package/dist/modules/subscriptionPlan/const.js +106 -0
  119. package/dist/modules/subscriptionPlan/const.js.map +1 -0
  120. package/dist/modules/subscriptionPlan/index.d.ts +5 -0
  121. package/dist/modules/subscriptionPlan/index.d.ts.map +1 -0
  122. package/dist/modules/subscriptionPlan/index.js +21 -0
  123. package/dist/modules/subscriptionPlan/index.js.map +1 -0
  124. package/dist/modules/subscriptionPlan/repository.d.ts +22 -0
  125. package/dist/modules/subscriptionPlan/repository.d.ts.map +1 -0
  126. package/dist/modules/subscriptionPlan/repository.js +95 -0
  127. package/dist/modules/subscriptionPlan/repository.js.map +1 -0
  128. package/dist/modules/subscriptionPlan/service.d.ts +21 -0
  129. package/dist/modules/subscriptionPlan/service.d.ts.map +1 -0
  130. package/dist/modules/subscriptionPlan/service.js +128 -0
  131. package/dist/modules/subscriptionPlan/service.js.map +1 -0
  132. package/dist/modules/subscriptionPlan/types.d.ts +40 -0
  133. package/dist/modules/subscriptionPlan/types.d.ts.map +1 -0
  134. package/dist/modules/subscriptionPlan/types.js +3 -0
  135. package/dist/modules/subscriptionPlan/types.js.map +1 -0
  136. package/dist/modules/token/const.d.ts +7 -0
  137. package/dist/modules/token/const.d.ts.map +1 -0
  138. package/dist/modules/token/const.js +66 -0
  139. package/dist/modules/token/const.js.map +1 -0
  140. package/dist/modules/token/index.d.ts +4 -0
  141. package/dist/modules/token/index.d.ts.map +1 -0
  142. package/dist/modules/token/index.js +20 -0
  143. package/dist/modules/token/index.js.map +1 -0
  144. package/dist/modules/token/service.d.ts +46 -0
  145. package/dist/modules/token/service.d.ts.map +1 -0
  146. package/dist/modules/token/service.js +249 -0
  147. package/dist/modules/token/service.js.map +1 -0
  148. package/dist/modules/token/types.d.ts +109 -0
  149. package/dist/modules/token/types.d.ts.map +1 -0
  150. package/dist/modules/token/types.js +3 -0
  151. package/dist/modules/token/types.js.map +1 -0
  152. package/dist/modules/tokenPack/const.d.ts +4 -0
  153. package/dist/modules/tokenPack/const.d.ts.map +1 -0
  154. package/dist/modules/tokenPack/const.js +10 -0
  155. package/dist/modules/tokenPack/const.js.map +1 -0
  156. package/dist/modules/tokenPack/index.d.ts +5 -0
  157. package/dist/modules/tokenPack/index.d.ts.map +1 -0
  158. package/dist/modules/tokenPack/index.js +21 -0
  159. package/dist/modules/tokenPack/index.js.map +1 -0
  160. package/dist/modules/tokenPack/repository.d.ts +32 -0
  161. package/dist/modules/tokenPack/repository.d.ts.map +1 -0
  162. package/dist/modules/tokenPack/repository.js +103 -0
  163. package/dist/modules/tokenPack/repository.js.map +1 -0
  164. package/dist/modules/tokenPack/service.d.ts +28 -0
  165. package/dist/modules/tokenPack/service.d.ts.map +1 -0
  166. package/dist/modules/tokenPack/service.js +106 -0
  167. package/dist/modules/tokenPack/service.js.map +1 -0
  168. package/dist/modules/tokenPack/types.d.ts +124 -0
  169. package/dist/modules/tokenPack/types.d.ts.map +1 -0
  170. package/dist/modules/tokenPack/types.js +3 -0
  171. package/dist/modules/tokenPack/types.js.map +1 -0
  172. package/package.json +9 -5
  173. package/src/config/ConfigurationManager.ts +159 -0
  174. package/src/config/defaults.ts +27 -0
  175. package/src/config/environment.ts +94 -0
  176. package/src/config/index.ts +22 -0
  177. package/src/config/types.ts +56 -0
  178. package/src/index.ts +29 -0
  179. package/src/modules/cache/InMemoryCache.ts +98 -0
  180. package/src/modules/cache/index.ts +2 -0
  181. package/src/modules/cache/types.ts +60 -0
  182. package/src/modules/cancellableAPI/utils.ts +60 -0
  183. package/src/modules/errors/index.ts +16 -0
  184. package/src/modules/errors/types.ts +201 -0
  185. package/src/modules/invoice/const.ts +7 -0
  186. package/src/modules/invoice/index.ts +4 -0
  187. package/src/modules/invoice/repository.ts +52 -0
  188. package/src/modules/invoice/service.ts +44 -0
  189. package/src/modules/invoice/types.ts +47 -0
  190. package/src/modules/logger/types.ts +8 -0
  191. package/src/modules/network/utils.ts +24 -0
  192. package/src/modules/payments/api.ts +289 -0
  193. package/src/modules/payments/const.ts +11 -0
  194. package/src/modules/payments/index.ts +14 -0
  195. package/src/modules/payments/repository.ts +125 -0
  196. package/src/modules/payments/service.test.ts +400 -0
  197. package/src/modules/payments/service.ts +365 -0
  198. package/src/modules/payments/subscription-check-webhook.handler.integration.test.ts +935 -0
  199. package/src/modules/payments/subscription-check-webhook.handler.ts +211 -0
  200. package/src/modules/payments/subscription-check-webhook.service.ts +398 -0
  201. package/src/modules/payments/subscription-check-webhook.types.ts +29 -0
  202. package/src/modules/payments/types.ts +193 -0
  203. package/src/modules/payments/utils.ts +428 -0
  204. package/src/modules/payments/wayforpay.service.test.ts +375 -0
  205. package/src/modules/payments/wayforpay.service.ts +284 -0
  206. package/src/modules/payments/wayforpay.types.ts +138 -0
  207. package/src/modules/payments/webhook.handler.integration.test.ts +975 -0
  208. package/src/modules/payments/webhook.handler.ts +219 -0
  209. package/src/modules/payments/webhook.service.ts +812 -0
  210. package/src/modules/payments/webhook.types.ts +38 -0
  211. package/src/modules/subscription/change.service.ts +317 -0
  212. package/src/modules/subscription/const.ts +9 -0
  213. package/src/modules/subscription/index.ts +5 -0
  214. package/src/modules/subscription/repository.ts +277 -0
  215. package/src/modules/subscription/service.test.ts +665 -0
  216. package/src/modules/subscription/service.ts +328 -0
  217. package/src/modules/subscription/status-check.handler.ts +254 -0
  218. package/src/modules/subscription/types.ts +267 -0
  219. package/src/modules/subscription/utils.ts +5 -0
  220. package/src/modules/subscriptionPlan/const.ts +106 -0
  221. package/src/modules/subscriptionPlan/index.ts +4 -0
  222. package/src/modules/subscriptionPlan/repository.ts +129 -0
  223. package/src/modules/subscriptionPlan/service.test.ts +401 -0
  224. package/src/modules/subscriptionPlan/service.ts +148 -0
  225. package/src/modules/subscriptionPlan/types.ts +67 -0
  226. package/src/modules/token/const.ts +64 -0
  227. package/src/modules/token/index.ts +3 -0
  228. package/src/modules/token/service.test.ts +499 -0
  229. package/src/modules/token/service.ts +297 -0
  230. package/src/modules/token/types.ts +124 -0
  231. package/src/modules/tokenPack/const.ts +9 -0
  232. package/src/modules/tokenPack/index.ts +4 -0
  233. package/src/modules/tokenPack/repository.ts +144 -0
  234. package/src/modules/tokenPack/service.ts +119 -0
  235. package/src/modules/tokenPack/types.ts +131 -0
  236. package/src/modules/user/index.ts +3 -0
  237. package/src/modules/user/types.ts +143 -0
  238. package/src/modules/user/userRepository.ts +64 -0
  239. package/src/modules/user/userService.ts +68 -0
  240. package/src/types/extend-express.d.ts +16 -0
  241. package/src/types/function.ts +5 -0
  242. package/src/types/utilities.ts +22 -0
  243. package/src/utils.ts +53 -0
  244. package/tsconfig.json +29 -0
  245. package/dist/modules/subscription/subscriptionPlan.d.ts +0 -4
  246. package/dist/modules/subscription/subscriptionPlan.d.ts.map +0 -1
  247. package/dist/modules/subscription/subscriptionPlan.js +0 -67
  248. package/dist/modules/subscription/subscriptionPlan.js.map +0 -1
@@ -0,0 +1,219 @@
1
+ import { Logger } from '../logger/types';
2
+ import { WebhookHandlerService } from './webhook.service';
3
+ import type { WayForPayCallbackData } from './wayforpay.types';
4
+ import type { IWebhookHandlerService, WebhookProcessingResult } from './webhook.types';
5
+ import type { TokenPackPlan } from '../token/types';
6
+
7
+ /**
8
+ * Request object interface for webhook handling.
9
+ * Compatible with Express.js and similar frameworks.
10
+ */
11
+ interface WebhookRequest {
12
+ body: unknown;
13
+ }
14
+
15
+ /**
16
+ * Response object interface for webhook handling.
17
+ * Compatible with Express.js and similar frameworks.
18
+ */
19
+ interface WebhookResponse {
20
+ status(code: number): WebhookResponse;
21
+ json(data: unknown): void;
22
+ }
23
+
24
+ /**
25
+ * Configuration options for the webhook handler.
26
+ */
27
+ interface WebhookHandlerConfig {
28
+ logger: Logger;
29
+ webhookService?: IWebhookHandlerService;
30
+ tokenPackPlans?: TokenPackPlan[];
31
+ }
32
+
33
+ /**
34
+ * Result of parsing the webhook request body.
35
+ */
36
+ interface ParseResult {
37
+ success: boolean;
38
+ data?: WayForPayCallbackData;
39
+ error?: string;
40
+ }
41
+
42
+ /**
43
+ * WebhookHandler provides HTTP request handling for WayForPay payment callbacks.
44
+ * It validates incoming requests and delegates processing to the WebhookHandlerService.
45
+ *
46
+ * Usage with Express:
47
+ * ```typescript
48
+ * const handler = new WebhookHandler({ logger });
49
+ * app.post('/webhook/wayforpay', (req, res) => handler.handleRequest(req, res));
50
+ * ```
51
+ *
52
+ * Usage with Cloud Functions:
53
+ * ```typescript
54
+ * const handler = new WebhookHandler({ logger });
55
+ * export const webhookFunction = (req, res) => handler.handleRequest(req, res);
56
+ * ```
57
+ */
58
+ export class WebhookHandler {
59
+ private readonly logger: Logger;
60
+ private readonly webhookService: IWebhookHandlerService;
61
+
62
+ constructor(config: WebhookHandlerConfig) {
63
+ this.logger = config.logger;
64
+ this.webhookService =
65
+ config.webhookService ||
66
+ new WebhookHandlerService({
67
+ logger: config.logger,
68
+ tokenPackPlans: config.tokenPackPlans,
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Handles an incoming HTTP request from WayForPay.
74
+ * Validates the request body, processes the payment callback,
75
+ * and returns the appropriate response.
76
+ *
77
+ * @param req - The HTTP request object
78
+ * @param res - The HTTP response object
79
+ */
80
+ public async handleRequest(req: WebhookRequest, res: WebhookResponse): Promise<void> {
81
+ this.logger.info({
82
+ message: 'Received WayForPay webhook request',
83
+ payload: {},
84
+ });
85
+
86
+ // Parse and validate the request body
87
+ const parseResult = this.parseRequestBody(req.body);
88
+
89
+ if (!parseResult.success || !parseResult.data) {
90
+ this.logger.warning({
91
+ message: 'Invalid webhook request body',
92
+ payload: { error: parseResult.error },
93
+ });
94
+
95
+ res.status(400).json({
96
+ error: parseResult.error || 'Invalid request body',
97
+ });
98
+ return;
99
+ }
100
+
101
+ const callbackData = parseResult.data;
102
+
103
+ // Process the webhook
104
+ const result = await this.webhookService.processPaymentWebhook(callbackData);
105
+
106
+ // Return the response to WayForPay
107
+ this.sendResponse(res, result);
108
+ }
109
+
110
+ /**
111
+ * Processes callback data directly without HTTP request parsing.
112
+ * Useful for testing or when data is already parsed.
113
+ *
114
+ * @param callbackData - The parsed callback data from WayForPay
115
+ * @returns Processing result with response to send back to WayForPay
116
+ */
117
+ public async processCallback(callbackData: WayForPayCallbackData): Promise<WebhookProcessingResult> {
118
+ return this.webhookService.processPaymentWebhook(callbackData);
119
+ }
120
+
121
+ /**
122
+ * Parses and validates the webhook request body.
123
+ */
124
+ private parseRequestBody(body: unknown): ParseResult {
125
+ if (!body || typeof body !== 'object') {
126
+ return {
127
+ success: false,
128
+ error: 'Request body must be a JSON object',
129
+ };
130
+ }
131
+
132
+ const data = body as Record<string, unknown>;
133
+
134
+ // Validate required fields
135
+ const requiredFields = [
136
+ 'merchantAccount',
137
+ 'orderReference',
138
+ 'merchantSignature',
139
+ 'amount',
140
+ 'currency',
141
+ 'transactionStatus',
142
+ ];
143
+
144
+ const missingFields = requiredFields.filter(
145
+ (field) => data[field] === undefined || data[field] === null,
146
+ );
147
+
148
+ if (missingFields.length > 0) {
149
+ return {
150
+ success: false,
151
+ error: `Missing required fields: ${missingFields.join(', ')}`,
152
+ };
153
+ }
154
+
155
+ // Validate transaction status
156
+ const validStatuses = ['Approved', 'Declined', 'Expired', 'Pending', 'InProcessing', 'Refunded'];
157
+ if (!validStatuses.includes(data.transactionStatus as string)) {
158
+ return {
159
+ success: false,
160
+ error: `Invalid transaction status: ${data.transactionStatus}`,
161
+ };
162
+ }
163
+
164
+ return {
165
+ success: true,
166
+ data: data as unknown as WayForPayCallbackData,
167
+ };
168
+ }
169
+
170
+ /**
171
+ * Sends the response back to WayForPay.
172
+ * WayForPay expects a 200 status with a signed response body.
173
+ */
174
+ private sendResponse(res: WebhookResponse, result: WebhookProcessingResult): void {
175
+ // WayForPay expects a 200 status code even for declined transactions
176
+ // The accept/decline status is communicated in the response body
177
+ res.status(200).json(result.response);
178
+
179
+ this.logger.info({
180
+ message: 'Webhook response sent',
181
+ payload: {
182
+ success: result.success,
183
+ status: result.response.status,
184
+ orderReference: result.response.orderReference,
185
+ },
186
+ });
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Creates a webhook handler function for use with serverless platforms.
192
+ * Returns a function that can be directly used as a Cloud Function or Lambda handler.
193
+ *
194
+ * @param config - Configuration options for the webhook handler
195
+ * @returns An async function that handles webhook requests
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * // Firebase Cloud Functions
200
+ * import * as functions from 'firebase-functions';
201
+ * import { createWebhookHandler } from 'miit-bot-payment';
202
+ *
203
+ * const logger = { info: console.log, warning: console.warn, error: console.error };
204
+ * export const paymentWebhook = functions.https.onRequest(createWebhookHandler({ logger }));
205
+ * ```
206
+ */
207
+ export function createWebhookHandler(
208
+ config: WebhookHandlerConfig,
209
+ ): (req: WebhookRequest, res: WebhookResponse) => Promise<void> {
210
+ const handler = new WebhookHandler(config);
211
+ return (req: WebhookRequest, res: WebhookResponse) => handler.handleRequest(req, res);
212
+ }
213
+
214
+ /**
215
+ * Type export for the webhook handler function signature.
216
+ */
217
+ export type WebhookHandlerFunction = (req: WebhookRequest, res: WebhookResponse) => Promise<void>;
218
+
219
+ export type { WebhookRequest, WebhookResponse, WebhookHandlerConfig };