@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.
- package/dist/config/ConfigurationManager.d.ts +64 -0
- package/dist/config/ConfigurationManager.d.ts.map +1 -0
- package/dist/config/ConfigurationManager.js +144 -0
- package/dist/config/ConfigurationManager.js.map +1 -0
- package/dist/config/defaults.d.ts +18 -0
- package/dist/config/defaults.d.ts.map +1 -0
- package/dist/config/defaults.js +26 -0
- package/dist/config/defaults.js.map +1 -0
- package/dist/config/environment.d.ts +38 -0
- package/dist/config/environment.d.ts.map +1 -0
- package/dist/config/environment.js +91 -0
- package/dist/config/environment.js.map +1 -0
- package/dist/config/index.d.ts +5 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +18 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/types.d.ts +53 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +3 -0
- package/dist/config/types.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -1
- package/dist/modules/cache/InMemoryCache.d.ts +17 -0
- package/dist/modules/cache/InMemoryCache.d.ts.map +1 -0
- package/dist/modules/cache/InMemoryCache.js +77 -0
- package/dist/modules/cache/InMemoryCache.js.map +1 -0
- package/dist/modules/cache/index.d.ts +3 -0
- package/dist/modules/cache/index.d.ts.map +1 -0
- package/dist/modules/cache/index.js +19 -0
- package/dist/modules/cache/index.js.map +1 -0
- package/dist/modules/cache/types.d.ts +52 -0
- package/dist/modules/cache/types.d.ts.map +1 -0
- package/dist/modules/cache/types.js +3 -0
- package/dist/modules/cache/types.js.map +1 -0
- package/dist/modules/errors/index.d.ts +2 -0
- package/dist/modules/errors/index.d.ts.map +1 -0
- package/dist/modules/errors/index.js +19 -0
- package/dist/modules/errors/index.js.map +1 -0
- package/dist/modules/errors/types.d.ts +112 -0
- package/dist/modules/errors/types.d.ts.map +1 -0
- package/dist/modules/errors/types.js +174 -0
- package/dist/modules/errors/types.js.map +1 -0
- package/dist/modules/payments/api.d.ts +63 -1
- package/dist/modules/payments/api.d.ts.map +1 -1
- package/dist/modules/payments/api.js +103 -1
- package/dist/modules/payments/api.js.map +1 -1
- package/dist/modules/payments/const.d.ts.map +1 -1
- package/dist/modules/payments/const.js +1 -0
- package/dist/modules/payments/const.js.map +1 -1
- package/dist/modules/payments/index.d.ts +8 -0
- package/dist/modules/payments/index.d.ts.map +1 -1
- package/dist/modules/payments/index.js +8 -0
- package/dist/modules/payments/index.js.map +1 -1
- package/dist/modules/payments/service.d.ts +42 -2
- package/dist/modules/payments/service.d.ts.map +1 -1
- package/dist/modules/payments/service.js +132 -3
- package/dist/modules/payments/service.js.map +1 -1
- package/dist/modules/payments/subscription-check-webhook.handler.d.ts +85 -0
- package/dist/modules/payments/subscription-check-webhook.handler.d.ts.map +1 -0
- package/dist/modules/payments/subscription-check-webhook.handler.js +155 -0
- package/dist/modules/payments/subscription-check-webhook.handler.js.map +1 -0
- package/dist/modules/payments/subscription-check-webhook.service.d.ts +59 -0
- package/dist/modules/payments/subscription-check-webhook.service.d.ts.map +1 -0
- package/dist/modules/payments/subscription-check-webhook.service.js +330 -0
- package/dist/modules/payments/subscription-check-webhook.service.js.map +1 -0
- package/dist/modules/payments/subscription-check-webhook.types.d.ts +25 -0
- package/dist/modules/payments/subscription-check-webhook.types.d.ts.map +1 -0
- package/dist/modules/payments/subscription-check-webhook.types.js +3 -0
- package/dist/modules/payments/subscription-check-webhook.types.js.map +1 -0
- package/dist/modules/payments/types.d.ts +69 -2
- package/dist/modules/payments/types.d.ts.map +1 -1
- package/dist/modules/payments/utils.d.ts +151 -5
- package/dist/modules/payments/utils.d.ts.map +1 -1
- package/dist/modules/payments/utils.js +253 -9
- package/dist/modules/payments/utils.js.map +1 -1
- package/dist/modules/payments/wayforpay.service.d.ts +39 -0
- package/dist/modules/payments/wayforpay.service.d.ts.map +1 -0
- package/dist/modules/payments/wayforpay.service.js +217 -0
- package/dist/modules/payments/wayforpay.service.js.map +1 -0
- package/dist/modules/payments/wayforpay.types.d.ts +115 -0
- package/dist/modules/payments/wayforpay.types.d.ts.map +1 -0
- package/dist/modules/payments/wayforpay.types.js +3 -0
- package/dist/modules/payments/wayforpay.types.js.map +1 -0
- package/dist/modules/payments/webhook.handler.d.ts +98 -0
- package/dist/modules/payments/webhook.handler.d.ts.map +1 -0
- package/dist/modules/payments/webhook.handler.js +153 -0
- package/dist/modules/payments/webhook.handler.js.map +1 -0
- package/dist/modules/payments/webhook.service.d.ts +99 -0
- package/dist/modules/payments/webhook.service.d.ts.map +1 -0
- package/dist/modules/payments/webhook.service.js +672 -0
- package/dist/modules/payments/webhook.service.js.map +1 -0
- package/dist/modules/payments/webhook.types.d.ts +35 -0
- package/dist/modules/payments/webhook.types.d.ts.map +1 -0
- package/dist/modules/payments/webhook.types.js +3 -0
- package/dist/modules/payments/webhook.types.js.map +1 -0
- package/dist/modules/subscription/change.service.d.ts +80 -0
- package/dist/modules/subscription/change.service.d.ts.map +1 -0
- package/dist/modules/subscription/change.service.js +226 -0
- package/dist/modules/subscription/change.service.js.map +1 -0
- package/dist/modules/subscription/index.d.ts +2 -0
- package/dist/modules/subscription/index.d.ts.map +1 -1
- package/dist/modules/subscription/index.js +2 -0
- package/dist/modules/subscription/index.js.map +1 -1
- package/dist/modules/subscription/service.d.ts +8 -1
- package/dist/modules/subscription/service.d.ts.map +1 -1
- package/dist/modules/subscription/service.js +58 -1
- package/dist/modules/subscription/service.js.map +1 -1
- package/dist/modules/subscription/status-check.handler.d.ts +117 -0
- package/dist/modules/subscription/status-check.handler.d.ts.map +1 -0
- package/dist/modules/subscription/status-check.handler.js +164 -0
- package/dist/modules/subscription/status-check.handler.js.map +1 -0
- package/dist/modules/subscription/types.d.ts +37 -1
- package/dist/modules/subscription/types.d.ts.map +1 -1
- package/dist/modules/subscriptionPlan/const.d.ts +5 -0
- package/dist/modules/subscriptionPlan/const.d.ts.map +1 -0
- package/dist/modules/subscriptionPlan/const.js +106 -0
- package/dist/modules/subscriptionPlan/const.js.map +1 -0
- package/dist/modules/subscriptionPlan/index.d.ts +5 -0
- package/dist/modules/subscriptionPlan/index.d.ts.map +1 -0
- package/dist/modules/subscriptionPlan/index.js +21 -0
- package/dist/modules/subscriptionPlan/index.js.map +1 -0
- package/dist/modules/subscriptionPlan/repository.d.ts +22 -0
- package/dist/modules/subscriptionPlan/repository.d.ts.map +1 -0
- package/dist/modules/subscriptionPlan/repository.js +95 -0
- package/dist/modules/subscriptionPlan/repository.js.map +1 -0
- package/dist/modules/subscriptionPlan/service.d.ts +21 -0
- package/dist/modules/subscriptionPlan/service.d.ts.map +1 -0
- package/dist/modules/subscriptionPlan/service.js +128 -0
- package/dist/modules/subscriptionPlan/service.js.map +1 -0
- package/dist/modules/subscriptionPlan/types.d.ts +40 -0
- package/dist/modules/subscriptionPlan/types.d.ts.map +1 -0
- package/dist/modules/subscriptionPlan/types.js +3 -0
- package/dist/modules/subscriptionPlan/types.js.map +1 -0
- package/dist/modules/token/const.d.ts +7 -0
- package/dist/modules/token/const.d.ts.map +1 -0
- package/dist/modules/token/const.js +66 -0
- package/dist/modules/token/const.js.map +1 -0
- package/dist/modules/token/index.d.ts +4 -0
- package/dist/modules/token/index.d.ts.map +1 -0
- package/dist/modules/token/index.js +20 -0
- package/dist/modules/token/index.js.map +1 -0
- package/dist/modules/token/service.d.ts +46 -0
- package/dist/modules/token/service.d.ts.map +1 -0
- package/dist/modules/token/service.js +249 -0
- package/dist/modules/token/service.js.map +1 -0
- package/dist/modules/token/types.d.ts +109 -0
- package/dist/modules/token/types.d.ts.map +1 -0
- package/dist/modules/token/types.js +3 -0
- package/dist/modules/token/types.js.map +1 -0
- package/dist/modules/tokenPack/const.d.ts +4 -0
- package/dist/modules/tokenPack/const.d.ts.map +1 -0
- package/dist/modules/tokenPack/const.js +10 -0
- package/dist/modules/tokenPack/const.js.map +1 -0
- package/dist/modules/tokenPack/index.d.ts +5 -0
- package/dist/modules/tokenPack/index.d.ts.map +1 -0
- package/dist/modules/tokenPack/index.js +21 -0
- package/dist/modules/tokenPack/index.js.map +1 -0
- package/dist/modules/tokenPack/repository.d.ts +32 -0
- package/dist/modules/tokenPack/repository.d.ts.map +1 -0
- package/dist/modules/tokenPack/repository.js +103 -0
- package/dist/modules/tokenPack/repository.js.map +1 -0
- package/dist/modules/tokenPack/service.d.ts +28 -0
- package/dist/modules/tokenPack/service.d.ts.map +1 -0
- package/dist/modules/tokenPack/service.js +106 -0
- package/dist/modules/tokenPack/service.js.map +1 -0
- package/dist/modules/tokenPack/types.d.ts +124 -0
- package/dist/modules/tokenPack/types.d.ts.map +1 -0
- package/dist/modules/tokenPack/types.js +3 -0
- package/dist/modules/tokenPack/types.js.map +1 -0
- package/package.json +9 -5
- package/src/config/ConfigurationManager.ts +159 -0
- package/src/config/defaults.ts +27 -0
- package/src/config/environment.ts +94 -0
- package/src/config/index.ts +22 -0
- package/src/config/types.ts +56 -0
- package/src/index.ts +29 -0
- package/src/modules/cache/InMemoryCache.ts +98 -0
- package/src/modules/cache/index.ts +2 -0
- package/src/modules/cache/types.ts +60 -0
- package/src/modules/cancellableAPI/utils.ts +60 -0
- package/src/modules/errors/index.ts +16 -0
- package/src/modules/errors/types.ts +201 -0
- package/src/modules/invoice/const.ts +7 -0
- package/src/modules/invoice/index.ts +4 -0
- package/src/modules/invoice/repository.ts +52 -0
- package/src/modules/invoice/service.ts +44 -0
- package/src/modules/invoice/types.ts +47 -0
- package/src/modules/logger/types.ts +8 -0
- package/src/modules/network/utils.ts +24 -0
- package/src/modules/payments/api.ts +289 -0
- package/src/modules/payments/const.ts +11 -0
- package/src/modules/payments/index.ts +14 -0
- package/src/modules/payments/repository.ts +125 -0
- package/src/modules/payments/service.test.ts +400 -0
- package/src/modules/payments/service.ts +365 -0
- package/src/modules/payments/subscription-check-webhook.handler.integration.test.ts +935 -0
- package/src/modules/payments/subscription-check-webhook.handler.ts +211 -0
- package/src/modules/payments/subscription-check-webhook.service.ts +398 -0
- package/src/modules/payments/subscription-check-webhook.types.ts +29 -0
- package/src/modules/payments/types.ts +193 -0
- package/src/modules/payments/utils.ts +428 -0
- package/src/modules/payments/wayforpay.service.test.ts +375 -0
- package/src/modules/payments/wayforpay.service.ts +284 -0
- package/src/modules/payments/wayforpay.types.ts +138 -0
- package/src/modules/payments/webhook.handler.integration.test.ts +975 -0
- package/src/modules/payments/webhook.handler.ts +219 -0
- package/src/modules/payments/webhook.service.ts +812 -0
- package/src/modules/payments/webhook.types.ts +38 -0
- package/src/modules/subscription/change.service.ts +317 -0
- package/src/modules/subscription/const.ts +9 -0
- package/src/modules/subscription/index.ts +5 -0
- package/src/modules/subscription/repository.ts +277 -0
- package/src/modules/subscription/service.test.ts +665 -0
- package/src/modules/subscription/service.ts +328 -0
- package/src/modules/subscription/status-check.handler.ts +254 -0
- package/src/modules/subscription/types.ts +267 -0
- package/src/modules/subscription/utils.ts +5 -0
- package/src/modules/subscriptionPlan/const.ts +106 -0
- package/src/modules/subscriptionPlan/index.ts +4 -0
- package/src/modules/subscriptionPlan/repository.ts +129 -0
- package/src/modules/subscriptionPlan/service.test.ts +401 -0
- package/src/modules/subscriptionPlan/service.ts +148 -0
- package/src/modules/subscriptionPlan/types.ts +67 -0
- package/src/modules/token/const.ts +64 -0
- package/src/modules/token/index.ts +3 -0
- package/src/modules/token/service.test.ts +499 -0
- package/src/modules/token/service.ts +297 -0
- package/src/modules/token/types.ts +124 -0
- package/src/modules/tokenPack/const.ts +9 -0
- package/src/modules/tokenPack/index.ts +4 -0
- package/src/modules/tokenPack/repository.ts +144 -0
- package/src/modules/tokenPack/service.ts +119 -0
- package/src/modules/tokenPack/types.ts +131 -0
- package/src/modules/user/index.ts +3 -0
- package/src/modules/user/types.ts +143 -0
- package/src/modules/user/userRepository.ts +64 -0
- package/src/modules/user/userService.ts +68 -0
- package/src/types/extend-express.d.ts +16 -0
- package/src/types/function.ts +5 -0
- package/src/types/utilities.ts +22 -0
- package/src/utils.ts +53 -0
- package/tsconfig.json +29 -0
- package/dist/modules/subscription/subscriptionPlan.d.ts +0 -4
- package/dist/modules/subscription/subscriptionPlan.d.ts.map +0 -1
- package/dist/modules/subscription/subscriptionPlan.js +0 -67
- package/dist/modules/subscription/subscriptionPlan.js.map +0 -1
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WayForPayService = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const moment_1 = __importDefault(require("moment"));
|
|
9
|
+
const wayforpay_api_1 = require("@misterhomer1992/wayforpay-api");
|
|
10
|
+
const utils_1 = require("../../utils");
|
|
11
|
+
const utils_2 = require("./utils");
|
|
12
|
+
const SIGNATURE_DELIMITER = ';';
|
|
13
|
+
/**
|
|
14
|
+
* WayForPayService provides integration with the WayForPay payment provider.
|
|
15
|
+
* Supports both subscription (regular) and one-time payment flows.
|
|
16
|
+
*/
|
|
17
|
+
class WayForPayService {
|
|
18
|
+
constructor({ logger, merchantAccount, merchantSecretKey, merchantDomainName, serviceUrl, }) {
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.merchantAccount = merchantAccount || process.env.WAYFORPAY_MERCHANT_ACCOUNT;
|
|
21
|
+
this.merchantSecretKey = merchantSecretKey || process.env.WAYFORPAY_MERCHANT_SECRET_KEY;
|
|
22
|
+
this.merchantDomainName = merchantDomainName || process.env.WAYFORPAY_MERCHANT_DOMAIN;
|
|
23
|
+
this.serviceUrl = serviceUrl || process.env.WAYFORPAY_SERVICE_URL;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generates a payment URL for subscription (regular) payments.
|
|
27
|
+
*/
|
|
28
|
+
async generateSubscriptionPaymentUrl(params) {
|
|
29
|
+
const { userId, platform, planId, productName, productPrice, currency, regularCount = 3, regularMode = 'daily', language = 'UA', dateNext, } = params;
|
|
30
|
+
try {
|
|
31
|
+
const orderDate = Date.now();
|
|
32
|
+
const formattedUtc = moment_1.default.utc().toISOString();
|
|
33
|
+
const orderReference = (0, utils_2.createSubscriptionOrderReference)({
|
|
34
|
+
userId,
|
|
35
|
+
platform,
|
|
36
|
+
planId,
|
|
37
|
+
utcDate: formattedUtc,
|
|
38
|
+
});
|
|
39
|
+
const response = (await (0, wayforpay_api_1.generateRegularPurchase)({
|
|
40
|
+
merchantAccount: this.merchantAccount,
|
|
41
|
+
merchantDomainName: this.merchantDomainName,
|
|
42
|
+
merchantSecretKey: this.merchantSecretKey,
|
|
43
|
+
orderDate,
|
|
44
|
+
orderReference,
|
|
45
|
+
productName: [productName],
|
|
46
|
+
productPrice: [productPrice],
|
|
47
|
+
currency: currency,
|
|
48
|
+
language: language,
|
|
49
|
+
serviceUrl: this.serviceUrl,
|
|
50
|
+
regularMode,
|
|
51
|
+
regularCount,
|
|
52
|
+
dateNext,
|
|
53
|
+
}));
|
|
54
|
+
if (!(0, utils_1.isObject)(response) || (0, utils_1.isBadOrEmptyString)(response.url)) {
|
|
55
|
+
this.logger.error({
|
|
56
|
+
message: 'Invalid response from WayForPay generateRegularPurchase',
|
|
57
|
+
payload: {
|
|
58
|
+
orderReference,
|
|
59
|
+
response: JSON.stringify(response),
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
url: response.url,
|
|
66
|
+
orderReference,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
this.logger.error({
|
|
71
|
+
message: 'Error in WayForPayService generateSubscriptionPaymentUrl',
|
|
72
|
+
payload: {
|
|
73
|
+
userId,
|
|
74
|
+
platform,
|
|
75
|
+
planId,
|
|
76
|
+
productName,
|
|
77
|
+
productPrice,
|
|
78
|
+
currency,
|
|
79
|
+
error: JSON.stringify(error),
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Generates a payment URL for one-time payments.
|
|
87
|
+
*/
|
|
88
|
+
async generateOneTimePaymentUrl(params) {
|
|
89
|
+
const { userId, platform, productId, productName, productPrice, currency, language = 'UA', returnUrl } = params;
|
|
90
|
+
try {
|
|
91
|
+
const orderDate = Date.now();
|
|
92
|
+
const formattedUtc = moment_1.default.utc().toISOString();
|
|
93
|
+
const orderReference = (0, utils_2.createSubscriptionOrderReference)({
|
|
94
|
+
userId,
|
|
95
|
+
platform,
|
|
96
|
+
planId: productId,
|
|
97
|
+
utcDate: formattedUtc,
|
|
98
|
+
});
|
|
99
|
+
const response = (await (0, wayforpay_api_1.generatePurchase)({
|
|
100
|
+
merchantAccount: this.merchantAccount,
|
|
101
|
+
merchantDomainName: this.merchantDomainName,
|
|
102
|
+
merchantSecretKey: this.merchantSecretKey,
|
|
103
|
+
orderDate,
|
|
104
|
+
orderReference,
|
|
105
|
+
productName: [productName],
|
|
106
|
+
productPrice: [productPrice],
|
|
107
|
+
currency: currency,
|
|
108
|
+
language: language,
|
|
109
|
+
serviceUrl: this.serviceUrl,
|
|
110
|
+
returnUrl,
|
|
111
|
+
}));
|
|
112
|
+
if (!(0, utils_1.isObject)(response) || (0, utils_1.isBadOrEmptyString)(response.url)) {
|
|
113
|
+
this.logger.error({
|
|
114
|
+
message: 'Invalid response from WayForPay generatePurchase',
|
|
115
|
+
payload: {
|
|
116
|
+
orderReference,
|
|
117
|
+
response: JSON.stringify(response),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
url: response.url,
|
|
124
|
+
orderReference,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
this.logger.error({
|
|
129
|
+
message: 'Error in WayForPayService generateOneTimePaymentUrl',
|
|
130
|
+
payload: {
|
|
131
|
+
userId,
|
|
132
|
+
platform,
|
|
133
|
+
productId,
|
|
134
|
+
productName,
|
|
135
|
+
productPrice,
|
|
136
|
+
currency,
|
|
137
|
+
error: JSON.stringify(error),
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Verifies the signature of a WayForPay webhook callback.
|
|
145
|
+
* The signature is calculated as HMAC_MD5 of concatenated values.
|
|
146
|
+
*/
|
|
147
|
+
verifyCallbackSignature(callbackData) {
|
|
148
|
+
try {
|
|
149
|
+
const { merchantAccount, orderReference, amount, currency, authCode, cardPan, transactionStatus, reasonCode, merchantSignature, } = callbackData;
|
|
150
|
+
if (!merchantSignature) {
|
|
151
|
+
return {
|
|
152
|
+
isValid: false,
|
|
153
|
+
error: 'Missing merchant signature in callback data',
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
// WayForPay signature is calculated from these fields in this exact order
|
|
157
|
+
const signatureData = [
|
|
158
|
+
merchantAccount,
|
|
159
|
+
orderReference,
|
|
160
|
+
amount,
|
|
161
|
+
currency,
|
|
162
|
+
authCode,
|
|
163
|
+
cardPan,
|
|
164
|
+
transactionStatus,
|
|
165
|
+
reasonCode,
|
|
166
|
+
];
|
|
167
|
+
const signatureString = signatureData.join(SIGNATURE_DELIMITER);
|
|
168
|
+
const calculatedSignature = crypto_1.default
|
|
169
|
+
.createHmac('md5', this.merchantSecretKey)
|
|
170
|
+
.update(signatureString)
|
|
171
|
+
.digest('hex');
|
|
172
|
+
const isValid = calculatedSignature.toLowerCase() === merchantSignature.toLowerCase();
|
|
173
|
+
if (!isValid) {
|
|
174
|
+
this.logger.warning({
|
|
175
|
+
message: 'WayForPay signature verification failed',
|
|
176
|
+
payload: {
|
|
177
|
+
orderReference,
|
|
178
|
+
expectedSignature: calculatedSignature,
|
|
179
|
+
receivedSignature: merchantSignature,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return { isValid };
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
this.logger.error({
|
|
187
|
+
message: 'Error in WayForPayService verifyCallbackSignature',
|
|
188
|
+
payload: {
|
|
189
|
+
orderReference: callbackData?.orderReference,
|
|
190
|
+
error: JSON.stringify(error),
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
return {
|
|
194
|
+
isValid: false,
|
|
195
|
+
error: 'Error verifying signature',
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Builds a response to send back to WayForPay after processing a webhook.
|
|
201
|
+
* WayForPay expects a signed response to confirm receipt.
|
|
202
|
+
*/
|
|
203
|
+
buildWebhookResponse(orderReference, status) {
|
|
204
|
+
const time = Date.now();
|
|
205
|
+
const signatureData = [orderReference, status, time];
|
|
206
|
+
const signatureString = signatureData.join(SIGNATURE_DELIMITER);
|
|
207
|
+
const signature = crypto_1.default.createHmac('md5', this.merchantSecretKey).update(signatureString).digest('hex');
|
|
208
|
+
return {
|
|
209
|
+
orderReference,
|
|
210
|
+
status,
|
|
211
|
+
time,
|
|
212
|
+
signature,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
exports.WayForPayService = WayForPayService;
|
|
217
|
+
//# sourceMappingURL=wayforpay.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wayforpay.service.js","sourceRoot":"","sources":["../../../src/modules/payments/wayforpay.service.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAC5B,oDAA4B;AAC5B,kEAA+G;AAE/G,uCAA2D;AAC3D,mCAA2D;AAW3D,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAOhC;;;GAGG;AACH,MAAa,gBAAgB;IAOzB,YAAY,EACR,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AAK,CAAC;gBACd,OAAO,EAAE,mDAAmD;gBAC5D,OAAO,EAAE;oBACL,cAAc,EAAE,YAAY,EAAE,cAAc;oBAC5C,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;iBAC/B;aACJ,CAAC,CAAC;YACH,OAAO;gBACH,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,2BAA2B;aACrC,CAAC;QACN,CAAC;IACL,CAAC;IAED;;;OAGG;IACI,oBAAoB,CAAC,cAAsB,EAAE,MAA4B;QAC5E,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAExB,MAAM,aAAa,GAAG,CAAC,cAAc,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QACrD,MAAM,eAAe,GAAG,aAAa,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAChE,MAAM,SAAS,GAAG,gBAAM,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEzG,OAAO;YACH,cAAc;YACd,MAAM;YACN,IAAI;YACJ,SAAS;SACZ,CAAC;IACN,CAAC;CACJ;AAhQD,4CAgQC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Currency, ResponseReasonCode, ResponseTransactionStatus } from '@misterhomer1992/wayforpay-api';
|
|
2
|
+
/**
|
|
3
|
+
* WayForPay webhook callback data structure.
|
|
4
|
+
* This is the data received from WayForPay when a payment status changes.
|
|
5
|
+
*/
|
|
6
|
+
type WayForPayCallbackData = {
|
|
7
|
+
merchantAccount: string;
|
|
8
|
+
orderReference: string;
|
|
9
|
+
merchantSignature: string;
|
|
10
|
+
amount: number;
|
|
11
|
+
currency: Currency;
|
|
12
|
+
authCode: string;
|
|
13
|
+
email: string;
|
|
14
|
+
phone: string;
|
|
15
|
+
createdDate: number;
|
|
16
|
+
processingDate: number;
|
|
17
|
+
cardPan: string;
|
|
18
|
+
cardType: string;
|
|
19
|
+
issuerBankCountry: string;
|
|
20
|
+
issuerBankName: string;
|
|
21
|
+
recToken: string;
|
|
22
|
+
transactionStatus: ResponseTransactionStatus;
|
|
23
|
+
reason: string;
|
|
24
|
+
reasonCode: ResponseReasonCode;
|
|
25
|
+
fee: number;
|
|
26
|
+
paymentSystem: string;
|
|
27
|
+
/** Regular payment specific fields */
|
|
28
|
+
regularOn?: string;
|
|
29
|
+
dateBegin?: string;
|
|
30
|
+
dateEnd?: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Parameters for generating a subscription (regular) payment URL.
|
|
34
|
+
*/
|
|
35
|
+
type GenerateSubscriptionPaymentParams = {
|
|
36
|
+
userId: string;
|
|
37
|
+
platform: string;
|
|
38
|
+
planId: string;
|
|
39
|
+
productName: string;
|
|
40
|
+
productPrice: number;
|
|
41
|
+
currency: Currency;
|
|
42
|
+
regularCount?: number;
|
|
43
|
+
regularMode?: 'daily' | 'weekly' | 'monthly' | 'yearly';
|
|
44
|
+
language?: 'EN' | 'UA';
|
|
45
|
+
dateNext?: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Parameters for generating a one-time payment URL.
|
|
49
|
+
*/
|
|
50
|
+
type GenerateOneTimePaymentParams = {
|
|
51
|
+
userId: string;
|
|
52
|
+
platform: string;
|
|
53
|
+
productId: string;
|
|
54
|
+
productName: string;
|
|
55
|
+
productPrice: number;
|
|
56
|
+
currency: Currency;
|
|
57
|
+
language?: 'EN' | 'UA';
|
|
58
|
+
returnUrl?: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Result of payment URL generation.
|
|
62
|
+
*/
|
|
63
|
+
type PaymentUrlResult = {
|
|
64
|
+
url: string;
|
|
65
|
+
orderReference: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Result of signature verification.
|
|
69
|
+
*/
|
|
70
|
+
type SignatureVerificationResult = {
|
|
71
|
+
isValid: boolean;
|
|
72
|
+
error?: string;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Response to send back to WayForPay after processing a webhook.
|
|
76
|
+
*/
|
|
77
|
+
type WayForPayWebhookResponse = {
|
|
78
|
+
orderReference: string;
|
|
79
|
+
status: 'accept' | 'decline';
|
|
80
|
+
time: number;
|
|
81
|
+
signature: string;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Interface for WayForPay service operations.
|
|
85
|
+
* Provides methods for payment URL generation and signature verification.
|
|
86
|
+
*/
|
|
87
|
+
interface IWayForPayService {
|
|
88
|
+
/**
|
|
89
|
+
* Generates a payment URL for subscription (regular) payments.
|
|
90
|
+
* @param params - Payment parameters
|
|
91
|
+
* @returns Promise resolving to payment URL and order reference
|
|
92
|
+
*/
|
|
93
|
+
generateSubscriptionPaymentUrl(params: GenerateSubscriptionPaymentParams): Promise<PaymentUrlResult | null>;
|
|
94
|
+
/**
|
|
95
|
+
* Generates a payment URL for one-time payments.
|
|
96
|
+
* @param params - Payment parameters
|
|
97
|
+
* @returns Promise resolving to payment URL and order reference
|
|
98
|
+
*/
|
|
99
|
+
generateOneTimePaymentUrl(params: GenerateOneTimePaymentParams): Promise<PaymentUrlResult | null>;
|
|
100
|
+
/**
|
|
101
|
+
* Verifies the signature of a WayForPay webhook callback.
|
|
102
|
+
* @param callbackData - The callback data received from WayForPay
|
|
103
|
+
* @returns Verification result indicating if signature is valid
|
|
104
|
+
*/
|
|
105
|
+
verifyCallbackSignature(callbackData: WayForPayCallbackData): SignatureVerificationResult;
|
|
106
|
+
/**
|
|
107
|
+
* Builds a response to send back to WayForPay after processing a webhook.
|
|
108
|
+
* @param orderReference - The order reference from the callback
|
|
109
|
+
* @param status - Whether to accept or decline the transaction
|
|
110
|
+
* @returns Response object to send to WayForPay
|
|
111
|
+
*/
|
|
112
|
+
buildWebhookResponse(orderReference: string, status: 'accept' | 'decline'): WayForPayWebhookResponse;
|
|
113
|
+
}
|
|
114
|
+
export type { WayForPayCallbackData, GenerateSubscriptionPaymentParams, GenerateOneTimePaymentParams, PaymentUrlResult, SignatureVerificationResult, WayForPayWebhookResponse, IWayForPayService, };
|
|
115
|
+
//# sourceMappingURL=wayforpay.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wayforpay.types.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/wayforpay.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAEzG;;;GAGG;AACH,KAAK,qBAAqB,GAAG;IACzB,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,QAAQ,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,yBAAyB,CAAC;IAC7C,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,kBAAkB,CAAC;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,EAAE,MAAM,CAAC;IACtB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;GAEG;AACH,KAAK,iCAAiC,GAAG;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ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;;;;OAIG;IACH,yBAAyB,CAAC,MAAM,EAAE,4BAA4B,GAAG,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAC;IAElG;;;;OAIG;IACH,uBAAuB,CAAC,YAAY,EAAE,qBAAqB,GAAG,2BAA2B,CAAC;IAE1F;;;;;OAKG;IACH,oBAAoB,CAChB,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,QAAQ,GAAG,SAAS,GAC7B,wBAAwB,CAAC;CAC/B;AAED,YAAY,EACR,qBAAqB,EACrB,iCAAiC,EACjC,4BAA4B,EAC5B,gBAAgB,EAChB,2BAA2B,EAC3B,wBAAwB,EACxB,iBAAiB,GACpB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wayforpay.types.js","sourceRoot":"","sources":["../../../src/modules/payments/wayforpay.types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Logger } from '../logger/types';
|
|
2
|
+
import type { WayForPayCallbackData } from './wayforpay.types';
|
|
3
|
+
import type { IWebhookHandlerService, WebhookProcessingResult } from './webhook.types';
|
|
4
|
+
import type { TokenPackPlan } from '../token/types';
|
|
5
|
+
/**
|
|
6
|
+
* Request object interface for webhook handling.
|
|
7
|
+
* Compatible with Express.js and similar frameworks.
|
|
8
|
+
*/
|
|
9
|
+
interface WebhookRequest {
|
|
10
|
+
body: unknown;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Response object interface for webhook handling.
|
|
14
|
+
* Compatible with Express.js and similar frameworks.
|
|
15
|
+
*/
|
|
16
|
+
interface WebhookResponse {
|
|
17
|
+
status(code: number): WebhookResponse;
|
|
18
|
+
json(data: unknown): void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Configuration options for the webhook handler.
|
|
22
|
+
*/
|
|
23
|
+
interface WebhookHandlerConfig {
|
|
24
|
+
logger: Logger;
|
|
25
|
+
webhookService?: IWebhookHandlerService;
|
|
26
|
+
tokenPackPlans?: TokenPackPlan[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* WebhookHandler provides HTTP request handling for WayForPay payment callbacks.
|
|
30
|
+
* It validates incoming requests and delegates processing to the WebhookHandlerService.
|
|
31
|
+
*
|
|
32
|
+
* Usage with Express:
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const handler = new WebhookHandler({ logger });
|
|
35
|
+
* app.post('/webhook/wayforpay', (req, res) => handler.handleRequest(req, res));
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Usage with Cloud Functions:
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const handler = new WebhookHandler({ logger });
|
|
41
|
+
* export const webhookFunction = (req, res) => handler.handleRequest(req, res);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare class WebhookHandler {
|
|
45
|
+
private readonly logger;
|
|
46
|
+
private readonly webhookService;
|
|
47
|
+
constructor(config: WebhookHandlerConfig);
|
|
48
|
+
/**
|
|
49
|
+
* Handles an incoming HTTP request from WayForPay.
|
|
50
|
+
* Validates the request body, processes the payment callback,
|
|
51
|
+
* and returns the appropriate response.
|
|
52
|
+
*
|
|
53
|
+
* @param req - The HTTP request object
|
|
54
|
+
* @param res - The HTTP response object
|
|
55
|
+
*/
|
|
56
|
+
handleRequest(req: WebhookRequest, res: WebhookResponse): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Processes callback data directly without HTTP request parsing.
|
|
59
|
+
* Useful for testing or when data is already parsed.
|
|
60
|
+
*
|
|
61
|
+
* @param callbackData - The parsed callback data from WayForPay
|
|
62
|
+
* @returns Processing result with response to send back to WayForPay
|
|
63
|
+
*/
|
|
64
|
+
processCallback(callbackData: WayForPayCallbackData): Promise<WebhookProcessingResult>;
|
|
65
|
+
/**
|
|
66
|
+
* Parses and validates the webhook request body.
|
|
67
|
+
*/
|
|
68
|
+
private parseRequestBody;
|
|
69
|
+
/**
|
|
70
|
+
* Sends the response back to WayForPay.
|
|
71
|
+
* WayForPay expects a 200 status with a signed response body.
|
|
72
|
+
*/
|
|
73
|
+
private sendResponse;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates a webhook handler function for use with serverless platforms.
|
|
77
|
+
* Returns a function that can be directly used as a Cloud Function or Lambda handler.
|
|
78
|
+
*
|
|
79
|
+
* @param config - Configuration options for the webhook handler
|
|
80
|
+
* @returns An async function that handles webhook requests
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // Firebase Cloud Functions
|
|
85
|
+
* import * as functions from 'firebase-functions';
|
|
86
|
+
* import { createWebhookHandler } from 'miit-bot-payment';
|
|
87
|
+
*
|
|
88
|
+
* const logger = { info: console.log, warning: console.warn, error: console.error };
|
|
89
|
+
* export const paymentWebhook = functions.https.onRequest(createWebhookHandler({ logger }));
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function createWebhookHandler(config: WebhookHandlerConfig): (req: WebhookRequest, res: WebhookResponse) => Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Type export for the webhook handler function signature.
|
|
95
|
+
*/
|
|
96
|
+
export type WebhookHandlerFunction = (req: WebhookRequest, res: WebhookResponse) => Promise<void>;
|
|
97
|
+
export type { WebhookRequest, WebhookResponse, WebhookHandlerConfig };
|
|
98
|
+
//# sourceMappingURL=webhook.handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhook.handler.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/webhook.handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,KAAK,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AACvF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CA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
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebhookHandler = void 0;
|
|
4
|
+
exports.createWebhookHandler = createWebhookHandler;
|
|
5
|
+
const webhook_service_1 = require("./webhook.service");
|
|
6
|
+
/**
|
|
7
|
+
* WebhookHandler provides HTTP request handling for WayForPay payment callbacks.
|
|
8
|
+
* It validates incoming requests and delegates processing to the WebhookHandlerService.
|
|
9
|
+
*
|
|
10
|
+
* Usage with Express:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const handler = new WebhookHandler({ logger });
|
|
13
|
+
* app.post('/webhook/wayforpay', (req, res) => handler.handleRequest(req, res));
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Usage with Cloud Functions:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const handler = new WebhookHandler({ logger });
|
|
19
|
+
* export const webhookFunction = (req, res) => handler.handleRequest(req, res);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
class WebhookHandler {
|
|
23
|
+
constructor(config) {
|
|
24
|
+
this.logger = config.logger;
|
|
25
|
+
this.webhookService =
|
|
26
|
+
config.webhookService ||
|
|
27
|
+
new webhook_service_1.WebhookHandlerService({
|
|
28
|
+
logger: config.logger,
|
|
29
|
+
tokenPackPlans: config.tokenPackPlans,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Handles an incoming HTTP request from WayForPay.
|
|
34
|
+
* Validates the request body, processes the payment callback,
|
|
35
|
+
* and returns the appropriate response.
|
|
36
|
+
*
|
|
37
|
+
* @param req - The HTTP request object
|
|
38
|
+
* @param res - The HTTP response object
|
|
39
|
+
*/
|
|
40
|
+
async handleRequest(req, res) {
|
|
41
|
+
this.logger.info({
|
|
42
|
+
message: 'Received WayForPay webhook request',
|
|
43
|
+
payload: {},
|
|
44
|
+
});
|
|
45
|
+
// Parse and validate the request body
|
|
46
|
+
const parseResult = this.parseRequestBody(req.body);
|
|
47
|
+
if (!parseResult.success || !parseResult.data) {
|
|
48
|
+
this.logger.warning({
|
|
49
|
+
message: 'Invalid webhook request body',
|
|
50
|
+
payload: { error: parseResult.error },
|
|
51
|
+
});
|
|
52
|
+
res.status(400).json({
|
|
53
|
+
error: parseResult.error || 'Invalid request body',
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const callbackData = parseResult.data;
|
|
58
|
+
// Process the webhook
|
|
59
|
+
const result = await this.webhookService.processPaymentWebhook(callbackData);
|
|
60
|
+
// Return the response to WayForPay
|
|
61
|
+
this.sendResponse(res, result);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Processes callback data directly without HTTP request parsing.
|
|
65
|
+
* Useful for testing or when data is already parsed.
|
|
66
|
+
*
|
|
67
|
+
* @param callbackData - The parsed callback data from WayForPay
|
|
68
|
+
* @returns Processing result with response to send back to WayForPay
|
|
69
|
+
*/
|
|
70
|
+
async processCallback(callbackData) {
|
|
71
|
+
return this.webhookService.processPaymentWebhook(callbackData);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Parses and validates the webhook request body.
|
|
75
|
+
*/
|
|
76
|
+
parseRequestBody(body) {
|
|
77
|
+
if (!body || typeof body !== 'object') {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: 'Request body must be a JSON object',
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const data = body;
|
|
84
|
+
// Validate required fields
|
|
85
|
+
const requiredFields = [
|
|
86
|
+
'merchantAccount',
|
|
87
|
+
'orderReference',
|
|
88
|
+
'merchantSignature',
|
|
89
|
+
'amount',
|
|
90
|
+
'currency',
|
|
91
|
+
'transactionStatus',
|
|
92
|
+
];
|
|
93
|
+
const missingFields = requiredFields.filter((field) => data[field] === undefined || data[field] === null);
|
|
94
|
+
if (missingFields.length > 0) {
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
error: `Missing required fields: ${missingFields.join(', ')}`,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
// Validate transaction status
|
|
101
|
+
const validStatuses = ['Approved', 'Declined', 'Expired', 'Pending', 'InProcessing', 'Refunded'];
|
|
102
|
+
if (!validStatuses.includes(data.transactionStatus)) {
|
|
103
|
+
return {
|
|
104
|
+
success: false,
|
|
105
|
+
error: `Invalid transaction status: ${data.transactionStatus}`,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
success: true,
|
|
110
|
+
data: data,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Sends the response back to WayForPay.
|
|
115
|
+
* WayForPay expects a 200 status with a signed response body.
|
|
116
|
+
*/
|
|
117
|
+
sendResponse(res, result) {
|
|
118
|
+
// WayForPay expects a 200 status code even for declined transactions
|
|
119
|
+
// The accept/decline status is communicated in the response body
|
|
120
|
+
res.status(200).json(result.response);
|
|
121
|
+
this.logger.info({
|
|
122
|
+
message: 'Webhook response sent',
|
|
123
|
+
payload: {
|
|
124
|
+
success: result.success,
|
|
125
|
+
status: result.response.status,
|
|
126
|
+
orderReference: result.response.orderReference,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.WebhookHandler = WebhookHandler;
|
|
132
|
+
/**
|
|
133
|
+
* Creates a webhook handler function for use with serverless platforms.
|
|
134
|
+
* Returns a function that can be directly used as a Cloud Function or Lambda handler.
|
|
135
|
+
*
|
|
136
|
+
* @param config - Configuration options for the webhook handler
|
|
137
|
+
* @returns An async function that handles webhook requests
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* // Firebase Cloud Functions
|
|
142
|
+
* import * as functions from 'firebase-functions';
|
|
143
|
+
* import { createWebhookHandler } from 'miit-bot-payment';
|
|
144
|
+
*
|
|
145
|
+
* const logger = { info: console.log, warning: console.warn, error: console.error };
|
|
146
|
+
* export const paymentWebhook = functions.https.onRequest(createWebhookHandler({ logger }));
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
function createWebhookHandler(config) {
|
|
150
|
+
const handler = new WebhookHandler(config);
|
|
151
|
+
return (req, res) => handler.handleRequest(req, res);
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=webhook.handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhook.handler.js","sourceRoot":"","sources":["../../../src/modules/payments/webhook.handler.ts"],"names":[],"mappings":";;;AA8MA,oDAKC;AAlND,uDAA0D;AAwC1D;;;;;;;;;;;;;;;GAeG;AACH,MAAa,cAAc;IAIvB,YAAY,MAA4B;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,cAAc;YACf,MAAM,CAAC,cAAc;gBACrB,IAAI,uCAAqB,CAAC;oBACtB,MAAM,EAAE,MAAM,CAAC,MAAM;oBACrB,cAAc,EAAE,MAAM,CAAC,cAAc;iBACxC,CAAC,CAAC;IACX,CAAC;IAED;;;;;;;OAOG;IACI,KAAK,CAAC,aAAa,CAAC,GAAmB,EAAE,GAAoB;QAChE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;YACb,OAAO,EAAE,oCAAoC;YAC7C,OAAO,EAAE,EAAE;SACd,CAAC,CAAC;QAEH,sCAAsC;QACtC,MAAM,WAAW,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAEpD,IAAI,CAAC,WAAW,CAAC,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;gBAChB,OAAO,EAAE,8BAA8B;gBACvC,OAAO,EAAE,EAAE,KAAK,EAAE,WAAW,CAAC,KAAK,EAAE;aACxC,CAAC,CAAC;YAEH,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBACjB,KAAK,EAAE,W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