-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support subscriptions, webhooks, success URL, etc.
- Loading branch information
Ivan
committed
Apr 4, 2023
1 parent
61e53f1
commit b2b53f9
Showing
13 changed files
with
597 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,50 @@ | ||
from http import HTTPStatus | ||
import json | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import PermissionDenied | ||
from django.http import HttpResponse | ||
from django.utils.decorators import method_decorator | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
import stripe | ||
from payments.core import stripe | ||
from payments.models import get_payment_instance, Subscription | ||
|
||
|
||
@method_decorator(csrf_exempt, name='dispatch') | ||
class StripeWebhook(View): | ||
def post(self, request, *args, **kwargs): | ||
try: | ||
sig_header = request.headers['Stripe-Signature'] | ||
signature = request.headers['Stripe-Signature'] | ||
except KeyError: | ||
return HttpResponse(status=HTTPStatus.FORBIDDEN) | ||
|
||
try: | ||
event = stripe.Webhook.construct_event( | ||
request.body, sig_header, settings.STRIPE_ENDPOINT_SECRET | ||
) | ||
event = stripe.Webhook.construct_event(request.body, signature, settings.STRIPE_ENDPOINT_SECRET) | ||
except ValueError as e: | ||
raise PermissionDenied() | ||
except stripe.error.SignatureVerificationError as verification_err: | ||
return HttpResponse(status=HTTPStatus.FORBIDDEN) | ||
except stripe.error.SignatureVerificationError: | ||
return HttpResponse(status=HTTPStatus.FORBIDDEN) | ||
|
||
print(event.data.object.object) | ||
if event.type == 'checkout.session.completed': | ||
self.checkout_session_completed(event) | ||
elif event.type == 'invoice.payment_succeeded': | ||
self.invoice_payment_succeeded(event) | ||
elif event.type == 'customer.subscription.updated': | ||
self.customer_subscription_updated(event) | ||
|
||
return HttpResponse(status=HTTPStatus.OK) | ||
|
||
@staticmethod | ||
def customer_subscription_updated(event: stripe.Event): | ||
payment_instance = Subscription.objects.get(psp_id=event.data.object.id) | ||
payment_instance.update_from_event(event) | ||
|
||
@staticmethod | ||
def checkout_session_completed(event: stripe.Event): | ||
payment_instance = get_payment_instance(event) | ||
payment_instance.from_event(event, save=True) | ||
|
||
@staticmethod | ||
def invoice_payment_succeeded(event: stripe.Event): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Generated by Django 4.1.3 on 2023-04-04 19:48 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('payments', '0002_subscription_psp_id_subscription_psp_reference'), | ||
] | ||
|
||
operations = [ | ||
migrations.DeleteModel( | ||
name='Subscription', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Generated by Django 4.1.3 on 2023-04-04 21:31 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('payments', '0003_delete_subscription'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Product', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('active', models.BooleanField(default=False, verbose_name='Active')), | ||
('description', models.CharField(blank=True, max_length=256, null=True, verbose_name='Description')), | ||
('psp_id', models.CharField(max_length=64, verbose_name='PSP ID')), | ||
('metadata', models.JSONField(blank=True, null=True, verbose_name='Metadata')), | ||
('name', models.CharField(blank=True, max_length=256, null=True, verbose_name='Description')), | ||
('object_name', models.CharField(max_length=16, verbose_name='Object name')), | ||
('product_type', models.CharField(max_length=32, verbose_name='Product type')), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='Subscription', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('psp_id', models.CharField(max_length=64, verbose_name='PSP ID')), | ||
('active', models.BooleanField(default=False, verbose_name='Active')), | ||
('current_period_end', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Current period end')), | ||
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='subscriptions', to='payments.product')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='subscriptions', to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='Price', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('active', models.BooleanField(default=False, verbose_name='Active')), | ||
('billing_scheme', models.CharField(max_length=128, verbose_name='Billing scheme')), | ||
('currency', models.CharField(max_length=8, verbose_name='Currency')), | ||
('psp_id', models.CharField(max_length=64, verbose_name='PSP ID')), | ||
('metadata', models.JSONField(blank=True, null=True, verbose_name='Metadata')), | ||
('object_name', models.CharField(max_length=16, verbose_name='Object name')), | ||
('recurring', models.JSONField(blank=True, null=True, verbose_name='Recurring')), | ||
('payment_type', models.CharField(max_length=32, verbose_name='Payment type')), | ||
('unit_amount', models.PositiveIntegerField(verbose_name='Unit amount')), | ||
('unit_amount_decimal', models.CharField(max_length=32, verbose_name='Unit amount decimal')), | ||
('product', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='prices', to='payments.product')), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='Payment', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('psp_id', models.CharField(max_length=64, verbose_name='PSP ID')), | ||
('active', models.BooleanField(default=False, verbose_name='Active')), | ||
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='payments', to='payments.product')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='payments', to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
Oops, something went wrong.