Skip to content

Commit

Permalink
Added Voucherify util methods to Python SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Reczko committed Jun 3, 2016
1 parent 831141a commit 20121f6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 18 deletions.
20 changes: 19 additions & 1 deletion examples/voucherify_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
Get voucher
'''

pprint.pprint('=== Get voucher ===')
voucher = client.get(voucher['code'])
pprint.pprint(voucher)

'''
Validate voucher
Utils
'''

pprint.pprint('=== Calculate Discount Amount and Price after discount===')

unit_price = 83.45
items_count = 13
base_price = unit_price * items_count

discount = voucherify.utils.calculate_discount(base_price, voucher, unit_price)
new_price = voucherify.utils.calculate_price(base_price, voucher, unit_price)
pprint.pprint(unit_price)
pprint.pprint(items_count)
pprint.pprint(base_price)
pprint.pprint(new_price)
pprint.pprint(discount)
2 changes: 1 addition & 1 deletion voucherify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from voucherify.client import Client
from voucherify.utils import calculatePrice, calculatePrice
import voucherify.utils as utils
77 changes: 61 additions & 16 deletions voucherify/utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,73 @@
import pprint

def round_money(value):
"""@TODO"""
return None
if value is None or value < 0:
raise Exception('Invalid value, amount should be a number and higher than zero.')
return round(value, 2)


def validate_percent_discount(discount):
"""@TODO"""
return None
if discount is None or discount < 0 or discount > 100:
raise Exception('Invalid voucher, percent discount should be between 0-100.')


def validate_amount_discount(discount=None):
if discount is None or discount < 0:
raise Exception('Invalid voucher, amount discount must be higher than zero.')


def validate_unit_discount(discount=None):
if discount is None or discount < 0:
raise Exception('Invalid voucher, unit discount must be higher than zero.')


def calculate_price(base_price, voucher, unit_price):
e = 100

if voucher['discount']['type'] == 'PERCENT':
discount = voucher['discount']['percent_off']
validate_percent_discount(discount)
price_discount = base_price * (discount / 100)
return round_money(base_price - price_discount)

elif voucher['discount']['type'] == 'AMOUNT':
discount = voucher['discount']['amount_off'] / e
validate_amount_discount(discount)
new_price = base_price - discount
return round_money(new_price if new_price > 0 else 0)

elif voucher['discount']['type'] == 'UNIT':
discount = voucher['discount']['unit_off']
validate_unit_discount(discount)
new_price = base_price - unit_price * discount
return round_money(new_price if new_price > 0 else 0)

else:
raise Exception('Unsupported voucher type.')


def validate_amount_discount(discount):
"""@TODO"""
return None
def calculate_discount(base_price, voucher, unit_price):
e = 100

if voucher['discount']['type'] == 'PERCENT':
discount = voucher['discount']['percent_off']
validate_percent_discount(discount)
return round_money(base_price * (discount / 100))

def validate_unit_discount(discount):
"""@TODO"""
return None
elif voucher['discount']['type'] == 'AMOUNT':
discount = voucher['discount']['amount_off'] / e
validate_amount_discount(discount)
new_price = base_price - discount
return round_money(discount if new_price > 0 else base_price)

elif voucher['discount']['type'] == 'UNIT':
discount = voucher['discount']['unit_off']
validate_unit_discount(discount)
price_discount = unit_price * discount
return round_money(base_price if price_discount > base_price else price_discount)

def calculatePrice(base_price, voucher, unit_price):
"""@TODO"""
return 0
else:
raise Exception('Unsupported voucher type.')


def calculateDiscount(base_price, voucher, unit_price):
"""@TODO"""
return 0
__all__ = ['calculate_price', 'calculate_discount']

0 comments on commit 20121f6

Please sign in to comment.