#Django/Stripe integration issue.

10 messages · Page 1 of 1 (latest)

vestal canyon
#

I've been following this tutorial (https://www.saaspegasus.com/guides/django-stripe-integrate/) on integrating a subscription service to my application. Everything seems fine, when i checkout it updates my stripe database with the new subscription, but i get a NoReverseMatch error after clicking the checkout button. Specifically, the error says "NoReverseMatch at /subscription-confirm/ Reverse for 'subscription_details' not found. 'subscription_details' is not a valid view function or pattern name." Nowhere in my code do i define a "subscription_details", i was assuming that maybe this was something that came included in the djstripe package? Also the tutorial does nothing to define it either. I'm kind of lost here. The one thing that i see is a little odd is on my views file, when trying to import 'from djstripe.settings import djstripe_settings' and 'from djstripe.models import Subscription', that Pylance isnt recognizing djstripe.settings or djstripe.models. Here is some of the code i think is relevant, if you guys want to see more just ask. Thanks in advance!

views.py:

from typing import Any
from django.db.models.query import QuerySet
from django.urls import reverse_lazy, reverse
from django.views.generic.edit import CreateView
from django.views.generic import TemplateView, ListView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from .models import Picks
from django.shortcuts import render
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth import get_user_model
import stripe
from djstripe.settings import djstripe_settings
from djstripe.models import Subscription
from .forms import CustomUserCreationForm

@login_required
def subscription_confirm(request):
    # set our stripe keys up
    stripe.api_key = djstripe_settings.STRIPE_SECRET_KEY

    # get the session id from the URL and retrieve the session object from Stripe
    session_id = request.GET.get("session_id")
    session = stripe.checkout.Session.retrieve(session_id)

    # get the subscribing user from the client_reference_id we passed in above
    client_reference_id = int(session.client_reference_id)
    subscription_holder = get_user_model().objects.get(id=client_reference_id)
    # sanity check that the logged in user is the one being updated
    assert subscription_holder == request.user

    # get the subscription object form Stripe and sync to djstripe
    subscription = stripe.Subscription.retrieve(session.subscription)
    djstripe_subscription = Subscription.sync_from_stripe_data(subscription)

    # set the subscription and customer on our user
    subscription_holder.subscription = djstripe_subscription
    subscription_holder.customer = djstripe_subscription.customer
    subscription_holder.save()

    # show a message to the user and redirect
    messages.success(request, f"You've successfully signed up. Thanks for the support!")
    return HttpResponseRedirect(reverse("subscription_details"))

urls.py:

from django.urls import path

from .views import SignUpView, PicksView, HomeView, ThanksView, subscription_confirm

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('signup/', SignUpView.as_view(), name='signup'),
    path('picks/', PicksView.as_view(), name='picks'),
    path('thankyou', ThanksView.as_view(), name='thanks'),
    path("subscription-confirm/", subscription_confirm, name="subscription_confirm"),
]
SaaS Pegasus

All the technical details of creating a subscription SaaS business using the Python-based Django web framework and Stripe payment processor.

full nimbus
#

return HttpResponseRedirect(reverse("subscription_details"))

#

In this line you are telling to reverse a url you have not created ( and then redirect to it )

#

In your urlpatterns

#

Because this is a tutorial and I have not followed the whole , but my guess is that this 'subscription_details' will show some details to the user ( That the transaction was okey and staff like that )

#
views.py : 

def sub_details(request):
  retun HttpResponse(200)

urls.py
urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('signup/', SignUpView.as_view(), name='signup'),
    path('picks/', PicksView.as_view(), name='picks'),
    path('thankyou', ThanksView.as_view(), name='thanks'),
    path("subscription-confirm/", subscription_confirm, name="subscription_confirm"),
    path("subscription_details/", subscription_confirm, name="subscription_details"), # ADDED THIS URL HERE
]

#

you can create a view that just returns the 200 status or renders a template or anything you want

vestal canyon
#

Yeah I saw that but the tutorial mentions literally nothing about it, I was kind of under the impression it had something to do with the djstripe package or something of that sort. But obviously not lol. So what about the pylance not recognizing importing djstripe… that’s kind of weirding me out.

full nimbus
#

pylance not recognising import ,
If you mean on your editor the import stripe is underlined with yellow line meaning not found or something could be due to your ( vscode interpreter environment not being your virtualenviroment you created ( if you created one ) )

#

because if the dependency was not installed properly , the scenario would be just to get error uknown stripe module and staff like that .