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 tax rate to example checkout session #7

Merged
merged 3 commits 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
31 changes: 31 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Linter

on:
pull_request:
branches: ['master', 'main']
paths-ignore: ['docs/**']

push:
branches: ['master', 'main']
paths-ignore: ['docs/**']

concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
linter:
runs-on: ubuntu-latest
steps:
- name: Checkout Code Repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
# Consider using pre-commit.ci for open source project
- name: Run pre-commit
uses: abe-101/[email protected]
with:
extra_args: --hook-stage push
51 changes: 51 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
exclude: '^docs/|/migrations/|devcontainer.json'
default_stages: [commit]

default_language_version:
python: python3.12

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-json
- id: check-toml
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: detect-private-key

- repo: https://github.com/adamchainz/django-upgrade
rev: '1.16.0'
hooks:
- id: django-upgrade
args: ['--target-version', '4.2']

# Run the Ruff linter.
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
# Linter
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
# Formatter
- id: ruff-format

- repo: https://github.com/Riverside-Healthcare/djLint
rev: v1.34.1
hooks:
- id: djlint-reformat-django
- id: djlint-django



# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
autoupdate_schedule: weekly
skip: []
submodules: false
2 changes: 0 additions & 2 deletions djstripe_example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import os
from pathlib import Path
import environ

Expand Down Expand Up @@ -111,7 +110,6 @@

USE_I18N = True

USE_L10N = True

USE_TZ = True

Expand Down
13 changes: 10 additions & 3 deletions djstripe_example/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Dj-Stripe Example Project">
<meta name="author" content="Abe Hanoka">
<meta name="keywords"
content="Dj-Stripe, Stripe, Django, Payment Integration">
<title>Dj-Stripe Example Project</title>
<script src="https://js.stripe.com/v3/"></script>
<link rel="stylesheet" href="https://unpkg.com/mvp.css" />
<link rel="stylesheet" href="https://unpkg.com/mvp.css">
</head>
<body>
{% block content %} {% endblock %}
{% block content %}
{% endblock content %}
</body>
</html>
11 changes: 5 additions & 6 deletions djstripe_example/templates/checkout_redirect.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{% extends "base.html" %} {% block content %}

<script type="application/javascript">
{% extends "base.html" %}
{% block content %}
<script type="application/javascript">
const stripe = Stripe("{{ stripe_public_key }}");
stripe.redirectToCheckout({
sessionId: "{{ checkout_session_id }}",
});
</script>

{% endblock %}
</script>
{% endblock content %}
3 changes: 2 additions & 1 deletion djstripe_example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import include, path

Expand All @@ -26,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
1 change: 1 addition & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ version = "0.1.0"
description = ""
authors = ["Jerome Leclanche <[email protected]>"]
# Uncomment if using a local copy
# package-mode = false
package-mode = false

[tool.poetry.dependencies]
python = "^3.9"
django = "^4.2"
django-environ = "^0.11.2"
# Choose between using the latest verion or a local copy
dj-stripe = {git = "https://github.com/dj-stripe/dj-stripe.git"}
# dj-stripe = { path = "../dj-stripe" }
# dj-stripe = {git = "https://github.com/dj-stripe/dj-stripe.git"}
dj-stripe = { path = "../dj-stripe" }

[tool.poetry.dev-dependencies]

Expand Down