首页 > 解决方案 > 如何在 Django 中使用 Stripe?

问题描述

我的网站包含三本书,每本书都有不同的价格标签,比如 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')

标签: django

解决方案


推荐阅读