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

* Set old subscription to 'pending-cancel' status when customers


  upgrade through profile page.

* Convert cancel_subscription into set_subscription_status.

* Add upgrades YAML config example file.
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent 56dbe057
Pipeline #16563 passed with stage
in 40 seconds
......@@ -5,7 +5,8 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from .forms import ChooseBundleForm
from .models import BundleChange
from limitmonitor.common import get_user_subscriptions, parse_subscription, \
get_user_upgrade_choices, get_user_external_credit, get_available_bundles
set_subscription_status, get_user_external_credit, get_available_bundles, \
get_user_upgrade_choices
from limitmonitor.task_resources.subscription import upgrade_user_subscription
......@@ -76,5 +77,7 @@ class UpgradeView(LoginRequiredMixin, FormView):
old_subscription_id=parsed_sub['external_key']
)
bundle_change.save()
set_subscription_status(parsed_sub['external_key'],
'pending-cancel')
return super(UpgradeView, self).form_valid(form)
12345:
title: Example Basic Bundle (monthly)
short_title: Basic (monthly)
type: BASIC
period: monthly
domain: example.com
upgrades:
- 12346
- 12347
- 12348
- 12349
downgrades:
12350:
title: Example Basic Bundle (annual)
short_title: Basic (annual)
type: BASIC
period: annual
domain: example.com
upgrades:
- 12351
- 12347
- 12348
- 12349
downgrades:
12351:
title: Example Complete Bundle (monthly)
short_title: Complete (monthly)
type: COMPLETE
period: monthly
domain: example.com
upgrades:
- 12347
- 12348
- 12349
downgrades:
- 12345
- 12350
12347:
title: Example Complete Bundle (annual)
short_title: Complete (annual)
type: COMPLETE
period: annual
domain: example.com
upgrades:
- 12348
- 12349
downgrades:
- 12345
- 12350
12348:
title: Example Family Pack (monthly)
short_title: Family pack (monthly)
type: GROUP
period: monthly
domain: example.com
upgrades:
downgrades:
- 12345
- 12350
- 12347
- 12351
12349:
title: Example Family Pack (annual)
short_title: Family pack (annual)
type: GROUP
period: annual
domain: example.com
upgrades:
downgrades:
- 12345
- 12350
- 12347
- 12351
......@@ -325,9 +325,10 @@ def order_paid(order_id):
return False
def cancel_subscription(subscription_id):
"""Given a subscription_id cancels the subscription returning True
when cancellation succeeds and False when it does not.
def set_subscription_status(subscription_id, status):
"""Given a subscription_id and a status it set that status for the
subscription returning True when it succeeds and False when it
does not.
This has only be tested with WC instances with Stripe payment
gateway enabled. When used against a WC instance with Check
......@@ -336,16 +337,20 @@ def cancel_subscription(subscription_id):
get cancelled.
"""
valid_statuses = ['pending', 'active', 'on-hold', 'expired', 'cancelled',
'pending-cancel']
if status not in valid_statuses:
return False
woo = get_woo_connection()
data = {
'status': 'cancelled'
'status': status
}
query = 'subscriptions/{}'.format(subscription_id)
result = woo.put(query, data).json()
if result:
status = result.get('status', None)
r_status = result.get('status', None)
code = result.get('code', None)
if status == 'cancelled':
if r_status == status:
return True
elif code == 'woocommerce_rest_invalid_payment_data':
......@@ -353,8 +358,8 @@ def cancel_subscription(subscription_id):
# was really cancelled.
sub = woo.get('subscriptions/{}'.format(subscription_id)).json()
if sub:
status2 = sub.get('status', None)
if status2 == 'cancelled':
r_status2 = sub.get('status', None)
if r_status2 == status:
return True
return False
......
......@@ -4,7 +4,7 @@ from cart.models import ChosenReward
from bundlechange.models import BundleChange
from limitmonitor.common import parse_subscription, get_user_subscriptions, \
subscription_was_processed, get_services_from_bundle, forced_update, \
get_users_expired_subs, order_paid, cancel_subscription
get_users_expired_subs, order_paid, set_subscription_status
logger = get_task_logger(__name__)
......@@ -135,7 +135,8 @@ def process_bundle_changes():
for pending_change in pending_changes:
if order_paid(pending_change.new_order_id):
cancel_subscription(pending_change.old_subscription_id)
set_subscription_status(pending_change.old_subscription_id,
'cancelled')
forced_update(pending_change.user)
pending_change.is_pending = False
pending_change.save()
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