From 53a5cc6577d3b55cdcc586ff4657585befe6e4b0 Mon Sep 17 00:00:00 2001 From: Arpitkumar Chaudhari Date: Mon, 9 Aug 2021 02:58:27 +0530 Subject: [PATCH] Calculating Amount & Stripe APIs --- README.md | 3 + docs/swagger.yaml | 130 ++++++++++++++++++++++++++++++ package-lock.json | 9 +++ package.json | 1 + src/controllers/TeamController.js | 22 ++++- src/routes/TeamRoutes.js | 14 +++- src/services/TeamService.js | 27 +++++++ 7 files changed, 204 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9c32d290..a8215223 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ ES_HOST=http://dockerhost:9200 DATABASE_URL=postgres://postgres:postgres@dockerhost:5432/postgres BUSAPI_URL=http://dockerhost:8002/v5 + # stripe + STRIPE_SECRET_KEY= + CURRENCY=usd ``` - Values from this file would be automatically used by many `npm` commands. diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 65d420f1..2ab802b2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -3435,6 +3435,110 @@ paths: application/json: schema: $ref: "#/components/schemas/Error" + /taas-teams/calculateAmount: + post: + tags: + - Teams + description: | + Calculates total amount for the team. + + **Authorization** Any Topcoder user with valid token is allowed. For not logged users Topcoder m2m token with create:taas-teams scope is allowed. + security: + - bearerAuth: [] + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/CalculateAmountRequestBody" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/CalculateAmountResponse" + "400": + description: Bad request + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "401": + description: Not authenticated + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "403": + description: Forbidden + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "409": + description: Conflict + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + description: Internal Server Error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + /taas-teams/createPayment: + post: + tags: + - Teams + description: | + Calculates total amount for the team. + + **Authorization** Any Topcoder user with valid token is allowed. For not logged users Topcoder m2m token with create:taas-teams scope is allowed. + security: + - bearerAuth: [] + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/CreatePaymentRequestBody" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/CreatePaymentResponse" + "400": + description: Bad request + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "401": + description: Not authenticated + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "403": + description: Forbidden + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "409": + description: Conflict + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + description: Internal Server Error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" /taas-teams/members-suggest/{fragment}: get: tags: @@ -5589,6 +5693,32 @@ components: type: string description: "Optional job title." example: "Lead Application Developer" + CalculateAmountRequestBody: + properties: + numberOfResources: + type: number + description: "No. of resources required." + rates: + type: number + description: "Weekly rates" + durationWeeks: + type: number + description: "No. of weeks" + CalculateAmountResponse: + properties: + totalAmount: + type: number + description: "Total amount calculated" + CreatePaymentRequestBody: + properties: + totalAmount: + type: number + description: "Total amount charged to user via stripe" + CreatePaymentResponse: + properties: + paymentIntentToken: + type: string + description: " Token required by stripe for completing payment." SubmitTeamRequestBody: properties: teamName: diff --git a/package-lock.json b/package-lock.json index 3d174e12..56bb51a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7155,6 +7155,15 @@ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==" }, + "stripe": { + "version": "8.168.0", + "resolved": "https://registry.npmjs.org/stripe/-/stripe-8.168.0.tgz", + "integrity": "sha512-MQXTarijIOagtLajGe1zBFc9KMbB7jIoFv/kr1WsDPJO/S+/hhZjsXCgBkNvnlwK7Yl0VUn+YrgXl9/9wU6WCw==", + "requires": { + "@types/node": ">=8.1.0", + "qs": "^6.6.0" + } + }, "success-symbol": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/success-symbol/-/success-symbol-0.1.0.tgz", diff --git a/package.json b/package.json index 6c47684a..4beb0c38 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "prompt-confirm": "^2.0.4", "rewire": "^5.0.0", "sequelize": "^6.3.5", + "stripe": "^8.168.0", "superagent": "^6.1.0", "tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6", "util": "^0.12.3", diff --git a/src/controllers/TeamController.js b/src/controllers/TeamController.js index 65e5262f..e34fa943 100644 --- a/src/controllers/TeamController.js +++ b/src/controllers/TeamController.js @@ -155,6 +155,24 @@ async function suggestMembers (req, res) { res.send(await service.suggestMembers(req.authUser, req.params.fragment)) } +/** + * + * @param req the request + * @param res the response + */ + async function calculateAmount(req, res) { + res.send(await service.calculateAmount(req.body)); +} + +/** + * + * @param req the request + * @param res the response + */ +async function createPayment(req, res) { + res.send(await service.createPayment(req.body.totalAmount)); +} + module.exports = { searchTeams, getTeam, @@ -169,5 +187,7 @@ module.exports = { roleSearchRequest, createTeam, searchSkills, - suggestMembers + suggestMembers, + createPayment, + calculateAmount } diff --git a/src/routes/TeamRoutes.js b/src/routes/TeamRoutes.js index a4c1ca5e..3c83d5f2 100644 --- a/src/routes/TeamRoutes.js +++ b/src/routes/TeamRoutes.js @@ -107,5 +107,17 @@ module.exports = { auth: 'jwt', scopes: [] } - } + }, + "/taas-teams/calculateAmount": { + post: { + controller: "TeamController", + method: "calculateAmount", + }, + }, + "/taas-teams/createPayment": { + post: { + controller: "TeamController", + method: "createPayment", + }, +} } diff --git a/src/services/TeamService.js b/src/services/TeamService.js index 3d1f390b..35269e8d 100644 --- a/src/services/TeamService.js +++ b/src/services/TeamService.js @@ -20,6 +20,7 @@ const { getAuditM2Muser } = require('../common/helper') const { matchedSkills, unMatchedSkills } = require('../../scripts/emsi-mapping/esmi-skills-mapping') const Role = models.Role const RoleSearchRequest = models.RoleSearchRequest +const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY); const emailTemplates = _.mapValues(emailTemplateConfig, (template) => { return { @@ -1162,6 +1163,30 @@ suggestMembers.schema = Joi.object().keys({ fragment: Joi.string().required() }).required() +/** + * Calculates total amount + * @param {Object} body + * @returns {int} totalAmount + */ + async function calculateAmount(body) { + const totalAmount = body.numberOfResources * body.rates * body.durationWeeks; + return { totalAmount }; +} + +/** + * Creates token for stripe + * @param {int} totalAmount + * @returns {string} paymentIntentToken + */ +async function createPayment(totalAmount) { + const paymentIntent = await stripe.paymentIntents.create({ + amount: totalAmount, + currency: process.env.CURRENCY, + }); + return { paymentIntentToken: paymentIntent.client_secret }; +} + + module.exports = { searchTeams, getTeam, @@ -1180,6 +1205,8 @@ module.exports = { createRoleSearchRequest, isExternalMember, createTeam, + calculateAmount, + createPayment, searchSkills, suggestMembers }