Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checkout success view #8

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion djstripe_example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.contrib import admin
from django.urls import include, path

from .views import checkout_redirect, create_checkout_session
from .views import checkout_redirect, create_checkout_session, checkout_session_success

urlpatterns = [
path("admin/", admin.site.urls),
Expand All @@ -27,5 +27,8 @@
name="checkout_redirect",
),
path("checkout/create/", create_checkout_session, name="create_checkout_session"),
path(
"checkout/success/", checkout_session_success, name="checkout_session_success"
),
path("djstripe/", include("djstripe.urls", namespace="djstripe")),
]
17 changes: 17 additions & 0 deletions djstripe_example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from djstripe.enums import APIKeyType
from djstripe.models import APIKey, Price, Product, TaxRate

from django.views import View
from django.http import JsonResponse

stripe.api_key = settings.STRIPE_TEST_SECRET_KEY


Expand Down Expand Up @@ -101,4 +104,18 @@ def get_redirect_url(self, *args, **kwargs):
)


class CheckoutSessionSuccessView(View):
def get(self, request, *args, **kwargs):
session_id = request.GET.get("session_id")
if not session_id:
return JsonResponse({"error": "No session ID provided."}, status=400)

try:
checkout_session = stripe.checkout.Session.retrieve(session_id)
return JsonResponse(checkout_session, safe=False)
except stripe.error.StripeError as e:
return JsonResponse({"error": str(e)}, status=400)


create_checkout_session = CreateCheckoutSession.as_view()
checkout_session_success = CheckoutSessionSuccessView.as_view()