我不断收到错误,Stripe 错误:“创建价格时必须指定
product
或 product_data
”。我对如何解决这个问题有点迷茫,非常感谢一些帮助。
我会把所有相关代码放在下面,谢谢!
服务器.js
app.post('/checkout', async (req, res) => {
const { title_wa, price_wa} = req.body
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: title_wa,
},
unit_amount: price_wa,
},
quantity: 1
},
],
mode: 'payment',
shipping_address_collection: {
allowed_countries: ['US', 'BR']
},
success_url: `${process.env.BASE_URL}/complete?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.BASE_URL}/cancel`
})
res.redirect(session.url)
})
index.ejs
<div class="checkOut">
<form id="checkout-form" action="/checkout" method="POST">
<input type="submit" value="checkout">
</form>
<div class="closeShopping">
<p> x </p>
</div>
</div>
//code break
function addItemtoStripe(event)
{
event.preventDefault();
var button2 = event.target
var StripeItem = button2.parentElement.parentElement
var title_wa = StripeItem.getElementsByClassName('title')[0].innerText;
var price_wa = StripeItem.getElementsByClassName('thingPrice')[0].innerText;
var form = document.getElementById('checkout-form');
fetch('/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ title_wa: title_wa, price_wa: price_wa })
})
.then(response => response.json())
.then(data => {
window.location.href = data.url; // Redirect to the Stripe session URL
})
.catch(error => {
console.error('Error:', error);
});
}
我尝试过这个,但是事情变得更加有趣,我又回头了。
try {
// Create a product in Stripe first (if not already created)
const product = await stripe.products.create({
name: title_wa,
type: 'good', // adjust type as per your product type
});
// Create a price associated with the product
const price = await stripe.prices.create({
currency: 'usd',
unit_amount: price_wa,
product: product.id,
});