-
Notifications
You must be signed in to change notification settings - Fork 0
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
feature-02-add-orders-endpoints #10
Draft
andresWeitzel
wants to merge
4
commits into
master
Choose a base branch
from
feature-02-add-orders-endpoints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 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 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 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 |
---|---|---|
@@ -1,23 +1,21 @@ | ||
//External | ||
import {Router} from 'express'; | ||
import { Router } from "express"; | ||
//Controllers | ||
import { createOrderController } from '../../controllers/orders/create.controller'; | ||
import { getOrderController } from '../../controllers/orders/get.controller'; | ||
import { updateOrderController } from '../../controllers/orders/update.controller'; | ||
import { createOrderController } from "../../controllers/orders/create.controller"; | ||
import { getOrderController } from "../../controllers/orders/get.controller"; | ||
import { updateOrderController } from "../../controllers/orders/update.controller"; | ||
import { confirmOrderController } from "../../controllers/orders/confirm.controller"; | ||
import { authorizePaymentOrderController } from "../../controllers/orders/authorize-payment.controller"; | ||
//Const-vars | ||
export const ordersRouter = Router(); | ||
|
||
ordersRouter.post( | ||
"/create-order", | ||
createOrderController | ||
); | ||
ordersRouter.post("/create", createOrderController); | ||
|
||
ordersRouter.get( | ||
"/get-order/:id", | ||
getOrderController | ||
); | ||
ordersRouter.get("/get/:id", getOrderController); | ||
//For review | ||
ordersRouter.patch("/update/:id", updateOrderController); | ||
|
||
ordersRouter.patch( | ||
"/update-order/:id", | ||
updateOrderController | ||
); | ||
ordersRouter.post("/confirm/:id", confirmOrderController); | ||
|
||
//For review | ||
ordersRouter.post("/authorize-payment/:id", authorizePaymentOrderController); | ||
This file contains 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,63 @@ | ||
//External | ||
import { Request, Response } from "express"; | ||
import "dotenv/config"; | ||
//Enums | ||
import { statusCode } from "../../enum/http/status-code"; | ||
//Helpers | ||
import { validateHeadersAndKeys } from "../../helpers/validations/headers/validateHeadersAndKeys"; | ||
//services | ||
import { authorizePaymentFromPaypal } from "../../services/orders/authorize-payment.service"; | ||
//const | ||
const statusCodeInternalServerError = statusCode.INTERNAL_SERVER_ERROR; | ||
const statusCodeBadRequest = statusCode.BAD_REQUEST; | ||
const statusCodeOk = statusCode.OK; | ||
//vars | ||
let orderData: any; | ||
let eventHeaders: any; | ||
let checkEventHeadersAndKeys: any; | ||
let msgResponse: string; | ||
let msgLog: string; | ||
|
||
/** | ||
* @description Controller to authorize a payment for an order from paypal api | ||
* @param {any} req any type | ||
* @param {any} res any type | ||
* @returns an object with order information from paypal api | ||
* @example | ||
*/ | ||
export const authorizePaymentOrderController = async (req: Request, res: Response) => { | ||
try { | ||
//-- start with validation headers and keys --- | ||
eventHeaders = req.headers; | ||
|
||
checkEventHeadersAndKeys = await validateHeadersAndKeys(eventHeaders); | ||
|
||
if (checkEventHeadersAndKeys != (null || "")) { | ||
return res | ||
.status(statusCodeBadRequest) | ||
.send({ error: checkEventHeadersAndKeys }); | ||
} | ||
//-- end with validation headers and keys --- | ||
|
||
//-- start with axios order operation --- | ||
orderData = await authorizePaymentFromPaypal(req); | ||
//-- end with axios order operation --- | ||
switch (orderData) { | ||
case null: | ||
return res | ||
.status(statusCodeInternalServerError) | ||
.send({ error: "Could not authorize a payment for an order. Check the credentials" }); | ||
case orderData != null: | ||
return res.status(statusCodeOk).send(orderData); | ||
default: | ||
return res.status(statusCodeOk).send(orderData); | ||
} | ||
} catch (error) { | ||
msgResponse = "ERROR in authorizePaymentOrderController() function controller."; | ||
msgLog = msgResponse + `Caused by ${error}`; | ||
console.log(msgLog); | ||
return res | ||
.status(statusCodeInternalServerError) | ||
.send({ error: msgResponse }); | ||
} | ||
}; |
This file contains 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,63 @@ | ||
//External | ||
import { Request, Response } from "express"; | ||
import "dotenv/config"; | ||
//Enums | ||
import { statusCode } from "../../enum/http/status-code"; | ||
//Helpers | ||
import { validateHeadersAndKeys } from "../../helpers/validations/headers/validateHeadersAndKeys"; | ||
//services | ||
import { confirmOrderFromPaypal } from "../../services/orders/confirm.service"; | ||
//const | ||
const statusCodeInternalServerError = statusCode.INTERNAL_SERVER_ERROR; | ||
const statusCodeBadRequest = statusCode.BAD_REQUEST; | ||
const statusCodeOk = statusCode.OK; | ||
//vars | ||
let orderData: any; | ||
let eventHeaders: any; | ||
let checkEventHeadersAndKeys: any; | ||
let msgResponse: string; | ||
let msgLog: string; | ||
|
||
/** | ||
* @description Controller to confirm a order from paypal api | ||
* @param {any} req any type | ||
* @param {any} res any type | ||
* @returns an object with order information from paypal api | ||
* @example | ||
*/ | ||
export const confirmOrderController = async (req: Request, res: Response) => { | ||
try { | ||
//-- start with validation headers and keys --- | ||
eventHeaders = req.headers; | ||
|
||
checkEventHeadersAndKeys = await validateHeadersAndKeys(eventHeaders); | ||
|
||
if (checkEventHeadersAndKeys != (null || "")) { | ||
return res | ||
.status(statusCodeBadRequest) | ||
.send({ error: checkEventHeadersAndKeys }); | ||
} | ||
//-- end with validation headers and keys --- | ||
|
||
//-- start with axios order operation --- | ||
orderData = await confirmOrderFromPaypal(req); | ||
//-- end with axios order operation --- | ||
switch (orderData) { | ||
case null: | ||
return res | ||
.status(statusCodeInternalServerError) | ||
.send({ error: "Could not confirm a order. Check the credentials" }); | ||
case orderData != null: | ||
return res.status(statusCodeOk).send(orderData); | ||
default: | ||
return res.status(statusCodeOk).send(orderData); | ||
} | ||
} catch (error) { | ||
msgResponse = "ERROR in confirmOrderController() function controller."; | ||
msgLog = msgResponse + `Caused by ${error}`; | ||
console.log(msgLog); | ||
return res | ||
.status(statusCodeInternalServerError) | ||
.send({ error: msgResponse }); | ||
} | ||
}; |
This file contains 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 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 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,64 @@ | ||
//External | ||
import { Request } from "express"; | ||
import "dotenv/config"; | ||
//Helpers | ||
import { sendPostRequest } from "../../helpers/axios/request/post"; | ||
//Const | ||
//paypal base | ||
const API_PAYPAL_BASE_URL: string = process.env.API_PAYPAL_BASE_URL || ""; | ||
//paypal confirm order | ||
const API_PAYPAL_ORDERS_BASE_URL: string = | ||
process.env.API_PAYPAL_ORDERS_BASE_URL || ""; | ||
const API_PAYPAL_AUTHORIZE_PAYMENT_ORDER_RESOURCE: string = | ||
process.env.API_PAYPAL_AUTHORIZE_PAYMENT_ORDER_RESOURCE || ""; | ||
//vars | ||
let reqBody: any; | ||
let reqHeaders: any; | ||
let reqParams: any; | ||
let axiosData: any; | ||
let axiosConfig: any; | ||
let orderCreated: any; | ||
let msgResponse: string; | ||
let msgLog: string; | ||
|
||
/** | ||
* @description Function to send a axios post request for authorize a payment for an order from paypal api | ||
* @param {any} req any type | ||
* @returns an object with order information from paypal api | ||
* @example | ||
*/ | ||
export const authorizePaymentFromPaypal = async (req: Request) => { | ||
try { | ||
reqHeaders = req.headers; | ||
reqBody = req.body; | ||
reqParams = req.params; | ||
orderCreated = null; | ||
|
||
const API_PAYPAL_AUTHORIZE_PAYMENT_URL: string = | ||
`${API_PAYPAL_BASE_URL}${API_PAYPAL_ORDERS_BASE_URL}${reqParams.id}${API_PAYPAL_AUTHORIZE_PAYMENT_ORDER_RESOURCE}` || | ||
""; | ||
|
||
console.log(API_PAYPAL_AUTHORIZE_PAYMENT_URL); | ||
|
||
axiosConfig = { | ||
headers: { | ||
"Content-Type": "application/json", | ||
//"PayPal-Request-Id": reqHeaders?.paypalRequestId, | ||
Authorization: reqHeaders?.authorization, | ||
}, | ||
}; | ||
|
||
orderCreated = await sendPostRequest( | ||
API_PAYPAL_AUTHORIZE_PAYMENT_URL, | ||
null, | ||
axiosConfig | ||
); | ||
|
||
return orderCreated; | ||
} catch (error) { | ||
msgResponse = "ERROR in authorizePaymentFromPaypal() function."; | ||
msgLog = msgResponse + `Caused by ${error}`; | ||
console.log(msgLog); | ||
return null; | ||
} | ||
}; |
This file contains 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,63 @@ | ||
//External | ||
import { Request } from "express"; | ||
import "dotenv/config"; | ||
//Helpers | ||
import { sendPostRequest } from "../../helpers/axios/request/post"; | ||
//Const | ||
//paypal base | ||
const API_PAYPAL_BASE_URL: string = process.env.API_PAYPAL_BASE_URL || ""; | ||
//paypal confirm order | ||
const API_PAYPAL_ORDERS_BASE_URL: string = | ||
process.env.API_PAYPAL_ORDERS_BASE_URL || ""; | ||
const API_PAYPAL_CONFIRM_ORDER_RESOURCE:string = process.env.API_PAYPAL_CONFIRM_ORDER_RESOURCE || ""; | ||
//vars | ||
let reqBody: any; | ||
let reqHeaders: any; | ||
let reqParams:any | ||
let axiosData: any; | ||
let axiosConfig: any; | ||
let orderCreated: any; | ||
let msgResponse: string; | ||
let msgLog: string; | ||
|
||
/** | ||
* @description Function to send a axios post request for create an order from paypal api | ||
* @param {any} req any type | ||
* @returns an object with order information from paypal api | ||
* @example | ||
*/ | ||
export const confirmOrderFromPaypal = async (req: Request) => { | ||
try { | ||
reqHeaders = req.headers; | ||
reqBody = req.body; | ||
reqParams = req.params; | ||
orderCreated = null; | ||
|
||
const API_PAYPAL_CONFIRM_ORDER_URL: string = | ||
`${API_PAYPAL_BASE_URL}${API_PAYPAL_ORDERS_BASE_URL}${reqParams.id}${API_PAYPAL_CONFIRM_ORDER_RESOURCE}` || | ||
""; | ||
|
||
axiosData = reqBody; | ||
|
||
axiosConfig = { | ||
headers: { | ||
"Content-Type": "application/json", | ||
//"PayPal-Request-Id": reqHeaders?.paypalRequestId, | ||
Authorization: reqHeaders?.authorization | ||
} | ||
}; | ||
|
||
orderCreated = await sendPostRequest( | ||
API_PAYPAL_CONFIRM_ORDER_URL, | ||
axiosData, | ||
axiosConfig | ||
); | ||
|
||
return orderCreated; | ||
} catch (error) { | ||
msgResponse = "ERROR in confirmOrderFromPaypal() function."; | ||
msgLog = msgResponse + `Caused by ${error}`; | ||
console.log(msgLog); | ||
return null; | ||
} | ||
}; |
This file contains 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 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
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.
Check failure
Code scanning / CodeQL
Missing rate limiting High