Django models and helpers for integrating Paddle.com subscriptions with your Django app
Currently this package includes:
- Django Models for plans, plan prices, subscriptions, payments (invoices)
- Django management commands for sycing plans, subscriptions, payments
- Webhook receivers that handle subscription creation, subscription cancellation
Requires:
- Python 3.6+
- Django 2.1.0+
- Install the
django-paddle
package
pip install django-paddle
- Add
django_paddle
to your INSTALLED_APPS
INSTALLED_APPS = [
# ...
'django_paddle',
# ...
]
- In your
settings.py
add the following settings:
PADDLE_VENDOR_ID = 'your-vendor-id-here' # https://vendors.paddle.com/authentication
PADDLE_AUTH_CODE = 'your-auth-code-here' # https://vendors.paddle.com/authentication
PADDLE_PUBLIC_KEY = """-----BEGIN PUBLIC KEY-----
your
public
key
here
-----END PUBLIC KEY-----""" # https://vendors.paddle.com/public-key
PADDLE_ACCOUNT_MODEL = 'auth.User'
ℹ️ If you are using the default Django User model, set PADDLE_ACCOUNT_MODEL
to auth.User
. If you are using a custom User model set this to something like your_custom_app.YourUserModel
.
- In your projects main
urls.py
add thedjango_paddle
URLs for receiving webhooks:
urlpatterns = [
path('', include('django_paddle.urls')),
]
ℹ️ This will result in an absolute webhook URL https://example.com/webhook
. Make sure this is the Webhook URL you set in your Paddle settings (https://vendors.paddle.com/alerts-webhooks).
- Run migrations
python manage.py migrate
The User Model specified in PADDLE_ACCOUNT_MODEL
will now have a back-reference to the PaddleSubscription and vice versa.
Example:
sub = PaddleSubscription.objects.all()[0]
print(sub.account) # <User: johndoe@example.com>
or
user = User.objects.get(username='johndoe@example.com')
print(u.subscriptions.all()) # <QuerySet [<PaddleSubscription: PaddleSubscription object (123456)>]>
- Done!
We need a shared identifier between the User model and the PaddleSubscription model. This needs to be provided when we redirect a user to the Paddle checkout. If you are using the default Django User model you can provide a unique user ID as a passthrough value. The subscription_created
webook will check the passtrough field and see if a User with this ID exists and automatically connect it to the newly created subscription.
Example:
<script src="https://cdn.paddle.com/paddle/paddle.js"></script>
<script>
Paddle.Setup({ vendor: your-vendor-id-here });
var uid = "{{ request.user.id }}";
Paddle.Checkout.open({
product: 'your-plan-id-here',
passthrough: uid
});
</script>
manage.py paddle_sync_plans
- Syncs Subscription Plansmanage.py paddle_sync_subscriptions
- Syncs Subscriptionsmanage.py paddle_sync_payments
- Syncs payments for all subscriptions
python runtests.py