将Stripe Checkout ID传递给Django URL



成功页面包含完成购买后下载文件的下载链接,只有在完成付款后才能访问。

其想法是将{CHECKOUT_SESSION_ID}传递到成功url。只有插入了正确的url.../success/?session_id={CHECKOUT_SESSION_ID},才能访问该页面。

如何将{CHECKOUT_SESSION_ID}传递到我的url?

这是我的views.py文件:

def success(request):
return render(request, 'checkout/success.html')
def index(request):
# sendMail()
try:
checkout_session = stripe.checkout.Session.create(
line_items = [
{
'price': 'price_1LUvcWKKYbcIekP0ZtUlCmAI',
'quantity': 1,
},
],
mode = 'payment',
success_url = 'http://127.0.0.1:8000/checkout/success' + '/?session_id={CHECKOUT_SESSION_ID}',
cancel_url = 'http://127.0.0.1:8000/create/',
)

except Exception as e:
return str(e)
return redirect(checkout_session.url, code=303)

这是我的urls.py文件:

from django.urls import path
from checkout.views import *
app_name = 'checkout'
urlpatterns = [
path('', index, name='index'),
path('success/', success, name='success'),
]

这是我从stripe复制的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>
<form action="/create-checkout-session" method="POST">
<button type="submit" id="checkout-button">Checkout</button>
</form>
</section>
</body>
</html>

您将成功页面中的{CHECKOUT_SESSION_ID}设置为查询参数:

success_url = 'http://127.0.0.1:8000/checkout/success' + '/?session_id={CHECKOUT_SESSION_ID}'

当签出会话成功时,Stripe将用正确的值填充{checkout_session_ID},并将用户重定向到适当的success_url

就您而言,在应用程序中定义页面/checkout/success时,您应该获得查询参数session_id的值,并使用它来自定义您的成功页面

有关更多详细信息,您可以参考此链接:https://stripe.com/docs/payments/checkout/custom-success-page

最新更新