New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Card matching query does not exist on Customer.add_card from stripe token from stripe checkout.js #1838
Comments
Can you replicate this in 2.7.2? |
I'll try this and see if that works. I'll update soon |
@jleclanche Just updated to the latest version. I can confirm that the bug is still present. Still appears to be happening when the add card action is executing at the same time as a webhook is being received. The trace was slightly different this time, and appears to also include some webhook exceptions:
|
@martinmain93 Are you able to reliably reproduce this? It seems like a race condition to me. In production stripe retries webhooks several times so this usually is not a blocker for most. |
@arnav13081994 I am not able to reliably reproduce, but it’s happened on our production site about 50 times over the past month, resulting in lost revenue for our business. The webhooks get errors that we ignore all the time, as is well documented, and usually after one or two retries they come through without issue. The problem here is that the add_card method is failing. I don’t care about the webhooks failing. I am concerned about the add_card method. |
@martinmain93 I will take a look at this issue and see what can be done. Is this happening when you create a new customer or for returning customers as well? I think what perhaps is causing this is Card and Customer objects getting created at around the same time. This causes the error as the Card needs to be on the Customer (which needs to exist before card can be added). |
@arnav13081994 This is only happening with new users. The customer is created immediately before the card is added |
@arnav13081994 I've added a simple method to 'retry until success' which will literately attempt the add_card method until it goes through. It seems to be resolving the issue for now. Do you foresee any issues with this? I believe Djstripe uses idempotency keys internally so it shouldn't make any erroneous duplicate actions. Also, we have noticed similar webhook munging issue in a few other places in our app. Particularly for the 'change_card' action, as well as the 'subscribe' action. The 'retry until success' thing seems to be working for now |
For reference: def keep_trying(func, *args, max_tries=10, **kwargs):
"""
Utility function to repeatedly attempt a function
"""
tries = 0
while True:
tries += 1
try:
return func(
*args,
**kwargs
)
except Exception as e:
if tries < max_tries:
logger.warning(f"{str(func)} attempt failed. Retrying...")
time.sleep(1)
else:
logger.warning(f"{str(func)} attempt failed {max_tries} times. Giving up now..")
raise e
# Use like:
keep_trying(customer.add_card, stripe_token) |
@martinmain93 I think that's the best you can do at the moment. But please test thoroughly to ensure there are no unwarranted side effects. @jleclanche Perhaps we can add a default retry mechanism for all methods using idempotency keys? |
@arnav13081994 So this has fixed most of our issues. Now, we are occasionally getting the |
@martinmain93 Could you please post some traceback as it's very difficult for me to help without that. |
Here's the traceback. In this case, I've added a workaround by detecting if this specific error has been raised, and then trying to detect if the customer has a valid source. If the user has a valid source (after retrying a few times), then we ignore the error.
There must have been something that changed between Djstripe 2.5 and 2.6. Our system seems to be hacked-together working for now, with lots of error catching and retrying, but our server logs are crammed with Webhook error messages every time anyone does anything stripe related. |
|
Describe the bug
I'm running an app which uses stripe checkout in the front end (Source imported from here https://checkout.stripe.com/v2/checkout.js ), in which the user enters their credit card information and then Stripe provides a token which is passed to our API for further processing. On our server we use DJstripe to create a customer and then call
add_card
with the token from the stripe checkout to add the user's card. Since upgrading to DJstripe 2.6.2 (Previously 2.5.1), we sometimes run into adjstripe.models.payment_methods.Card.DoesNotExist: Card matching query does not exist
error when attempting to add the card. Before the upgrade our system has processed thousands of payments with this same set up with no issues. Typically we are seeing this problem in about 1 in 5 payment attempts.From our server logs, I can see that when the error occurs, a number of webhooks are received and processed between the time that the
add_card
action is started and when the error trace is printed out. One of the webhooks being processed is started before the error trace, and completes after the error trace, so it really seems like some sort of race condition problem related to the webhooks. In some cases, the webhooks all complete without errors, and in other cases, the webhook handler also raises a similarCard.DoesNotExist
errorFull trace:
To Reproduce
I have been unable to reliably reproduce
Software versions
The text was updated successfully, but these errors were encountered: