0

Node code:

app.post("/create-payment-intent", async (req, res) => {
  try {

      const  { items }  = req.body;
      console.log(items)
      

    const paymentIntent = await stripe.paymentIntents.create({
      currency: "gbp",
      amount: items,
      automatic_payment_methods: { enabled: true },
    });

    // Send publishable key and PaymentIntent details to client
    res.send({
      clientSecret: paymentIntent.client_secret,
    });

React code:

 useEffect(() => {
    fetch("/create-payment-intent", {
      method: "POST",
      headers: {
        "Content-Type" : "application/json"
      },
      body: JSON.stringify({items: [{ price: totalAmount }]}),
    }).then(async (result) => {
      var { clientSecret } = await result.json();
      setClientSecret(clientSecret);
    });
  }, [totalAmount]);

Error: error

I do not understand why I cannot destructure items on the back end when it has been passed.

I have used the Stripe docs in order to attempt this: https://stripe.com/docs/payments/quickstart?locale=en-GB&lang=node

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • You should at least be able to narrow the problem to React _or_ Node, by looking at the request and response between them. – jonrsharpe Feb 09 '23 at 19:54
  • I can't. Is there an obvious error I am missing? – Callan George Feb 09 '23 at 19:58
  • Well looking at the request should show the body _is_ there and the problem's in the backend, no? The same request _without_ React would have the same behaviour. Then basic research would get you to e.g. https://stackoverflow.com/q/66555172/3001761, https://stackoverflow.com/q/9177049/3001761 – jonrsharpe Feb 09 '23 at 20:02
  • I'd literally been looking at that for hours and just overlooked app.use(express.json());. really appreciate it. – Callan George Feb 09 '23 at 20:12

0 Answers0