Django Stripe InvalidRequestError: Request req_******: Not a valid URL
Django Stripe InvalidRequestError: Request req_******: Not a valid URL
我在 Django 中使用 Stripe,但是在单击 checkout 时 return stripe.error.InvalidRequestError: Request req_N8rlEhXn42Cyxz: Not a valid URL
(由于 问题,首先我尝试了我的 localhost:8000/payment/done,然后使用 ngrok 进行了尝试,这样它就可以全球可访问,但仍然无法正常工作 )。
这是我的settings.py
.....
STRIPE_PUBLIC_KEY = (
"pk_test_51J5kFsFfjIr2jaBwRlGyDJ9McmvvBGclXxkasIGggbh3Fz076sWsGv9z7pSWxy7WuncldIMXDFOq1U3j2bv2Zstu00lMmMnLjD"
)
STRIPE_SECRET_KEY = (
"sk_test_51J5kFsFfjIr2jaBwg1Khel6thuio8gTshaTeIwe0GjqaM57x6XvjZNxQE0udNk8BAVqjjEkCHtFs8KXBEglu2zbR005LkwAzaq"
)
STRIPE_WEBHOOK_SECRET = ""
和我的views.py
class CreateCheckOutSessoionView(View):
def post(self, request, *args, **kwargs):
ng = "https://localhost:8000"
product_id = kwargs.get("pk")
product = Product.objects.get(id=product_id)
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[
{
"price_data": {
"currency": "usd",
"unit_amount": product.get_cent_price,
"product_data": {
"name": product.name,
"images": [product.image.url],
},
},
"quantity": 1,
},
],
mode="payment",
success_url=ng + "/payment/done",
cancel_url=ng + "/payment/cancel",
)
return JsonResponse({"id": checkout_session.id})
这就是我将 post
发送到此视图的方式
<button type="button" id="checkout-button">Checkout</button>
<script type="text/javascript">
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("{{ STRIPE_PUBLIC_KEY }}");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("{% url 'payment:create-checkout-session' 1 %}", {
method: "POST",
headers: {
'X-CSRFToken': csrftoken,
},
})
.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>
我不知道..
product.image.url
是亲戚URL。它必须是绝对 URL.
我在 Django 中使用 Stripe,但是在单击 checkout 时 return stripe.error.InvalidRequestError: Request req_N8rlEhXn42Cyxz: Not a valid URL
(由于
这是我的settings.py
.....
STRIPE_PUBLIC_KEY = (
"pk_test_51J5kFsFfjIr2jaBwRlGyDJ9McmvvBGclXxkasIGggbh3Fz076sWsGv9z7pSWxy7WuncldIMXDFOq1U3j2bv2Zstu00lMmMnLjD"
)
STRIPE_SECRET_KEY = (
"sk_test_51J5kFsFfjIr2jaBwg1Khel6thuio8gTshaTeIwe0GjqaM57x6XvjZNxQE0udNk8BAVqjjEkCHtFs8KXBEglu2zbR005LkwAzaq"
)
STRIPE_WEBHOOK_SECRET = ""
和我的views.py
class CreateCheckOutSessoionView(View):
def post(self, request, *args, **kwargs):
ng = "https://localhost:8000"
product_id = kwargs.get("pk")
product = Product.objects.get(id=product_id)
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[
{
"price_data": {
"currency": "usd",
"unit_amount": product.get_cent_price,
"product_data": {
"name": product.name,
"images": [product.image.url],
},
},
"quantity": 1,
},
],
mode="payment",
success_url=ng + "/payment/done",
cancel_url=ng + "/payment/cancel",
)
return JsonResponse({"id": checkout_session.id})
这就是我将 post
发送到此视图的方式
<button type="button" id="checkout-button">Checkout</button>
<script type="text/javascript">
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("{{ STRIPE_PUBLIC_KEY }}");
var checkoutButton = document.getElementById("checkout-button");
checkoutButton.addEventListener("click", function () {
fetch("{% url 'payment:create-checkout-session' 1 %}", {
method: "POST",
headers: {
'X-CSRFToken': csrftoken,
},
})
.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>
我不知道..
product.image.url
是亲戚URL。它必须是绝对 URL.