【发布时间】:2021-10-19 00:28:07
【问题描述】:
您好,我一直在尝试学习如何将 Stripe 预建结帐页面集成到我的 django 项目中。
条纹文档适用于烧瓶,因此我正在尝试阅读或观看有关如何将它们转换为 django 的 youtube 教程,但我并没有真正做到。教程中的代码与当前的条带文档不同
https://stripe.com/docs/checkout/integration-builder
from django.shortcuts import render, redirect
import stripe
from django.conf import settings
from django.http import JsonResponse
# Create your views here.
from django.views import View
from django.views.generic import TemplateView
stripe.api_key = settings.STRIPE_SECRET_KEY
class ProductLandingPageView(TemplateView):
template_name = "landing.html"
class CreateCheckoutSessionView(View):
def post(self, request, *args, **kwargs):
YOUR_DOMAIN = "http://127.0.0.1:8000"
checkout_session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[
{
# TODO: replace this with the `price` of the product you want to sell
'price': '{{PRICE_ID}}',
'quantity': 1,
},
],
mode='payment',
success_url=YOUR_DOMAIN + "/success",
cancel_url=YOUR_DOMAIN + "/cancel",
)
return redirect(checkout_session.url, code=303)
class Successview(TemplateView):
template_name = "success.html"
class Cancelview(TemplateView):
template_name = "cancel.html"
我可以让 checkout.html 显示,但是当我单击结帐按钮时出现此错误
TypeError at /create-checkout-session
__init__() takes 1 positional argument but 2 were given
Request Method: POST
Request URL: http://127.0.0.1:8000/create-checkout-session
Django Version: 3.2.5
Exception Type: TypeError
Exception Value:
__init__() takes 1 positional argument but 2 were given
Exception Location: C:\Program Files\Python39\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
Python Executable: C:\Program Files\Python39\python.exe
Python Version: 3.9.5
Python Path:
['C:\\Users\\TYS\\Desktop\\Web Development\\Django\\stripetest_project',
'C:\\Program Files\\Python39\\python39.zip',
'C:\\Program Files\\Python39\\DLLs',
'C:\\Program Files\\Python39\\lib',
'C:\\Program Files\\Python39',
'C:\\Users\\TYS\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Program Files\\Python39\\lib\\site-packages',
'C:\\Program Files\\Python39\\lib\\site-packages\\ibapi-9.76.1-py3.9.egg',
'C:\\Program Files\\Python39\\lib\\site-packages\\win32',
'C:\\Program Files\\Python39\\lib\\site-packages\\win32\\lib',
'C:\\Program Files\\Python39\\lib\\site-packages\\Pythonwin']
Server time: Tue, 17 Aug 2021 03:52:08 +0000
我也试过这个页面上的代码
https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=checkout
from django.shortcuts import render, redirect
import stripe
from django.conf import settings
from django.http import JsonResponse
# Create your views here.
from django.views import View
from django.views.generic import TemplateView
stripe.api_key = settings.STRIPE_SECRET_KEY
class ProductLandingPageView(TemplateView):
template_name = "landing.html"
class CreateCheckoutSessionView(View):
def post(self, request, *args, **kwargs):
YOUR_DOMAIN = "http://127.0.0.1:8000"
checkout_session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[{
'price_data': {
'currency': 'usd',
'product_data': {
'name': 'T-shirt',
},
'unit_amount': 2000,
},
'quantity': 1,
}],
mode='payment',
success_url=YOUR_DOMAIN + "/success",
cancel_url=YOUR_DOMAIN + "/cancel",
)
return redirect(checkout_session.url, code=303)
class Successview(TemplateView):
template_name = "success.html"
class Cancelview(TemplateView):
template_name = "cancel.html"
还是会报同样的错误
谁能帮我解决它?
谢谢
编辑:添加 urls.py 和landing.html 以供参考
from django.contrib import admin
from django.urls import path
from products.views import CreateCheckoutSessionView, ProductLandingPageView, Cancelview, Successview
urlpatterns = [
path('admin/', admin.site.urls),
path('cancel/', Cancelview.as_view(), name='cancel'),
path('success/', Successview.as_view(), name='success'),
path('create-checkout-session', CreateCheckoutSessionView, name='create-checkout-session'),
path('', ProductLandingPageView.as_view(), name='landing-page')
]
这是landing.html
<!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>$20.00</h5>
</div>
</div>
<form action="/create-checkout-session" method="POST">{% csrf_token %}
<button type="submit">Checkout</button>
</form>
</section>
</body>
</html>
【问题讨论】:
-
你能显示你的urls.py吗
-
嗨,已经添加了 urls.py 和landing.html。谢谢!
-
感谢分享。我添加了一个答案。