@@ -25,7 +25,6 @@ import {
2525 PlanDBScheme ,
2626 PlanProlongationPayload
2727} from '@hawk.so/types' ;
28- import { PENNY_MULTIPLIER } from 'codex-accounting-sdk' ;
2928import WorkspaceModel from '../models/workspace' ;
3029import HawkCatcher from '@hawk.so/nodejs' ;
3130import { publish } from '../rabbitmq' ;
@@ -45,6 +44,8 @@ import PlanModel from '../models/plan';
4544import { ClientApi , ClientService , CustomerReceiptItem , ReceiptApi , ReceiptTypes , TaxationSystem } from 'cloudpayments' ;
4645import { ComposePaymentPayload } from './types/composePaymentPayload' ;
4746
47+ const PENNY_MULTIPLIER = 100 ;
48+
4849interface 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,37 @@ 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+ } 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+
140163 let checksum ;
141164
142165 try {
143166 const checksumData = isCardLinkOperation ? {
144167 isCardLinkOperation : true ,
145168 workspaceId : workspace . _id . toString ( ) ,
146169 userId : userId ,
170+ nextPaymentDate : nextPaymentDate . toISOString ( ) ,
147171 } : {
148172 workspaceId : workspace . _id . toString ( ) ,
149173 userId : userId ,
150174 tariffPlanId : tariffPlan . _id . toString ( ) ,
151175 shouldSaveCard : shouldSaveCard === 'true' ,
176+ nextPaymentDate : nextPaymentDate . toISOString ( ) ,
152177 } ;
153178
154179 checksum = await checksumService . generateChecksum ( checksumData ) ;
@@ -170,6 +195,7 @@ export default class CloudPaymentsWebhooks {
170195 isCardLinkOperation,
171196 currency : 'RUB' ,
172197 checksum,
198+ nextPaymentDate : nextPaymentDate . toISOString ( ) ,
173199 } ) ;
174200 }
175201
@@ -268,7 +294,7 @@ export default class CloudPaymentsWebhooks {
268294 status : BusinessOperationStatus . Pending ,
269295 payload : {
270296 workspaceId : workspace . _id ,
271- amount : + body . Amount * PENNY_MULTIPLIER ,
297+ amount : + body . Amount ,
272298 currency : body . Currency ,
273299 userId : member . _id ,
274300 tariffPlanId : plan . _id ,
@@ -357,7 +383,6 @@ export default class CloudPaymentsWebhooks {
357383
358384 try {
359385 await businessOperation . setStatus ( BusinessOperationStatus . Confirmed ) ;
360- await workspace . resetBillingPeriod ( ) ;
361386 await workspace . changePlan ( tariffPlan . _id ) ;
362387
363388 const subscriptionId = body . SubscriptionId ;
@@ -427,7 +452,7 @@ export default class CloudPaymentsWebhooks {
427452
428453 try {
429454 await publish ( 'cron-tasks' , 'cron-tasks/limiter' , JSON . stringify ( {
430- type : 'check-single -workspace' ,
455+ type : 'unblock -workspace' ,
431456 workspaceId : data . workspaceId ,
432457 } ) ) ;
433458 } catch ( e ) {
@@ -479,8 +504,6 @@ export default class CloudPaymentsWebhooks {
479504 * Refund the money that were charged to link a card
480505 */
481506 if ( data . isCardLinkOperation ) {
482- this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Pay] Recurrent payments activated for «${ workspace . name } ». 1 RUB charged` , TelegramBotURLs . Money ) ) ;
483-
484507 await cloudPaymentsApi . cancelPayment ( body . TransactionId ) ;
485508
486509 const member = await this . getMember ( data . userId , workspace ) ;
@@ -503,7 +526,13 @@ export default class CloudPaymentsWebhooks {
503526 dtCreated : new Date ( ) ,
504527 } ) ;
505528
506- this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Pay] Recurrent payments activated for «${ workspace . name } ». 1 RUB returned` , TelegramBotURLs . Money ) ) ;
529+ this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Pay] Card linked
530+
531+ workspace id: ${ workspace . _id }
532+ date of operation: ${ body . DateTime }
533+ first payment date: ${ data . cloudPayments ?. recurrent . startDate }
534+ sum: ${ data . cloudPayments ?. recurrent . amount } ${ body . Currency } `
535+ , TelegramBotURLs . Money ) ) ;
507536 } else {
508537 /**
509538 * Russia code from ISO 3166-1
@@ -517,7 +546,14 @@ export default class CloudPaymentsWebhooks {
517546
518547 await this . sendReceipt ( workspace , tariffPlan , userEmail ) ;
519548
520- this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Pay] Payment passed successfully for «${ workspace . name } »` , TelegramBotURLs . Money ) ) ;
549+ this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Pay] New payment
550+
551+ amount: ${ + body . Amount } ${ body . Currency }
552+ next payment date: ${ data . cloudPayments ?. recurrent . startDate }
553+ workspace id: ${ workspace . _id }
554+ date of operation: ${ body . DateTime }
555+ subscription id: ${ body . SubscriptionId } `
556+ , TelegramBotURLs . Money ) ) ;
521557 }
522558 } catch ( e ) {
523559 const error = e as Error ;
@@ -607,7 +643,7 @@ export default class CloudPaymentsWebhooks {
607643 return ;
608644 }
609645
610- this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Fail] Transaction failed for «${ workspace . name } »` , TelegramBotURLs . Money ) ) ;
646+ this . handleSendingToTelegramError ( telegram . sendMessage ( `❌ [Billing / Fail] Transaction failed for «${ workspace . name } »` , TelegramBotURLs . Money ) ) ;
611647
612648 HawkCatcher . send ( new Error ( '[Billing / Fail] Transaction failed' ) , body as any ) ;
613649
@@ -629,7 +665,13 @@ export default class CloudPaymentsWebhooks {
629665
630666 console . log ( '💎 CloudPayments /recurrent request' , body ) ;
631667
632- this . handleSendingToTelegramError ( telegram . sendMessage ( `[Billing / Recurrent] New recurrent event with ${ body . Status } status` , TelegramBotURLs . Money ) ) ;
668+ this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Recurrent] New recurrent transaction
669+
670+ amount: ${ + body . Amount } ${ body . Currency }
671+ next payment date: ${ body . NextTransactionDate }
672+ workspace id: ${ body . AccountId }
673+ subscription id: ${ body . Id } `
674+ , TelegramBotURLs . Money ) ) ;
633675 HawkCatcher . send ( new Error ( `[Billing / Recurrent] New recurrent event with ${ body . Status } status` ) , req . body ) ;
634676
635677 switch ( body . Status ) {
0 commit comments