Home > Net >  Unexpected token ' in JSON for stripe payment intents
Unexpected token ' in JSON for stripe payment intents

Time:03-25

Hi I am working on create payment intent method in stripe the language is node.js but I got this error

unmatched close brace/bracket in URL position 13: currency:usd}'

in my code when I execute this command:

curl -X POST localhost:4242/create-payment-intent -H "Content-Type: application/json" -d '{"paymentMethodType":"card", "currency":"usd"}'

And this is my code

app.post('/create-payment-intent', async (req, res) => {
const {paymentMethodType, currency} = req.body;
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: 1999,
      currency: currency,
      payment_method_types: [paymentMethodType],
    });
    res.json({ clientSecret: paymentIntent.client_secret });
  } catch (e) {
    res.status(400).json({ error: { message: e.message } });
  }
}); 

please any one can help.

CodePudding user response:

If your request has a JSON payload, you need to make sure your server is parsing that JSON. I tested the code below and it works on my end.

CURL command (no changes)

curl -X POST localhost:4242/create-payment-intent -H "Content-Type: application/json" -d '{"paymentMethodType":"card", "currency":"usd"}'

Node.js server (added a middleware function to parse the JSON payload)

const json = express.json({ type: "application/json" });

app.post("/create-payment-intent", json, async (req, res) => {
  const { paymentMethodType, currency } = req.body;
  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: 1999,
      currency: currency,
      payment_method_types: [paymentMethodType],
    });
    res.json({ clientSecret: paymentIntent.client_secret });
  } catch (e) {
    res.status(400).json({ error: { message: e.message } });
  }
});

CodePudding user response:

You forget to add ' at the end of your command

curl -X POST localhost:4242/create-payment-intent -H "Content-Type: application/json" -d '{"paymentMethodType":"card", "currency":"usd"}'
  • Related