@@ -43,11 +43,13 @@ import { PaymentData } from './types/paymentData';
4343import cloudPaymentsApi from '../utils/cloudPaymentsApi' ;
4444import PlanModel from '../models/plan' ;
4545import { ClientApi , ClientService , CustomerReceiptItem , ReceiptApi , ReceiptTypes , TaxationSystem } from 'cloudpayments' ;
46+ import { ComposePaymentPayload } from './types/composePaymentPayload' ;
4647
47- /**
48- * Custom data of the plan prolongation request
49- */
50- type PlanProlongationData = PlanProlongationPayload & PaymentData ;
48+
49+ interface ComposePaymentRequest extends express . Request {
50+ query : ComposePaymentPayload & { [ key : string ] : any } ;
51+ context : import ( '../types/graphql' ) . ResolverContextBase
52+ } ;
5153
5254/**
5355 * Class for describing the logic of payment routes
@@ -99,8 +101,8 @@ export default class CloudPaymentsWebhooks {
99101 * @param req — Express request object
100102 * @param res - Express response object
101103 */
102- private async composePayment ( req : express . Request , res : express . Response ) : Promise < void > {
103- const { workspaceId, tariffPlanId, shouldSaveCard } = req . query as Record < string , string > ;
104+ private async composePayment ( req : ComposePaymentRequest , res : express . Response ) : Promise < void > {
105+ const { workspaceId, tariffPlanId, shouldSaveCard, isCardLinkOperation } = req . query ;
104106 const userId = req . context . user . id ;
105107
106108 if ( ! workspaceId || ! tariffPlanId || ! userId ) {
@@ -142,6 +144,7 @@ export default class CloudPaymentsWebhooks {
142144 userId : userId ,
143145 tariffPlanId : tariffPlan . _id . toString ( ) ,
144146 shouldSaveCard : shouldSaveCard === 'true' ,
147+ isCardLinkOperation : isCardLinkOperation === 'true' ,
145148 } ) ;
146149 } catch ( e ) {
147150 const error = e as Error ;
@@ -201,7 +204,7 @@ export default class CloudPaymentsWebhooks {
201204 let member : ConfirmedMemberDBScheme ;
202205 let plan : PlanDBScheme ;
203206
204- if ( ! data . workspaceId || ! data . tariffPlanId || ! data . userId ) {
207+ if ( ! data . workspaceId || ! data . tariffPlanId || ! data . userId || data . isCardLinkOperation === undefined ) {
205208 this . sendError ( res , CheckCodes . PAYMENT_COULD_NOT_BE_ACCEPTED , '[Billing / Check] There is no necessary data in the request' , body ) ;
206209
207210 return ;
@@ -235,6 +238,18 @@ export default class CloudPaymentsWebhooks {
235238 return ;
236239 }
237240
241+ if ( data . isCardLinkOperation ) {
242+ telegram
243+ . sendMessage ( `✅ [Billing / Check] Card linked for subscription workspace «${ workspace . name } »` , TelegramBotURLs . Money )
244+ . catch ( e => console . error ( 'Error while sending message to Telegram: ' + e ) ) ;
245+
246+ res . json ( {
247+ code : CheckCodes . SUCCESS ,
248+ } as CheckResponse ) ;
249+
250+ return ;
251+ }
252+
238253 /**
239254 * Create business operation about creation of subscription
240255 */
@@ -266,6 +281,7 @@ export default class CloudPaymentsWebhooks {
266281
267282 telegram . sendMessage ( `✅ [Billing / Check] All checks passed successfully «${ workspace . name } »` , TelegramBotURLs . Money )
268283 . catch ( e => console . error ( 'Error while sending message to Telegram: ' + e ) ) ;
284+
269285 HawkCatcher . send ( new Error ( '[Billing / Check] All checks passed successfully' ) , body as any ) ;
270286
271287 res . json ( {
@@ -720,9 +736,9 @@ export default class CloudPaymentsWebhooks {
720736 *
721737 * @param req - request with necessary data
722738 */
723- private async getDataFromRequest ( req : express . Request ) : Promise < PlanProlongationData > {
739+ private async getDataFromRequest ( req : express . Request ) : Promise < PaymentData > {
724740 const context = req . context ;
725- const body : CheckRequest = req . body ;
741+ const body : CheckRequest | PayRequest | FailRequest = req . body ;
726742
727743 /**
728744 * If Data is not presented in body means there is a recurring payment
@@ -752,6 +768,7 @@ export default class CloudPaymentsWebhooks {
752768 tariffPlanId : workspace . tariffPlanId . toString ( ) ,
753769 userId,
754770 shouldSaveCard : false ,
771+ isCardLinkOperation : false
755772 } ;
756773 }
757774
0 commit comments