Can't pass form values from react front end to server // Stripe API

Viewed 21

I have a form on my react app front end that when submitted takes the user to a stripe checkout.

I am struggling to work out how I can pass certain values from that form to the backend server which handles the call.

Eg: I would like to pass the customer ID from the frontend form to the backend call so that I can direct them to the checkout or customer portal linked to their specific stripe account.

Here is the front end code:

<form method="POST" action="http://localhost:3001/create-customer-portal-session">
    <input type="hidden" name="customerId" value='cus_MLzuNCoubhv5nk' />
    <button className="Button" type="submit">Manage billing</button>
</form> 

And the backend call on the server:

require('dotenv').config()

const bodyParser = require('body-parser')
const express = require('express')
const app = express ()
app.use(express.json())
app.use(express.static('public'))


const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)


  app.post('/create-customer-portal-session', async (req, res) => {

    // Authenticate your user.
    const session = await stripe.billingPortal.sessions.create({
      customer: process.env.CUSTOMER_ID,
    
    });
  console.log(session.url)
   res.redirect(session.url);
  });


I want the value of 'customer' in the backend to be taken from the form input on the front end with the name 'customerId'

0 Answers
Related