Skip to content

Commit

Permalink
Prevent refunds on a failed charge
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Creech authored and adrienverge committed Oct 31, 2024
1 parent caa0e94 commit 6b0ff20
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions localstripe/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,8 @@ def __init__(self, charge=None, payment_intent=None, amount=None,
charge = payment_intent_obj.latest_charge.id

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

# All exceptions must be raised before this point.
super().__init__()
Expand Down
8 changes: 8 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,14 @@ status=$(
| grep -oE '"status": "failed"')
[ -n "$status" ]

# cannot refund a failed charge
code=$(
curl -sg -o /dev/null -w "%{http_code}" \
-u $SK: $HOST/v1/refunds \
-d charge=$charge \
-X POST)
[ "$code" = 400 ]

# create a pre-auth charge, observe 402 response
code=$(
curl -sg -o /dev/null -w "%{http_code}" \
Expand Down

0 comments on commit 6b0ff20

Please sign in to comment.