Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Microservice_PayPal_Integration_ExpressJs
Microservicio para la integración de Paypal implementado con ExpressJs, Nodejs, Typescript, nodemon, morgan, dotenv, cors, etc.
# Microservice_Paypal_Orders_Express
Microservicio para la integración de Ordenes de Paypal implementado con ExpressJs, Nodejs, Typescript, nodemon, morgan, dotenv, cors, etc.


Módulo para la integración de los servicios de Paypal implementado con ExpressJs, Nodejs, Typescript, nodemon, morgan, dotenv, cors, etc.

### Documentación

* [Ejemplo básico de integración paypal](https://www.youtube.com/watch?v=sBenKZqEzpQ)
* [PayPal developer doc](https://developer.paypal.com/home)
* [Orders Endpoints](https://developer.paypal.com/docs/api/orders/v2/#orders_create)

### Códigos de integración Paypal

Expand Down
30 changes: 14 additions & 16 deletions src/config/routes/orders-routes.ts
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);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
63 changes: 63 additions & 0 deletions src/controllers/orders/authorize-payment.controller.ts
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 });
}
};
63 changes: 63 additions & 0 deletions src/controllers/orders/confirm.controller.ts
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 });
}
};
6 changes: 5 additions & 1 deletion src/helpers/axios/request/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ let msgLog: string;
*/
export const sendGetRequest = async (url: string, data: any, config: any) => {
try {
if (data == (null || undefined)) {
if (data == (null || undefined) && config == (null || undefined)) {
axiosResponse = await axios.get(url);
} else if (data == (null || undefined)) {
axiosResponse = await axios.get(url, config);
} else if (config == (null || undefined)) {
axiosResponse = await axios.get(url, data);
} else {
axiosResponse = await axios.get(url, data, config);
}
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/axios/request/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ let msgLog: string;
*/
export const sendPostRequest = async (url: string, data: any, config: any) => {
try {
axiosResponse = await axios.post(url, data, config);
if (data == (null || undefined) && config == (null || undefined)) {
axiosResponse = await axios.post(url);
} else if (data == (null || undefined)) {
axiosResponse = await axios.post(url, config);
} else if (config == (null || undefined)) {
axiosResponse = await axios.post(url, data);
} else {
axiosResponse = await axios.post(url, data, config);
}

axiosData = axiosResponse != null ? axiosResponse.data : null;

Expand Down
64 changes: 64 additions & 0 deletions src/services/orders/authorize-payment.service.ts
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;
}
};
63 changes: 63 additions & 0 deletions src/services/orders/confirm.service.ts
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;
}
};
6 changes: 3 additions & 3 deletions src/services/orders/create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { sendPostRequest } from "../../helpers/axios/request/post";
//paypal base
const API_PAYPAL_BASE_URL: string = process.env.API_PAYPAL_BASE_URL || "";
//paypal create order
const API_PAYPAL_CREATE_ORDER_RESOURCE: string =
process.env.API_PAYPAL_CREATE_ORDER_RESOURCE || "";
const API_PAYPAL_ORDERS_BASE_URL: string =
process.env.API_PAYPAL_ORDERS_BASE_URL || "";
const API_PAYPAL_CREATE_ORDER_URL: string =
`${API_PAYPAL_BASE_URL}:${API_PAYPAL_CREATE_ORDER_RESOURCE}` || "";
`${API_PAYPAL_BASE_URL}:${API_PAYPAL_ORDERS_BASE_URL}` || "";
//vars
let reqBody: any;
let reqHeaders: any;
Expand Down
6 changes: 3 additions & 3 deletions src/services/orders/get.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { sendGetRequest } from "../../helpers/axios/request/get";
//paypal base
const API_PAYPAL_BASE_URL: string = process.env.API_PAYPAL_BASE_URL || "";
//paypal get order
const API_PAYPAL_GET_ORDER_RESOURCE: string =
process.env.API_PAYPAL_GET_ORDER_RESOURCE || "";
const API_PAYPAL_ORDERS_BASE_URL: string =
process.env.API_PAYPAL_ORDERS_BASE_URL || "";
//vars
let reqHeaders: any;
let reqParams: any;
Expand All @@ -31,7 +31,7 @@ export const getOrderFromPaypal = async (req: Request) => {
orderData = null;

const API_PAYPAL_GET_ORDER_URL: string =
`${API_PAYPAL_BASE_URL}${API_PAYPAL_GET_ORDER_RESOURCE}${reqParams.id}` ||
`${API_PAYPAL_BASE_URL}${API_PAYPAL_ORDERS_BASE_URL}${reqParams.id}` ||
"";

axiosData = null;
Expand Down