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.

When a Price nickname attribute is set, the wrong id is sent in request

See original GitHub issue

Describe the bug

When the price has the nickname attribute set to a string like "Test Monthly" , and I try to create a checkout session, I get a stack trace like the following

[2021-08-03 18:01:56,317] ERROR main.views 94 create_checkout_session Creating checkout session failed
Internal Server Error: /stripe/create-checkout-session/
Traceback (most recent call last):
  File "/home/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/psacawa/.pyenv/versions/lektprojekt/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/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/sentry_sdk/integrations/django/views.py", line 67, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
  File "/home/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "/home/psacawa/Kody/lektprojekt/backend/main/views.py", line 95, in create_checkout_session
    raise (e)
  File "/home/psacawa/Kody/lektprojekt/backend/main/views.py", line 64, in create_checkout_session
    stripe_session = stripe.checkout.Session.create(
  File "/home/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/stripe/api_resources/abstract/createable_api_resource.py", line 22, in create
    response, api_key = requestor.request("post", url, params, headers)
  File "/home/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/stripe/api_requestor.py", line 122, in request
    resp = self.interpret_response(rbody, rcode, rheaders)
  File "/home/psacawa/.pyenv/versions/lektprojekt/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/psacawa/.pyenv/versions/lektprojekt/lib/python3.9/site-packages/stripe/api_requestor.py", line 159, in handle_error_response
    raise err
stripe.error.InvalidRequestError: Request req_nufdnsJ5VxmFd9: No such price: 'Test Monthly'

So it seems it is applying str() to the price to find the data for the request?

To Reproduce

stripe price update price_123myprice123 --nickname "Some name"
./manage.py djstripe_sync_models price

Then in the code somewhere:

import stripe
...
price = "price_123myprice123"

stripe_session = stripe.checkout.Session.create(
    success_url="success_url" ,
    cancel_url="cancel_url"
    payment_method_types=["card"],
    mode="subscription",
    metadata={},
    line_items=[{"price": price, "quantity": 1}],
)

Software versions

  • Dj-Stripe version: 2.5.1
  • python-stripe vrsion: 2.60.0
  • Python version: 3.9
  • Django version: 3.2.6
  • Stripe API version: 2020-08-27
  • Database type and version: postgres13

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ob-stripecommented, Aug 23, 2021

Sorry for the delayed reply, I was OOO until today.

The issue is that you’re trying to use the price’s nickname in place of its id. When creating a Checkout Session, Stripe’s API expects the line_items.price argument to be a price ID.

E.g:

>>> price = stripe.Price.retrieve("price_123")


>>> price.id
'price_123'


>>> price.nickname
'Test Monthly'


>>> stripe_session = stripe.checkout.Session.create(
...     success_url="https://stripe.com",
...     cancel_url="https://stripe.com",
...     payment_method_types=["card"],
...     mode="subscription",
...     metadata={},
...     line_items=[{"price": price.nickname, "quantity": 1}],
... )
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "/Users/ob/.pyenv/versions/3.7.3/lib/python3.7/site-packages/stripe/api_resources/abstract/createable_api_resource.py", line 22, in create
    response, api_key = requestor.request("post", url, params, headers)
  File "/Users/ob/.pyenv/versions/3.7.3/lib/python3.7/site-packages/stripe/api_requestor.py", line 122, in request
    resp = self.interpret_response(rbody, rcode, rheaders)
  File "/Users/ob/.pyenv/versions/3.7.3/lib/python3.7/site-packages/stripe/api_requestor.py", line 373, in interpret_response
    self.handle_error_response(rbody, rcode, resp.data, rheaders)
  File "/Users/ob/.pyenv/versions/3.7.3/lib/python3.7/site-packages/stripe/api_requestor.py", line 152, in handle_error_response
    raise err
stripe.error.InvalidRequestError: Request req_123: No such price: 'Test Monthly'

Request req_123: No such price: 'Test Monthly'


>>> stripe_session = stripe.checkout.Session.create(
...     success_url="https://stripe.com",
...     cancel_url="https://stripe.com",
...     payment_method_types=["card"],
...     mode="subscription",
...     metadata={},
...     line_items=[{"price": price.id, "quantity": 1}],
... )


>>> stripe_session.id
'cs_test_123'
1reaction
jleclanchecommented, Aug 4, 2021

cc @ob-stripe - I agree this is unlikely to be coming from dj-stripe. Do you recognize this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

QuerySet, Object has no attribute id - Django - Stack Overflow
I was getting this error because I wanted to create session from email id but it shows their is no attribute email so...
Read more >
The price object – curl - Stripe API reference
nickname string. A brief description of the price, hidden from customers. product ...
Read more >
MailNickname or Alias attribute in Exchange Online doesn't ...
To resolve this issue, update the Alias or Mailnickname attribute. To do this, use one of the following methods. Method 1: Use Exchange ......
Read more >
Install and Configure the NPS Extension for Azure MFA
There is one event you will always see is: “NPS Extension for Azure MFA: Radius request is missing NAS Identifier and Nas IpAddress...
Read more >
Change the Attributes of an Amazon Cognito User Pool
Note: You can add custom attributes to an existing user pool, but these attributes aren't required for user registration. Resolution. Set up a ......
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