diff --git a/djstripe_example/urls.py b/djstripe_example/urls.py index b1a10f2..575bb8f 100644 --- a/djstripe_example/urls.py +++ b/djstripe_example/urls.py @@ -27,5 +27,5 @@ name="checkout_redirect", ), path("checkout/create/", create_checkout_session, name="create_checkout_session"), - path("stripe/", include("djstripe.urls", namespace="djstripe")), + path("djstripe/", include("djstripe.urls", namespace="djstripe")), ] diff --git a/djstripe_example/views.py b/djstripe_example/views.py index a8ba398..9e199de 100644 --- a/djstripe_example/views.py +++ b/djstripe_example/views.py @@ -4,7 +4,7 @@ from django.urls import reverse from django.views.generic import RedirectView, TemplateView from djstripe.enums import APIKeyType -from djstripe.models import APIKey, Price, Product +from djstripe.models import APIKey, Price, Product, TaxRate stripe.api_key = settings.STRIPE_TEST_SECRET_KEY @@ -33,6 +33,22 @@ def get_or_create_starter_price(): return Price.sync_from_stripe_data(stripe_price) +def get_or_create_tax_rate(): + try: + tax_rate = TaxRate.objects.get(metadata__djstripe_example="example_tax_rate") + except TaxRate.DoesNotExist: + print("Could not find a tax rate to use. Will create one for you.") + stripe_tax_rate = stripe.TaxRate.create( + display_name="VAT", + description="VAT", + inclusive=False, + percentage=20, + metadata={"djstripe_example": "example_tax_rate"}, + ) + tax_rate = TaxRate.sync_from_stripe_data(stripe_tax_rate) + return tax_rate + + class CheckoutRedirectView(TemplateView): template_name = "checkout_redirect.html" @@ -68,12 +84,13 @@ class CreateCheckoutSession(RedirectView): def get_redirect_url(self, *args, **kwargs): price = get_or_create_starter_price() + tax_rate = get_or_create_tax_rate() checkout_session = stripe.checkout.Session.create( success_url="http://localhost:8000/checkout/success/?session_id={CHECKOUT_SESSION_ID}", cancel_url="http://localhost:8000/checkout/canceled/", mode="subscription", - line_items=[{"price": price.id, "quantity": 1}], + line_items=[{"price": price.id, "quantity": 1, "tax_rates": [tax_rate.id]}], payment_method_types=["card"], )