Skip to content

Commit b90061c

Browse files
Ben CreechH--o-l
Ben Creech
authored andcommitted
Fail if no payment method on confirm
I added the ability to add a payment method by ID on confirm here: #226 Per testing with the actual Stripe API, and common sense, we should return a 400 error if neither payment_method nor payment_method_data are supplied to the confirm request (the point of the confirm is to add a payment method, so it makes no sense to call confirm with no payment method). So I added validation and changed an old test case that seemed to be asserting a behavior unlike what Stripe actually does. This also addresses a one-liner review comment from this PR: #226 I was resetting payment_method_types within the confirm handler. Upon testing, this is not what the real Stripe API does, so I removed that recent addition.
1 parent 868974b commit b90061c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

localstripe/resources.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2769,16 +2769,18 @@ def _api_confirm(cls, id, use_stripe_sdk=None, client_secret=None,
27692769
pm = PaymentMethod(**payment_method_data)
27702770
obj._attach_pm(pm)
27712771
elif obj.payment_method is None:
2772-
obj.status = 'requires_payment_method'
2773-
obj.next_action = None
2772+
# If no payment method was specified upon SetupIntent creation, and
2773+
# none was specified in the confirm request, there's nothing to
2774+
# confirm. Stripe returns a 400 error in this case:
2775+
raise UserError(400, 'Bad request')
27742776
else:
27752777
obj.status = 'succeeded'
27762778
obj.next_action = None
2779+
27772780
return obj
27782781

27792782
def _attach_pm(self, pm):
27802783
self.payment_method = pm.id
2781-
self.payment_method_types = [pm.type]
27822784

27832785
if pm._attaching_is_declined():
27842786
self.status = 'canceled'

test.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,14 @@ res=$(curl -sSfg -u $SK: $HOST/v1/setup_intents -X POST)
598598
seti=$(echo "$res" | grep '"id"' | grep -oE 'seti_\w+' | head -n 1)
599599
seti_secret=$(echo $res | grep -oE 'seti_\w+_secret_\w+' | head -n 1)
600600

601-
curl -sSfg -u $SK: $HOST/v1/setup_intents/$seti/confirm -X POST
601+
# If there's no payment_method in the either the SetupIntent creation or the
602+
# confirm call, the confirm call fails:
603+
code=$(curl -sg -o /dev/null -w '%{http_code}' -u $SK: \
604+
-X POST $HOST/v1/setup_intents/$seti/confirm)
605+
[ "$code" -eq 400 ]
606+
607+
curl -sSfg -u $SK: $HOST/v1/setup_intents/$seti/confirm -X POST \
608+
-d payment_method=pm_card_visa
602609

603610
curl -sSfg -u $SK: $HOST/v1/setup_intents/$seti/cancel -X POST
604611

0 commit comments

Comments
 (0)