Skip to content

Commit

Permalink
more float fixes, round purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Feb 22, 2016
1 parent dbb1e28 commit 775b20a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion chezbetty/datalayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def purchase(user, account, items):
DBSession.add(pli)
amount += line_amount
if discount:
amount = amount - (amount * discount)
amount = round(amount - (amount * discount), 2)
t.update_amount(amount)

if isinstance(account, Pool):
Expand Down
6 changes: 3 additions & 3 deletions chezbetty/templates/user/deposit_cc_custom.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
<hr />

{% if account == 'user' %}
{% set balance = user.balance|float %}
{% set target = balance + amount %}
{% set balance = user.balance %}
{% set target = balance|float + amount|float %}
<dl class="dl-horizontal">
<dt>{{ _('Account Balance') }}</dt>
<dd id="user-balance">{{ user.balance|format_currency|safe }}</dd>
</dl>
{% else %}
{% set balance = pool.balance|float %}
{% set target = balance + amount %}
{% set target = balance + amount|float %}
<dl class="dl-horizontal">
<dt>{{ _('%(pool_name)s Balance')|format(pool_name=pool.name) }}</dt>
<dd id="pool-{{ pool.id }}-balance">{{ pool.balance|format_currency|safe }}</dd>
Expand Down
6 changes: 3 additions & 3 deletions chezbetty/templates/user/deposit_cc_row.jinja2
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% macro deposit_cc_custom(account, amount, balance, stripe_pk, class='') -%}
{% set charge = (amount + 0.3) / 0.971 %}
{% set fee = charge - amount %}
{% set charge = (amount|float + 0.3) / 0.971 %}
{% set fee = charge - amount|float %}
{% set total_cents = (charge*100)|round|int %}
{% set new_balance = balance|float + amount %}
{% set new_balance = balance|float + amount|float %}

<tr class="{{ class }}">
<td>{{ amount|format_currency|safe }}</td>
Expand Down
3 changes: 1 addition & 2 deletions chezbetty/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,13 @@ def post_stripe_payment(
# See http://stripe.com/docs/tutorials/charges
stripe.api_key = request.registry.settings['stripe.secret_key']

charge = (amount + 0.3) / 0.971
charge = (amount + Decimal('0.3')) / Decimal('0.971')
fee = charge - amount
if total_cents != int(round((amount + fee)*100)):
print("Stripe total mismatch. total_cents {} != {}".format(
total_cents, int(round((amount + fee)*100))))
request.session.flash('Unexpected error processing transaction. Card NOT charged.', 'error')
return False
amount = Decimal(amount)

if amount <= 0.0:
request.session.flash(
Expand Down
5 changes: 4 additions & 1 deletion chezbetty/views_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ def admin_restock(request):
values = packed_values.split(',')
if len(values) == 1:
# This is the global cost value
global_cost = round(Decimal(values[0]), 2)
try:
global_cost = round(Decimal(values[0] or 0), 2)
except:
global_cost = Decimal(0)
else:
line_values = {}
line_type = values[0]
Expand Down
4 changes: 2 additions & 2 deletions chezbetty/views_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def user_deposit_cc_custom(request):
pool = None
return {'user': request.user,
'stripe_pk': request.registry.settings['stripe.publishable_key'],
'amount': round(float(request.GET['deposit-amount']), 2),
'amount': round(Decimal(request.GET['deposit-amount']), 2),
'account': account,
'pool': pool,
}
Expand All @@ -194,7 +194,7 @@ def user_deposit_cc_custom(request):
permission='user')
def user_deposit_cc_submit(request):
token = request.POST['stripeToken']
amount = float(request.POST['betty_amount'])
amount = Decimal(request.POST['betty_amount'])
total_cents = int(request.POST['betty_total_cents'])
to_account = request.POST['betty_to_account']

Expand Down

0 comments on commit 775b20a

Please sign in to comment.