Skip to content

Commit 4249ae4

Browse files
authored
Merge pull request #616 from makerspace/aron/stripe_exceptio
Handle stripe exception.
2 parents f049e4e + 5a12718 commit 4249ae4

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

api/src/shop/stripe_payment_intent.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from enum import Enum
55
from logging import getLogger
66
from typing import Dict, List, Optional
7+
from zoneinfo import ZoneInfo
78

89
import stripe
910
from dataclasses_json import DataClassJsonMixin
@@ -13,7 +14,6 @@
1314
from service.error import EXCEPTION, BadRequest, InternalServerError
1415
from stripe import CardError, InvalidRequestError, PaymentIntent, StripeError
1516
from typing_extensions import Never
16-
from zoneinfo import ZoneInfo
1717

1818
from shop.models import StripePending, Transaction
1919
from shop.stripe_constants import (
@@ -202,5 +202,12 @@ def pay_with_stripe(
202202
f"created stripe payment_intent for transaction {transaction.id}, payment_intent id {payment_intent.id}"
203203
)
204204
return payment_intent
205+
except CardError as e:
206+
# Reason can be for example: 'Your card was declined. This transaction requires authentication'.
207+
# It seems weird that it fails already when trying to create the payment intent, but it has been observed.
208+
commit_fail_transaction(transaction)
209+
err = PaymentFailed(log=f"Payment failed: {str(e)}", level=EXCEPTION)
210+
err.message = e.user_message
211+
raise err
205212
except InvalidRequestError as e:
206213
raise_from_stripe_invalid_request_error(e)

0 commit comments

Comments
 (0)