内部服务器错误(我按照教程的信,我仍然得到2个错误)


https://github.com/WebDevSimplified/stripe-checkout-simple/commit/b1bac8e70cf783c983f2d41b7f1e17b0617c324d

上面的是指向github的链接,您可以在其中看到完整的代码。下面我将粘贴我的代码,以防你不想去github:服务器启动//

require("dotenv").config()
const express = require("express")
const app = express()
const cors = require("cors")
app.use(express.json())
app.use(
cors({
origin: "http://localhost:5500",
})
)
const stripe = require("stripe")(process.env.STRIPE_PRIVATE_KEY)
const storeItems = new Map([
[1, { priceInCents: 10000, name: "Learn React Today" }],
[2, { priceInCents: 20000, name: "Learn CSS Today" }],
])
app.post("/create-checkout-session", async (req, res) => {
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: "payment",
line_items: req.body.items.map(item => {
const storeItem = storeItems.get(item.id)
return {
price_data: {
currency: "usd",
product_data: {
name: storeItem.name,
},
unit_amount: storeItem.priceInCents,
},
quantity: item.quantity,
}
}),
success_url: `${process.env.CLIENT_URL}/success.html`,
cancel_url: `${process.env.CLIENT_URL}/cancel.html`,
})
res.json({ url: session.url })
} catch (e) {
res.status(500).json({ error: e.message })
}
})
app.listen(3000)
/** script.js start **/

const button = document.querySelector("button")
button.addEventListener("click", () => {
fetch("http://localhost:3000/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
items: [
{ id: 1, quantity: 3 },
{ id: 2, quantity: 1 },
],
}),
})
.then(res => {
if (res.ok) return res.json()
return res.json().then(json => Promise.reject(json))
})
.then(({ url }) => {
window.location = url
})
.catch(e => {
console.error(e.error)
})
})
/*** start of .env **/
CLIENT_URL=http://localhost:5500
STRIPE_PRIVATE_KEY:  I have it in my code but wont post it here. 

/*** index.html**/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="script.js" defer></script>
</head>
<body>
<button>Checkout</button>
</body>
</html>

我得到的错误是:1- script.js:3 POST http://localhost:3000/create-checkout-session 500(内部服务器错误)2-即使我提供了我的钥匙,甚至仔细检查了一下:您没有提供API密钥。您需要在授权头中提供您的API密钥,使用承载认证(例如:"授权:持有者YOUR_SECRET_KEY")。请参阅https://stripe.com/docs/api#authentication了解详细信息,或者我们可以在https://support.stripe.com/提供帮助。

错误明确指出

您需要在授权头中提供您的API密钥,使用承载认证(例如:'Authorization: Bearer YOUR_SECRET_KEY')

所以在script。js文件中当你在做取回操作时像这样修改取回操作

注意:script.js运行客户端所以你的不能访问process.env

fetch("http://localhost:3000/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization" : 'Bearer YOUR_SECRET_KEY' //? FIX Replace the scret key 
//?with your stripe secret key
},
body: JSON.stringify({
items: [
{ id: 1, quantity: 3 },
{ id: 2, quantity: 1 },
],
}),
})
.then(res => {
if (res.ok) return res.json()
return res.json().then(json => Promise.reject(json))
})
.then(({ url }) => {
window.location = url
})
.catch(e => {
console.error(e.error)
})

最新更新