Adding stripe integration
This commit is contained in:
parent
27a0f94c4e
commit
c62dbcacf8
@ -0,0 +1,29 @@
|
||||
# Generated by Django 4.0.2 on 2022-08-07 19:40
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('core', '0010_product_stripe_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='stripe_price_id',
|
||||
field=models.CharField(blank=True, max_length=255),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Subscription',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('stripe_id', models.CharField(blank=True, max_length=255)),
|
||||
('customer', models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='subscription', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@ -53,6 +53,7 @@ class Product(models.Model):
|
||||
blank=True,
|
||||
null=True,
|
||||
)
|
||||
stripe_price_id = models.CharField(max_length=255, blank=True)
|
||||
weight = MeasurementField(
|
||||
measurement=Weight,
|
||||
unit_choices=WeightUnits.CHOICES,
|
||||
|
||||
@ -22,12 +22,28 @@ def product_saved(sender, instance, created, **kwargs):
|
||||
logger.info('Product was saved')
|
||||
if created or not instance.stripe_id:
|
||||
stripe.api_key = settings.STRIPE_API_KEY
|
||||
response = stripe.Product.create(
|
||||
prod_response = stripe.Product.create(
|
||||
name=instance.name,
|
||||
description=instance.description
|
||||
)
|
||||
instance.stripe_id = response['id']
|
||||
price_response = stripe.Price.create(
|
||||
unit_amount=int(instance.price * 100),
|
||||
currency=settings.DEFAULT_CURRENCY,
|
||||
product=prod_response['id']
|
||||
)
|
||||
instance.stripe_id = prod_response['id']
|
||||
instance.stripe_price_id = price_response['id']
|
||||
instance.save()
|
||||
else:
|
||||
stripe.Product.modify(
|
||||
instance.stripe_id,
|
||||
name=instance.name,
|
||||
description=instance.description
|
||||
)
|
||||
stripe.Price.modify(
|
||||
instance.stripe_price_id,
|
||||
unit_amount=int(instance.price * 100)
|
||||
)
|
||||
|
||||
|
||||
@receiver(post_save, sender=Order, dispatch_uid="order_created")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user