@misterhomer1992/miit-bot-payment 1.0.2 → 1.0.3
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/modules/invoice/types.d.ts +2 -2
- package/dist/modules/payments/api.d.ts +34 -0
- package/dist/modules/payments/api.d.ts.map +1 -0
- package/dist/modules/payments/api.js +63 -0
- package/dist/modules/payments/api.js.map +1 -0
- package/dist/modules/payments/index.d.ts +1 -0
- package/dist/modules/payments/index.d.ts.map +1 -1
- package/dist/modules/payments/index.js +1 -0
- package/dist/modules/payments/index.js.map +1 -1
- package/dist/modules/payments/service.d.ts +31 -0
- package/dist/modules/payments/service.d.ts.map +1 -1
- package/dist/modules/payments/service.js +75 -0
- package/dist/modules/payments/service.js.map +1 -1
- package/dist/modules/payments/utils.d.ts +9 -0
- package/dist/modules/payments/utils.d.ts.map +1 -0
- package/dist/modules/payments/utils.js +7 -0
- package/dist/modules/payments/utils.js.map +1 -0
- package/dist/modules/subscription/const.d.ts +4 -0
- package/dist/modules/subscription/const.d.ts.map +1 -0
- package/dist/modules/subscription/const.js +10 -0
- package/dist/modules/subscription/const.js.map +1 -0
- package/dist/modules/subscription/index.d.ts +5 -0
- package/dist/modules/subscription/index.d.ts.map +1 -0
- package/dist/modules/subscription/index.js +23 -0
- package/dist/modules/subscription/index.js.map +1 -0
- package/dist/modules/subscription/repository.d.ts +91 -0
- package/dist/modules/subscription/repository.d.ts.map +1 -0
- package/dist/modules/subscription/repository.js +146 -0
- package/dist/modules/subscription/repository.js.map +1 -0
- package/dist/modules/subscription/service.d.ts +101 -0
- package/dist/modules/subscription/service.d.ts.map +1 -0
- package/dist/modules/subscription/service.js +205 -0
- package/dist/modules/subscription/service.js.map +1 -0
- package/dist/modules/subscription/subscriptionPlan.d.ts +4 -0
- package/dist/modules/subscription/subscriptionPlan.d.ts.map +1 -0
- package/dist/modules/subscription/subscriptionPlan.js +63 -0
- package/dist/modules/subscription/subscriptionPlan.js.map +1 -0
- package/dist/modules/subscription/types.d.ts +37 -0
- package/dist/modules/subscription/types.d.ts.map +1 -0
- package/dist/modules/subscription/types.js +3 -0
- package/dist/modules/subscription/types.js.map +1 -0
- package/dist/modules/subscription/utils.d.ts +5 -0
- package/dist/modules/subscription/utils.d.ts.map +1 -0
- package/dist/modules/subscription/utils.js +16 -0
- package/dist/modules/subscription/utils.js.map +1 -0
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./modules/payments"), exports);
|
|
18
18
|
__exportStar(require("./modules/invoice"), exports);
|
|
19
|
+
__exportStar(require("./modules/subscription"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,oDAAkC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,oDAAkC;AAClC,yDAAuC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AppNamespace } from '../app/types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a payment API URL for regular purchases via Wayforpay.
|
|
4
|
+
* Generates a unique order reference and creates a payment URL with the provided parameters.
|
|
5
|
+
*
|
|
6
|
+
* @param params - Payment creation parameters
|
|
7
|
+
* @param params.userId - The unique identifier of the user making the payment
|
|
8
|
+
* @param params.appNamespace - The application namespace (e.g., 'app' or 'telegram')
|
|
9
|
+
* @param params.productName - The name of the product being purchased
|
|
10
|
+
* @param params.productPrice - The price of the product
|
|
11
|
+
* @param params.planId - The identifier of the subscription plan
|
|
12
|
+
* @param params.dateNext - Optional date for the next payment in the regular purchase cycle
|
|
13
|
+
* @param params.currency - The currency code (e.g., 'UAH', 'USD')
|
|
14
|
+
* @param params.regularCount - Optional number of regular payments (defaults to 3)
|
|
15
|
+
* @returns Promise resolving to an object containing the payment URL and order reference, or null
|
|
16
|
+
* @throws Error with code 'CREATE_PAYMENT_API' if the API response is invalid or missing URL
|
|
17
|
+
*/
|
|
18
|
+
declare function createPaymentAPI({ userId, appNamespace, productName, productPrice, planId, dateNext, currency, regularCount, regularMode, language, }: {
|
|
19
|
+
userId: string;
|
|
20
|
+
appNamespace: AppNamespace;
|
|
21
|
+
productName: string;
|
|
22
|
+
productPrice: number;
|
|
23
|
+
planId: string;
|
|
24
|
+
dateNext?: string;
|
|
25
|
+
currency: string;
|
|
26
|
+
regularCount?: number;
|
|
27
|
+
regularMode?: 'daily' | 'weekly' | 'monthly' | 'yearly';
|
|
28
|
+
language?: string;
|
|
29
|
+
}): Promise<{
|
|
30
|
+
url: string;
|
|
31
|
+
orderReference: string;
|
|
32
|
+
} | null>;
|
|
33
|
+
export { createPaymentAPI };
|
|
34
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/api.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAS5C;;;;;;;;;;;;;;;GAeG;AACH,iBAAe,gBAAgB,CAAC,EAC5B,MAAM,EACN,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,YAAgB,EAChB,WAAqB,EACrB,QAAe,GAClB,EAAE;IACC,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,YAAY,CAAC;IAC3B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAC;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB,GAAG,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAoC1D;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
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.createPaymentAPI = createPaymentAPI;
|
|
7
|
+
const moment_1 = __importDefault(require("moment"));
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
9
|
+
// @ts-ignore - Package may not have type declarations
|
|
10
|
+
const wayforpay_api_1 = require("@misterhomer1992/wayforpay-api");
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const utils_2 = require("./utils");
|
|
13
|
+
/**
|
|
14
|
+
* Creates a payment API URL for regular purchases via Wayforpay.
|
|
15
|
+
* Generates a unique order reference and creates a payment URL with the provided parameters.
|
|
16
|
+
*
|
|
17
|
+
* @param params - Payment creation parameters
|
|
18
|
+
* @param params.userId - The unique identifier of the user making the payment
|
|
19
|
+
* @param params.appNamespace - The application namespace (e.g., 'app' or 'telegram')
|
|
20
|
+
* @param params.productName - The name of the product being purchased
|
|
21
|
+
* @param params.productPrice - The price of the product
|
|
22
|
+
* @param params.planId - The identifier of the subscription plan
|
|
23
|
+
* @param params.dateNext - Optional date for the next payment in the regular purchase cycle
|
|
24
|
+
* @param params.currency - The currency code (e.g., 'UAH', 'USD')
|
|
25
|
+
* @param params.regularCount - Optional number of regular payments (defaults to 3)
|
|
26
|
+
* @returns Promise resolving to an object containing the payment URL and order reference, or null
|
|
27
|
+
* @throws Error with code 'CREATE_PAYMENT_API' if the API response is invalid or missing URL
|
|
28
|
+
*/
|
|
29
|
+
async function createPaymentAPI({ userId, appNamespace, productName, productPrice, planId, dateNext, currency, regularCount = 3, regularMode = 'daily', language = 'UA', }) {
|
|
30
|
+
const orderDate = Date.now();
|
|
31
|
+
const formattedUtc = moment_1.default.utc().toISOString();
|
|
32
|
+
const orderReference = (0, utils_2.createOrderReference)({
|
|
33
|
+
userId,
|
|
34
|
+
appNamespace,
|
|
35
|
+
planId,
|
|
36
|
+
utcDate: formattedUtc,
|
|
37
|
+
});
|
|
38
|
+
const response = (await (0, wayforpay_api_1.generateRegularPurchaseUrl)({
|
|
39
|
+
merchantAccount: process.env.WAYFORPAY_MERCHANT_ACCOUNT,
|
|
40
|
+
merchantDomainName: process.env.WAYFORPAY_MERCHANT_DOMAIN,
|
|
41
|
+
merchantSecretKey: process.env.WAYFORPAY_MERCHANT_SECRET_KEY,
|
|
42
|
+
orderDate,
|
|
43
|
+
orderReference,
|
|
44
|
+
productName: [productName],
|
|
45
|
+
productPrice: [productPrice],
|
|
46
|
+
currency,
|
|
47
|
+
language,
|
|
48
|
+
serviceUrl: process.env.WAYFORPAY_SERVICE_URL,
|
|
49
|
+
regularMode,
|
|
50
|
+
regularCount,
|
|
51
|
+
dateNext,
|
|
52
|
+
}));
|
|
53
|
+
if (!(0, utils_1.isObject)(response) || (0, utils_1.isBadOrEmptyString)(response.url)) {
|
|
54
|
+
throw {
|
|
55
|
+
code: 'CREATE_PAYMENT_API',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
url: response.url,
|
|
60
|
+
orderReference,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/modules/payments/api.ts"],"names":[],"mappings":";;;;;AAyFS,4CAAgB;AAzFzB,oDAA4B;AAC5B,6DAA6D;AAC7D,sDAAsD;AACtD,kEAA4E;AAE5E,uCAA2D;AAC3D,mCAA+C;AAO/C;;;;;;;;;;;;;;;GAeG;AACH,KAAK,UAAU,gBAAgB,CAAC,EAC5B,MAAM,EACN,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,YAAY,GAAG,CAAC,EAChB,WAAW,GAAG,OAAO,EACrB,QAAQ,GAAG,IAAI,GAYlB;IACG,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC7B,MAAM,YAAY,GAAG,gBAAM,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC;IAChD,MAAM,cAAc,GAAG,IAAA,4BAAoB,EAAC;QACxC,MAAM;QACN,YAAY;QACZ,MAAM;QACN,OAAO,EAAE,YAAY;KACxB,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,CAAC,MAAM,IAAA,0CAA0B,EAAC;QAC/C,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,0BAAoC;QACjE,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,yBAAmC;QACnE,iBAAiB,EAAE,OAAO,CAAC,GAAG,CAAC,6BAAuC;QACtE,SAAS;QACT,cAAc;QACd,WAAW,EAAE,CAAC,WAAW,CAAC;QAC1B,YAAY,EAAE,CAAC,YAAY,CAAC;QAC5B,QAAQ;QACR,QAAQ;QACR,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,qBAA+B;QACvD,WAAW;QACX,YAAY;QACZ,QAAQ;KACX,CAAC,CAAsB,CAAC;IAEzB,IAAI,CAAC,IAAA,gBAAQ,EAAC,QAAQ,CAAC,IAAI,IAAA,0BAAkB,EAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1D,MAAM;YACF,IAAI,EAAE,oBAAoB;SAC7B,CAAC;IACN,CAAC;IAED,OAAO;QACH,GAAG,EAAE,QAAQ,CAAC,GAAG;QACjB,cAAc;KACjB,CAAC;AACN,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC"}
|
|
@@ -18,4 +18,5 @@ __exportStar(require("./repository"), exports);
|
|
|
18
18
|
__exportStar(require("./service"), exports);
|
|
19
19
|
__exportStar(require("./types"), exports);
|
|
20
20
|
__exportStar(require("./const"), exports);
|
|
21
|
+
__exportStar(require("./api"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/modules/payments/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,4CAA0B;AAC1B,0CAAwB;AACxB,0CAAwB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/modules/payments/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,4CAA0B;AAC1B,0CAAwB;AACxB,0CAAwB;AACxB,wCAAsB"}
|
|
@@ -61,5 +61,36 @@ export declare class PaymentService {
|
|
|
61
61
|
getExpiredPendingPayments(params?: {
|
|
62
62
|
hoursOld?: number;
|
|
63
63
|
}): Promise<PaymentEntity[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Creates a payment intent for a user subscription.
|
|
66
|
+
* This method orchestrates the payment creation process by:
|
|
67
|
+
* 1. Creating a payment URL via Wayforpay API
|
|
68
|
+
* 2. Storing the payment record in the database
|
|
69
|
+
* 3. Returning the complete payment entity with the payment link
|
|
70
|
+
*
|
|
71
|
+
* @param params - Payment intent parameters
|
|
72
|
+
* @param params.userId - The unique identifier of the user
|
|
73
|
+
* @param params.appNamespace - The application namespace/platform
|
|
74
|
+
* @param params.planId - The subscription plan identifier
|
|
75
|
+
* @param params.productName - The name of the product/plan being purchased
|
|
76
|
+
* @param params.productPrice - The price of the product
|
|
77
|
+
* @param params.currency - The currency code ('UAH' or 'USD')
|
|
78
|
+
* @param params.regularCount - Optional number of regular payments (defaults to 3)
|
|
79
|
+
* @param params.regularMode - Optional payment frequency ('daily', 'weekly', 'monthly', 'yearly')
|
|
80
|
+
* @param params.language - Optional language code for the payment page (defaults to 'UA')
|
|
81
|
+
* @returns Promise resolving to the created PaymentEntity with payment link
|
|
82
|
+
* @throws Error if payment URL generation or database creation fails
|
|
83
|
+
*/
|
|
84
|
+
createPaymentIntent(params: {
|
|
85
|
+
userId: string;
|
|
86
|
+
appNamespace: AppNamespace;
|
|
87
|
+
planId: string;
|
|
88
|
+
productName: string;
|
|
89
|
+
productPrice: number;
|
|
90
|
+
currency: 'UAH' | 'USD';
|
|
91
|
+
regularCount?: number;
|
|
92
|
+
regularMode?: 'daily' | 'weekly' | 'monthly' | 'yearly';
|
|
93
|
+
language?: string;
|
|
94
|
+
}): Promise<PaymentEntity>;
|
|
64
95
|
}
|
|
65
96
|
//# sourceMappingURL=service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAqB,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C;;;GAGG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAE/C;;;OAGG;gBACS,MAAM,EAAE,MAAM;IAK1B;;;;OAIG;IACU,mBAAmB,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAevF;;;;;;;OAOG;IACU,WAAW,CAAC,MAAM,EAAE;QAC7B,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,YAAY,CAAC;QAC3B,MAAM,CAAC,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAC;KACpC,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAiB5B;;;;;OAKG;IACU,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,aAAa,CAAC;IAiBnF;;;;;;OAMG;IACU,YAAY,CAAC,MAAM,EAAE;QAAE,cAAc,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,qBAAqB,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAiB3G;;;;;;OAMG;IACU,yBAAyB,CAClC,MAAM,GAAE;QACJ,QAAQ,CAAC,EAAE,MAAM,CAAC;KAChB,GACP,OAAO,CAAC,aAAa,EAAE,CAAC;IAe3B;;;;;;;;;;;;;;;;;;;OAmBG;IACU,mBAAmB,CAAC,MAAM,EAAE;QACrC,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,YAAY,CAAC;QAC3B,MAAM,EAAE,MAAM,CAAC;QACf,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;QACrB,QAAQ,EAAE,KAAK,GAAG,KAAK,CAAC;QACxB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,WAAW,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAC;QACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;KACrB,GAAG,OAAO,CAAC,aAAa,CAAC;CAgE7B"}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.PaymentService = void 0;
|
|
7
|
+
const moment_1 = __importDefault(require("moment"));
|
|
4
8
|
const repository_1 = require("./repository");
|
|
9
|
+
const api_1 = require("./api");
|
|
5
10
|
/**
|
|
6
11
|
* PaymentService class handles business logic related to payments.
|
|
7
12
|
* Acts as an intermediary between controllers/handlers and the repository layer.
|
|
@@ -127,6 +132,76 @@ class PaymentService {
|
|
|
127
132
|
return [];
|
|
128
133
|
}
|
|
129
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Creates a payment intent for a user subscription.
|
|
137
|
+
* This method orchestrates the payment creation process by:
|
|
138
|
+
* 1. Creating a payment URL via Wayforpay API
|
|
139
|
+
* 2. Storing the payment record in the database
|
|
140
|
+
* 3. Returning the complete payment entity with the payment link
|
|
141
|
+
*
|
|
142
|
+
* @param params - Payment intent parameters
|
|
143
|
+
* @param params.userId - The unique identifier of the user
|
|
144
|
+
* @param params.appNamespace - The application namespace/platform
|
|
145
|
+
* @param params.planId - The subscription plan identifier
|
|
146
|
+
* @param params.productName - The name of the product/plan being purchased
|
|
147
|
+
* @param params.productPrice - The price of the product
|
|
148
|
+
* @param params.currency - The currency code ('UAH' or 'USD')
|
|
149
|
+
* @param params.regularCount - Optional number of regular payments (defaults to 3)
|
|
150
|
+
* @param params.regularMode - Optional payment frequency ('daily', 'weekly', 'monthly', 'yearly')
|
|
151
|
+
* @param params.language - Optional language code for the payment page (defaults to 'UA')
|
|
152
|
+
* @returns Promise resolving to the created PaymentEntity with payment link
|
|
153
|
+
* @throws Error if payment URL generation or database creation fails
|
|
154
|
+
*/
|
|
155
|
+
async createPaymentIntent(params) {
|
|
156
|
+
const { userId, appNamespace, planId, productName, productPrice, currency, regularCount, regularMode, language, } = params;
|
|
157
|
+
try {
|
|
158
|
+
// Step 1: Create payment URL from Wayforpay API
|
|
159
|
+
// This also generates a unique order reference
|
|
160
|
+
const paymentResponse = await (0, api_1.createPaymentAPI)({
|
|
161
|
+
userId,
|
|
162
|
+
appNamespace,
|
|
163
|
+
productName,
|
|
164
|
+
productPrice,
|
|
165
|
+
planId,
|
|
166
|
+
currency,
|
|
167
|
+
regularCount,
|
|
168
|
+
regularMode,
|
|
169
|
+
language,
|
|
170
|
+
});
|
|
171
|
+
if (!paymentResponse?.url || !paymentResponse?.orderReference) {
|
|
172
|
+
throw new Error('Failed to get payment URL or order reference from Wayforpay');
|
|
173
|
+
}
|
|
174
|
+
// Step 2: Create payment record in database
|
|
175
|
+
const paymentEntity = await this.repository.create({
|
|
176
|
+
orderReference: paymentResponse.orderReference,
|
|
177
|
+
userId,
|
|
178
|
+
status: 'pending',
|
|
179
|
+
paymentLink: paymentResponse.url,
|
|
180
|
+
planId,
|
|
181
|
+
amount: productPrice,
|
|
182
|
+
currency,
|
|
183
|
+
createdAt: moment_1.default.utc().toISOString(),
|
|
184
|
+
platform: appNamespace,
|
|
185
|
+
provider: 'wayforpay',
|
|
186
|
+
});
|
|
187
|
+
return paymentEntity;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
this.logger.error({
|
|
191
|
+
message: 'Error in payment service createPaymentIntent',
|
|
192
|
+
payload: {
|
|
193
|
+
userId,
|
|
194
|
+
appNamespace,
|
|
195
|
+
planId,
|
|
196
|
+
productName,
|
|
197
|
+
productPrice,
|
|
198
|
+
currency,
|
|
199
|
+
error: JSON.stringify(error),
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
130
205
|
}
|
|
131
206
|
exports.PaymentService = PaymentService;
|
|
132
207
|
//# sourceMappingURL=service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/modules/payments/service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/modules/payments/service.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAE5B,6CAAwE;AAGxE,+BAAyC;AAEzC;;;GAGG;AACH,MAAa,cAAc;IAIvB;;;OAGG;IACH,YAAY,MAAc;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,UAAU,GAAG,IAAI,8BAAiB,EAAE,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,mBAAmB,CAAC,cAAsB;QACnD,IAAI,CAAC;YACD,OAAO,MAAM,IAAI,CAAC,UAAU,CAAC,mBAAmB,CAAC,cAAc,CAAC,CAAC;QACrE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,I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AAM;gBACN,YAAY;gBACZ,WAAW;gBACX,YAAY;gBACZ,MAAM;gBACN,QAAQ;gBACR,YAAY;gBACZ,WAAW;gBACX,QAAQ;aACX,CAAC,CAAC;YAEH,IAAI,CAAC,eAAe,EAAE,GAAG,IAAI,CAAC,eAAe,EAAE,cAAc,EAAE,CAAC;gBAC5D,MAAM,IAAI,KAAK,CAAC,6DAA6D,CAAC,CAAC;YACnF,CAAC;YAED,4CAA4C;YAC5C,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;gBAC/C,cAAc,EAAE,eAAe,CAAC,cAAc;gBAC9C,MAAM;gBACN,MAAM,EAAE,SAAS;gBACjB,WAAW,EAAE,eAAe,CAAC,GAAG;gBAChC,MAAM;gBACN,MAAM,EAAE,YAAY;gBACpB,QAAQ;gBACR,SAAS,EAAE,gBAAM,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE;gBACrC,QAAQ,EAAE,YAAY;gBACtB,QAAQ,EAAE,WAAW;aACxB,CAAC,CAAC;YAEH,OAAO,aAAa,CAAC;QACzB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;gBACd,OAAO,EAAE,8CAA8C;gBACvD,OAAO,EAAE;oBACL,MAAM;oBACN,YAAY;oBACZ,MAAM;oBACN,WAAW;oBACX,YAAY;oBACZ,QAAQ;oBACR,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;iBAC/B;aACJ,CAAC,CAAC;YAEH,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC;CACJ;AArOD,wCAqOC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AppNamespace } from '../app/types';
|
|
2
|
+
declare function createOrderReference({ userId, appNamespace, planId, utcDate, }: {
|
|
3
|
+
userId: string;
|
|
4
|
+
appNamespace: AppNamespace;
|
|
5
|
+
planId: string;
|
|
6
|
+
utcDate: string;
|
|
7
|
+
}): string;
|
|
8
|
+
export { createOrderReference };
|
|
9
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/modules/payments/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,iBAAS,oBAAoB,CAAC,EAC1B,MAAM,EACN,YAAY,EACZ,MAAM,EACN,OAAO,GACV,EAAE;IACC,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,YAAY,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACnB,GAAG,MAAM,CAET;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createOrderReference = createOrderReference;
|
|
4
|
+
function createOrderReference({ userId, appNamespace, planId, utcDate, }) {
|
|
5
|
+
return `miia_${appNamespace}_bot_${userId}_${planId}_v1_${utcDate}`;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/modules/payments/utils.ts"],"names":[],"mappings":";;AAgBS,oDAAoB;AAd7B,SAAS,oBAAoB,CAAC,EAC1B,MAAM,EACN,YAAY,EACZ,MAAM,EACN,OAAO,GAMV;IACG,OAAO,QAAQ,YAAY,QAAQ,MAAM,IAAI,MAAM,OAAO,OAAO,EAAE,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/const.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAE7C,QAAA,MAAM,2BAA2B,EAAE,OAAO,CAAC,kBAAkB,CAI5D,CAAC;AAEF,OAAO,EAAE,2BAA2B,EAAE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_SUBSCRIPTION_ENTITY = void 0;
|
|
4
|
+
const DEFAULT_SUBSCRIPTION_ENTITY = {
|
|
5
|
+
status: 'active',
|
|
6
|
+
platform: 'telegram',
|
|
7
|
+
provider: 'wayforpay',
|
|
8
|
+
};
|
|
9
|
+
exports.DEFAULT_SUBSCRIPTION_ENTITY = DEFAULT_SUBSCRIPTION_ENTITY;
|
|
10
|
+
//# sourceMappingURL=const.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const.js","sourceRoot":"","sources":["../../../src/modules/subscription/const.ts"],"names":[],"mappings":";;;AAEA,MAAM,2BAA2B,GAAgC;IAC7D,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,WAAW;CACxB,CAAC;AAEO,kEAA2B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.SUBSCRIPTION_PLAN = void 0;
|
|
18
|
+
__exportStar(require("./utils"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
20
|
+
__exportStar(require("./service"), exports);
|
|
21
|
+
var subscriptionPlan_1 = require("./subscriptionPlan");
|
|
22
|
+
Object.defineProperty(exports, "SUBSCRIPTION_PLAN", { enumerable: true, get: function () { return subscriptionPlan_1.SUBSCRIPTION_PLAN; } });
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/modules/subscription/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,0CAAwB;AACxB,4CAA0B;AAC1B,uDAAuD;AAA9C,qHAAA,iBAAiB,OAAA"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { FieldValue } from 'firebase-admin/firestore';
|
|
2
|
+
import { SubscriptionEntity, SubscriptionFieldPath } from './types';
|
|
3
|
+
import { AppNamespace } from '../app/types';
|
|
4
|
+
export type UpdateDBSubscriptionFields = [
|
|
5
|
+
SubscriptionFieldPath,
|
|
6
|
+
FieldValue | string | number | boolean | Date | [] | {}
|
|
7
|
+
][];
|
|
8
|
+
/**
|
|
9
|
+
* SubscriptionRepository class handles all database operations related to subscriptions.
|
|
10
|
+
* Implements repository pattern for subscription data access.
|
|
11
|
+
*/
|
|
12
|
+
export declare class SubscriptionRepository {
|
|
13
|
+
private readonly db;
|
|
14
|
+
private readonly collectionName;
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of SubscriptionRepository.
|
|
17
|
+
*/
|
|
18
|
+
constructor();
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves an active subscription for a specific user.
|
|
21
|
+
* @param params - Query parameters
|
|
22
|
+
* @param params.userId - The user's unique identifier
|
|
23
|
+
* @param params.appNamespace - The application namespace/platform
|
|
24
|
+
* @returns Promise resolving to SubscriptionEntity or null if not found
|
|
25
|
+
*/
|
|
26
|
+
getByUserId(params: {
|
|
27
|
+
userId: string;
|
|
28
|
+
appNamespace: string;
|
|
29
|
+
}): Promise<SubscriptionEntity | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new subscription record in the database.
|
|
32
|
+
* @param params - Subscription creation parameters
|
|
33
|
+
* @param params.userId - The user's unique identifier
|
|
34
|
+
* @param params.appNamespace - The application namespace/platform
|
|
35
|
+
* @param params.planId - The subscription plan identifier
|
|
36
|
+
* @param params.expiresAt - ISO timestamp when subscription expires
|
|
37
|
+
* @param params.startedAt - ISO timestamp when subscription started
|
|
38
|
+
* @returns Promise resolving to created SubscriptionEntity with ID
|
|
39
|
+
* @throws Error if subscription creation fails
|
|
40
|
+
*/
|
|
41
|
+
create(params: {
|
|
42
|
+
userId: string;
|
|
43
|
+
appNamespace: AppNamespace;
|
|
44
|
+
planId: string;
|
|
45
|
+
expiresAt: string;
|
|
46
|
+
startedAt: string;
|
|
47
|
+
}): Promise<SubscriptionEntity>;
|
|
48
|
+
/**
|
|
49
|
+
* Updates specific fields of a subscription identified by userId and appNamespace.
|
|
50
|
+
* @param params - Update parameters
|
|
51
|
+
* @param params.userId - The user's unique identifier
|
|
52
|
+
* @param params.appNamespace - The application namespace/platform
|
|
53
|
+
* @param params.fields - Array of field paths and values to update
|
|
54
|
+
* @throws Error if subscription not found or update fails
|
|
55
|
+
*/
|
|
56
|
+
updateFieldsByUserId(params: {
|
|
57
|
+
userId: string;
|
|
58
|
+
appNamespace: AppNamespace;
|
|
59
|
+
fields: UpdateDBSubscriptionFields;
|
|
60
|
+
}): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Retrieves all active subscriptions that have expired.
|
|
63
|
+
* Used for batch processing to update subscription statuses.
|
|
64
|
+
* @returns Promise resolving to array of expired SubscriptionEntity
|
|
65
|
+
*/
|
|
66
|
+
getExpiredActiveSubscriptions(): Promise<SubscriptionEntity[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Updates specific fields of a subscription identified by subscription ID.
|
|
69
|
+
* @param params - Update parameters
|
|
70
|
+
* @param params.subscriptionId - The subscription document ID
|
|
71
|
+
* @param params.fields - Array of field paths and values to update
|
|
72
|
+
* @throws Error if update fails
|
|
73
|
+
*/
|
|
74
|
+
updateFieldsById(params: {
|
|
75
|
+
subscriptionId: string;
|
|
76
|
+
fields: UpdateDBSubscriptionFields;
|
|
77
|
+
}): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Maps a Firestore document to a SubscriptionEntity.
|
|
80
|
+
* @param doc - Firestore document snapshot
|
|
81
|
+
* @returns SubscriptionEntity with document ID
|
|
82
|
+
*/
|
|
83
|
+
private mapDocumentToEntity;
|
|
84
|
+
/**
|
|
85
|
+
* Builds an update object from field tuples.
|
|
86
|
+
* @param fields - Array of field paths and values
|
|
87
|
+
* @returns Object with field paths as keys and values to update
|
|
88
|
+
*/
|
|
89
|
+
private buildUpdateObject;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=repository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAkD,MAAM,0BAA0B,CAAC;AACtG,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEpE,OAAO,EAAE,YAAY,EAAE,MAAM,c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|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SubscriptionRepository = void 0;
|
|
4
|
+
const firestore_1 = require("firebase-admin/firestore");
|
|
5
|
+
const const_1 = require("./const");
|
|
6
|
+
/**
|
|
7
|
+
* SubscriptionRepository class handles all database operations related to subscriptions.
|
|
8
|
+
* Implements repository pattern for subscription data access.
|
|
9
|
+
*/
|
|
10
|
+
class SubscriptionRepository {
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of SubscriptionRepository.
|
|
13
|
+
*/
|
|
14
|
+
constructor() {
|
|
15
|
+
this.collectionName = 'subscriptions';
|
|
16
|
+
this.db = (0, firestore_1.getFirestore)();
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves an active subscription for a specific user.
|
|
20
|
+
* @param params - Query parameters
|
|
21
|
+
* @param params.userId - The user's unique identifier
|
|
22
|
+
* @param params.appNamespace - The application namespace/platform
|
|
23
|
+
* @returns Promise resolving to SubscriptionEntity or null if not found
|
|
24
|
+
*/
|
|
25
|
+
async getByUserId(params) {
|
|
26
|
+
const { userId, appNamespace } = params;
|
|
27
|
+
const querySnapshot = await this.db
|
|
28
|
+
.collection(this.collectionName)
|
|
29
|
+
.where('platform', '==', appNamespace)
|
|
30
|
+
.where('userId', '==', userId)
|
|
31
|
+
.where('status', '==', 'active')
|
|
32
|
+
.limit(1)
|
|
33
|
+
.get();
|
|
34
|
+
if (querySnapshot.empty) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const doc = querySnapshot.docs[0];
|
|
38
|
+
return this.mapDocumentToEntity(doc);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new subscription record in the database.
|
|
42
|
+
* @param params - Subscription creation parameters
|
|
43
|
+
* @param params.userId - The user's unique identifier
|
|
44
|
+
* @param params.appNamespace - The application namespace/platform
|
|
45
|
+
* @param params.planId - The subscription plan identifier
|
|
46
|
+
* @param params.expiresAt - ISO timestamp when subscription expires
|
|
47
|
+
* @param params.startedAt - ISO timestamp when subscription started
|
|
48
|
+
* @returns Promise resolving to created SubscriptionEntity with ID
|
|
49
|
+
* @throws Error if subscription creation fails
|
|
50
|
+
*/
|
|
51
|
+
async create(params) {
|
|
52
|
+
const { userId, appNamespace, planId, expiresAt, startedAt } = params;
|
|
53
|
+
const subscriptionEntity = {
|
|
54
|
+
...const_1.DEFAULT_SUBSCRIPTION_ENTITY,
|
|
55
|
+
userId,
|
|
56
|
+
platform: appNamespace,
|
|
57
|
+
planId,
|
|
58
|
+
expiresAt,
|
|
59
|
+
startedAt,
|
|
60
|
+
};
|
|
61
|
+
const docRef = await this.db.collection(this.collectionName).add(subscriptionEntity);
|
|
62
|
+
if (!docRef.id) {
|
|
63
|
+
throw new Error(`Failed to create subscription for userId: ${userId}`);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
id: docRef.id,
|
|
67
|
+
...subscriptionEntity,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Updates specific fields of a subscription identified by userId and appNamespace.
|
|
72
|
+
* @param params - Update parameters
|
|
73
|
+
* @param params.userId - The user's unique identifier
|
|
74
|
+
* @param params.appNamespace - The application namespace/platform
|
|
75
|
+
* @param params.fields - Array of field paths and values to update
|
|
76
|
+
* @throws Error if subscription not found or update fails
|
|
77
|
+
*/
|
|
78
|
+
async updateFieldsByUserId(params) {
|
|
79
|
+
const { userId, appNamespace, fields } = params;
|
|
80
|
+
const updateObject = this.buildUpdateObject(fields);
|
|
81
|
+
const querySnapshot = await this.db
|
|
82
|
+
.collection(this.collectionName)
|
|
83
|
+
.where('platform', '==', appNamespace)
|
|
84
|
+
.where('userId', '==', userId)
|
|
85
|
+
.limit(1)
|
|
86
|
+
.get();
|
|
87
|
+
if (querySnapshot.empty) {
|
|
88
|
+
throw new Error(`Subscription not found for userId: ${userId}`);
|
|
89
|
+
}
|
|
90
|
+
await querySnapshot.docs[0].ref.update(updateObject);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves all active subscriptions that have expired.
|
|
94
|
+
* Used for batch processing to update subscription statuses.
|
|
95
|
+
* @returns Promise resolving to array of expired SubscriptionEntity
|
|
96
|
+
*/
|
|
97
|
+
async getExpiredActiveSubscriptions() {
|
|
98
|
+
const now = new Date().toISOString();
|
|
99
|
+
const querySnapshot = await this.db
|
|
100
|
+
.collection(this.collectionName)
|
|
101
|
+
.where('status', '==', 'active')
|
|
102
|
+
.where('expiresAt', '<', now)
|
|
103
|
+
.get();
|
|
104
|
+
if (querySnapshot.empty) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
return querySnapshot.docs.map((doc) => this.mapDocumentToEntity(doc));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Updates specific fields of a subscription identified by subscription ID.
|
|
111
|
+
* @param params - Update parameters
|
|
112
|
+
* @param params.subscriptionId - The subscription document ID
|
|
113
|
+
* @param params.fields - Array of field paths and values to update
|
|
114
|
+
* @throws Error if update fails
|
|
115
|
+
*/
|
|
116
|
+
async updateFieldsById(params) {
|
|
117
|
+
const { subscriptionId, fields } = params;
|
|
118
|
+
const updateObject = this.buildUpdateObject(fields);
|
|
119
|
+
await this.db.collection(this.collectionName).doc(subscriptionId).update(updateObject);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Maps a Firestore document to a SubscriptionEntity.
|
|
123
|
+
* @param doc - Firestore document snapshot
|
|
124
|
+
* @returns SubscriptionEntity with document ID
|
|
125
|
+
*/
|
|
126
|
+
mapDocumentToEntity(doc) {
|
|
127
|
+
return {
|
|
128
|
+
id: doc.id,
|
|
129
|
+
...doc.data(),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Builds an update object from field tuples.
|
|
134
|
+
* @param fields - Array of field paths and values
|
|
135
|
+
* @returns Object with field paths as keys and values to update
|
|
136
|
+
*/
|
|
137
|
+
buildUpdateObject(fields) {
|
|
138
|
+
const updateObject = {};
|
|
139
|
+
fields.forEach(([fieldPath, value]) => {
|
|
140
|
+
updateObject[fieldPath] = value;
|
|
141
|
+
});
|
|
142
|
+
return updateObject;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.SubscriptionRepository = SubscriptionRepository;
|
|
146
|
+
//# sourceMappingURL=repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../../src/modules/subscription/repository.ts"],"names":[],"mappings":";;;AAAA,wDAAsG;AAEtG,mCAAsD;AAQtD;;;GAGG;AACH,MAAa,sBAAsB;IAI/B;;OAEG;IACH;QALiB,mBAAc,GAAG,eAAe,CAAC;QAM9C,IAAI,CAAC,EAAE,GAAG,IAAA,wBAAY,GAAE,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,WAAW,CAAC,MAAgD;QACrE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;QAExC,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,EAAE;aAC9B,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC;aAC/B,KAAK,CAAC,UAAU,EAAE,IAAI,EAAE,YAAY,CAAC;aACrC,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,MAAM,CAAC;aAC7B,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC;aAC/B,KAAK,CAAC,CAAC,CAAC;aACR,GAAG,EAAE,CAAC;QAEX,IAAI,aAAa,CAAC,KAAK,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,GAAG,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClC,OAAO,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;;;;;OAUG;IACI,KAAK,CAAC,MAAM,CAAC,MAMnB;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,CAAC,GAAG,KAAK,CAAC;QACpC,CAAC,CAAC,CAAC;QAEH,OAAO,YAAY,CAAC;IACxB,CAAC;CACJ;AA9KD,wDA8KC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Logger } from '../logger/types';
|
|
2
|
+
import { UpdateDBSubscriptionFields } from './repository';
|
|
3
|
+
import { SubscriptionEntity } from './types';
|
|
4
|
+
import { AppNamespace } from '../app/types';
|
|
5
|
+
/**
|
|
6
|
+
* SubscriptionService class handles business logic related to subscriptions.
|
|
7
|
+
* Acts as an intermediary between controllers/handlers and the repository layer.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SubscriptionService {
|
|
10
|
+
private readonly logger;
|
|
11
|
+
private readonly repository;
|
|
12
|
+
private readonly paymentService;
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of SubscriptionService.
|
|
15
|
+
* @param logger - Application logger instance for logging operations
|
|
16
|
+
*/
|
|
17
|
+
constructor(logger: Logger);
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves an active subscription for a specific user.
|
|
20
|
+
* @param params - Query parameters
|
|
21
|
+
* @param params.userId - The user's unique identifier
|
|
22
|
+
* @param params.appNamespace - The application namespace/platform
|
|
23
|
+
* @returns Promise resolving to SubscriptionEntity or null if not found
|
|
24
|
+
*/
|
|
25
|
+
getByUserId(params: {
|
|
26
|
+
userId: string;
|
|
27
|
+
appNamespace: string;
|
|
28
|
+
}): Promise<SubscriptionEntity | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new subscription record in the database.
|
|
31
|
+
* @param params - Subscription creation parameters
|
|
32
|
+
* @param params.userId - The user's unique identifier
|
|
33
|
+
* @param params.appNamespace - The application namespace/platform
|
|
34
|
+
* @param params.planId - The subscription plan identifier
|
|
35
|
+
* @param params.expiresAt - ISO timestamp when subscription expires
|
|
36
|
+
* @param params.startedAt - ISO timestamp when subscription started
|
|
37
|
+
* @returns Promise resolving to created SubscriptionEntity with ID
|
|
38
|
+
* @throws Error if subscription creation fails
|
|
39
|
+
*/
|
|
40
|
+
create(params: {
|
|
41
|
+
userId: string;
|
|
42
|
+
appNamespace: AppNamespace;
|
|
43
|
+
planId: string;
|
|
44
|
+
expiresAt: string;
|
|
45
|
+
startedAt: string;
|
|
46
|
+
}): Promise<SubscriptionEntity>;
|
|
47
|
+
/**
|
|
48
|
+
* Updates specific fields of a subscription identified by userId and appNamespace.
|
|
49
|
+
* @param params - Update parameters
|
|
50
|
+
* @param params.userId - The user's unique identifier
|
|
51
|
+
* @param params.appNamespace - The application namespace/platform
|
|
52
|
+
* @param params.fields - Array of field paths and values to update
|
|
53
|
+
* @throws Error if subscription not found or update fails
|
|
54
|
+
*/
|
|
55
|
+
updateFieldsByUserId(params: {
|
|
56
|
+
userId: string;
|
|
57
|
+
appNamespace: AppNamespace;
|
|
58
|
+
fields: UpdateDBSubscriptionFields;
|
|
59
|
+
}): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves all active subscriptions that have expired.
|
|
62
|
+
* Used for batch processing to update subscription statuses.
|
|
63
|
+
* @returns Promise resolving to array of expired SubscriptionEntity
|
|
64
|
+
*/
|
|
65
|
+
getExpiredActiveSubscriptions(): Promise<SubscriptionEntity[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Updates specific fields of a subscription identified by subscription ID.
|
|
68
|
+
* @param params - Update parameters
|
|
69
|
+
* @param params.subscriptionId - The subscription document ID
|
|
70
|
+
* @param params.fields - Array of field paths and values to update
|
|
71
|
+
* @throws Error if update fails
|
|
72
|
+
*/
|
|
73
|
+
updateFieldsById(params: {
|
|
74
|
+
subscriptionId: string;
|
|
75
|
+
fields: UpdateDBSubscriptionFields;
|
|
76
|
+
}): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Gets or creates a subscription payment URL.
|
|
79
|
+
* Checks for recent pending payments (within 3 minutes) and reuses the payment link if found.
|
|
80
|
+
* Otherwise creates a new payment intent for the subscription.
|
|
81
|
+
* @param params - Payment URL creation parameters
|
|
82
|
+
* @param params.userId - The user's unique identifier
|
|
83
|
+
* @param params.appNamespace - The application namespace/platform
|
|
84
|
+
* @param params.planId - The subscription plan identifier
|
|
85
|
+
* @param params.languageCode - Language code for translations
|
|
86
|
+
* @param params.translate - Translation function
|
|
87
|
+
* @returns Promise resolving to the payment URL string
|
|
88
|
+
* @throws Error if subscription plan not found or payment creation fails
|
|
89
|
+
*/
|
|
90
|
+
getOrCreateSubscriptionPaymentUrl(params: {
|
|
91
|
+
userId: string;
|
|
92
|
+
appNamespace: AppNamespace;
|
|
93
|
+
planId: string;
|
|
94
|
+
languageCode: string;
|
|
95
|
+
translate: (params: {
|
|
96
|
+
code: string;
|
|
97
|
+
path: string;
|
|
98
|
+
}) => string;
|
|
99
|
+
}): Promise<string>;
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAA0B,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAClF,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAI5C;;;GAGG;AACH,qBAAa,mBAAmB;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyB;IACpD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAEhD;;;OAGG;gBACS,MAAM,EAAE,MAAM;IAM1B;;;;;;OAMG;IACU,WAAW,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAgB9G;;;;;;;;;;OAUG;IACU,MAAM,CAAC,MAAM,EAAE;QACxB,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,YAAY,CAAC;QAC3B,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACrB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAkB/B;;;;;;;OAOG;IACU,oBAAoB,CAAC,MAAM,EAAE;QACtC,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,YAAY,CAAC;QAC3B,MAAM,EAAE,0BAA0B,CAAC;KACtC,GAAG,OAAO,CAAC,IAAI,CAAC;IAkBjB;;;;OAIG;IACU,6BAA6B,IAAI,OAAO,CAAC,kBAAkB,EAAE,CAAC;IAc3E;;;;;;OAMG;IACU,gBAAgB,CAAC,MAAM,EAAE;QAClC,cAAc,EAAE,MAAM,CAAC;QACvB,MAAM,EAAE,0BAA0B,CAAC;KACtC,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBjB;;;;;;;;;;;;OAYG;IACU,iCAAiC,CAAC,MAAM,EAAE;QACnD,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,YAAY,CAAC;QAC3B,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,MAAM,CAAC;QACrB,SAAS,EAAE,CAAC,MAAM,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,KAAK,MAAM,CAAC;KACjE,GAAG,OAAO,CAAC,MAAM,CAAC;CAwDtB"}
|
|
@@ -0,0 +1,205 @@
|
|
|
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.SubscriptionService = void 0;
|
|
7
|
+
const moment_1 = __importDefault(require("moment"));
|
|
8
|
+
const repository_1 = require("./repository");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const service_1 = require("../payments/service");
|
|
11
|
+
/**
|
|
12
|
+
* SubscriptionService class handles business logic related to subscriptions.
|
|
13
|
+
* Acts as an intermediary between controllers/handlers and the repository layer.
|
|
14
|
+
*/
|
|
15
|
+
class SubscriptionService {
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of SubscriptionService.
|
|
18
|
+
* @param logger - Application logger instance for logging operations
|
|
19
|
+
*/
|
|
20
|
+
constructor(logger) {
|
|
21
|
+
this.logger = logger;
|
|
22
|
+
this.repository = new repository_1.SubscriptionRepository();
|
|
23
|
+
this.paymentService = new service_1.PaymentService(logger);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves an active subscription for a specific user.
|
|
27
|
+
* @param params - Query parameters
|
|
28
|
+
* @param params.userId - The user's unique identifier
|
|
29
|
+
* @param params.appNamespace - The application namespace/platform
|
|
30
|
+
* @returns Promise resolving to SubscriptionEntity or null if not found
|
|
31
|
+
*/
|
|
32
|
+
async getByUserId(params) {
|
|
33
|
+
try {
|
|
34
|
+
return await this.repository.getByUserId(params);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
this.logger.error({
|
|
38
|
+
message: 'Error in subscription service getByUserId',
|
|
39
|
+
payload: {
|
|
40
|
+
userId: params.userId,
|
|
41
|
+
appNamespace: params.appNamespace,
|
|
42
|
+
error: JSON.stringify(error),
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new subscription record in the database.
|
|
50
|
+
* @param params - Subscription creation parameters
|
|
51
|
+
* @param params.userId - The user's unique identifier
|
|
52
|
+
* @param params.appNamespace - The application namespace/platform
|
|
53
|
+
* @param params.planId - The subscription plan identifier
|
|
54
|
+
* @param params.expiresAt - ISO timestamp when subscription expires
|
|
55
|
+
* @param params.startedAt - ISO timestamp when subscription started
|
|
56
|
+
* @returns Promise resolving to created SubscriptionEntity with ID
|
|
57
|
+
* @throws Error if subscription creation fails
|
|
58
|
+
*/
|
|
59
|
+
async create(params) {
|
|
60
|
+
try {
|
|
61
|
+
return await this.repository.create(params);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
this.logger.error({
|
|
65
|
+
message: 'Error in subscription service create',
|
|
66
|
+
payload: {
|
|
67
|
+
userId: params.userId,
|
|
68
|
+
appNamespace: params.appNamespace,
|
|
69
|
+
planId: params.planId,
|
|
70
|
+
error: JSON.stringify(error),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Updates specific fields of a subscription identified by userId and appNamespace.
|
|
78
|
+
* @param params - Update parameters
|
|
79
|
+
* @param params.userId - The user's unique identifier
|
|
80
|
+
* @param params.appNamespace - The application namespace/platform
|
|
81
|
+
* @param params.fields - Array of field paths and values to update
|
|
82
|
+
* @throws Error if subscription not found or update fails
|
|
83
|
+
*/
|
|
84
|
+
async updateFieldsByUserId(params) {
|
|
85
|
+
try {
|
|
86
|
+
await this.repository.updateFieldsByUserId(params);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
this.logger.error({
|
|
90
|
+
message: 'Error in subscription service updateFieldsByUserId',
|
|
91
|
+
payload: {
|
|
92
|
+
userId: params.userId,
|
|
93
|
+
appNamespace: params.appNamespace,
|
|
94
|
+
fields: JSON.stringify(params.fields),
|
|
95
|
+
error: JSON.stringify(error),
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Retrieves all active subscriptions that have expired.
|
|
103
|
+
* Used for batch processing to update subscription statuses.
|
|
104
|
+
* @returns Promise resolving to array of expired SubscriptionEntity
|
|
105
|
+
*/
|
|
106
|
+
async getExpiredActiveSubscriptions() {
|
|
107
|
+
try {
|
|
108
|
+
return await this.repository.getExpiredActiveSubscriptions();
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
this.logger.error({
|
|
112
|
+
message: 'Error in subscription service getExpiredActiveSubscriptions',
|
|
113
|
+
payload: {
|
|
114
|
+
error: JSON.stringify(error),
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Updates specific fields of a subscription identified by subscription ID.
|
|
122
|
+
* @param params - Update parameters
|
|
123
|
+
* @param params.subscriptionId - The subscription document ID
|
|
124
|
+
* @param params.fields - Array of field paths and values to update
|
|
125
|
+
* @throws Error if update fails
|
|
126
|
+
*/
|
|
127
|
+
async updateFieldsById(params) {
|
|
128
|
+
try {
|
|
129
|
+
await this.repository.updateFieldsById(params);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
this.logger.error({
|
|
133
|
+
message: 'Error in subscription service updateFieldsById',
|
|
134
|
+
payload: {
|
|
135
|
+
subscriptionId: params.subscriptionId,
|
|
136
|
+
fields: JSON.stringify(params.fields),
|
|
137
|
+
error: JSON.stringify(error),
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Gets or creates a subscription payment URL.
|
|
145
|
+
* Checks for recent pending payments (within 3 minutes) and reuses the payment link if found.
|
|
146
|
+
* Otherwise creates a new payment intent for the subscription.
|
|
147
|
+
* @param params - Payment URL creation parameters
|
|
148
|
+
* @param params.userId - The user's unique identifier
|
|
149
|
+
* @param params.appNamespace - The application namespace/platform
|
|
150
|
+
* @param params.planId - The subscription plan identifier
|
|
151
|
+
* @param params.languageCode - Language code for translations
|
|
152
|
+
* @param params.translate - Translation function
|
|
153
|
+
* @returns Promise resolving to the payment URL string
|
|
154
|
+
* @throws Error if subscription plan not found or payment creation fails
|
|
155
|
+
*/
|
|
156
|
+
async getOrCreateSubscriptionPaymentUrl(params) {
|
|
157
|
+
try {
|
|
158
|
+
// Check for existing pending payments for this plan
|
|
159
|
+
const existingPayments = await this.paymentService.getByUserId({
|
|
160
|
+
userId: params.userId,
|
|
161
|
+
appNamespace: params.appNamespace,
|
|
162
|
+
status: 'pending',
|
|
163
|
+
});
|
|
164
|
+
const recentPayment = existingPayments
|
|
165
|
+
.filter((payment) => payment.planId === params.planId)
|
|
166
|
+
.find((payment) => moment_1.default.utc().diff(moment_1.default.utc(payment.createdAt), 'minutes') < 10);
|
|
167
|
+
if (recentPayment && recentPayment.paymentLink) {
|
|
168
|
+
return recentPayment.paymentLink;
|
|
169
|
+
}
|
|
170
|
+
const subscriptionPlan = (0, utils_1.getSubscriptionById)(params.planId);
|
|
171
|
+
if (!subscriptionPlan) {
|
|
172
|
+
throw new Error(`Subscription plan not found: ${params.planId}`);
|
|
173
|
+
}
|
|
174
|
+
const productName = (0, utils_1.getSubscriptionTitle)(params.translate({
|
|
175
|
+
code: params.languageCode,
|
|
176
|
+
path: subscriptionPlan.titleCode,
|
|
177
|
+
}));
|
|
178
|
+
const paymentEntity = await this.paymentService.createPaymentIntent({
|
|
179
|
+
userId: params.userId,
|
|
180
|
+
appNamespace: params.appNamespace,
|
|
181
|
+
planId: subscriptionPlan.id,
|
|
182
|
+
productName,
|
|
183
|
+
productPrice: subscriptionPlan.amount,
|
|
184
|
+
currency: subscriptionPlan.currency,
|
|
185
|
+
language: params.languageCode,
|
|
186
|
+
regularMode: subscriptionPlan.regularMode,
|
|
187
|
+
});
|
|
188
|
+
return paymentEntity.paymentLink;
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
this.logger.error({
|
|
192
|
+
message: 'Error in subscription service getOrCreateSubscriptionPaymentUrl',
|
|
193
|
+
payload: {
|
|
194
|
+
userId: params.userId,
|
|
195
|
+
appNamespace: params.appNamespace,
|
|
196
|
+
planId: params.planId,
|
|
197
|
+
error: JSON.stringify(error),
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.SubscriptionService = SubscriptionService;
|
|
205
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/modules/subscription/service.ts"],"names":[],"mappings":";;;;;;AAAA,oDAA4B;AAE5B,6CAAkF;AAGlF,mCAAoE;AACpE,iDAAqD;AAErD;;;GAGG;AACH,MAAa,mBAAmB;IAK5B;;;OAGG;IACH,YAAY,MAAc;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,UAAU,GAAG,IAAI,mCAAsB,EAAE,CAAC;QAC/C,IAAI,CAAC,cAAc,GAAG,IAAI,wBAAc,CAAC,MAAM,CAAC,CAAC;IACrD,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,WAAW,CAAC,MAAgD;QACrE,IAAI,CAAC;YACD,OAAO,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QACrD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;gBACd,OAAO,EAAE,2CAA2C;gBACpD,OAAO,EAAE;oBACL,MAAM,EAAE,MAAM,CAAC,MAAM;oBACrB,YAAY,EAAE,MAAM,CAAC,YAAY;oBACjC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;iBAC/B;aACJ,CAAC,CAAC;YACH,OAAO,IAAI,CAAC;QAChB,CAAC;IACL,CAAC;IAED;;;;;;;;;;OAUG;IACI,KAAK,CAAC,MAAM,CAAC,MAMnB;QACG,IAAI,CAAC;YACD,OAAO,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChD,CAAC;QAAC,OAAO,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ts","sourceRoot":"","sources":["../../../src/modules/subscription/subscriptionPlan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,QAAA,MAAM,iBAAiB,EAAE,gBAAgB,EAyDxC,CAAC;AAEF,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SUBSCRIPTION_PLAN = void 0;
|
|
4
|
+
const SUBSCRIPTION_PLAN = [
|
|
5
|
+
{
|
|
6
|
+
id: 'basic',
|
|
7
|
+
titleCode: 'subscription.plan.1.title',
|
|
8
|
+
descriptionCode: 'subscription.plan.1.description',
|
|
9
|
+
amount: 7,
|
|
10
|
+
currency: 'UAH',
|
|
11
|
+
features: {
|
|
12
|
+
messages: 1000,
|
|
13
|
+
images: 0,
|
|
14
|
+
voice: 50,
|
|
15
|
+
study: 2,
|
|
16
|
+
},
|
|
17
|
+
regularMode: 'monthly',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: 'pro',
|
|
21
|
+
titleCode: 'subscription.plan.2.title',
|
|
22
|
+
descriptionCode: 'subscription.plan.2.description',
|
|
23
|
+
amount: 10,
|
|
24
|
+
currency: 'UAH',
|
|
25
|
+
features: {
|
|
26
|
+
messages: 2000,
|
|
27
|
+
images: 20,
|
|
28
|
+
voice: 50,
|
|
29
|
+
study: 2,
|
|
30
|
+
},
|
|
31
|
+
regularMode: 'monthly',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'basic-daily',
|
|
35
|
+
titleCode: 'subscription.plan.3.title',
|
|
36
|
+
descriptionCode: 'subscription.plan.1.description',
|
|
37
|
+
amount: 7,
|
|
38
|
+
currency: 'UAH',
|
|
39
|
+
features: {
|
|
40
|
+
messages: 10,
|
|
41
|
+
images: 0,
|
|
42
|
+
voice: 50,
|
|
43
|
+
study: 2,
|
|
44
|
+
},
|
|
45
|
+
regularMode: 'daily',
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: 'pro-daily',
|
|
49
|
+
titleCode: 'subscription.plan.3.title',
|
|
50
|
+
descriptionCode: 'subscription.plan.2.description',
|
|
51
|
+
amount: 10,
|
|
52
|
+
currency: 'UAH',
|
|
53
|
+
features: {
|
|
54
|
+
messages: 20,
|
|
55
|
+
images: 20,
|
|
56
|
+
voice: 50,
|
|
57
|
+
study: 2,
|
|
58
|
+
},
|
|
59
|
+
regularMode: 'daily',
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
exports.SUBSCRIPTION_PLAN = SUBSCRIPTION_PLAN;
|
|
63
|
+
//# sourceMappingURL=subscriptionPlan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscriptionPlan.js","sourceRoot":"","sources":["../../../src/modules/subscription/subscriptionPlan.ts"],"names":[],"mappings":";;;AAEA,MAAM,iBAAiB,GAAuB;IAC1C;QACI,EAAE,EAAE,OAAO;QACX,SAAS,EAAE,2BAA2B;QACtC,eAAe,EAAE,iCAAiC;QAClD,MAAM,EAAE,CAAC;QACT,QAAQ,EAAE,KAAK;QACf,QAAQ,EAAE;YACN,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,CAAC;YACT,KAAK,EAAE,EAAE;YACT,KAAK,EAAE,CAAC;SACX;QACD,WAAW,EAAE,SAAS;KACzB;IACD;QACI,EAAE,EAAE,KAAK;QACT,SAAS,EAAE,2BAA2B;QACtC,eAAe,EAAE,iCAAiC;QAClD,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE,KAAK;QACf,QAAQ,EAAE;YACN,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,EAAE;YACV,KAAK,EAAE,EAAE;YACT,KAAK,EAAE,CAAC;SACX;QACD,WAAW,EAAE,SAAS;KACzB;IACD;QACI,EAAE,EAAE,aAAa;QACjB,SAAS,EAAE,2BAA2B;QACtC,eAAe,EAAE,iCAAiC;QAClD,MAAM,EAAE,CAAC;QACT,QAAQ,EAAE,KAAK;QACf,QAAQ,EAAE;YACN,QAAQ,EAAE,EAAE;YACZ,MAAM,EAAE,CAAC;YACT,KAAK,EAAE,EAAE;YACT,KAAK,EAAE,CAAC;SACX;QACD,WAAW,EAAE,OAAO;KACvB;IACD;QACI,EAAE,EAAE,WAAW;QACf,SAAS,EAAE,2BAA2B;QACtC,eAAe,EAAE,iCAAiC;QAClD,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE,KAAK;QACf,QAAQ,EAAE;YACN,QAAQ,EAAE,EAAE;YACZ,MAAM,EAAE,EAAE;YACV,KAAK,EAAE,EAAE;YACT,KAAK,EAAE,CAAC;SACX;QACD,WAAW,EAAE,OAAO;KACvB;CACJ,CAAC;AAEO,8CAAiB"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { NestedPathsAccess } from '../../types/utilities';
|
|
2
|
+
import { AppNamespace } from '../app/types';
|
|
3
|
+
type SubscriptionEntity = {
|
|
4
|
+
/** Auto-generated unique ID (document ID) */
|
|
5
|
+
id?: string;
|
|
6
|
+
/** The Telegram User ID this subscription belongs to. Links back to users. */
|
|
7
|
+
userId: string;
|
|
8
|
+
/** An identifier for the subscription plan (e.g., "basic-daily", "pro-monthly") */
|
|
9
|
+
planId: string;
|
|
10
|
+
/** The lifecycle state of this service period */
|
|
11
|
+
status: 'active' | 'expired' | 'cancelled';
|
|
12
|
+
/** The key field for the cron job. Timestamp when this paid-for period ends. */
|
|
13
|
+
expiresAt: string;
|
|
14
|
+
/** Timestamp when this period began */
|
|
15
|
+
startedAt: string;
|
|
16
|
+
/** Platform/app namespace */
|
|
17
|
+
platform: AppNamespace;
|
|
18
|
+
/** Payment provider */
|
|
19
|
+
provider: 'wayforpay';
|
|
20
|
+
};
|
|
21
|
+
type SubscriptionPlan = {
|
|
22
|
+
id: string;
|
|
23
|
+
titleCode: string;
|
|
24
|
+
descriptionCode: string;
|
|
25
|
+
amount: number;
|
|
26
|
+
currency: 'UAH' | 'USD';
|
|
27
|
+
features: {
|
|
28
|
+
messages: number;
|
|
29
|
+
images: number;
|
|
30
|
+
voice: number;
|
|
31
|
+
study: number;
|
|
32
|
+
};
|
|
33
|
+
regularMode: 'daily' | 'monthly' | 'yearly';
|
|
34
|
+
};
|
|
35
|
+
type SubscriptionFieldPath = NestedPathsAccess<SubscriptionEntity>;
|
|
36
|
+
export type { SubscriptionEntity, SubscriptionPlan, SubscriptionFieldPath };
|
|
37
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,KAAK,kBAAkB,GAAG;IACtB,6CAA6C;IAC7C,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,8EAA8E;IAC9E,MAAM,EAAE,MAAM,CAAC;IACf,mFAAmF;IACnF,MAAM,EAAE,MAAM,CAAC;IACf,iDAAiD;IACjD,MAAM,EAAE,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAC;IAC3C,gFAAgF;IAChF,SAAS,EAAE,MAAM,CAAC;IAClB,uCAAuC;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,6BAA6B;IAC7B,QAAQ,EAAE,YAAY,CAAC;IACvB,uBAAuB;IACvB,QAAQ,EAAE,WAAW,CAAC;CACzB,CAAC;AAEF,KAAK,gBAAgB,GAAG;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,KAAK,GAAG,KAAK,CAAC;IACxB,QAAQ,EAAE;QACN,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,WAAW,EAAE,OAAO,GAAG,SAAS,GAAG,QAAQ,CAAC;CAC/C,CAAC;AAEF,KAAK,qBAAqB,GAAG,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;AAEnE,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/modules/subscription/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare function getSubscriptionById(planId: string): import("./types").SubscriptionPlan;
|
|
2
|
+
declare function getSubscriptionTitle(planTitle: string): string;
|
|
3
|
+
declare function subscriptionPlanExists(planId: string): boolean;
|
|
4
|
+
export { getSubscriptionById, getSubscriptionTitle, subscriptionPlanExists };
|
|
5
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/modules/subscription/utils.ts"],"names":[],"mappings":"AAEA,iBAAS,mBAAmB,CAAC,MAAM,EAAE,MAAM,sCAE1C;AAED,iBAAS,oBAAoB,CAAC,SAAS,EAAE,MAAM,UAE9C;AAED,iBAAS,sBAAsB,CAAC,MAAM,EAAE,MAAM,WAE7C;AAED,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSubscriptionById = getSubscriptionById;
|
|
4
|
+
exports.getSubscriptionTitle = getSubscriptionTitle;
|
|
5
|
+
exports.subscriptionPlanExists = subscriptionPlanExists;
|
|
6
|
+
const subscriptionPlan_1 = require("./subscriptionPlan");
|
|
7
|
+
function getSubscriptionById(planId) {
|
|
8
|
+
return subscriptionPlan_1.SUBSCRIPTION_PLAN.find((plan) => plan.id === planId);
|
|
9
|
+
}
|
|
10
|
+
function getSubscriptionTitle(planTitle) {
|
|
11
|
+
return `Miia telegram bot: ${planTitle}`;
|
|
12
|
+
}
|
|
13
|
+
function subscriptionPlanExists(planId) {
|
|
14
|
+
return subscriptionPlan_1.SUBSCRIPTION_PLAN.some((plan) => plan.id === planId);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/modules/subscription/utils.ts"],"names":[],"mappings":";;AAcS,kDAAmB;AAAE,oDAAoB;AAAE,wDAAsB;AAd1E,yDAAuD;AAEvD,SAAS,mBAAmB,CAAC,MAAc;IACvC,OAAO,oCAAiB,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,MAAM,CAAC,CAAC;AAChE,CAAC;AAED,SAAS,oBAAoB,CAAC,SAAiB;IAC3C,OAAO,sBAAsB,SAAS,EAAE,CAAC;AAC7C,CAAC;AAED,SAAS,sBAAsB,CAAC,MAAc;IAC1C,OAAO,oCAAiB,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,MAAM,CAAC,CAAC;AAChE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@misterhomer1992/miit-bot-payment",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "A TypeScript utility library for payment validation and formatting",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"moment": "2.30.1",
|
|
29
29
|
"dotenv": "17.2.2",
|
|
30
30
|
"axios": "1.12.2",
|
|
31
|
-
"firebase-admin": "^11.6.0"
|
|
31
|
+
"firebase-admin": "^11.6.0",
|
|
32
|
+
"@misterhomer1992/wayforpay-api": "2.0.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"@types/node": "^20.10.0",
|