Skip to content

Commit

Permalink
Merge pull request #86 from buildly-release-management/master
Browse files Browse the repository at this point in the history
Add Subscription Intergration
  • Loading branch information
glind authored Mar 9, 2023
2 parents 14a9a03 + 57fdec4 commit 5f34399
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7-alpine3.10
FROM --platform=linux/amd64 python:3.7-alpine3.10

# Do not buffer log messages in memory; some messages can be lost otherwise
ENV PYTHONUNBUFFERED 1
Expand All @@ -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.5"
LABEL version="0.5.6"
19 changes: 19 additions & 0 deletions core/migrations/0014_subscription_stripe_product_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.28 on 2023-02-15 14:43

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0013_auto_20230207_1403'),
]

operations = [
migrations.AddField(
model_name='subscription',
name='stripe_product_info',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True),
),
]
18 changes: 18 additions & 0 deletions core/migrations/0015_coreuser_coupon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.28 on 2023-03-01 06:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0014_subscription_stripe_product_info'),
]

operations = [
migrations.AddField(
model_name='coreuser',
name='coupon',
field=models.CharField(blank=True, max_length=48, null=True),
),
]
18 changes: 18 additions & 0 deletions core/migrations/0016_auto_20230301_1310.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.28 on 2023-03-01 13:10

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0015_coreuser_coupon'),
]

operations = [
migrations.RenameField(
model_name='coreuser',
old_name='coupon',
new_name='coupon_code',
),
]
2 changes: 2 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class CoreUser(AbstractUser):
edit_date = models.DateTimeField(null=True, blank=True)
user_type = models.CharField(blank=True, null=True, max_length=50, choices=USER_TYPE_CHOICES, default='Product Team')
survey_status = models.BooleanField(default=False)
coupon_code = models.CharField(max_length=48, blank=True, null=True)

class Meta:
ordering = ('first_name',)
Expand Down Expand Up @@ -281,6 +282,7 @@ class Partner(models.Model):
class Subscription(models.Model):
subscription_uuid = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
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)
trial_start_date = models.DateField(null=True, blank=True)
Expand Down
7 changes: 5 additions & 2 deletions core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def create(self, validated_data):

core_groups = validated_data.pop('core_groups', [])
invitation_token = validated_data.pop('invitation_token', None)
coupon_code = validated_data.pop('coupon_code', None)
coupon_code = validated_data.get('coupon_code', None)

# create core user
if settings.AUTO_APPROVE_USER: # If auto-approval set to true
Expand Down Expand Up @@ -356,7 +356,10 @@ class Meta:
fields = '__all__'

def get_subscription(self, organization):
return SubscriptionSerializer(organization.organization_subscription.all()).data
return SubscriptionSerializer(
organization.organization_subscription.all(),
many=True
).data


class OrganizationNestedSerializer(serializers.ModelSerializer):
Expand Down
35 changes: 34 additions & 1 deletion core/views/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework.decorators import action
from rest_framework.permissions import (AllowAny, IsAuthenticated)

from core.email_utils import send_email
from core.models import Subscription
from core.serializers import SubscriptionSerializer

Expand Down Expand Up @@ -60,6 +61,24 @@ def update(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)

product_info = data.get('stripe_product_info', {})
# send the email
context = {
'frontend_link': settings.FRONTEND_URL,
'product_name': product_info.get('name'),
'product_description': product_info.get('description')
}
subject = 'Subscription Success'
template_name = 'email/coreuser/subscription.txt'
html_template_name = 'email/coreuser/subscription.html'
send_email(
self.request.user.email,
subject,
context,
template_name,
html_template_name
)

return Response(
serializer.data,
status=status.HTTP_201_CREATED
Expand Down Expand Up @@ -126,11 +145,25 @@ def get_stripe_details(self):
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,
)

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', ''),
)
)
)

except stripe.error.InvalidRequestError:
return None

return data

46 changes: 46 additions & 0 deletions templates/email/coreuser/subscription.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body style="margin:0px;">
<div style="background-color: #f5f5f5; padding: 30px 0px 30px 0px; display: run-in; text-align: center;"><img align="center" alt="Buildly" src="" width="312.5" style="max-width: 625px; padding-bottom: 0; display: inline !important; vertical-align: bottom;" /></div>
<p><br/><br/></p>
<table align="center" style="width: 719px;">
<tbody>
<tr style="height: 104px;">
<td style="width: 691px; height: 104px;">
<h1 style="text-align: center;"><span style="color: #151f56;"><strong><span style="font-size: 40px; font-family: roboto,helvetica neue,helvetica,arial,sans-serif;">Subscription successful!</span></strong></span></h1>
<p style="padding-top: 20px; padding-bottom: 10px; text-align: center; font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #737581;"><span style="font-size: 12pt; font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #737581;">You have successfully subscribed to<span style="font-family: roboto,helvetica neue,helvetica,arial,sans-serif; color: #151f56;"><strong> Insights </strong></span> on Buildly!<br />Click the button below to return to the app</span></p>
</td>
</tr>
</tbody>
</table>
<table align="center" style="width: 160px;">
<tbody>
<tr style="height: 20px;">
<td style="font-family: Roboto, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18pt; padding: 12pt; background-color: #ff9f00; color: #ffffff; font-weight: bold; text-align: center;"><a href="{{ frontend_link }}" target="_blank" style="font-weight: bold; letter-spacing: normal; line-height: 100%; text-align: center; text-decoration: none; color: #ffffff;">Buildly Insights</a></td>
</tr>
</tbody>
</table>
<table align="center" style="width: 719px;">
<tbody>
<tr style="height: 35px;">
<td style="width: 691px; height: 35px;">
<p><span style="color: #737581; font-size: 12pt; font-family: roboto,helvetica neue,helvetica,arial,sans-serif;"><i>Package</i></span></p>
</td>
</tr>
<tr>
<td>{{ product_name }}</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>{{ product_description }}</td>
</tr>
</tbody>
</table>
</body>
</html>
11 changes: 11 additions & 0 deletions templates/email/coreuser/subscription.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Subscription successful!

You have successfully subscribed to Insights on Buildly.

Go to {{ frontend_link }} to access the app.

Package!

{{ product_name }}

{{ product_description }}

0 comments on commit 5f34399

Please sign in to comment.