xadun
xadun

Reputation: 146

PayPal + Node.js - Getting "Order Already Captured" if though I logged with another User and cleared browser Cache

I'm developing a website with Nuxt + Node + Express and I'm trying to implement PayPal. I'm using PayPal Node SDK and I created the files just as described in their Github page. I'm developing in my pc (so, localhost..) and using PayPal Sandbox.

The problem is: it works perfectly for the first time. I click the 'Buy' button in the frontend, I'm redirected to PayPal payment page, I pay using Sandbox account, then I'm redirect to my website again with the TOKEN in the URL. The output from the call to the backend API is 'COMPLETED'. BUT, if I login with another user, clear browser cache or even change the browser, and try to BUY again it says that: "Order already captured". After a few hours I can buy again. The only thing that works is RESTARTING the server. I've checked if there's some cookies in the server with cookies-parser but there isn't.

Call anyone help me understand why it happens?

Here is the files/code I used:

Created the paypal_controller.js with:

const paypal = require("@paypal/checkout-server-sdk");
let clientId = "<<MY CLIENT ID>>";
let clientSecret = "<<MY CLIENT SECRET>>";
let environment = new paypal.core.SandboxEnvironment(clientId, clientSecret);
let client = new paypal.core.PayPalHttpClient(environment);
let request = new paypal.orders.OrdersCreateRequest();

request.requestBody({
  intent: "CAPTURE",
  purchase_units: [
    {
      description: "Subscription",
      amount: {
        currency_code: "USD",
        value: "200.00",
      },
    },
  ],
  application_context: {
    return_url: "http://localhost:3000/user/dashboard",
    cancel_url: "http://localhost:3000",
  },
});  

const createOrder = async (req, res) => {
  console.log("creating order");
  await client
    .execute(request)
    .then((data) => {
      res.json(data);
    })
    .catch((err) => {
      console.log(err);
      res.json(err);
    });
};

Created the route in the api.js:

const express = require("express");

const {
  createOrder,
  captureOrder,
  getOrderDetail,
} = require("../controllers/paypal_controller");

const router = express.Router();
router.get("/payment/createorder", createOrder)

module.exports = router;

and in the frontend I call:

await this.$axios.$get('http://localhost:9000/api/payment/createorder')
  .then((det) => {
    window.location.href = det['result']['links'][1]['href']
})

Upvotes: 1

Views: 575

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46650

On boot of the server the following gets initialised, so is the same order for every user:

let request = new paypal.orders.OrdersCreateRequest();

request.requestBody({
  intent: "CAPTURE",
  purchase_units: [
    {
      description: "Subscription",
      amount: {
        currency_code: "USD",
        value: "200.00",
      },
    },
  ],
  application_context: {
    return_url: "http://localhost:3000/user/dashboard",
    cancel_url: "http://localhost:3000",
  },
});  

You want to place that code in the route, not outside

const createOrder = async (req, res) => {
  console.log("creating order");
  
  // place aforementioned code here, after validation!

  await client
    .execute(request)
    .then((data) => {

Upvotes: 2

Related Questions