@@ -42,15 +42,9 @@ import { PaymentData } from './types/paymentData';
4242import cloudPaymentsApi from '../utils/cloudPaymentsApi' ;
4343import PlanModel from '../models/plan' ;
4444import { ClientApi , ClientService , CustomerReceiptItem , ReceiptApi , ReceiptTypes , TaxationSystem } from 'cloudpayments' ;
45- import { ComposePaymentPayload } from './types/composePaymentPayload' ;
4645
4746const PENNY_MULTIPLIER = 100 ;
4847
49- interface ComposePaymentRequest extends express . Request {
50- query : ComposePaymentPayload & { [ key : string ] : any } ;
51- context : import ( '../types/graphql' ) . ResolverContextBase ;
52- } ;
53-
5448/**
5549 * Class for describing the logic of payment routes
5650 */
@@ -86,7 +80,6 @@ export default class CloudPaymentsWebhooks {
8680 public getRouter ( ) : express . Router {
8781 const router = express . Router ( ) ;
8882
89- router . get ( '/compose-payment' , this . composePayment . bind ( this ) ) ;
9083 router . all ( '/check' , this . check . bind ( this ) ) ;
9184 router . all ( '/pay' , this . pay . bind ( this ) ) ;
9285 router . all ( '/fail' , this . fail . bind ( this ) ) ;
@@ -95,120 +88,6 @@ export default class CloudPaymentsWebhooks {
9588 return router ;
9689 }
9790
98- /**
99- * Prepares payment data before charge
100- *
101- * @param req — Express request object
102- * @param res - Express response object
103- */
104- private async composePayment ( req : ComposePaymentRequest , res : express . Response ) : Promise < void > {
105- const { workspaceId, tariffPlanId, shouldSaveCard } = req . query ;
106- const userId = req . context . user . id ;
107-
108- if ( ! workspaceId || ! tariffPlanId || ! userId ) {
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 ) ;
115-
116- return ;
117- }
118-
119- let workspace ;
120- let tariffPlan ;
121-
122- try {
123- workspace = await this . getWorkspace ( req , workspaceId ) ;
124- tariffPlan = await this . getPlan ( req , tariffPlanId ) ;
125- } catch ( e ) {
126- const error = e as Error ;
127-
128- this . sendError ( res , 1 , `[Billing / Compose payment] Can't get data from Database ${ error . toString ( ) } ` , req . query ) ;
129-
130- return ;
131- }
132-
133- try {
134- await this . getMember ( userId , workspace ) ;
135- } catch ( e ) {
136- const error = e as Error ;
137-
138- this . sendError ( res , 1 , `[Billing / Compose payment] Can't compose payment due to error: ${ error . toString ( ) } ` , req . query ) ;
139-
140- return ;
141- }
142- const invoiceId = this . generateInvoiceId ( tariffPlan , workspace ) ;
143-
144- const isCardLinkOperation = workspace . tariffPlanId . toString ( ) === tariffPlanId && ! workspace . isTariffPlanExpired ( ) ;
145-
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-
163- let checksum ;
164-
165- try {
166- const checksumData = isCardLinkOperation ? {
167- isCardLinkOperation : true ,
168- workspaceId : workspace . _id . toString ( ) ,
169- userId : userId ,
170- nextPaymentDate : nextPaymentDate . toISOString ( ) ,
171- } : {
172- workspaceId : workspace . _id . toString ( ) ,
173- userId : userId ,
174- tariffPlanId : tariffPlan . _id . toString ( ) ,
175- shouldSaveCard : shouldSaveCard === 'true' ,
176- nextPaymentDate : nextPaymentDate . toISOString ( ) ,
177- } ;
178-
179- checksum = await checksumService . generateChecksum ( checksumData ) ;
180- } catch ( e ) {
181- const error = e as Error ;
182-
183- this . sendError ( res , 1 , `[Billing / Compose payment] Can't generate checksum: ${ error . toString ( ) } ` , req . query ) ;
184-
185- return ;
186- }
187-
188- this . handleSendingToTelegramError ( telegram . sendMessage ( `✅ [Billing / Compose payment]
189-
190- card link operation: ${ isCardLinkOperation }
191- amount: ${ + tariffPlan . monthlyCharge } RUB
192- last charge date: ${ workspace . lastChargeDate ?. toISOString ( ) }
193- next payment date: ${ nextPaymentDate . toISOString ( ) }
194- workspace id: ${ workspace . _id . toString ( ) }
195- debug: ${ Boolean ( workspace . isDebug ) } `
196- , TelegramBotURLs . Money ) ) ;
197-
198- res . send ( {
199- invoiceId,
200- plan : {
201- id : tariffPlan . _id . toString ( ) ,
202- name : tariffPlan . name ,
203- monthlyCharge : tariffPlan . monthlyCharge ,
204- } ,
205- isCardLinkOperation,
206- currency : 'RUB' ,
207- checksum,
208- nextPaymentDate : nextPaymentDate . toISOString ( ) ,
209- } ) ;
210- }
211-
21291 /**
21392 * Generates invoice id for payment
21493 *
0 commit comments