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