Skip to content

Commit

Permalink
server/order: create balances with amount taken from actual payment t…
Browse files Browse the repository at this point in the history
…ransaction

Take the amount from the order doesn't always work, as invoices may apply customer balances which won't reflect the actual payment amount
  • Loading branch information
frankie567 committed Oct 22, 2024
1 parent df84e4f commit 9c15b8b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions server/polar/order/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,17 @@ async def _create_order_balance(
session, product.organization_id
)

transfer_amount = order.amount

# Retrieve the payment transaction and link it to the order
payment_transaction = await balance_transaction_service.get_by(
session, type=TransactionType.payment, charge_id=charge_id
)
if payment_transaction is None:
raise PaymentTransactionForChargeDoesNotExist(charge_id)

# Make sure to take the amount from the payment transaction and not the order
# Orders invoices may apply customer balances which won't reflect the actual payment amount
transfer_amount = payment_transaction.amount

payment_transaction.order = order
session.add(payment_transaction)

Expand Down
2 changes: 1 addition & 1 deletion server/tests/order/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ async def test_subscription_with_account(
)
assert (
transaction_service_mock.create_balance_from_charge.call_args[1]["amount"]
== invoice_total
== payment_transaction.amount
)

platform_fee_transaction_service_mock.create_fees_reversal_balances.assert_called_once()
Expand Down

0 comments on commit 9c15b8b

Please sign in to comment.