Skip to content

Commit fa97d84

Browse files
committed
Merge branch 'fix/payments-logs' into stage
2 parents a2c29f4 + a047258 commit fa97d84

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

src/billing/cloudpayments.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
PlanDBScheme,
2626
PlanProlongationPayload
2727
} from '@hawk.so/types';
28-
import { PENNY_MULTIPLIER } from 'codex-accounting-sdk';
2928
import WorkspaceModel from '../models/workspace';
3029
import HawkCatcher from '@hawk.so/nodejs';
3130
import { publish } from '../rabbitmq';
@@ -45,6 +44,8 @@ import PlanModel from '../models/plan';
4544
import { ClientApi, ClientService, CustomerReceiptItem, ReceiptApi, ReceiptTypes, TaxationSystem } from 'cloudpayments';
4645
import { ComposePaymentPayload } from './types/composePaymentPayload';
4746

47+
const PENNY_MULTIPLIER = 100;
48+
4849
interface ComposePaymentRequest extends express.Request {
4950
query: ComposePaymentPayload & { [key: string]: any };
5051
context: import('../types/graphql').ResolverContextBase;
@@ -105,7 +106,12 @@ export default class CloudPaymentsWebhooks {
105106
const userId = req.context.user.id;
106107

107108
if (!workspaceId || !tariffPlanId || !userId) {
108-
this.sendError(res, 1, `[Billing / Compose payment] No workspace, tariff plan or user id in request body`, req.query);
109+
this.sendError(res, 1, `[Billing / Compose payment] No workspace, tariff plan or user id in request body
110+
Details:
111+
workspaceId: ${workspaceId}
112+
tariffPlanId: ${tariffPlanId}
113+
userId: ${userId}`
114+
, req.query);
109115

110116
return;
111117
}
@@ -137,18 +143,33 @@ export default class CloudPaymentsWebhooks {
137143

138144
const isCardLinkOperation = workspace.tariffPlanId.toString() === tariffPlanId && !workspace.isTariffPlanExpired();
139145

146+
// Calculate next payment date
147+
const lastChargeDate = new Date(workspace.lastChargeDate);
148+
const now = new Date();
149+
let nextPaymentDate: Date;
150+
151+
if (isCardLinkOperation) {
152+
nextPaymentDate = new Date(lastChargeDate);
153+
nextPaymentDate.setMonth(lastChargeDate.getMonth() + 1);
154+
} else {
155+
nextPaymentDate = new Date(now);
156+
nextPaymentDate.setMonth(nextPaymentDate.getMonth() + 1);
157+
}
158+
140159
let checksum;
141160

142161
try {
143162
const checksumData = isCardLinkOperation ? {
144163
isCardLinkOperation: true,
145164
workspaceId: workspace._id.toString(),
146165
userId: userId,
166+
nextPaymentDate: nextPaymentDate.toISOString(),
147167
} : {
148168
workspaceId: workspace._id.toString(),
149169
userId: userId,
150170
tariffPlanId: tariffPlan._id.toString(),
151171
shouldSaveCard: shouldSaveCard === 'true',
172+
nextPaymentDate: nextPaymentDate.toISOString(),
152173
};
153174

154175
checksum = await checksumService.generateChecksum(checksumData);
@@ -170,6 +191,7 @@ export default class CloudPaymentsWebhooks {
170191
isCardLinkOperation,
171192
currency: 'RUB',
172193
checksum,
194+
nextPaymentDate: nextPaymentDate.toISOString(),
173195
});
174196
}
175197

@@ -478,8 +500,6 @@ export default class CloudPaymentsWebhooks {
478500
* Refund the money that were charged to link a card
479501
*/
480502
if (data.isCardLinkOperation) {
481-
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] Recurrent payments activated for «${workspace.name}». 1 RUB charged`, TelegramBotURLs.Money));
482-
483503
await cloudPaymentsApi.cancelPayment(body.TransactionId);
484504

485505
const member = await this.getMember(data.userId, workspace);
@@ -502,7 +522,13 @@ export default class CloudPaymentsWebhooks {
502522
dtCreated: new Date(),
503523
});
504524

505-
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] Recurrent payments activated for «${workspace.name}». 1 RUB returned`, TelegramBotURLs.Money));
525+
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] Card linked
526+
Transaction details:
527+
workspace id: ${workspace._id}
528+
date of operation: ${body.DateTime}
529+
first payment date: ${data.cloudPayments?.recurrent.startDate}
530+
sum: ${data.cloudPayments?.recurrent.amount}${body.Currency}`
531+
, TelegramBotURLs.Money));
506532
} else {
507533
/**
508534
* Russia code from ISO 3166-1
@@ -516,7 +542,15 @@ export default class CloudPaymentsWebhooks {
516542

517543
await this.sendReceipt(workspace, tariffPlan, userEmail);
518544

519-
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] Payment passed successfully for «${workspace.name}»`, TelegramBotURLs.Money));
545+
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] New payment
546+
Transaction details:
547+
amount: ${+body.Amount * PENNY_MULTIPLIER}
548+
currency: ${body.Currency}
549+
next payment date: ${data.cloudPayments?.recurrent.startDate}
550+
workspace id: ${workspace._id}
551+
date of operation: ${body.DateTime}
552+
subscription id: ${body.SubscriptionId}`
553+
, TelegramBotURLs.Money));
520554
}
521555
} catch (e) {
522556
const error = e as Error;
@@ -606,7 +640,7 @@ export default class CloudPaymentsWebhooks {
606640
return;
607641
}
608642

609-
this.handleSendingToTelegramError(telegram.sendMessage(` [Billing / Fail] Transaction failed for «${workspace.name}»`, TelegramBotURLs.Money));
643+
this.handleSendingToTelegramError(telegram.sendMessage(` [Billing / Fail] Transaction failed for «${workspace.name}»`, TelegramBotURLs.Money));
610644

611645
HawkCatcher.send(new Error('[Billing / Fail] Transaction failed'), body as any);
612646

@@ -628,7 +662,14 @@ export default class CloudPaymentsWebhooks {
628662

629663
console.log('💎 CloudPayments /recurrent request', body);
630664

631-
this.handleSendingToTelegramError(telegram.sendMessage(`[Billing / Recurrent] New recurrent event with ${body.Status} status`, TelegramBotURLs.Money));
665+
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Recurrent] New recurrent transaction
666+
Transaction details:
667+
amount: ${+body.Amount * PENNY_MULTIPLIER}
668+
currency: ${body.Currency}
669+
next payment date: ${body.NextTransactionDate}
670+
workspace id: ${body.AccountId}
671+
subscription id: ${body.Id}`
672+
, TelegramBotURLs.Money));
632673
HawkCatcher.send(new Error(`[Billing / Recurrent] New recurrent event with ${body.Status} status`), req.body);
633674

634675
switch (body.Status) {

src/utils/checksumService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ interface PlanPurchaseChecksumData {
2020
* If true, we will save user card
2121
*/
2222
shouldSaveCard: boolean;
23+
/**
24+
* Next payment date
25+
*/
26+
nextPaymentDate: string;
2327
}
2428

2529
interface CardLinkChecksumData {
@@ -35,6 +39,10 @@ interface CardLinkChecksumData {
3539
* True if this is card linking operation – charging minimal amount of money to validate card info
3640
*/
3741
isCardLinkOperation: boolean;
42+
/**
43+
* Next payment date
44+
*/
45+
nextPaymentDate: string;
3846
}
3947

4048
/**
@@ -67,13 +75,15 @@ class ChecksumService {
6775
workspaceId: payload.workspaceId,
6876
userId: payload.userId,
6977
isCardLinkOperation: payload.isCardLinkOperation,
78+
nextPaymentDate: payload.nextPaymentDate,
7079
};
7180
} else {
7281
return {
7382
workspaceId: payload.workspaceId,
7483
userId: payload.userId,
7584
tariffPlanId: payload.tariffPlanId,
7685
shouldSaveCard: payload.shouldSaveCard,
86+
nextPaymentDate: payload.nextPaymentDate,
7787
};
7888
}
7989
}

0 commit comments

Comments
 (0)