|
|
for payment in self:
tx_sudo = payment.payment_transaction_id.sudo()
payment_method = (
tx_sudo.payment_method_id.primary_payment_method_id
or tx_sudo.payment_method_id
)
if (
tx_sudo # The payment was created by a transaction.
and tx_sudo.provider_id.support_refund != 'none'
and payment_method.support_refund != 'none'
and tx_sudo.operation != 'refund'
):
# Only consider refund transactions that are confirmed by summing the amounts of
# payments linked to such refund transactions. Indeed, should a refund transaction
# be stuck forever in a transient state (due to webhook failure, for example), the
# user would never be allowed to refund the source transaction again.
refund_payments = self.search([('source_payment_id', '=', payment.id)])
refunded_amount = abs(sum(refund_payments.mapped('amount')))
payment.amount_available_for_refund = payment.amount - refunded_amount
else:
payment.amount_available_for_refund = 0
|
|