Skip to content

Commit 7698721

Browse files
committed
Remove redundant, add tg notification
1 parent 3039cbb commit 7698721

File tree

3 files changed

+17
-137
lines changed

3 files changed

+17
-137
lines changed

src/billing/cloudpayments.ts

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,9 @@ import { PaymentData } from './types/paymentData';
4242
import cloudPaymentsApi from '../utils/cloudPaymentsApi';
4343
import PlanModel from '../models/plan';
4444
import { ClientApi, ClientService, CustomerReceiptItem, ReceiptApi, ReceiptTypes, TaxationSystem } from 'cloudpayments';
45-
import { ComposePaymentPayload } from './types/composePaymentPayload';
4645

4746
const PENNY_MULTIPLIER = 100;
4847

49-
interface ComposePaymentRequest extends express.Request {
50-
query: ComposePaymentPayload & { [key: string]: any };
51-
context: import('../types/graphql').ResolverContextBase;
52-
};
5348

5449
/**
5550
* Class for describing the logic of payment routes
@@ -86,7 +81,6 @@ export default class CloudPaymentsWebhooks {
8681
public getRouter(): express.Router {
8782
const router = express.Router();
8883

89-
router.get('/compose-payment', this.composePayment.bind(this));
9084
router.all('/check', this.check.bind(this));
9185
router.all('/pay', this.pay.bind(this));
9286
router.all('/fail', this.fail.bind(this));
@@ -95,119 +89,6 @@ export default class CloudPaymentsWebhooks {
9589
return router;
9690
}
9791

98-
/**
99-
* Prepares payment data before charge
100-
*
101-
* @param req — Express request object
102-
* @param res - Express response object
103-
*/
104-
private async composePayment(req: ComposePaymentRequest, res: express.Response): Promise<void> {
105-
const { workspaceId, tariffPlanId, shouldSaveCard } = req.query;
106-
const userId = req.context.user.id;
107-
108-
if (!workspaceId || !tariffPlanId || !userId) {
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);
115-
116-
return;
117-
}
118-
119-
let workspace;
120-
let tariffPlan;
121-
122-
try {
123-
workspace = await this.getWorkspace(req, workspaceId);
124-
tariffPlan = await this.getPlan(req, tariffPlanId);
125-
} catch (e) {
126-
const error = e as Error;
127-
128-
this.sendError(res, 1, `[Billing / Compose payment] Can't get data from Database ${error.toString()}`, req.query);
129-
130-
return;
131-
}
132-
133-
try {
134-
await this.getMember(userId, workspace);
135-
} catch (e) {
136-
const error = e as Error;
137-
138-
this.sendError(res, 1, `[Billing / Compose payment] Can't compose payment due to error: ${error.toString()}`, req.query);
139-
140-
return;
141-
}
142-
const invoiceId = this.generateInvoiceId(tariffPlan, workspace);
143-
144-
const isCardLinkOperation = workspace.tariffPlanId.toString() === tariffPlanId && !workspace.isTariffPlanExpired();
145-
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-
} else {
154-
nextPaymentDate = new Date(now);
155-
}
156-
157-
if (workspace.isDebug) {
158-
nextPaymentDate.setDate(nextPaymentDate.getDate() + 1);
159-
} else {
160-
nextPaymentDate.setMonth(nextPaymentDate.getMonth() + 1);
161-
}
162-
163-
let checksum;
164-
165-
try {
166-
const checksumData = isCardLinkOperation ? {
167-
isCardLinkOperation: true,
168-
workspaceId: workspace._id.toString(),
169-
userId: userId,
170-
nextPaymentDate: nextPaymentDate.toISOString(),
171-
} : {
172-
workspaceId: workspace._id.toString(),
173-
userId: userId,
174-
tariffPlanId: tariffPlan._id.toString(),
175-
shouldSaveCard: shouldSaveCard === 'true',
176-
nextPaymentDate: nextPaymentDate.toISOString(),
177-
};
178-
179-
checksum = await checksumService.generateChecksum(checksumData);
180-
} catch (e) {
181-
const error = e as Error;
182-
183-
this.sendError(res, 1, `[Billing / Compose payment] Can't generate checksum: ${error.toString()}`, req.query);
184-
185-
return;
186-
}
187-
188-
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Compose payment]
189-
190-
card link operation: ${isCardLinkOperation}
191-
amount: ${+tariffPlan.monthlyCharge} RUB
192-
last charge date: ${workspace.lastChargeDate?.toISOString()}
193-
next payment date: ${nextPaymentDate.toISOString()}
194-
workspace id: ${workspace._id.toString()}
195-
debug: ${Boolean(workspace.isDebug)}`
196-
, TelegramBotURLs.Money));
197-
198-
res.send({
199-
invoiceId,
200-
plan: {
201-
id: tariffPlan._id.toString(),
202-
name: tariffPlan.name,
203-
monthlyCharge: tariffPlan.monthlyCharge,
204-
},
205-
isCardLinkOperation,
206-
currency: 'RUB',
207-
checksum,
208-
nextPaymentDate: nextPaymentDate.toISOString(),
209-
});
210-
}
21192

21293
/**
21394
* Generates invoice id for payment

src/billing/types/composePaymentPayload.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/resolvers/billingNew.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
import checksumService from '../utils/checksumService';
1111
import { UserInputError } from 'apollo-server-express';
1212
import cloudPaymentsApi, { CloudPaymentsJsonData } from '../utils/cloudPaymentsApi';
13+
import * as telegram from '../utils/telegram';
14+
import { TelegramBotURLs } from '../utils/telegram';
1315

1416
/**
1517
* The amount we will debit to confirm the subscription.
@@ -136,6 +138,21 @@ export default {
136138

137139
const checksum = await checksumService.generateChecksum(checksumData);
138140

141+
/**
142+
* Send info to Telegram (non-blocking)
143+
*/
144+
telegram
145+
.sendMessage(`✅ [Billing / Compose payment]
146+
147+
card link operation: ${isCardLinkOperation}
148+
amount: ${+plan.monthlyCharge} RUB
149+
last charge date: ${workspace.lastChargeDate?.toISOString()}
150+
next payment date: ${nextPaymentDate.toISOString()}
151+
workspace id: ${workspace._id.toString()}
152+
debug: ${Boolean(workspace.isDebug)}`
153+
, TelegramBotURLs.Money)
154+
.catch(e => console.error('Error while sending message to Telegram: ' + e));
155+
139156
return {
140157
invoiceId,
141158
plan: {

0 commit comments

Comments
 (0)