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

djstripe.exceptions.InvalidStripeAPIKey: Invalid API key: '' when attaching a payment method #2037

Open dbartenstein opened 1 month ago

dbartenstein commented 1 month ago

Describe the bug As described here https://dj-stripe.dev/dj-stripe/2.7/api_keys/, we got rid of STRIPE_LIVE_SECRET_KEY and STRIPE_TEST_SECRET_KEY.

However when calling Customer.add_payment_method, PaymentMethod.attach is called without explicit api_key param.

Fallback is djstripe_settings.STRIPE_SECRET_KEY, which will be "" in our case: https://github.com/dj-stripe/dj-stripe/blob/dbe360255a2d8385bb1004fc162c95ad23eb2a8e/djstripe/models/payment_methods.py#L1168

A clear and concise description of what the bug is.

To Reproduce

*** djstripe.exceptions.InvalidStripeAPIKey: Invalid API key: ''

Software versions

dbartenstein commented 1 month ago

Possible fix: payment_method = PaymentMethod.attach(payment_method, stripe_customer, api_key=self.default_api_key) https://github.com/dj-stripe/dj-stripe/blob/dbe360255a2d8385bb1004fc162c95ad23eb2a8e/djstripe/models/core.py#L1164