Commit df20f228 authored by Birin Sanchez's avatar Birin Sanchez
Browse files

Add the remaining days of the current subscription when upgrading

from monthly to annual.
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <>
parent 97ea8cf6
Pipeline #30329 passed with stage
in 30 seconds
from django.urls import reverse_lazy
from django.conf import settings
from django.utils import timezone
from django.views.generic.edit import FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from .forms import ChooseBundleForm
......@@ -7,8 +8,9 @@ from .models import BundleChange
from limitmonitor.common import parse_subscription, set_subscription_status, \
from limitmonitor.models import get_available_bundles
from limitmonitor.task_resources.subscription import upgrade_user_subscription
from limitmonitor.task_resources.subscription import create_user_subscription
from purist.models import AccountType
#import pdb
class UpgradeView(LoginRequiredMixin, FormView):
......@@ -18,6 +20,7 @@ class UpgradeView(LoginRequiredMixin, FormView):
def get_form_kwargs(self):
self.avl_bundles = get_available_bundles()
choices = self.request.user.get_upgrade_choices(add_downgrade=True)
kwargs = super(UpgradeView, self).get_form_kwargs()
kwargs['choices'] = choices
......@@ -35,18 +38,17 @@ class UpgradeView(LoginRequiredMixin, FormView):
pending_changes = self.request.user.bundlechange_set.filter(
avl_bundles = get_available_bundles()
u_ec = self.request.user.get_external_credit()
if u_ec is not None:
context['current_bdl'] = (
if len(pending_changes) > 0:
context['has_pending_changes'] = True
context['from_bdl'] = (
context['to_bdl'] = (
context['pay_url'] = pending_changes[0].pay_url
context['has_pending_changes'] = False
......@@ -66,8 +68,15 @@ class UpgradeView(LoginRequiredMixin, FormView):
print('Error processing bundle_change for user {}: {}'.format(
self.request.user, repr(e)))
return super(UpgradeView, self).form_valid(form)
order = upgrade_user_subscription(self.request.user,
acc_type = self.avl_bundles[variation_id]['type']
new_period = self.avl_bundles[variation_id]['period']
order = create_user_subscription(self.request.user,
pay_url = settings.WOO_UPGRADE_PAY_URL.format(order['id'],
bundle_change = BundleChange(
......@@ -88,53 +88,6 @@ def process_renewals():
def upgrade_user_subscription(user, variation_id,
"""Given a user and a variation_id creates a new WooCommerce order
with a subscription associated to it. The billing and shipping
information are taken from the user previous subscritpion. The
item in the new subscription will be the product variation
specified by variation_id and product_id. By default product_id is
the default product configured in LDH.
Returns a dictionary containing:
* id: This is the order id.
* key: This is the order_key for the order. This can be used to
create the payment link.
* subscription: The created subscription in JSON format.
subs = user.get_woocommerce_subscriptions()
if subs is None:
order_data = {
"payment_method": "stripe",
"payment_method_title": "Credit Card (Stripe)",
"set_paid": False,
"line_items": [
"product_id": product_id,
"variation_id": variation_id,
"quantity": 1
order_data['billing'] = subs[0]['billing']
order_data['shipping'] = subs[0]['shipping']
order_data['customer_id'] = subs[0]['customer_id']
order = user.woo_post_json('orders', order_data)
subs_data = {}
subs_data['parent_id'] = order['id']
subs_data['customer_id'] = order['customer_id']
sub_order = user.woo_post_json('subscriptions', subs_data)
return {'id': order['id'],
'key': order['order_key'],
'subscription': sub_order}
def process_bundle_changes():
# We get all pending BundleChanges in reverse order ("-id") so we
......@@ -167,12 +120,29 @@ def process_bundle_changes():
and sub_status == 'active'
is_active = True
new_sub = parse_subscription(sub)
# if subscription is active and order is paid we process the
# bundle change
if is_paid and is_active:
pay_accts = [AccountType.COMPLETE, AccountType.GROUP]
if (
new_sub['billing_period'] == 'annual' and
old_sub['billing_period'] == 'monthly' and
old_sub['account_type'] in pay_accts
# Modify the new subscritpion
next_pay_date = (old_sub['next_renewal']
+ timezone.timedelta(days=365))
start_date = old_sub['next_renewal']
data = {'next_payment_date':
next_pay_date.strftime('%Y-%m-%d %H:%M:%S'),
'start_date': start_date.strftime('%Y-%m-%d %H:%M:%S')}
'subscriptions/' + new_sub['external_key'],
# if this is a Family pack downgrade deal with the
......@@ -233,6 +203,7 @@ def downgrade_invitees(user, old_sub=None):
def create_user_subscription(user, bundle_type='BASIC',
billing_period='monthly', paid=False,
next_payment_date=None, billing_email=None,
start_date=None, status=None,
"""Creates a new WooCommerce order with a subscription for the user
using bundle_type, billing_period, etc as specified by parameters.
......@@ -243,6 +214,7 @@ def create_user_subscription(user, bundle_type='BASIC',
paid = False
next_payment_date = 30 days from now
billing_email = email set by WC
status = active
product_id = As configured in LDH
Returns a dictionary containing:
......@@ -303,9 +275,14 @@ def create_user_subscription(user, bundle_type='BASIC',
"next_payment_date": next_payment_date.strftime('%Y-%m-%d %H:%M:%S'),
"billing_period": bill_p
if start_date is not None:
subs_data['start_date'] = start_date.strftime('%Y-%m-%d %H:%M:%S')
subs_data['parent_id'] = order['id']
subs_data['customer_id'] = order['customer_id']
subs_data['status'] = 'active'
if status is None:
subs_data['status'] = 'active'
subs_data['status'] = status
if billing_email:
subs_data['billing'] = order_data['billing']
sub_order = user.woo_post_json('subscriptions', subs_data)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment