Skip to content

Commit

Permalink
Update processing fee and refundable calculations
Browse files Browse the repository at this point in the history
The processing fee calculation will now default to calculating based on the transaction's amount, which I think was actually needed in an earlier commit (whoops). Also marks transactions as nonrefundable if they're on a closed receipt or their amount left equals their processing fees. Also updates the "total" text on closed receipts because it looked silly.
  • Loading branch information
kitsuta committed Sep 29, 2023
1 parent f9af724 commit dd4f3a6
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions uber/models/commerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ def all_sorted_items_and_txns(self):

@property
def total_processing_fees(self):
return sum([txn.calc_partial_processing_fee(txn.amount) for txn in self.refundable_txns])
return sum([txn.calc_processing_fee(txn.amount) for txn in self.refundable_txns])

@property
def remaining_processing_fees(self):
return sum([txn.calc_partial_processing_fee(txn.amount_left) for txn in self.refundable_txns])
return sum([txn.calc_processing_fee(txn.amount_left) for txn in self.refundable_txns])

@property
def open_receipt_items(self):
Expand Down Expand Up @@ -189,6 +189,10 @@ def txn_total(self):

@property
def total_str(self):
if self.closed:
return "{} in {}".format(format_currency(abs(self.txn_total / 100)),
"Payments" if self.txn_total >= 0 else "Refunds")

return "{} in {} and {} in {} = {} owe {}".format(format_currency(abs(self.item_total / 100)),
"Purchases" if self.item_total >= 0 else "Credit",
format_currency(abs(self.txn_total / 100)),
Expand Down Expand Up @@ -283,7 +287,8 @@ def available_actions(self):

@property
def refundable(self):
return self.charge_id and self.amount_left and self.amount > 0
return not self.receipt.closed and self.charge_id and self.amount > 0 and \
self.amount_left and self.amount_left != self.calc_processing_fee()

@property
def stripe_url(self):
Expand Down Expand Up @@ -313,8 +318,9 @@ def stripe_id(self):
# Return the most relevant Stripe ID for admins
return self.refund_id or self.charge_id or self.intent_id

def get_processing_fee(self):
if self.processing_fee:
@request_cached_property
def total_processing_fee(self):
if self.processing_fee and self.amount == self.txn_total:
return self.processing_fee

if c.AUTHORIZENET_LOGIN_ID:
Expand All @@ -326,14 +332,17 @@ def get_processing_fee(self):

return intent.charges.data[0].balance_transaction.fee_details[0].amount

def calc_partial_processing_fee(self, refund_amount):
def calc_processing_fee(self, amount=0):
from decimal import Decimal

if not refund_amount:
return 0
if not amount:
if self.processing_fee:
return self.processing_fee

amount = self.amount

refund_pct = Decimal(refund_amount) / Decimal(self.txn_total)
return refund_pct * Decimal(self.get_processing_fee())
refund_pct = Decimal(amount) / Decimal(self.txn_total)
return refund_pct * Decimal(self.total_processing_fee)

def check_stripe_id(self):
# Check all possible Stripe IDs for invalid request errors
Expand Down

0 comments on commit dd4f3a6

Please sign in to comment.