Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Tiered Pricing Plans #629

Open
wants to merge 14 commits into
base: original
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix Failing TieredPricingManagerTests
  • Loading branch information
jksimoniii committed Feb 1, 2019
commit eb0492f716f6fb95e0ee04a7eef5b25f2c3517d8
24 changes: 16 additions & 8 deletions pinax/stripe/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,23 @@ def calculate_final_cost(self, plan, quantity, mode):
raise Exception("Received wrong type of mode ({})".format(mode))

all_tiers = self.all_tiers(plan)
applicable_tiers = filter(lambda t: quantity <= t.up_to, all_tiers)

if mode == self.TIERS_MODE_VOLUME:
tiers = applicable_tiers[:-1] if len(applicable_tiers) else all_tiers[:-1]
applicable_tiers = filter(lambda t: quantity <= t.up_to, all_tiers)
tier = applicable_tiers[0] if applicable_tiers else all_tiers[-1]
cost = tier.calculate_cost(quantity)
elif mode == self.TIERS_MODE_GRADUATED:
tiers = applicable_tiers if len(applicable_tiers) else all_tiers
quantity_billed = 0
cost = 0
idx = 0
while quantity > 0:
tier = all_tiers[idx]
quantity_to_bill = min(quantity, tier.up_to - quantity_billed) if tier.up_to else quantity
cost += tier.calculate_cost(quantity_to_bill)
quantity -= quantity_to_bill
quantity_billed += quantity_to_bill
idx += 1
else:
tiers = []
cost = 0

# Accumulate cost for each tier
return reduce(
lambda ax, t: (ax[0] + t.calculate_cost(ax[1]), ax[1] - t.up_to if t.up_to else 0), tiers, (0, quantity)
)[0]
return cost