django - 如何在 Django 中使用 Stripe?
问题描述
- 语言:Python
- 操作系统:Windows 10
- 框架 - Django 3
我的网站包含三本书,每本书都有不同的价格标签,比如 500、1000 和 2000 INR。当我直接指定金额时,金额字段工作正常(如下面的代码所示,我使用了 500 INR)。但我想让金额字段动态化。这样我就可以为其他书籍提供不同的价格标签。
视图.py
from django.shortcuts import render
from django.views.generic.base import TemplateView
from django.views.generic import DetailView
from django.conf import settings
from books.models import Book
import stripe
stripe.api_key = settings.STRIPE_SECRET_KEY
class PaymentsPage(DetailView):
model = Book
template_name = 'payments.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['key'] = settings.STRIPE_PUBLISHABLE_KEY
return context
def charge(request): # will handle our payments (via tokens)
if request.method == 'POST':
charge = stripe.Charge.create(
amount=500,
currency='inr',
description='A Django charge',
source=request.POST['stripeToken']
)
payment_intent = stripe.PaymentIntent.create(
amount=1099,
currency='inr',
description='Software development services',
)
customer = stripe.Customer.create(
name='Jenny Rosen',
address={
'line1': 'Delhi',
'postal_code': '110059',
'city': 'delhi',
'state': 'New Delhi',
'country': 'INDIA',
},
)
return render(request, 'charge.html')
解决方案
推荐阅读
- cassandra - 无法在带有 JDK 15.0.2 的 Windows 上启动 Cassandra 3.11.10
- sql - 无法通过 12 c 中的 sql developer 在 sql 查询中添加空列
- c - 多线程应用程序中的线程不并行运行
- python - PermissionError:如何通过身份验证从远程文件服务器(可通过 IP 访问)读取文件?
- firebase - 在firebase中创建用户时是否可以自动发送验证链接电子邮件?
- amazon-web-services - AWS Lambda - 超时
- html - 如何设置引导行的最小高度
- c# - 如何在 asp.net core 中找到属性的总和
- php - docker logs 返回“致命错误:未捕获的错误:未找到类 'mysqli'”
- css - 网格从具有固定高度的父容器垂直溢出