#Quisk - unexpected token
1 messages · Page 1 of 1 (latest)
So that sounds like your server itself is responding with HTML. Have you debugged what is happening on this endpoint on your server?
Yes, my server has the same code shown in the video. cjav, a stripe dev on reddit looked at it and said it's fine. I'll post my server code in the next message if you want to take a look at it.
// Replace if using a different env file or config
require("dotenv").config({ path: "./.env" });
const express = require("express");
const app = express();
const { resolve } = require("path");
const bodyParser = require("body-parser");
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
app.use(express.static(process.env.STATIC_DIR));
// Use JSON parser for all non-webhook routes
app.use((req, res, next) => {
if (req.originalUrl === "/webhook") {
next();
} else {
bodyParser.json()(req, res, next);
}
});
app.post("/create-payment-intent", async (req, res) =>{
const {paymentMethodType, currency} = req.body;
try{
const paymentIntent = await stripe.paymentIntent.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}})
}
})
app.get("/", (req, res) => {
const path = resolve(process.env.STATIC_DIR + "/index.html");
res.sendFile(path);
});
// Stripe requires the raw body to construct the event
app.post(
"/webhook",
bodyParser.raw({ type: "application/json" }),
(req, res) => {
const sig = req.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
// On error, log and return the error message
console.log(`❌ Error message: ${err.message}`);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
if(event.type === "payment_intent.created"){
const paymentIntent = event.data.object;
console.log(`[${event.id}] PaymentIntent (${paymentIntent.id}): ${paymentIntent.status}`)
}
// Return a response to acknowledge receipt of the event
res.json({ received: true });
}
);
app.listen(4242, () => console.log(Node server listening on port ${4242}!));
Unfortunately I don't have time to review your full code at the moment but I do only see json returns so it looks like this may be either an error getting thrown or something else in your code. Have you looked at the full HTML response that you are getting? That might contain an error if this is related to an error
This is the html error: Not much is in it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>SyntaxError: Unexpected token ' in JSON at position 0<br> at JSON.parse (<anonymous>)<br> at createStrictSyntaxError (C:\Users\mua\webDev\stripe\totorial\server\node_modules\body-parser\lib\types\json.js:160:10)<br> at parse (C:\Users\mua\webDev\stripe\totorial\server\node_modules\body-parser\lib\types\json.js:83:15)<br> at C:\Users\mua\webDev\stripe\totorial\server\node_modules\body-parser\lib\read.js:128:18<br> at AsyncResource.runInAsyncScope (node:async_hooks:202:9)<br> at invokeCallback (C:\Users\mua\webDev\stripe\totorial\server\node_modules\raw-body\index.js:231:16)<br> at done (C:\Users\mua\webDev\stripe\totorial\server\node_modules\raw-body\index.js:220:7)<br> at IncomingMessage.onEnd (C:\Users\mua\webDev\stripe\totorial\server\node_modules\raw-body\index.js:280:7)<br> at IncomingMessage.emit (node:events:527:28)<br> at endReadableNT (node:internal/streams/readable:1344:12)</pre>
</body>
</html>
Hi 👋 lurking and think I see what may be happening. I think your endpoint is complaining about the data being sent to it rather than your client receiving a malformed response.
Can you remove the single quotes wrapping your JSON in the curl request and see if that helps?
samething, I inputed this and got the same error:
curl -X POST http://localhost:4242/create-payment-intent -H "Content-Type: application/json" -d {"paymentMethodType":"card","currency":"usd"}
Based on the stack trace you posted, it seems like your code is crashing while parsing the json. Have you added any logging to check the values that your server is receiving?
no, I'm just trying to follow the tutorial🥺
What tutorial are you referring to?
Accepting a one-time payment with a custom form requires two steps. First, creating a PaymentIntent on the server. Second, confirming the payment intent on the client. It's also recommended that you handle fulfillment for a purchase when receiving a webhook notification of successful payment.
In this video, you'll learn how to create a Payment...
we have the same server code, but he seems to be getting the desired json response, while I get the html page.
It looks like your server side code is crashing before it gets to the point of serving a response. Your server side code also appears to be different from the sample code provided.
Right now, based on my understanding of the errors, your curl request includes data that your server can't parse into JSON data. You'll need to work on figuring out where that breakdown is occurring.
I'll try, but my code is identical to theirs, they made some changes whilst on the video.
Hm, interesting, this is the example server code that I'm seeing posted in the repo:
https://github.com/stripe-samples/accept-a-payment/blob/main/custom-payment-flow/server/node/server.js
oh yeah I checked the repo, it has a different sample to the one shown on the video.