Merge branch 'feature/6561/link-billlineitem-with-stripeplan' into 'master'

Feature/6561/link billlineitem with stripeplan

See merge request ungleich-public/dynamicweb!696
This commit is contained in:
pcoder116 2019-04-20 13:56:05 +02:00
commit c0c938e65c
5 changed files with 98 additions and 3 deletions

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2019-04-20 09:52
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('datacenterlight', '0027_dclcalculatorpluginmodel_enable_512mb_ram'),
]
operations = [
migrations.AddField(
model_name='stripeplan',
name='stripe_plan_name',
field=models.CharField(default='', max_length=512, null=True),
),
]

View file

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2019-04-20 10:22
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('datacenterlight', '0028_stripeplan_stripe_plan_name'),
]
operations = [
migrations.AddField(
model_name='stripeplan',
name='amount',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='stripeplan',
name='interval',
field=models.CharField(default='', max_length=128, null=True),
),
]

View file

@ -103,6 +103,9 @@ class StripePlan(models.Model):
A model to store Data Center Light's created Stripe plans
"""
stripe_plan_id = models.CharField(max_length=256, null=True)
stripe_plan_name = models.CharField(max_length=512, default="", null=True)
amount = models.PositiveIntegerField(default=0)
interval = models.CharField(max_length=128, default="", null=True)
@classmethod
def create(cls, stripe_plan_id):

View file

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2019-04-20 10:10
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('datacenterlight', '0028_stripeplan_stripe_plan_name'),
('hosting', '0052_hostingbilllineitem'),
]
operations = [
migrations.AddField(
model_name='hostingbilllineitem',
name='stripe_plan',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='datacenterlight.StripePlan'),
),
]

View file

@ -10,7 +10,7 @@ from django.db import models
from django.utils import timezone
from django.utils.functional import cached_property
from datacenterlight.models import VMPricing, VMTemplate
from datacenterlight.models import VMPricing, VMTemplate, StripePlan
from membership.models import StripeCustomer, CustomUser
from utils.mixins import AssignPermissionsMixin
from utils.models import BillingAddress
@ -342,6 +342,27 @@ class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
if 'line_items' in args:
line_items = args['line_items']
for item in line_items:
stripe_plan = None
if item.type == "subscription" or item.type == "invoiceitem":
# Check stripe plan and prepare it for linking to bill item
stripe_plan_id = item.plan.id
try:
stripe_plan = StripePlan.objects.get(
stripe_plan_name=stripe_plan_id
)
except StripePlan.DoesNotExist as dne:
logger.error(
"StripePlan %s doesn't exist" % stripe_plan_id
)
if stripe_plan_id is not None:
# Create Stripe Plan because we don't have it
stripe_plan = StripePlan.objects.create(
stripe_plan_id=stripe_plan_id,
stripe_plan_name=item.plan.name,
amount=item.plan.amount,
interval=item.plan.interval
)
logger.debug("Creatd StripePlan " + stripe_plan_id)
line_item_instance = HostingBillLineItem.objects.create(
monthly_hosting_bill=instance,
amount=item.amount,
@ -358,7 +379,8 @@ class MonthlyHostingBill(AssignPermissionsMixin, models.Model):
# https://stripe.com/docs/api/invoiceitems/object#invoiceitem_object-unit_amount
# So, for the time being I set the unit_amount to 0 if not
# found in the line item
unit_amount=item.unit_amount if hasattr(item, "unit_amount") else 0
unit_amount=item.unit_amount if hasattr(item, "unit_amount") else 0,
stripe_plan=stripe_plan
)
line_item_instance.assign_permissions(instance.customer.user)
instance.assign_permissions(instance.customer.user)
@ -431,7 +453,10 @@ class HostingBillLineItem(AssignPermissionsMixin, models.Model):
"""
Corresponds to InvoiceItem object of Stripe
"""
monthly_hosting_bill = models.ForeignKey(MonthlyHostingBill)
monthly_hosting_bill = models.ForeignKey(MonthlyHostingBill,
on_delete=models.CASCADE)
stripe_plan = models.ForeignKey(StripePlan, null=True,
on_delete=models.CASCADE)
amount = models.PositiveSmallIntegerField()
description = models.CharField(max_length=255)
discountable = models.BooleanField()