@@ -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,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 ) {
0 commit comments