Skip to content

Commit 41d3a6f

Browse files
authored
Merge pull request #454 from arpitchaudhari/roles-checkout
Calculating Amount & Stripe APIs
2 parents e5d8118 + 53a5cc6 commit 41d3a6f

File tree

7 files changed

+204
-2
lines changed

7 files changed

+204
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
ES_HOST=http://dockerhost:9200
4848
DATABASE_URL=postgres://postgres:postgres@dockerhost:5432/postgres
4949
BUSAPI_URL=http://dockerhost:8002/v5
50+
# stripe
51+
STRIPE_SECRET_KEY=
52+
CURRENCY=usd
5053
```
5154

5255
- Values from this file would be automatically used by many `npm` commands.

docs/swagger.yaml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,6 +3435,110 @@ paths:
34353435
application/json:
34363436
schema:
34373437
$ref: "#/components/schemas/Error"
3438+
/taas-teams/calculateAmount:
3439+
post:
3440+
tags:
3441+
- Teams
3442+
description: |
3443+
Calculates total amount for the team.
3444+
3445+
**Authorization** Any Topcoder user with valid token is allowed. For not logged users Topcoder m2m token with create:taas-teams scope is allowed.
3446+
security:
3447+
- bearerAuth: []
3448+
requestBody:
3449+
content:
3450+
application/json:
3451+
schema:
3452+
$ref: "#/components/schemas/CalculateAmountRequestBody"
3453+
responses:
3454+
"200":
3455+
description: OK
3456+
content:
3457+
application/json:
3458+
schema:
3459+
$ref: "#/components/schemas/CalculateAmountResponse"
3460+
"400":
3461+
description: Bad request
3462+
content:
3463+
application/json:
3464+
schema:
3465+
$ref: "#/components/schemas/Error"
3466+
"401":
3467+
description: Not authenticated
3468+
content:
3469+
application/json:
3470+
schema:
3471+
$ref: "#/components/schemas/Error"
3472+
"403":
3473+
description: Forbidden
3474+
content:
3475+
application/json:
3476+
schema:
3477+
$ref: "#/components/schemas/Error"
3478+
"409":
3479+
description: Conflict
3480+
content:
3481+
application/json:
3482+
schema:
3483+
$ref: "#/components/schemas/Error"
3484+
"500":
3485+
description: Internal Server Error
3486+
content:
3487+
application/json:
3488+
schema:
3489+
$ref: "#/components/schemas/Error"
3490+
/taas-teams/createPayment:
3491+
post:
3492+
tags:
3493+
- Teams
3494+
description: |
3495+
Calculates total amount for the team.
3496+
3497+
**Authorization** Any Topcoder user with valid token is allowed. For not logged users Topcoder m2m token with create:taas-teams scope is allowed.
3498+
security:
3499+
- bearerAuth: []
3500+
requestBody:
3501+
content:
3502+
application/json:
3503+
schema:
3504+
$ref: "#/components/schemas/CreatePaymentRequestBody"
3505+
responses:
3506+
"200":
3507+
description: OK
3508+
content:
3509+
application/json:
3510+
schema:
3511+
$ref: "#/components/schemas/CreatePaymentResponse"
3512+
"400":
3513+
description: Bad request
3514+
content:
3515+
application/json:
3516+
schema:
3517+
$ref: "#/components/schemas/Error"
3518+
"401":
3519+
description: Not authenticated
3520+
content:
3521+
application/json:
3522+
schema:
3523+
$ref: "#/components/schemas/Error"
3524+
"403":
3525+
description: Forbidden
3526+
content:
3527+
application/json:
3528+
schema:
3529+
$ref: "#/components/schemas/Error"
3530+
"409":
3531+
description: Conflict
3532+
content:
3533+
application/json:
3534+
schema:
3535+
$ref: "#/components/schemas/Error"
3536+
"500":
3537+
description: Internal Server Error
3538+
content:
3539+
application/json:
3540+
schema:
3541+
$ref: "#/components/schemas/Error"
34383542
/taas-teams/members-suggest/{fragment}:
34393543
get:
34403544
tags:
@@ -5589,6 +5693,32 @@ components:
55895693
type: string
55905694
description: "Optional job title."
55915695
example: "Lead Application Developer"
5696+
CalculateAmountRequestBody:
5697+
properties:
5698+
numberOfResources:
5699+
type: number
5700+
description: "No. of resources required."
5701+
rates:
5702+
type: number
5703+
description: "Weekly rates"
5704+
durationWeeks:
5705+
type: number
5706+
description: "No. of weeks"
5707+
CalculateAmountResponse:
5708+
properties:
5709+
totalAmount:
5710+
type: number
5711+
description: "Total amount calculated"
5712+
CreatePaymentRequestBody:
5713+
properties:
5714+
totalAmount:
5715+
type: number
5716+
description: "Total amount charged to user via stripe"
5717+
CreatePaymentResponse:
5718+
properties:
5719+
paymentIntentToken:
5720+
type: string
5721+
description: " Token required by stripe for completing payment."
55925722
SubmitTeamRequestBody:
55935723
properties:
55945724
teamName:

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"prompt-confirm": "^2.0.4",
5959
"rewire": "^5.0.0",
6060
"sequelize": "^6.3.5",
61+
"stripe": "^8.168.0",
6162
"superagent": "^6.1.0",
6263
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6",
6364
"util": "^0.12.3",

src/controllers/TeamController.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ async function suggestMembers (req, res) {
155155
res.send(await service.suggestMembers(req.authUser, req.params.fragment))
156156
}
157157

158+
/**
159+
*
160+
* @param req the request
161+
* @param res the response
162+
*/
163+
async function calculateAmount(req, res) {
164+
res.send(await service.calculateAmount(req.body));
165+
}
166+
167+
/**
168+
*
169+
* @param req the request
170+
* @param res the response
171+
*/
172+
async function createPayment(req, res) {
173+
res.send(await service.createPayment(req.body.totalAmount));
174+
}
175+
158176
module.exports = {
159177
searchTeams,
160178
getTeam,
@@ -169,5 +187,7 @@ module.exports = {
169187
roleSearchRequest,
170188
createTeam,
171189
searchSkills,
172-
suggestMembers
190+
suggestMembers,
191+
createPayment,
192+
calculateAmount
173193
}

src/routes/TeamRoutes.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,17 @@ module.exports = {
107107
auth: 'jwt',
108108
scopes: []
109109
}
110-
}
110+
},
111+
"/taas-teams/calculateAmount": {
112+
post: {
113+
controller: "TeamController",
114+
method: "calculateAmount",
115+
},
116+
},
117+
"/taas-teams/createPayment": {
118+
post: {
119+
controller: "TeamController",
120+
method: "createPayment",
121+
},
122+
}
111123
}

src/services/TeamService.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { getAuditM2Muser } = require('../common/helper')
2020
const { matchedSkills, unMatchedSkills } = require('../../scripts/emsi-mapping/esmi-skills-mapping')
2121
const Role = models.Role
2222
const RoleSearchRequest = models.RoleSearchRequest
23+
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
2324

2425
const emailTemplates = _.mapValues(emailTemplateConfig, (template) => {
2526
return {
@@ -1162,6 +1163,30 @@ suggestMembers.schema = Joi.object().keys({
11621163
fragment: Joi.string().required()
11631164
}).required()
11641165

1166+
/**
1167+
* Calculates total amount
1168+
* @param {Object} body
1169+
* @returns {int} totalAmount
1170+
*/
1171+
async function calculateAmount(body) {
1172+
const totalAmount = body.numberOfResources * body.rates * body.durationWeeks;
1173+
return { totalAmount };
1174+
}
1175+
1176+
/**
1177+
* Creates token for stripe
1178+
* @param {int} totalAmount
1179+
* @returns {string} paymentIntentToken
1180+
*/
1181+
async function createPayment(totalAmount) {
1182+
const paymentIntent = await stripe.paymentIntents.create({
1183+
amount: totalAmount,
1184+
currency: process.env.CURRENCY,
1185+
});
1186+
return { paymentIntentToken: paymentIntent.client_secret };
1187+
}
1188+
1189+
11651190
module.exports = {
11661191
searchTeams,
11671192
getTeam,
@@ -1180,6 +1205,8 @@ module.exports = {
11801205
createRoleSearchRequest,
11811206
isExternalMember,
11821207
createTeam,
1208+
calculateAmount,
1209+
createPayment,
11831210
searchSkills,
11841211
suggestMembers
11851212
}

0 commit comments

Comments
 (0)