Skip to content

Commit

Permalink
Merge pull request #87 from buildly-release-management/master
Browse files Browse the repository at this point in the history
Hook Up Subscription Logic
  • Loading branch information
glind authored Mar 22, 2023
2 parents 5f34399 + 131ad20 commit 54472aa
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ EXPOSE 8080
ENTRYPOINT ["bash", "/code/scripts/docker-entrypoint.sh"]

# Specify tag name to be created on github
LABEL version="0.5.6"
LABEL version="0.5.7"
18 changes: 18 additions & 0 deletions core/migrations/0017_subscription_stripe_subscription_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.28 on 2023-03-19 10:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0016_auto_20230301_1310'),
]

operations = [
migrations.AddField(
model_name='subscription',
name='stripe_subscription_id',
field=models.CharField(max_length=255, null=True),
),
]
27 changes: 27 additions & 0 deletions core/migrations/0018_auto_20230319_1707.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.2.28 on 2023-03-19 17:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0017_subscription_stripe_subscription_id'),
]

operations = [
migrations.RenameField(
model_name='subscription',
old_name='stripe_card_id',
new_name='stripe_payment_method_id',
),
migrations.RemoveField(
model_name='subscription',
name='customer_stripe_id',
),
migrations.AddField(
model_name='subscription',
name='stripe_customer_id',
field=models.CharField(max_length=255, null=True),
),
]
5 changes: 3 additions & 2 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,11 @@ class Partner(models.Model):

class Subscription(models.Model):
subscription_uuid = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
stripe_subscription_id = models.CharField(max_length=255, null=True)
stripe_product = models.CharField(max_length=255)
stripe_product_info = JSONField(blank=True, null=True)
customer_stripe_id = models.CharField(max_length=255)
stripe_card_id = models.CharField(max_length=255, null=True, blank=True)
stripe_customer_id = models.CharField(max_length=255, null=True)
stripe_payment_method_id = models.CharField(max_length=255, null=True, blank=True)
trial_start_date = models.DateField(null=True, blank=True)
trial_end_date = models.DateField(null=True, blank=True)
subscription_start_date = models.DateField(null=True, blank=True)
Expand Down
95 changes: 63 additions & 32 deletions core/views/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ class SubscriptionViewSet(viewsets.ModelViewSet):

def create(self, request, *args, **kwargs):
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
stripe.api_version = '2022-11-15'
data = self.get_stripe_details()
if data:
serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)

return Response(
serializer.data,
status=status.HTTP_201_CREATED
)
return Response(
data,
status=status.HTTP_201_CREATED
)

return Response(
dict(
Expand All @@ -55,6 +57,8 @@ def create(self, request, *args, **kwargs):
def update(self, request, *args, **kwargs):
instance = self.get_object()
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
stripe.api_version = '2022-11-15'
data = self.get_stripe_details()
if data:
serializer = self.get_serializer(instance, data=data, partial=True)
Expand Down Expand Up @@ -83,6 +87,13 @@ def update(self, request, *args, **kwargs):
serializer.data,
status=status.HTTP_201_CREATED
)
return Response(
dict(
code='stripe_api_error',
message='There was an error creating subscription'
),
status=status.HTTP_400_BAD_REQUEST
)

return Response(
dict(
Expand All @@ -104,15 +115,16 @@ def perform_create(self, serializer):
permission_classes=[AllowAny],
name='Fetch all existing products',
)
def stripe_products(self, request, pk=None, *args, **kwargs):
def stripe_products(self, request, *args, **kwargs):
"""
Fetch all existing Products in Stripe Platform
"""
# all products on stripe platform
products = []
if settings.STRIPE_SECRET:
stripe.api_key = settings.STRIPE_SECRET
stripe_products = stripe.Product.list()
stripe.api_version = '2022-11-15'
stripe_products = stripe.Product.search(query="active:'true'",)
products = stripe_products.data

return Response(
Expand All @@ -125,45 +137,64 @@ def get_stripe_details(self):
Get stripe details
"""
data = self.request.data.copy()
product = data.get('product')
card_id = data.pop('card_id', None)
product_id = data.get('product')
payment_method_id = data.pop('card_id', None)

if not (product and card_id):
if not (product_id and payment_method_id):
return None

# get stripe product
stripe_product = stripe.Product.retrieve(product_id)

try:
stripe.api_key = settings.STRIPE_SECRET
customer = stripe.Customer.create(
email=self.request.user.email,
name=str(self.request.user.organization.name).capitalize()
name=str(self.request.user.organization.name).capitalize(),

)
stripe.PaymentMethod.attach(card_id, customer=customer.id)
stripe_subscription_details = dict(
customer_stripe_id=customer.id,
stripe_product=product,
stripe_card_id=card_id,
trial_start_date=timezone.now().date(),
trial_end_date=timezone.now().date() + relativedelta.relativedelta(months=1),
subscription_start_date=timezone.now().date() + relativedelta.relativedelta(months=1),
subscription_end_date=timezone.now().date() + relativedelta.relativedelta(months=2),
organization=self.request.user.organization.organization_uuid,
stripe.PaymentMethod.attach(payment_method_id, customer=customer.id)

# set default payment method
customer = stripe.Customer.modify(
customer.id,
invoice_settings={
'default_payment_method': payment_method_id
}
)
data.update(stripe_subscription_details)

# get product details
stripe_product = stripe.Product.retrieve(product)
data.update(
dict(
stripe_product_info=dict(
id=stripe_product.get('id'),
name=stripe_product.get('name'),
description=stripe_product.get('description', ''),
# create subscription:
stripe_subscription = stripe.Subscription.create(
customer=customer.id,
items=[
{"price": stripe_product.default_price},
],
)

if stripe_subscription:
stripe_subscription_details = dict(
customer_stripe_id=customer.id,
stripe_subscription_id=stripe_subscription.id,
stripe_product=product_id,
stripe_payment_method_id=payment_method_id,
trial_start_date=timezone.now().date() - relativedelta.relativedelta(months=1),
trial_end_date=timezone.now().date(),
subscription_start_date=timezone.now().date(),
subscription_end_date=timezone.now().date() + relativedelta.relativedelta(months=1),
organization=self.request.user.organization.organization_uuid,
)
data.update(stripe_subscription_details)

data.update(
dict(
stripe_product_info=dict(
id=stripe_product.get('id'),
name=stripe_product.get('name'),
description=stripe_product.get('description', ''),
)
)
)
)

except stripe.error.InvalidRequestError:
return None

return data

0 comments on commit 54472aa

Please sign in to comment.