【发布时间】:2021-03-15 00:27:29
【问题描述】:
我正在按照文档在我的 Web 应用程序中包含条纹支付,当我检查页面元素时出现以下错误。
SyntaxError: Unexpected end of JSON input
this 指向这条线
checkoutButton.addEventListener("click", function () {
fetch("/daily-sub.php", {
method: "POST",
})
.then(function (response) {
return response.json();
})
.then(function (session) {
return stripe.redirectToCheckout({ sessionId: session.id });
})
.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);
});
});
但是在我的 apache 错误日志中我得到了这个错误
Uncaught (Status 400) (Request req_UgtiYTz1TvAO4B) Not a valid URL /vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php line 38
上面的错误指向这段代码
$instance = new static($message);
我要做的就是使用条纹创建一个简单的结帐。
这是我用来尝试实现此目的的代码:
checkout.html
<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
<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>$20.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_51Htt1MJsCP23BRielu7Sm8TYmxp1JFfh43Epik4adlxpcj853sj6BjBG0fd322IrtOABSebJHc0gfd32fdf6d00WYOrShjR");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("/daily-sub.php", {
method: "POST",
})
.then(function (response) {
return response.json();
})
.then(function (session) {
return stripe.redirectToCheckout({ sessionId: session.id });
})
.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>
daily-sub.php
<?php
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51Htt1MJsCP2rfedIBCr2zNH7qIecAqwEDQlxSOrfedVTBpXK4YlR323sdsskCC00Bdfd22EYxX');
header('Content-Type: application/json');
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'aud',
'unit_amount' => 1,
'product_data' => [
'name' => 'Daily Subscription',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => 'https:://google.com',
'cancel_url' => 'https:://youtube.com',
]);
echo json_encode(['id' => $checkout_session->id]);
?>
【问题讨论】:
-
我认为你没有正确安装 Stripe PHP 库。
-
@barmar 我使用了作曲家,它说成功,没有错误。有没有办法让我检查?
-
我看错了错误信息。
标签: php html json stripe-payments