【发布时间】:2021-04-01 16:48:33
【问题描述】:
我很难在本地 xampp 服务器上测试来自 single-checkout-subscription 的 Stripe Checkout。到目前为止,我有一个 Stripe 帐户,创建了我的测试密钥、产品和价格,安装了 Stripe Cli 并创建了一个测试 webhook 并将它们全部添加到我服务器中的 .env 文件中
DOMAIN="http://localhost/stripe/server/public/"
BASIC_PRICE_ID="price_xxx"
PRO_PRICE_ID="price_xxx"
STATIC_DIR="STATIC_DIR=../../client"
STRIPE_PUBLISHABLE_KEY="pk_test_xxx"
STRIPE_SECRET_KEY="sk_test_xxx"
STRIPE_WEBHOOK_SECRET="1mwhxxx"
但是当我在本地主机上测试它时:http://localhost/stripe/server/public/ 我得到了前端,但是当我点击按钮时没有任何反应。它甚至没有进入预建的结帐页面。
我检查了控制台,问题似乎来自我的config.php
fetch("/config.php").then(function(json)
这是路由:
我的 config.php > 需要 shared.php > 需要 '..vendor/autoload.php' & parses '../conifg/ini' > conifg.ini 包含我的测试密钥:
stripe_secret_key ="sk_test_444"
stripe_publishable_key = "pk_test_444"
stripe_webhook_secret = "1mw444"
domain = "http://localhost/stripe/server/public/"
basic_price_id = "price_444"
pro_price_id = "price_555"
Script.js 在我的服务器上:
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function(priceId) {
return fetch("/create-checkout-session.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId
})
}).then(function(result) {
return result.json();
});
};
// Handle any errors returned from Checkout
var handleResult = function(result) {
if (result.error) {
var displayError = document.getElementById("error-message");
displayError.textContent = result.error.message;
}
};
/* Get your Stripe publishable key to initialize Stripe.js */
fetch("/config.php")
.then(function(result) {
return result.json();
})
.then(function(json) {
var publishableKey = json.publishableKey;
var basicPriceId = json.basicPrice;
var proPriceId = json.proPrice;
var stripe = Stripe(publishableKey);
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("basic-plan-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(basicPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("pro-plan-btn")
.addEventListener("click", function(evt) {
createCheckoutSession(proPriceId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
});
我精通 HTML、Bootstrap、CSS 以及一些 PHP 和 JavaScript,但我似乎无法遵循如何让 Stripe Subscription Checkout 正常工作的困难指导。有人可以指出我正确的方向或告诉我如何修复我的代码。假期的最后期限很紧。
【问题讨论】:
-
您是否检查了对 ajax 的响应以查看它返回的内容?我问是因为它显然不是 JSON,因为 JSON 解析在解析响应时失败。
-
在哪里查看对 ajax 的响应?在控制台中,它只会给我 congif.php 的错误
-
网络标签;单击查询,然后单击响应或预览选项卡。
标签: php json stripe-payments php-5.3