Skip to content

Commit c06fc38

Browse files
committed
Merge remote-tracking branch 'origin/master' into prod
2 parents 02528cc + 70780ed commit c06fc38

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hawk.api",
3-
"version": "1.1.12",
3+
"version": "1.1.13",
44
"main": "index.ts",
55
"license": "UNLICENSED",
66
"scripts": {

src/billing/cloudpayments.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export default class CloudPaymentsWebhooks {
135135
}
136136
const invoiceId = this.generateInvoiceId(tariffPlan, workspace);
137137

138-
const isCardLinkOperation = workspace.tariffPlanId.toString() === tariffPlanId && !this.isPlanExpired(workspace);
138+
const isCardLinkOperation = workspace.tariffPlanId.toString() === tariffPlanId && !workspace.isTariffPlanExpired();
139139

140140
let checksum;
141141

@@ -173,26 +173,6 @@ export default class CloudPaymentsWebhooks {
173173
});
174174
}
175175

176-
/**
177-
* Returns true if workspace's plan is expired
178-
* @param workspace - workspace to check
179-
*/
180-
private isPlanExpired(workspace: WorkspaceModel): boolean {
181-
const lastChargeDate = new Date(workspace.lastChargeDate);
182-
183-
let planExpiracyDate;
184-
185-
if (workspace.isDebug) {
186-
planExpiracyDate = lastChargeDate.setDate(lastChargeDate.getDate() + 1);
187-
} else {
188-
planExpiracyDate = lastChargeDate.setMonth(lastChargeDate.getMonth() + 1);
189-
}
190-
191-
const isPlanExpired = planExpiracyDate < Date.now();
192-
193-
return isPlanExpired;
194-
}
195-
196176
/**
197177
* Generates invoice id for payment
198178
*
@@ -216,6 +196,8 @@ export default class CloudPaymentsWebhooks {
216196
const context = req.context;
217197
const body: CheckRequest = req.body;
218198
let data;
199+
200+
console.log('💎 CloudPayments /check request', body);
219201

220202
try {
221203
data = await this.getDataFromRequest(req);
@@ -326,6 +308,8 @@ export default class CloudPaymentsWebhooks {
326308
const body: PayRequest = req.body;
327309
let data;
328310

311+
console.log('💎 CloudPayments /pay request', body);
312+
329313
try {
330314
data = await this.getDataFromRequest(req);
331315
} catch (e) {
@@ -492,11 +476,33 @@ export default class CloudPaymentsWebhooks {
492476

493477
try {
494478
/**
495-
* Cancel payment if it is deferred
479+
* Refund the money that were charged to link a card
496480
*/
497-
if (data.cloudPayments?.recurrent?.startDate) {
481+
if (data.isCardLinkOperation) {
498482
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] Recurrent payments activated for «${workspace.name}». 1 RUB charged`, TelegramBotURLs.Money));
483+
499484
await cloudPaymentsApi.cancelPayment(body.TransactionId);
485+
486+
const member = await this.getMember(data.userId, workspace);
487+
const plan = await this.getPlan(req, planId);
488+
489+
/**
490+
* Create business operation about refund
491+
*/
492+
await req.context.factories.businessOperationsFactory.create<PayloadOfWorkspacePlanPurchase>({
493+
transactionId: body.TransactionId.toString(),
494+
type: BusinessOperationType.CardLinkRefund,
495+
status: BusinessOperationStatus.Confirmed,
496+
payload: {
497+
workspaceId: workspace._id,
498+
amount: +body.Amount * PENNY_MULTIPLIER * -1,
499+
currency: body.Currency,
500+
userId: member._id,
501+
tariffPlanId: plan._id,
502+
},
503+
dtCreated: new Date(),
504+
});
505+
500506
this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Pay] Recurrent payments activated for «${workspace.name}». 1 RUB returned`, TelegramBotURLs.Money));
501507
} else {
502508
/**
@@ -516,7 +522,7 @@ export default class CloudPaymentsWebhooks {
516522
} catch (e) {
517523
const error = e as Error;
518524

519-
this.sendError(res, PayCodes.SUCCESS, error.toString(), body);
525+
this.sendError(res, PayCodes.SUCCESS, `[Billing / Pay] Error trying to refund for card linking occured: ${error.toString()}`, body);
520526

521527
return;
522528
}
@@ -537,6 +543,8 @@ export default class CloudPaymentsWebhooks {
537543
const body: FailRequest = req.body;
538544
let data: PlanProlongationPayload;
539545

546+
console.log('💎 CloudPayments /fail request', body);
547+
540548
try {
541549
data = await this.getDataFromRequest(req);
542550
} catch (e) {
@@ -549,6 +557,10 @@ export default class CloudPaymentsWebhooks {
549557
let workspace;
550558
let user;
551559

560+
/**
561+
* @todo handle card linking and update business operation status
562+
*/
563+
552564
if (!data.workspaceId || !data.userId || !data.tariffPlanId) {
553565
this.sendError(res, FailCodes.SUCCESS, `[Billing / Fail] No workspace or user id or plan id in request body`, body);
554566

@@ -615,6 +627,8 @@ export default class CloudPaymentsWebhooks {
615627
const body: RecurrentRequest = req.body;
616628
const context = req.context;
617629

630+
console.log('💎 CloudPayments /recurrent request', body);
631+
618632
this.handleSendingToTelegramError(telegram.sendMessage(`[Billing / Recurrent] New recurrent event with ${body.Status} status`, TelegramBotURLs.Money));
619633
HawkCatcher.send(new Error(`[Billing / Recurrent] New recurrent event with ${body.Status} status`), req.body);
620634

@@ -763,6 +777,9 @@ export default class CloudPaymentsWebhooks {
763777
code: errorCode,
764778
});
765779

780+
console.log(errorText, backtrace);
781+
console.log('We responded with an error code to CloudPayments: ' + errorCode);
782+
766783
this.handleSendingToTelegramError(telegram.sendMessage(`❌ ${errorText}`, TelegramBotURLs.Money));
767784

768785
HawkCatcher.send(new Error(errorText), backtrace);

src/typeDefs/billing.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ enum BusinessOperationType {
6363
Workspace deposit balance by user
6464
"""
6565
DEPOSIT_BY_USER
66+
67+
"""
68+
Charge minimal amount of money to link a card for further recurrent payments
69+
"""
70+
CARD_LINK_CHARGE
71+
72+
"""
73+
Refund the money that were charged to link a card
74+
"""
75+
CARD_LINK_REFUND
6676
}
6777
6878

0 commit comments

Comments
 (0)