Skip to content

Commit 6b0ff20

Browse files
Ben Creechadrienverge
Ben Creech
authored andcommitted
Prevent refunds on a failed charge
1 parent caa0e94 commit 6b0ff20

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

localstripe/resources.py

+2
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,8 @@ def __init__(self, charge=None, payment_intent=None, amount=None,
26072607
charge = payment_intent_obj.latest_charge.id
26082608

26092609
charge_obj = Charge._api_retrieve(charge)
2610+
if charge_obj.status == 'failed':
2611+
raise UserError(400, 'Cannot refund a failed payment.')
26102612

26112613
# All exceptions must be raised before this point.
26122614
super().__init__()

test.sh

+8
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,14 @@ status=$(
856856
| grep -oE '"status": "failed"')
857857
[ -n "$status" ]
858858

859+
# cannot refund a failed charge
860+
code=$(
861+
curl -sg -o /dev/null -w "%{http_code}" \
862+
-u $SK: $HOST/v1/refunds \
863+
-d charge=$charge \
864+
-X POST)
865+
[ "$code" = 400 ]
866+
859867
# create a pre-auth charge, observe 402 response
860868
code=$(
861869
curl -sg -o /dev/null -w "%{http_code}" \

0 commit comments

Comments
 (0)