diff --git a/pass/app/payment_processor/Paypal.js b/pass/app/payment_processor/Paypal.js index 2c0a6997df2f9643bc7a829cf3b483bf1690f532..7d469ef2d804c18476502d2fbbdff4915c2045d1 100644 --- a/pass/app/payment_processor/Paypal.js +++ b/pass/app/payment_processor/Paypal.js @@ -293,6 +293,24 @@ class Paypal extends PaymentProcessor { }); } + // Client Token for handling credit card payments + async generateClientToken() { + const accessToken = await this.#generateAccessToken(); + + const response = await fetch(`${base}/v1/identity/generate-token`, { + method: "post", + headers: { + Authorization: `Bearer ${accessToken}`, + "Accept-Language": "en_US", + "Content-Type": "application/json", + }, + }); + + const data = await response.json(); + + return data.client_token; + } + /** * Turns class object into serializable data to store in redis db * diff --git a/pass/routes/checkout/paypal.js b/pass/routes/checkout/paypal.js index d5d7e220a54cfc142f9742087994dcb3b8083bb6..90339c6e51765506b33ba288e98e785b360ac682 100644 --- a/pass/routes/checkout/paypal.js +++ b/pass/routes/checkout/paypal.js @@ -31,7 +31,7 @@ router.get("/:funding_source", async (req, res) => { res.header({ "Access-Control-Allow-Origin": "https://www.paypal.com" }); if (req.params.funding_source === "card") { - let client_token = await generateClientToken(); + let client_token = await new Paypal().generateClientToken(); req.data.checkout.payment.paypal.client_token = client_token; } @@ -119,18 +119,20 @@ router.post("/:funding_source/order/cancel", async (req, res) => { router.use("/card/order/capture", async (req, res, next) => { console.debug(req.body.order_id); Order.LOAD_ORDER_FROM_ID(req.body.order_id) - .then((order) => loaded_order.getPaymentProcessor().verify3D()) + .then((order) => order.getPaymentProcessor().verify_3D()) .then(() => next("route")) .catch((reason) => { console.debug(reason); - res.status(400).json({ msg: "3D Verification Failed" }); + res.status(400).json({ + errors: [ + { type: "PAYPAL_CARD_3D_ERROR", msg: "3D Verification Failed" }, + ], + }); }); }); // capture payment & store order information or fullfill order router.post("/:funding_source/order/capture", async (req, res) => { - //res.status(200).json({ test: "test" }); - //return; Order.LOAD_ORDER_FROM_ID(req.body.order_id).then( /** * @param {Order} loaded_order @@ -155,7 +157,14 @@ router.post("/:funding_source/order/capture", async (req, res) => { console.debug(reason); res .status(400) - .json({ errors: [{ msg: "Couldn't capture the payment" }] }); + .json({ + errors: [ + { + type: "PAYMENT_NOT_COMPLETED_ERROR", + msg: "Couldn't capture the payment", + }, + ], + }); }); } ); @@ -339,22 +348,4 @@ async function refundPayment(capture_ids) { return Promise.all(promises); } -// Client Token for handling credit card payments -async function generateClientToken() { - const accessToken = await generateAccessToken(); - - const response = await fetch(`${base}/v1/identity/generate-token`, { - method: "post", - headers: { - Authorization: `Bearer ${accessToken}`, - "Accept-Language": "en_US", - "Content-Type": "application/json", - }, - }); - - const data = await response.json(); - - return data.client_token; -} - // generate an access token using client id and app secret