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

Add support for discount coupons in subscriptions.


Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent dc5a62aa
Pipeline #49679 passed with stage
in 32 seconds
......@@ -215,7 +215,7 @@ class Subscription():
variation_name, next_payment_date, line_item_id, order_id,
status=SubscriptionStatus.PENDING,
account_type=AccountType.UNDEFINED,
billing_period=BillingPeriod.MONTHLY
billing_period=BillingPeriod.MONTHLY, price=None, coupons=None
):
self.user = user
self.subscription_id = subscription_id
......@@ -228,6 +228,8 @@ class Subscription():
self.billing_period = billing_period
self.line_item_id = line_item_id
self.order_id = order_id
self.price = price
self.coupons = coupons
@classmethod
def parse_subscription(cls, json_entry, user=None):
......@@ -252,6 +254,8 @@ class Subscription():
"Bad quantity {} in subscription {}".format(
quantity, subscription_id)
)
price = float(line_item.get('price', 0))
coupons = json_entry['coupon_lines']
line_item_id = line_item['id']
# calculate next renewal date
if json_entry["next_payment_date"] != "":
......@@ -317,7 +321,8 @@ class Subscription():
account_type=account_type,
billing_period=billing_period,
line_item_id=line_item_id,
order_id=json_entry['parent_id']
order_id=json_entry['parent_id'],
price=price, coupons=coupons
)
def upgrade_choices(self, add_downgrade=False):
......@@ -383,3 +388,59 @@ class Subscription():
if self.status == SubscriptionStatus.ACTIVE:
return True
return False
def apply_coupon(self, code):
# Get all coupons
req = self.woo.get('coupons')
if req.status_code != 200:
try:
raise SubscriptionError('Error when retrieving coupons: '
+ req.json()['message'])
except ValueError:
raise SubscriptionError('Error when retrieving coupons')
# Find the coupon by code an get useful data
discount_type, amount, data = None, None, None
for coupon in req.json():
coupon_code = coupon.get('code', None)
if coupon_code == code:
discount_type = coupon.get('discount_type', None)
amount = coupon.get('amount', None)
break
# Check if coupon was already applied
for coupon in self.coupons:
if coupon.get('code') == code:
# coupon already applied
return None
if discount_type == 'recurring_percent' and amount:
# Calculate total price with discount
total = round(self.price * (1 - float(amount) * 0.01), 2)
# Prepare data to update subscription
line_items = [
{
'id': self.line_item_id,
'name': self.variation_name,
'product_id': self.product_id,
'variation_id': self.variation_id,
'total': str(total)
}
]
data = {
'coupon_lines': [
{
'code': code,
'discount': str(round(self.price - total, 2))
}
],
'line_items': line_items
}
query = 'subscriptions/{}'.format(self.subscription_id)
req = self.woo.put(query, data)
if req.status_code != 200:
try:
raise SubscriptionError('Error when applying coupon to subscription: '
+ req.json()['message'])
except ValueError:
raise SubscriptionError('Error when applying coupon to subscription.')
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