-
Notifications
You must be signed in to change notification settings - Fork 2
fix(payments): Hide card linking business operation #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,11 +43,12 @@ | |
| import cloudPaymentsApi from '../utils/cloudPaymentsApi'; | ||
| import PlanModel from '../models/plan'; | ||
| import { ClientApi, ClientService, CustomerReceiptItem, ReceiptApi, ReceiptTypes, TaxationSystem } from 'cloudpayments'; | ||
| import { ComposePaymentPayload } from './types/composePaymentPayload'; | ||
|
|
||
| /** | ||
| * Custom data of the plan prolongation request | ||
| */ | ||
| type PlanProlongationData = PlanProlongationPayload & PaymentData; | ||
| interface ComposePaymentRequest extends express.Request { | ||
| query: ComposePaymentPayload & { [key: string]: any }; | ||
| context: import('../types/graphql').ResolverContextBase; | ||
| }; | ||
|
|
||
| /** | ||
| * Class for describing the logic of payment routes | ||
|
|
@@ -99,8 +100,8 @@ | |
| * @param req — Express request object | ||
| * @param res - Express response object | ||
| */ | ||
| private async composePayment(req: express.Request, res: express.Response): Promise<void> { | ||
| const { workspaceId, tariffPlanId, shouldSaveCard } = req.query as Record<string, string>; | ||
| private async composePayment(req: ComposePaymentRequest, res: express.Response): Promise<void> { | ||
| const { workspaceId, tariffPlanId, shouldSaveCard } = req.query; | ||
| const userId = req.context.user.id; | ||
|
|
||
| if (!workspaceId || !tariffPlanId || !userId) { | ||
|
|
@@ -134,15 +135,23 @@ | |
| } | ||
| const invoiceId = this.generateInvoiceId(tariffPlan, workspace); | ||
|
|
||
| const isCardLinkOperation = workspace.tariffPlanId.toString() === tariffPlanId && !this.isPlanExpired(workspace); | ||
|
|
||
| let checksum; | ||
|
|
||
| try { | ||
| checksum = await checksumService.generateChecksum({ | ||
| const checksumData = isCardLinkOperation ? { | ||
| isCardLinkOperation: true, | ||
| workspaceId: workspace._id.toString(), | ||
| userId: userId, | ||
| } : { | ||
| workspaceId: workspace._id.toString(), | ||
| userId: userId, | ||
| tariffPlanId: tariffPlan._id.toString(), | ||
| shouldSaveCard: shouldSaveCard === 'true', | ||
| }); | ||
| }; | ||
|
|
||
| checksum = await checksumService.generateChecksum(checksumData); | ||
| } catch (e) { | ||
| const error = e as Error; | ||
|
|
||
|
|
@@ -158,12 +167,33 @@ | |
| name: tariffPlan.name, | ||
| monthlyCharge: tariffPlan.monthlyCharge, | ||
| }, | ||
| isCardLinkOperation, | ||
| currency: 'RUB', | ||
| checksum, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
Check warning on line 176 in src/billing/cloudpayments.ts
|
||
| * Returns true if workspace's plan is expired | ||
| * @param workspace - workspace to check | ||
| */ | ||
| private isPlanExpired(workspace: WorkspaceModel): boolean { | ||
| const lastChargeDate = new Date(workspace.lastChargeDate); | ||
|
|
||
| let planExpiracyDate; | ||
|
|
||
| if (workspace.isDebug) { | ||
| planExpiracyDate = lastChargeDate.setDate(lastChargeDate.getDate() + 1); | ||
| } else { | ||
| planExpiracyDate = lastChargeDate.setMonth(lastChargeDate.getMonth() + 1); | ||
| } | ||
|
|
||
| const isPlanExpired = planExpiracyDate < Date.now(); | ||
|
|
||
| return isPlanExpired; | ||
| } | ||
|
|
||
| /** | ||
|
Check warning on line 196 in src/billing/cloudpayments.ts
|
||
| * Generates invoice id for payment | ||
| * | ||
| * @param tariffPlan - tariff plan to generate invoice id | ||
|
|
@@ -201,7 +231,13 @@ | |
| let member: ConfirmedMemberDBScheme; | ||
| let plan: PlanDBScheme; | ||
|
|
||
| if (!data.workspaceId || !data.tariffPlanId || !data.userId) { | ||
| if (data.isCardLinkOperation && (!data.userId || !data.workspaceId)) { | ||
| this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, '[Billing / Check] Card linking – invalid data', body); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!data.isCardLinkOperation && (!data.userId || !data.workspaceId || !data.tariffPlanId)) { | ||
| this.sendError(res, CheckCodes.PAYMENT_COULD_NOT_BE_ACCEPTED, '[Billing / Check] There is no necessary data in the request', body); | ||
|
|
||
| return; | ||
|
|
@@ -235,6 +271,18 @@ | |
| return; | ||
| } | ||
|
|
||
| if (data.isCardLinkOperation) { | ||
| telegram | ||
| .sendMessage(`✅ [Billing / Check] Card linked for subscription workspace «${workspace.name}»`, TelegramBotURLs.Money) | ||
| .catch(e => console.error('Error while sending message to Telegram: ' + e)); | ||
|
|
||
| res.json({ | ||
| code: CheckCodes.SUCCESS, | ||
| } as CheckResponse); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * Create business operation about creation of subscription | ||
| */ | ||
|
|
@@ -266,7 +314,8 @@ | |
|
|
||
| telegram.sendMessage(`✅ [Billing / Check] All checks passed successfully «${workspace.name}»`, TelegramBotURLs.Money) | ||
| .catch(e => console.error('Error while sending message to Telegram: ' + e)); | ||
|
|
||
| HawkCatcher.send(new Error('[Billing / Check] All checks passed successfully'), body as any); | ||
|
|
||
| res.json({ | ||
| code: CheckCodes.SUCCESS, | ||
|
|
@@ -544,7 +593,7 @@ | |
|
|
||
| this.handleSendingToTelegramError(telegram.sendMessage(`✅ [Billing / Fail] Transaction failed for «${workspace.name}»`, TelegramBotURLs.Money)); | ||
|
|
||
| HawkCatcher.send(new Error('[Billing / Fail] Transaction failed'), body as any); | ||
|
|
||
| res.json({ | ||
| code: FailCodes.SUCCESS, | ||
|
|
@@ -705,7 +754,7 @@ | |
| * @param errorText - error description | ||
| * @param backtrace - request data and error data | ||
| */ | ||
| private sendError(res: express.Response, errorCode: CheckCodes | PayCodes | FailCodes | RecurrentCodes, errorText: string, backtrace: { [key: string]: any }): void { | ||
| res.json({ | ||
| code: errorCode, | ||
| }); | ||
|
|
@@ -720,9 +769,9 @@ | |
| * | ||
| * @param req - request with necessary data | ||
| */ | ||
| private async getDataFromRequest(req: express.Request): Promise<PlanProlongationData> { | ||
| private async getDataFromRequest(req: express.Request): Promise<PaymentData> { | ||
| const context = req.context; | ||
| const body: CheckRequest = req.body; | ||
| const body: CheckRequest | PayRequest | FailRequest = req.body; | ||
|
|
||
| /** | ||
| * If Data is not presented in body means there is a recurring payment | ||
|
|
@@ -752,6 +801,7 @@ | |
| tariffPlanId: workspace.tariffPlanId.toString(), | ||
| userId, | ||
| shouldSaveCard: false, | ||
| isCardLinkOperation: false, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -766,7 +816,7 @@ | |
| promise.catch(e => console.error('Error while sending message to Telegram: ' + e)); | ||
| } | ||
|
|
||
| /** | ||
|
Check warning on line 819 in src/billing/cloudpayments.ts
|
||
| * Parses body and returns card data | ||
| * @param request - request body to parse | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export interface ComposePaymentPayload { | ||
| /** | ||
| * Workspace Identifier | ||
| */ | ||
| workspaceId: string; | ||
| /** | ||
| * Id of the user making the payment | ||
| */ | ||
| userId: string; | ||
| /** | ||
| * Workspace current plan id or plan id to change | ||
| */ | ||
| tariffPlanId: string; | ||
| /** | ||
| * If true, we will save user card | ||
| */ | ||
| shouldSaveCard: 'true' | 'false'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need these fields?