question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webhook events for database-stored webhooks seem to be processed using the API key stored in environment

See original GitHub issue

Describe the bug When Stripe calls the webhook URL for a database-stored webhook, the Stripe account seems to be lost on the way. Event.process seems to only receive the JSON body but not the webhook’s Stripe account: https://github.com/dj-stripe/dj-stripe/blob/f27936ca7880e35526417174ce15a15fbd81c42a/djstripe/models/webhooks.py#L315

To Reproduce

  1. Create webhook in Stripe admin
  2. Create SetupIntent in Stripe
  3. See attached error on webhook

Expected behavior When processing the Event the Stripe account (API key) of the webhook has to be used.

ERROR 2022-01-24 21:57:55,700 198796 log Internal Server Error: /stripe/webhook/1113ddf9-eadf-47ed-9a78-************/
Traceback (most recent call last):
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/views.py", line 47, in post
    trigger = WebhookEventTrigger.from_request(
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/models/webhooks.py", line 230, in from_request
    raise e
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/models/webhooks.py", line 216, in from_request
    obj.process(save=False)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/models/webhooks.py", line 315, in process
    self.event = Event.process(self.json_body)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/models/core.py", line 1525, in process
    ret.invoke_webhook_handlers()
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/models/core.py", line 1537, in invoke_webhook_handlers
    webhooks.call_handlers(event=self)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/webhooks.py", line 98, in call_handlers
    handler_func(event=event)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/event_handlers.py", line 315, in other_object_webhook_handler
    _handle_crud_like_event(target_cls=target_cls, event=event)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/event_handlers.py", line 406, in _handle_crud_like_event
    data = target_cls(**kwargs).api_retrieve(stripe_account=stripe_account)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/djstripe/models/base.py", line 196, in api_retrieve
    return self.stripe_class.retrieve(
File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/stripe/api_resources/abstract/api_resource.py", line 12, in retrieve
    instance.refresh()
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/stripe/api_resources/abstract/api_resource.py", line 16, in refresh
    self.refresh_from(self.request("get", self.instance_url()))
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/stripe/stripe_object.py", line 244, in request
    response, api_key = requestor.request(method, url, params, headers)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/stripe/api_requestor.py", line 122, in request
    resp = self.interpret_response(rbody, rcode, rheaders)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/stripe/api_requestor.py", line 399, in interpret_response
    self.handle_error_response(rbody, rcode, resp.data, rheaders)
  File "/home/zemtu/.virtualenvs/django22_py39/lib/python3.9/site-packages/stripe/api_requestor.py", line 159, in handle_error_response
    raise err
stripe.error.InvalidRequestError: Request req_GT4RAUEvbdaybQ: No such setupintent: 'seti_1KLZbqBkCYu9naYUc5aYgwAS'

Environment

  • dj-stripe version: 2.7.0a2
  • Your Stripe account’s default API version: 2020-08-27
  • Database: PostgreSQL 10
  • Python version: 3.9.10
  • Django version: 3.2

Can you reproduce the issue with the latest version of master?

Yes

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dbartensteincommented, Feb 1, 2022

@jleclanche @arnav13081994 With the following changes the events of the other Stripe account worked: https://github.com/dj-stripe/dj-stripe/pull/1575

1reaction
arnav13081994commented, Jan 25, 2022

Thank you for all the information. I’ll try to recreate it. If I can recreate the issue, finding and fixing the root cause should be straightforward.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webhooks API Documentation - Mindbody developer's portal
The Webhooks API uses API keys from your developer account, which are located on the ... When Mindbody sends an event notification to...
Read more >
Storing webhook authentication secrets - Inkit
Learn how to secure your webhook authentication secrets by reading this article. ... give you an overview of how you can establish safety...
Read more >
Setting up your development environment to create a GitHub ...
This guide will walk through the steps needed to configure a GitHub App and run it on a server. GitHub Apps require some...
Read more >
Use webhooks to enable effective TeamDesk integrations
Step 1. New webhook for HelloSign ... First, you have to have an account with HelloSign, sign in and navigate to HelloSign API...
Read more >
Listen for events - Stripe CLI Reference
Receive webhook events from Stripe on your local machine via a direct connection to Stripe's API. The listen command can receive events based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found