Yuhan Chang
Yuhan Chang

Reputation: 1

Stripe integration on Django project TypeError

I am new to Django and Stripe API. I am following the Stripe document and I copied the code from the document to my view.py file:

this is my views.py file

from django.shortcuts import render,redirect
from django.conf import settings
from django.urls import reverse
from django.views import View
import stripe
stripe.api_key = settings.STRIPE_SECRET_KEY

class Create_checkout_session (View):
    def post(self, request, *args, **kwargs):
        DOMAIN = "http://127.0.0.1:8000"
        checkout_session = stripe.checkout.Session.create(
                line_items=[
                    {
                        # Provide the exact Price ID (e.g. pr_1234) of the product you want to sell
                        'price': 'price_id',
                        'quantity': 1,
                    },
                ],
                payment_method_types=[
                'card',
                ],
                # mode can be subscription, setup, payment
                mode='payment',
                success_url=DOMAIN+'/success/',
                cancel_url=DOMAIN+'/cancel/',
            )

       
        return redirect(checkout_session.url, code=303)

This is my urls.py in payment app:

from django.urls import path, include

#import views.py
from . import views
app_name ='payment'
#set urls for the app
urlpatterns = [
   path('create-checkout-session/',views.Create_checkout_session, name='create-checkout-session')
]

The error message I have is when I render this page, I have:

TypeError at /create-checkout-session/ init() takes 1 positional argument but 2 were given Request Method: GET Request URL: http://127.0.0.1:8000/create-checkout-session/ Django Version: 3.2.8 Exception Type: TypeError Exception Value:
init() takes 1 positional argument but 2 were given Exception Location: /Users/hannah/Desktop/TDMock/venv/lib/python3.9/site-packages/django/core/handlers/base.py, line 181, in _get_response Python Executable: /Users/hannah/Desktop/TDMock/venv/bin/python

I put the stripe secret key and public key in the settings file. What else should I do to make the integration work? Thank you so much.

Upvotes: 0

Views: 216

Answers (1)

Onyilimba
Onyilimba

Reputation: 1217

I believe your view is class based view, so it's missing ".as_view()" call. Write it like this. views.Create_checkout_session.as_view() in your urls.py file.

from django.urls import path, include

#import views.py
from . import views
app_name ='payment'
#set urls for the app
urlpatterns = [
   path('create-checkout-session/',views.Create_checkout_session.as_view(), name='create-checkout-session')
]

Upvotes: 1

Related Questions