创建结帐 Session.php 404 未找到
Create Checkout Session.php 404 not found
<?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51HhrRSFFkuyUh5wxaaS0l0yn5S..........R0nzVUJ23jePY6iXme2bOwxtuImLrWv9QilqW4umYvRCIo00vHKf5mKW');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'http://localhost:4242';
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
<link rel="stylesheet" href="style.css">
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<section>
<div class="product">
<img
src="https://i.imgur.com/EHyR2nP.png"
alt="The cover of Stubborn Attachments"
/>
<div class="description">
<h3>Stubborn Attachments</h3>
<h5>.00</h5>
</div>
</div>
<button id="checkout-button">Checkout</button>
</section>
</body>
<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("pk_test_51HhrRSFFkuyUh5wxONDavkFRWnPlNcJ.............rOT7xce3CyW1fTnClCUsVJ2kctOtuJ0hFdf5004x4Gs2YC");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("/create-checkout-session.php", {
method: "POST",
})
.then(function (response) {
return response.json();
})
.then(function (session) {
//return stripe.redirectToCheckout({ sessionId: session.id });
return stripe.redirectToCheckout({ sessionId: session.sessionId });
})
.then(function (result) {
// If redirectToCheckout fails due to a browser or network
// error, you should display the localized error message to your
// customer using error.message.
if (result.error) {
alert(result.error.message);
}
})
.catch(function (error) {
console.error("Error:", error);
});
});
</script>
</html>
我已经从这里 https://stripe.com/docs/checkout/integration-builder 下载了上面的代码,并将文件放在 htdocs 下我的 Xampp 文件夹中,所以每当我 运行ning http://localhost/my-projects/stripe-payments-prebuilt/checkout.html 所以它显示结帐页面但是当我点击结帐按钮时它显示“checkout.html:30 POST http://localhost/create-checkout-session。 php 404(未找到)”我的意思是我做错了什么,我可以 运行 其他项目所以这意味着没有服务器问题,那么问题是什么?
首先,出现 404 错误,因为该文件根本不存在。在 localhost 或所有服务器中,如果你在文件名前加上 /
,它会自动变成在主机之后,所以 /config.php
会变成 http://localhost/config.php
。为防止此错误,您应该使用 ./
意外标记 <
表示服务器正在返回 404 文档。
简而言之,在文件名前加一个点,因为我假设这个项目不在根目录中。 (表示项目在http://localhost/projectName
)
<?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51HhrRSFFkuyUh5wxaaS0l0yn5S..........R0nzVUJ23jePY6iXme2bOwxtuImLrWv9QilqW4umYvRCIo00vHKf5mKW');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'http://localhost';
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
<link rel="stylesheet" href="style.css">
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<section>
<div class="product">
<img
src="https://i.imgur.com/EHyR2nP.png"
alt="The cover of Stubborn Attachments"
/>
<div class="description">
<h3>Stubborn Attachments</h3>
<h5>.00</h5>
</div>
</div>
<button id="checkout-button">Checkout</button>
</section>
</body>
<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("pk_test_51HhrRSFFkuyUh5wxONDavkFRWnPlNcJ.............rOT7xce3CyW1fTnClCUsVJ2kctOtuJ0hFdf5004x4Gs2YC");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("./create-checkout-session.php", {
method: "POST",
})
.then(function (response) {
return response.json();
})
.then(function (session) {
//return stripe.redirectToCheckout({ sessionId: session.id });
return stripe.redirectToCheckout({ sessionId: session.sessionId });
})
.then(function (result) {
// If redirectToCheckout fails due to a browser or network
// error, you should display the localized error message to your
// customer using error.message.
if (result.error) {
alert(result.error.message);
}
})
.catch(function (error) {
console.error("Error:", error);
});
});
</script>
</html>
<?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51HhrRSFFkuyUh5wxaaS0l0yn5S..........R0nzVUJ23jePY6iXme2bOwxtuImLrWv9QilqW4umYvRCIo00vHKf5mKW');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'http://localhost:4242';
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
<link rel="stylesheet" href="style.css">
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<section>
<div class="product">
<img
src="https://i.imgur.com/EHyR2nP.png"
alt="The cover of Stubborn Attachments"
/>
<div class="description">
<h3>Stubborn Attachments</h3>
<h5>.00</h5>
</div>
</div>
<button id="checkout-button">Checkout</button>
</section>
</body>
<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("pk_test_51HhrRSFFkuyUh5wxONDavkFRWnPlNcJ.............rOT7xce3CyW1fTnClCUsVJ2kctOtuJ0hFdf5004x4Gs2YC");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("/create-checkout-session.php", {
method: "POST",
})
.then(function (response) {
return response.json();
})
.then(function (session) {
//return stripe.redirectToCheckout({ sessionId: session.id });
return stripe.redirectToCheckout({ sessionId: session.sessionId });
})
.then(function (result) {
// If redirectToCheckout fails due to a browser or network
// error, you should display the localized error message to your
// customer using error.message.
if (result.error) {
alert(result.error.message);
}
})
.catch(function (error) {
console.error("Error:", error);
});
});
</script>
</html>
我已经从这里 https://stripe.com/docs/checkout/integration-builder 下载了上面的代码,并将文件放在 htdocs 下我的 Xampp 文件夹中,所以每当我 运行ning http://localhost/my-projects/stripe-payments-prebuilt/checkout.html 所以它显示结帐页面但是当我点击结帐按钮时它显示“checkout.html:30 POST http://localhost/create-checkout-session。 php 404(未找到)”我的意思是我做错了什么,我可以 运行 其他项目所以这意味着没有服务器问题,那么问题是什么?
首先,出现 404 错误,因为该文件根本不存在。在 localhost 或所有服务器中,如果你在文件名前加上 /
,它会自动变成在主机之后,所以 /config.php
会变成 http://localhost/config.php
。为防止此错误,您应该使用 ./
意外标记 <
表示服务器正在返回 404 文档。
简而言之,在文件名前加一个点,因为我假设这个项目不在根目录中。 (表示项目在http://localhost/projectName
)
<?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51HhrRSFFkuyUh5wxaaS0l0yn5S..........R0nzVUJ23jePY6iXme2bOwxtuImLrWv9QilqW4umYvRCIo00vHKf5mKW');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'http://localhost';
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
echo json_encode(['id' => $checkout_session->id]);
<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
<link rel="stylesheet" href="style.css">
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<section>
<div class="product">
<img
src="https://i.imgur.com/EHyR2nP.png"
alt="The cover of Stubborn Attachments"
/>
<div class="description">
<h3>Stubborn Attachments</h3>
<h5>.00</h5>
</div>
</div>
<button id="checkout-button">Checkout</button>
</section>
</body>
<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("pk_test_51HhrRSFFkuyUh5wxONDavkFRWnPlNcJ.............rOT7xce3CyW1fTnClCUsVJ2kctOtuJ0hFdf5004x4Gs2YC");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("./create-checkout-session.php", {
method: "POST",
})
.then(function (response) {
return response.json();
})
.then(function (session) {
//return stripe.redirectToCheckout({ sessionId: session.id });
return stripe.redirectToCheckout({ sessionId: session.sessionId });
})
.then(function (result) {
// If redirectToCheckout fails due to a browser or network
// error, you should display the localized error message to your
// customer using error.message.
if (result.error) {
alert(result.error.message);
}
})
.catch(function (error) {
console.error("Error:", error);
});
});
</script>
</html>