Commit 5a9b399d authored by Birin Sanchez's avatar Birin Sanchez
Browse files

* Downgrade invitees to Complete bundle rather than Basic with


  renewal date set to the original Family pack owner renewal date.

* Remove price parameter from create_user_subscription as it is RO.

* Set billing_period when creating the subscription in
  create_user_subscription
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent 90d94fda
Pipeline #24170 passed with stage
in 28 seconds
......@@ -168,20 +168,26 @@ def process_bundle_changes():
if is_paid and is_active:
set_subscription_status(pending_change.old_subscription_id,
'cancelled')
# save old subs renewal date for later
old_ren_date = pending_change.user.limit_set.first().renewal_date
forced_update(pending_change.user)
# if this is a Family pack downgrade deal with the
# invitees
if pending_change.is_family_downgrade():
downgrade_invitees(pending_change.user)
downgrade_invitees(pending_change.user, old_ren_date)
pending_change.is_pending = False
pending_change.save()
def downgrade_invitees(user):
def downgrade_invitees(user, renewal_date=None):
invs = user.get_invitees()
if renewal_date is None:
ren_date = timezone.now() + timezone.timedelta(days=30)
else:
ren_date = renewal_date
for inv in invs:
# Create Basic monthly subscription if the invitee has a
# recovery email
......@@ -198,8 +204,10 @@ def downgrade_invitees(user):
# cancell invitee old subs
set_subscription_status(p_sub['external_key'], 'cancelled')
# create new Basic subs
order_info = create_user_subscription(inv, price=0,
paid=True)
order_info = create_user_subscription(
inv, bundle_type='COMPLETE', paid=True,
next_payment_date=ren_date
)
if order_info.get('id', None) is not None:
forced_update(inv)
# remove the invitation
......@@ -217,17 +225,15 @@ def downgrade_invitees(user):
def create_user_subscription(user, bundle_type='BASIC',
billing_period='monthly',
price=None, paid=False, next_payment_date=None,
billing_email=None,
billing_period='monthly', paid=False,
next_payment_date=None, billing_email=None,
product_id=settings.WOO_PARENT_PROD_ID):
"""Creates a new WooCommerce order with a subscription for the user
using bundle_type, billing_period, price, etc as specified by
parameters. Default values if not specified will be:
using bundle_type, billing_period, etc as specified by parameters.
Default values if not specified will be:
bundle_type = BASIC
billing_period = monthly
price = as provided by WC
paid = False
next_payment_date = 30 days from now
billing_email = email set by WC
......@@ -256,11 +262,6 @@ def create_user_subscription(user, bundle_type='BASIC',
if next_payment_date is None:
next_payment_date = timezone.now() + timezone.timedelta(days=30)
if price is None:
price = ''
else:
price = str(price)
order_data = {
"payment_method": "stripe",
"payment_method_title": "Credit Card (Stripe)",
......@@ -270,7 +271,6 @@ def create_user_subscription(user, bundle_type='BASIC',
"product_id": product_id,
"variation_id": variation_id,
"quantity": 1,
"price": price
}
],
}
......@@ -281,6 +281,11 @@ def create_user_subscription(user, bundle_type='BASIC',
order_data['customer_id'] = user_wc_id
order = user.woo_post_json('orders', order_data)
# Set billing period for subscription.
bill_p = 'month'
if billing_period == 'annual':
bill_p = 'year'
subs_data = {
"line_items": [
{
......@@ -290,6 +295,7 @@ 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
}
subs_data['parent_id'] = order['id']
subs_data['customer_id'] = order['customer_id']
......@@ -311,7 +317,7 @@ def create_invitee_subscription(user):
return result
renewal_date = invitation.owner.limit_set.first().renewal_date
result = create_user_subscription(user, bundle_type='INVITED', price=0,
paid=True, billing_email=user.email,
result = create_user_subscription(user, bundle_type='INVITED', paid=True,
billing_email=user.email,
next_payment_date=renewal_date)
return result
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