Skip to content

Commit 4ce6c91

Browse files
committed
update from main
2 parents a295403 + ac7755a commit 4ce6c91

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

src/billing/cloudpayments.ts

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -205,36 +205,6 @@ export default class CloudPaymentsWebhooks {
205205
return `${workspace.name} ${now.getDate()}/${now.getMonth() + 1} ${tariffPlan.name}`;
206206
}
207207

208-
/**
209-
* Confirms the correctness of a user's payment for card linking
210-
* @param req - express request
211-
* @param res - express response
212-
* @param data - payment data receinved from checksum and request payload
213-
*/
214-
private async checkCardLinkOperation(req: express.Request, res: express.Response, data: PaymentData): Promise<void> {
215-
if (data.isCardLinkOperation && (!data.userId || !data.workspaceId)) {
216-
this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, '[Billing / Check] Card linking – invalid data', req.body);
217-
218-
return;
219-
}
220-
221-
try {
222-
const workspace = await this.getWorkspace(req, data.workspaceId);
223-
224-
telegram
225-
.sendMessage(`✅ [Billing / Check] Card linked for subscription workspace «${workspace.name}»`, TelegramBotURLs.Money)
226-
.catch(e => console.error('Error while sending message to Telegram: ' + e));
227-
228-
res.json({
229-
code: CheckCodes.SUCCESS,
230-
} as CheckResponse);
231-
} catch (e) {
232-
const error = e as Error;
233-
234-
this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, `[Billing / Check] ${error.toString()}`, req.body);
235-
}
236-
}
237-
238208
/**
239209
* Route to confirm the correctness of a user's payment
240210
* https://developers.cloudpayments.ru/#check
@@ -257,28 +227,33 @@ export default class CloudPaymentsWebhooks {
257227
return;
258228
}
259229

230+
/** Data validation */
260231
if (data.isCardLinkOperation) {
261-
this.checkCardLinkOperation(req, res, data);
232+
if (!data.userId || !data.workspaceId) {
233+
this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, '[Billing / Check] There is no necessary data in the card linking request', req.body);
262234

263-
return;
235+
return;
236+
}
237+
} else {
238+
if (!data.userId || !data.workspaceId || !data.tariffPlanId) {
239+
this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, '[Billing / Check] There is no necessary data in the request', body);
240+
241+
return;
242+
}
264243
}
265244

266245
let workspace: WorkspaceModel;
267246
let member: ConfirmedMemberDBScheme;
268247
let plan: PlanDBScheme;
269-
270-
if (!data.userId || !data.workspaceId || !data.tariffPlanId) {
271-
this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, '[Billing / Check] There is no necessary data in the request', body);
272-
273-
return;
274-
}
248+
let planId: string;
275249

276250
const { workspaceId, userId, tariffPlanId } = data;
277251

278252
try {
279253
workspace = await this.getWorkspace(req, workspaceId);
280254
member = await this.getMember(userId, workspace);
281-
plan = await this.getPlan(req, tariffPlanId);
255+
planId = data.isCardLinkOperation ? workspace.tariffPlanId.toString() : tariffPlanId;
256+
plan = await this.getPlan(req, planId);
282257
} catch (e) {
283258
const error = e as Error;
284259

@@ -307,7 +282,7 @@ export default class CloudPaymentsWebhooks {
307282
try {
308283
await context.factories.businessOperationsFactory.create<PayloadOfWorkspacePlanPurchase>({
309284
transactionId: body.TransactionId.toString(),
310-
type: BusinessOperationType.WorkspacePlanPurchase,
285+
type: data.isCardLinkOperation ? BusinessOperationType.CardLinkCharge : BusinessOperationType.WorkspacePlanPurchase,
311286
status: BusinessOperationStatus.Pending,
312287
payload: {
313288
workspaceId: workspace._id,
@@ -361,16 +336,19 @@ export default class CloudPaymentsWebhooks {
361336
return;
362337
}
363338

364-
if (data.isCardLinkOperation && (!data.userId || !data.workspaceId)) {
365-
this.sendError(res, PayCodes.SUCCESS, '[Billing / Pay] No workspace or user id in request body', req.body);
366-
367-
return;
368-
}
339+
/** Data validation */
340+
if (data.isCardLinkOperation) {
341+
if (!data.userId || !data.workspaceId) {
342+
this.sendError(res, PayCodes.SUCCESS, '[Billing / Pay] No workspace or user id in request body', req.body);
369343

370-
if (!data.isCardLinkOperation && (!data.workspaceId || !data.tariffPlanId || !data.userId)) {
371-
this.sendError(res, PayCodes.SUCCESS, `[Billing / Pay] No workspace, tariff plan or user id in request body`, body);
344+
return;
345+
}
346+
} else {
347+
if (!data.workspaceId || !data.tariffPlanId || !data.userId) {
348+
this.sendError(res, PayCodes.SUCCESS, `[Billing / Pay] No workspace, tariff plan or user id in request body`, body);
372349

373-
return;
350+
return;
351+
}
374352
}
375353

376354
let businessOperation;
@@ -384,7 +362,6 @@ export default class CloudPaymentsWebhooks {
384362
workspace = await this.getWorkspace(req, data.workspaceId);
385363
user = await this.getUser(req, data.userId);
386364
planId = data.isCardLinkOperation ? workspace.tariffPlanId.toString() : data.tariffPlanId;
387-
388365
tariffPlan = await this.getPlan(req, planId);
389366
} catch (e) {
390367
const error = e as Error;

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,13 @@
451451
axios "^0.21.1"
452452
stack-trace "^0.0.10"
453453

454+
"@hawk.so/[email protected]":
455+
version "0.1.23"
456+
resolved "https://registry.yarnpkg.com/@hawk.so/types/-/types-0.1.23.tgz#45dae057fd29d4735a51baa5f00d8e0d245075f4"
457+
integrity sha512-b9W8TZJj6kBh3rVS4tKCmVbM44XJ/Ya8kwXY12QNf5/U4O2iuegIIEcFwr6N10SJI1VbuPLYFrckxt/8ymQScw==
458+
dependencies:
459+
"@types/mongodb" "^3.5.34"
460+
454461
"@hawk.so/types@^0.1.15":
455462
version "0.1.18"
456463
resolved "https://registry.yarnpkg.com/@hawk.so/types/-/types-0.1.18.tgz#746537634756825f066182737429d11ea124d5c5"

0 commit comments

Comments
 (0)