@misterhomer1992/miit-bot-payment 1.1.6 → 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 +59 -2
- 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,365 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import { Logger } from '../logger/types';
|
|
3
|
+
import { PaymentRepository } from './repository';
|
|
4
|
+
import { PaymentEntity, IPaymentRepository, IPaymentService, SignatureValidationResult } from './types';
|
|
5
|
+
import { createPaymentAPI, createTokenPackPaymentAPI, createUpgradePaymentAPI } from './api';
|
|
6
|
+
import { WayForPayService } from './wayforpay.service';
|
|
7
|
+
import type { IWayForPayService, WayForPayCallbackData } from './wayforpay.types';
|
|
8
|
+
|
|
9
|
+
export class PaymentService implements IPaymentService {
|
|
10
|
+
private readonly logger: Logger;
|
|
11
|
+
private readonly repository: IPaymentRepository;
|
|
12
|
+
private readonly wayForPayService: IWayForPayService;
|
|
13
|
+
|
|
14
|
+
constructor({
|
|
15
|
+
logger,
|
|
16
|
+
repository,
|
|
17
|
+
wayForPayService,
|
|
18
|
+
}: {
|
|
19
|
+
logger: Logger;
|
|
20
|
+
repository?: IPaymentRepository;
|
|
21
|
+
wayForPayService?: IWayForPayService;
|
|
22
|
+
}) {
|
|
23
|
+
this.logger = logger;
|
|
24
|
+
this.repository = repository || new PaymentRepository();
|
|
25
|
+
this.wayForPayService = wayForPayService || new WayForPayService({ logger });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async getByOrderReference(orderReference: string): Promise<PaymentEntity | null> {
|
|
29
|
+
try {
|
|
30
|
+
return await this.repository.getByOrderReference(orderReference);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
this.logger.error({
|
|
33
|
+
message: 'Error in payment service getByOrderReference',
|
|
34
|
+
payload: {
|
|
35
|
+
orderReference,
|
|
36
|
+
error: JSON.stringify(error),
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async getByUser(params: {
|
|
44
|
+
userId: string;
|
|
45
|
+
platform: string;
|
|
46
|
+
status?: PaymentEntity['status'];
|
|
47
|
+
}): Promise<PaymentEntity[]> {
|
|
48
|
+
try {
|
|
49
|
+
return await this.repository.getByUser(params);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
this.logger.error({
|
|
52
|
+
message: 'Error in payment service getByUser',
|
|
53
|
+
payload: {
|
|
54
|
+
userId: params.userId,
|
|
55
|
+
platform: params.platform,
|
|
56
|
+
status: params.status,
|
|
57
|
+
error: JSON.stringify(error),
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public async create(paymentData: Omit<PaymentEntity, 'id'>): Promise<PaymentEntity> {
|
|
65
|
+
try {
|
|
66
|
+
return await this.repository.create(paymentData);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
this.logger.error({
|
|
69
|
+
message: 'Error in payment service create',
|
|
70
|
+
payload: {
|
|
71
|
+
orderReference: paymentData.orderReference,
|
|
72
|
+
userId: paymentData.userId,
|
|
73
|
+
error: JSON.stringify(error),
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public async updateStatus({ id, status }: { id: string; status: PaymentEntity['status'] }): Promise<void> {
|
|
82
|
+
try {
|
|
83
|
+
await this.repository.updateFields({
|
|
84
|
+
id,
|
|
85
|
+
fields: [['status', status]],
|
|
86
|
+
});
|
|
87
|
+
} catch (error) {
|
|
88
|
+
this.logger.error({
|
|
89
|
+
message: 'Error in payment service updateStatus',
|
|
90
|
+
payload: {
|
|
91
|
+
id,
|
|
92
|
+
status,
|
|
93
|
+
error: JSON.stringify(error),
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public async changeStatus({ id, status }: { id: string; status: PaymentEntity['status'] }): Promise<void> {
|
|
102
|
+
return this.updateStatus({ id, status });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public async getExpiredPendingPayments(
|
|
106
|
+
params: {
|
|
107
|
+
hoursOld?: number;
|
|
108
|
+
} = {},
|
|
109
|
+
): Promise<PaymentEntity[]> {
|
|
110
|
+
try {
|
|
111
|
+
return await this.repository.getExpiredPendingPayments(params);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
this.logger.error({
|
|
114
|
+
message: 'Error in payment service getExpiredPendingPayments',
|
|
115
|
+
payload: {
|
|
116
|
+
hoursOld: params.hoursOld,
|
|
117
|
+
error: JSON.stringify(error),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public async createPaymentIntent(params: {
|
|
125
|
+
userId: string;
|
|
126
|
+
platform: string;
|
|
127
|
+
planId: string;
|
|
128
|
+
productName: string;
|
|
129
|
+
productPrice: number;
|
|
130
|
+
currency: 'UAH' | 'USD';
|
|
131
|
+
regularCount?: number;
|
|
132
|
+
regularMode?: 'daily' | 'weekly' | 'monthly' | 'yearly';
|
|
133
|
+
language?: string;
|
|
134
|
+
}): Promise<PaymentEntity> {
|
|
135
|
+
const { userId, platform, planId, productName, productPrice, currency, regularCount, regularMode, language } =
|
|
136
|
+
params;
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
// Step 1: Create payment URL from Wayforpay API
|
|
140
|
+
// This also generates a unique order reference
|
|
141
|
+
const paymentResponse = await createPaymentAPI({
|
|
142
|
+
userId,
|
|
143
|
+
platform,
|
|
144
|
+
productName,
|
|
145
|
+
productPrice,
|
|
146
|
+
planId,
|
|
147
|
+
currency,
|
|
148
|
+
regularCount,
|
|
149
|
+
regularMode,
|
|
150
|
+
language,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (!paymentResponse?.url || !paymentResponse?.orderReference) {
|
|
154
|
+
throw new Error('Failed to get payment URL or order reference from Wayforpay');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Step 2: Create payment record in database
|
|
158
|
+
const paymentEntity = await this.repository.create({
|
|
159
|
+
orderReference: paymentResponse.orderReference,
|
|
160
|
+
userId,
|
|
161
|
+
status: 'pending',
|
|
162
|
+
paymentLink: paymentResponse.url,
|
|
163
|
+
planId,
|
|
164
|
+
paymentType: 'subscription',
|
|
165
|
+
amount: productPrice,
|
|
166
|
+
currency,
|
|
167
|
+
createdAt: moment.utc().toISOString(),
|
|
168
|
+
platform: platform,
|
|
169
|
+
provider: 'wayforpay',
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
return paymentEntity;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
this.logger.error({
|
|
175
|
+
message: 'Error in payment service createPaymentIntent',
|
|
176
|
+
payload: {
|
|
177
|
+
userId,
|
|
178
|
+
platform,
|
|
179
|
+
planId,
|
|
180
|
+
productName,
|
|
181
|
+
productPrice,
|
|
182
|
+
currency,
|
|
183
|
+
error: JSON.stringify(error),
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public async createTokenPackPaymentIntent(params: {
|
|
192
|
+
userId: string;
|
|
193
|
+
platform: string;
|
|
194
|
+
packId: string;
|
|
195
|
+
productName: string;
|
|
196
|
+
productPrice: number;
|
|
197
|
+
currency: 'UAH' | 'USD';
|
|
198
|
+
language?: string;
|
|
199
|
+
returnUrl?: string;
|
|
200
|
+
}): Promise<PaymentEntity> {
|
|
201
|
+
const { userId, platform, packId, productName, productPrice, currency, language, returnUrl } = params;
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
// Step 1: Create payment URL from Wayforpay API for token pack
|
|
205
|
+
const paymentResponse = await createTokenPackPaymentAPI({
|
|
206
|
+
userId,
|
|
207
|
+
platform,
|
|
208
|
+
productName,
|
|
209
|
+
productPrice,
|
|
210
|
+
packId,
|
|
211
|
+
currency,
|
|
212
|
+
language,
|
|
213
|
+
returnUrl,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
if (!paymentResponse?.url || !paymentResponse?.orderReference) {
|
|
217
|
+
throw new Error('Failed to get payment URL or order reference from Wayforpay for token pack');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Step 2: Create payment record in database
|
|
221
|
+
const paymentEntity = await this.repository.create({
|
|
222
|
+
orderReference: paymentResponse.orderReference,
|
|
223
|
+
userId,
|
|
224
|
+
status: 'pending',
|
|
225
|
+
paymentLink: paymentResponse.url,
|
|
226
|
+
planId: packId,
|
|
227
|
+
paymentType: 'token_pack',
|
|
228
|
+
amount: productPrice,
|
|
229
|
+
currency,
|
|
230
|
+
createdAt: moment.utc().toISOString(),
|
|
231
|
+
platform: platform,
|
|
232
|
+
provider: 'wayforpay',
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
return paymentEntity;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
this.logger.error({
|
|
238
|
+
message: 'Error in payment service createTokenPackPaymentIntent',
|
|
239
|
+
payload: {
|
|
240
|
+
userId,
|
|
241
|
+
platform,
|
|
242
|
+
packId,
|
|
243
|
+
productName,
|
|
244
|
+
productPrice,
|
|
245
|
+
currency,
|
|
246
|
+
error: JSON.stringify(error),
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
public validateSignature(callbackData: {
|
|
255
|
+
merchantAccount: string;
|
|
256
|
+
orderReference: string;
|
|
257
|
+
amount: number;
|
|
258
|
+
currency: string;
|
|
259
|
+
authCode: string;
|
|
260
|
+
cardPan: string;
|
|
261
|
+
transactionStatus: string;
|
|
262
|
+
reasonCode: string | number;
|
|
263
|
+
merchantSignature: string;
|
|
264
|
+
}): SignatureValidationResult {
|
|
265
|
+
try {
|
|
266
|
+
const result = this.wayForPayService.verifyCallbackSignature(callbackData as WayForPayCallbackData);
|
|
267
|
+
return {
|
|
268
|
+
isValid: result.isValid,
|
|
269
|
+
error: result.error,
|
|
270
|
+
};
|
|
271
|
+
} catch (error) {
|
|
272
|
+
this.logger.error({
|
|
273
|
+
message: 'Error in payment service validateSignature',
|
|
274
|
+
payload: {
|
|
275
|
+
orderReference: callbackData.orderReference,
|
|
276
|
+
error: JSON.stringify(error),
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
isValid: false,
|
|
282
|
+
error: 'Error validating signature',
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
public async createUpgradePaymentIntent(params: {
|
|
288
|
+
userId: string;
|
|
289
|
+
platform: string;
|
|
290
|
+
currentPlanId: string;
|
|
291
|
+
newPlanId: string;
|
|
292
|
+
productName: string;
|
|
293
|
+
upgradeCost: number;
|
|
294
|
+
currency: 'UAH' | 'USD';
|
|
295
|
+
regularMode?: 'daily' | 'monthly' | 'yearly';
|
|
296
|
+
regularCount?: number;
|
|
297
|
+
language?: string;
|
|
298
|
+
}): Promise<PaymentEntity> {
|
|
299
|
+
const {
|
|
300
|
+
userId,
|
|
301
|
+
platform,
|
|
302
|
+
currentPlanId,
|
|
303
|
+
newPlanId,
|
|
304
|
+
productName,
|
|
305
|
+
upgradeCost,
|
|
306
|
+
currency,
|
|
307
|
+
regularMode,
|
|
308
|
+
regularCount,
|
|
309
|
+
language,
|
|
310
|
+
} = params;
|
|
311
|
+
|
|
312
|
+
try {
|
|
313
|
+
// Step 1: Create payment URL from Wayforpay API for upgrade
|
|
314
|
+
const paymentResponse = await createUpgradePaymentAPI({
|
|
315
|
+
userId,
|
|
316
|
+
platform,
|
|
317
|
+
currentPlanId,
|
|
318
|
+
newPlanId,
|
|
319
|
+
productName,
|
|
320
|
+
productPrice: upgradeCost,
|
|
321
|
+
currency,
|
|
322
|
+
regularMode,
|
|
323
|
+
regularCount,
|
|
324
|
+
language,
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
if (!paymentResponse?.url || !paymentResponse?.orderReference) {
|
|
328
|
+
throw new Error('Failed to get payment URL or order reference from Wayforpay for upgrade');
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Step 2: Create payment record in database
|
|
332
|
+
const paymentEntity = await this.repository.create({
|
|
333
|
+
orderReference: paymentResponse.orderReference,
|
|
334
|
+
userId,
|
|
335
|
+
status: 'pending',
|
|
336
|
+
paymentLink: paymentResponse.url,
|
|
337
|
+
planId: newPlanId,
|
|
338
|
+
paymentType: 'subscription_upgrade',
|
|
339
|
+
amount: upgradeCost,
|
|
340
|
+
currency,
|
|
341
|
+
createdAt: moment.utc().toISOString(),
|
|
342
|
+
platform: platform,
|
|
343
|
+
provider: 'wayforpay',
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
return paymentEntity;
|
|
347
|
+
} catch (error) {
|
|
348
|
+
this.logger.error({
|
|
349
|
+
message: 'Error in payment service createUpgradePaymentIntent',
|
|
350
|
+
payload: {
|
|
351
|
+
userId,
|
|
352
|
+
platform,
|
|
353
|
+
currentPlanId,
|
|
354
|
+
newPlanId,
|
|
355
|
+
productName,
|
|
356
|
+
upgradeCost,
|
|
357
|
+
currency,
|
|
358
|
+
error: JSON.stringify(error),
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|