Skip to content

Commit

Permalink
add tax rate to example checkout session (#7)
Browse files Browse the repository at this point in the history
* run linter

* add Linter action

* add tax rate to example checkout session
  • Loading branch information
abe-101 authored Jun 19, 2024
1 parent 5b090f7 commit 98c79bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion djstripe_example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
]
21 changes: 19 additions & 2 deletions djstripe_example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"],
)

Expand Down

0 comments on commit 98c79bf

Please sign in to comment.