From 6449ec8cb68f9055ab35358554f82761fa06139b Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 31 Oct 2024 18:18:19 +0100 Subject: [PATCH 1/3] fix `execute_transfer` add missing update of the transfer's `destination` --- liberapay/payin/stripe.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/liberapay/payin/stripe.py b/liberapay/payin/stripe.py index d7220d177..546d56c49 100644 --- a/liberapay/payin/stripe.py +++ b/liberapay/payin/stripe.py @@ -501,7 +501,7 @@ def settle_charge_and_transfers( ) elif pt.status in ('pre', 'pending'): pt = execute_transfer( - db, pt, pt.destination_id, charge.id, + db, pt, charge.id, update_donor=(update_donor and i == last), ) else: @@ -552,12 +552,11 @@ def settle_charge_and_transfers( return payin -def execute_transfer(db, pt, destination, source_transaction, update_donor=True): +def execute_transfer(db, pt, source_transaction, update_donor=True): """Create a Transfer. Args: pt (Record): a row from the `payin_transfers` table - destination (str): the Stripe ID of the destination account source_transaction (str): the ID of the Charge this transfer is linked to Returns: @@ -570,7 +569,7 @@ def execute_transfer(db, pt, destination, source_transaction, update_donor=True) amount=Money_to_int(pt.amount), currency=pt.amount.currency, description=generate_transfer_description(pt), - destination=destination, + destination=pt.destination_id, metadata={'payin_transfer_id': pt.id}, source_transaction=source_transaction, idempotency_key='payin_transfer_%i' % pt.id, @@ -583,9 +582,9 @@ def execute_transfer(db, pt, destination, source_transaction, update_donor=True) SET is_current = null WHERE provider = 'stripe' AND id = %s - """, (destination,)) + """, (pt.destination_id,)) alternate_destination = db.one(""" - SELECT id + SELECT id, pk FROM payment_accounts WHERE participant = %(p_id)s AND provider = 'stripe' @@ -597,7 +596,14 @@ def execute_transfer(db, pt, destination, source_transaction, update_donor=True) LIMIT 1 """, dict(p_id=pt.recipient, SEPA=SEPA, currency=pt.amount.currency)) if alternate_destination: - return execute_transfer(db, pt, alternate_destination, source_transaction) + pt = db.one(""" + UPDATE payin_transfers + SET destination = %s + WHERE id = %s + RETURNING * + """, (alternate_destination.pk, pt.id)) + pt.destination_id = alternate_destination.id + return execute_transfer(db, pt, source_transaction) error = "The recipient's account no longer exists." return update_payin_transfer( db, pt.id, None, 'failed', error, update_donor=update_donor, From e769505f1bcd7d76b11de3a502eebe0fff935b90 Mon Sep 17 00:00:00 2001 From: Changaco Date: Thu, 31 Oct 2024 23:04:01 +0100 Subject: [PATCH 2/3] fix `settle_charge_and_transfers` stop assuming that the charge has been successfully settled when a balance transaction exists --- liberapay/payin/stripe.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/liberapay/payin/stripe.py b/liberapay/payin/stripe.py index 546d56c49..f42552244 100644 --- a/liberapay/payin/stripe.py +++ b/liberapay/payin/stripe.py @@ -483,7 +483,7 @@ def settle_charge_and_transfers( ORDER BY pt.id """, (payin.id,)) last = len(payin_transfers) - 1 - if amount_settled is not None: + if charge.status == 'succeeded': payer = db.Participant.from_id(payin.payer) undeliverable_amount = amount_settled.zero() for i, pt in enumerate(payin_transfers): @@ -542,7 +542,8 @@ def settle_charge_and_transfers( update_donor=(update_donor and i == last), ) - elif charge.status in ('failed', 'pending'): + else: + assert charge.status in ('failed', 'pending') for i, pt in enumerate(payin_transfers): update_payin_transfer( db, pt.id, None, charge.status, error, From 9c20fa2587ffa33b41bdc90d357f016a711ea50c Mon Sep 17 00:00:00 2001 From: Changaco Date: Sat, 2 Nov 2024 16:40:25 +0100 Subject: [PATCH 3/3] fix Stripe payment page for minimum donations fixes LIBERAPAYCOM-27B --- liberapay/payin/common.py | 3 +++ www/%username/giving/pay/stripe/%payin_id.spt | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/liberapay/payin/common.py b/liberapay/payin/common.py index 1eb35d88d..f727f26ac 100644 --- a/liberapay/payin/common.py +++ b/liberapay/payin/common.py @@ -558,6 +558,9 @@ def resolve_amounts( Returns a copy of `base_amounts` with updated values. """ + if available_amount < (minimum_amount or 0): + raise ValueError("available_amount can't be less than minimum_amount or 0") + currency = available_amount.currency zero = Money.ZEROS[currency] inf = Money('inf', currency) diff --git a/www/%username/giving/pay/stripe/%payin_id.spt b/www/%username/giving/pay/stripe/%payin_id.spt index 9ed8d036c..59097965c 100644 --- a/www/%username/giving/pay/stripe/%payin_id.spt +++ b/www/%username/giving/pay/stripe/%payin_id.spt @@ -256,7 +256,7 @@ if tippees: if len(tips) == 1: proto_transfers = resolve_tip( website.db, tips[0], tips[0].tippee_p, 'stripe', payer, payer.guessed_country, - tips[0].periodic_amount + tips[0].amount * 52 ) if len(proto_transfers) == 1: if proto_transfers[0].destination.country not in constants.SEPA: