dj-stripe / dj-stripe

dj-stripe automatically syncs your Stripe Data to your local database as pre-implemented Django Models allowing you to use the Django ORM, in your code, to work with the data making it easier and faster.
https://dj-stripe.dev
MIT License
1.56k stars 474 forks source link

The stripe api key placement is very confusing #2055

Closed jTiKey closed 4 days ago

jTiKey commented 1 week ago

So, your docs say to put them in .env. If you do that, the server start showing warnings:

?: (djstripe.I002) Your keys are defined in the settings files and are also in the admin. You can now add and manage them directly from the django admin.
    HINT: We suggest adding STRIPE_TEST_SECRET_KEY and STRIPE_LIVE_SECRET_KEY directly from the Django Admin. And removing them from the settings files.

If I remove them from the .env then the webhook breaks:

   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/webhooks.py", line 207, in from_request
     stripe_account = StripeModel._find_owner_account(data=data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/base.py", line 344, in _find_owner_account
     return Account.get_or_retrieve_for_api_key(api_key)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/account.py", line 147, in get_or_retrieve_for_api_key
    apikey_instance, _ = APIKey.objects.get_or_create_by_api_key(api_key)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 42, in get_or_create_by_api_key
     key_type, livemode = get_api_key_details_by_prefix(secret)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 28, in get_api_key_details_by_prefix
     raise InvalidStripeAPIKey(f"Invalid API key: {api_key!r}")
 djstripe.exceptions.InvalidStripeAPIKey: Invalid API key: ''`

Software versions

Dj-Stripe version: 2.8.4 Python version: 3.11.8 Django version:4.2.11 Stripe API version: default Database type and version: postgres 15

hyusetiawan commented 4 days ago

I am having the same problem here, the way it's set up is rather confusing and not pythonic (there should only be 1 way of doing things). This also happens for DJSTRIPE_WEBHOOK_SECRET, do we add it in the settings.py or do we add it through webhook here: https://github.com/dj-stripe/dj-stripe/blob/master/docs/usage/webhooks.md?

jleclanche commented 4 days ago

The settings are deprecated and they're only there for compatibility with older versions. Everything is in database.

jTiKey commented 4 days ago

@jleclanche What is this AI generated response? Do you care to read the whole thing, not only one line? If I used my time to report a bug, why can't you put some effort into reading issues before closing them?

Your docs say to use the settings for the API keys. The webhook stops functioning if there is no api keys in the settings. If you looks into your source code it literally uses the settings api keys there.

jleclanche commented 4 days ago

@jTiKey The docs are outdated, this is a known issue (#1841) unrelated to this particular issue, sorry for lack of clarity here but please don't be hostile.

In any case, I understand they right now are saying something incorrect on the website - use the in-database api keys and you shouldn't be getting this error. If you are still getting it in that setup, then something else is at play and feel free to reopen.

jleclanche commented 4 days ago

If you looks into your source code it literally uses the settings api keys there.

That part is the legacy compatibility piece of code which creates API keys in database from the settings, if those are set. Make sure they are not set. This code will be gone in 3.0

jTiKey commented 4 days ago

I cannot reopen an issue you closed.

I'm sure they are not set and I'm getting this error:


`File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/webhooks.py", line 207, in from_request
     stripe_account = StripeModel._find_owner_account(data=data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/base.py", line 344, in _find_owner_account
     return Account.get_or_retrieve_for_api_key(api_key)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/account.py", line 147, in get_or_retrieve_for_api_key
    apikey_instance, _ = APIKey.objects.get_or_create_by_api_key(api_key)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 42, in get_or_create_by_api_key
     key_type, livemode = get_api_key_details_by_prefix(secret)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 28, in get_api_key_details_by_prefix
     raise InvalidStripeAPIKey(f"Invalid API key: {api_key!r}")
 djstripe.exceptions.InvalidStripeAPIKey: Invalid API key: ''