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

Fix/decimal to int and billing_start_date #462

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ universal = true

[project]
name = "django-vendor"
version = "0.4.18"
version = "0.4.19"

authors = [
{ name="Grant Viklund", email="[email protected]" },
Expand Down
22 changes: 17 additions & 5 deletions src/vendor/models/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,30 +253,40 @@ def has_trial(self):
return True
return False

def has_valid_billing_start_date(self, today=timezone.now()):
def has_valid_billing_start_date(self, today=None):
if today is None:
today = timezone.now()

if self.billing_start_date and self.billing_start_date > today:
return True
return False

def get_offer_start_date(self, start_date=timezone.now()):
def get_offer_start_date(self, start_date=None):
if start_date is None:
start_date = timezone.now()
if self.term_start_date and self.term_start_date > start_date:
return self.term_start_date

return start_date

def get_offer_end_date(self, start_date=timezone.now()):
def get_offer_end_date(self, start_date=None):
"""
Determines the start date offset so the payment gateway starts charging the monthly offer
"""
units = self.term_details.get('term_units', TermDetailUnits.MONTH)
if start_date is None:
start_date = timezone.now()

if units == TermDetailUnits.MONTH:
return get_future_date_months(start_date, self.get_period_length())

elif units == TermDetailUnits.DAY:
return get_future_date_days(start_date, self.get_period_length())

def get_trial_end_date(self, start_date=timezone.now()):
def get_trial_end_date(self, start_date=None):
if start_date is None:
start_date = timezone.now()

if self.billing_start_date and self.billing_start_date > start_date:
return self.billing_start_date - timedelta(days=1)

Expand All @@ -292,7 +302,9 @@ def get_trial_end_date(self, start_date=timezone.now()):

return start_date + timedelta(days=self.get_trial_days())

def get_payment_start_date_trial_offset(self, start_date=timezone.now()):
def get_payment_start_date_trial_offset(self, start_date=None):
if start_date is None:
start_date = timezone.now()
trial_end_date = self.get_trial_end_date(start_date)

if trial_end_date == start_date:
Expand Down
18 changes: 10 additions & 8 deletions src/vendor/processors/stripe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
from decimal import Decimal, ROUND_UP
from math import modf

import stripe
from django.conf import settings
Expand Down Expand Up @@ -350,16 +349,19 @@ def stripe_call(self, *args):
def convert_decimal_to_integer(self, decimal):
if decimal == 0:
return 0

fraction_part, whole_part = modf(decimal)


fraction_part = decimal % 1
whole_part = decimal - fraction_part
rounded_fraction = round(fraction_part, 2)

whole_str = str(whole_part).split('.')[0]
# need to use Decimal since floats will not always round as expected
rounded_fraction = Decimal(fraction_part).quantize(Decimal('.00'), rounding=ROUND_UP)
fraction_str = str(rounded_fraction).split('.')[1]
if fraction_part == 0:
fraction_str = "00"
else:
fraction_str = str(rounded_fraction).split('.')[1][:2]

if len(fraction_str) < 2:
fraction_str = "0" + fraction_str
fraction_str = fraction_str + "0"

stripe_amount = int("".join([whole_str, fraction_str]))

Expand Down
Loading