Reputation: 31
I use nextjs and mongodb, in my checkout page in api I made a payment stripe session :
import {initMongoose} from "../../lib/mongoose";
import Product from "../../models/Products";
import Order from "../../models/Order";
import jwt from "jsonwebtoken";
// Import Stripe and set the secret key
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
// Export an async function that will be used as the request handler
export default async function handler(req,res) {
// Connect to the database
await initMongoose();
// Check if the request method is POST, if not, return an error message
if (req.method !== 'POST') {
res.json('should a post but its not!');
return;
}
// Destructure the request body to get the customer information
const {email,name,address,city} = req.body;
// Get the list of product IDs from the request body, remove duplicates, and convert to an array of MongoDB ObjectIds
const productsIds = req.body.products.split(',');
const uniqIds = [...new Set(productsIds)];
// Find the products in the database
const products = await Product.find({_id:{$in:uniqIds}}).exec();
// Create the line items array that will be used to create the Stripe session
let line_items = [];
for (let productId of uniqIds) {
// Get the quantity of the current product by counting the number of occurrences in the productsIds array
const quantity = productsIds.filter(id => id === productId).length;
// Find the product in the products array
const product = products.find(p => p._id.toString() === productId);
// Add a line item for the current product to the line_items array
line_items.push({
quantity,
price_data: {
currency: 'EUR',
product_data: {
name:product.name,
images: [product.picture],
},
unit_amount: product.price * 100,
},
});
}
// Get the JWT token from the cookies
const token = req.cookies["OursiteJWT"];
// Decode the JWT token to get the user ID
const decoded = jwt.decode(token);
const userId = decoded._id;
// Create a new order in the database
const order = await Order.create({
products:line_items,
user_id: userId,
name,
email,
address,
city,
paid:0,
});
// Create a Stripe checkout session
const session = await stripe.checkout.sessions.create({
line_items: line_items,
mode: 'payment',
customer_email: email,
// Set the success and cancel URLs to the origin of the request
success_url: `${req.headers.origin}/?success=true`,
cancel_url: `${req.headers.origin}/?canceled=true`,
metadata: {orderId:order._id.toString()},
});
// Redirect the user to the Stripe checkout session URL
res.redirect(303, session.url);
}
So I checked the url that I passed in succes_url, I made a console.log of req.headers.origin and I found http://localhost:3000 So the url is valid, I don't see where it can come from. I commented the code for ease of understanding.
Error Messsage : error - StripeInvalidRequestError: Not a valid URL
Upvotes: 0
Views: 1366
Reputation: 7439
You should review your API request logs for Checkout session create requests in the developer dashboard to see what success_url
and cancel_url
values you are actually sending to Stripe. Most likely you will find that this contains an unexpected (invalid) value.
Upvotes: 3