courtneypattison / thegrouch

Stripe integrated with django-oscar, django-oscar-api and a basic react app bootstrapped with Vite
MIT License
1 stars 1 forks source link

webhook, missing basket #3

Open j2l opened 5 months ago

j2l commented 5 months ago

Hello, It looks like "Basket" is missing:

Internal Server Error: /stripe/webhook/
Traceback (most recent call last):
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/oscar/apps/checkout/session.py", line 68, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/thegrouch/thegrouch/apps/stripe/views.py", line 35, in post
    basket = Basket.objects.get(pk=self.checkout_session.get_submitted_basket_id())
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/pm/Documents/github/django_tests/thegrouch-stripe-restapi/.venv/lib/python3.10/site-packages/django/db/models/query.py", line 637, in get
    raise self.model.DoesNotExist(
oscar.apps.basket.models.Basket.DoesNotExist: Basket matching query does not exist

Also in the Stripe Received Events, there's no sign of Basket.

I'm using the latest API version (2023-10-16), Django 4.2.9, Django-Oscar 3.2.2, stripe 8.0.0 Which versions work for you? Any idea what is wrong in my case?

courtneypattison commented 5 months ago

Thank you so much for letting me know about the db address! Super sloppy of me.

I don't think I can be much help. This project was for a take home interview so I rushed through it and I have no previous experience with Django or Stripe. I put it in GitHub in the hopes that it might help someone else since the examples I found used Stripe's old API. It seems like it's causing you more trouble than good though!

It works for me with Django 3.2.21, Django-Oscar 3.2 and Stripe 6.5.0.

I'm probably way off, but maybe make sure your events are getting correctly forwarded to your webhook.

j2l commented 5 months ago

Thank you very much for the honest review of your process and sharing this project. It actually does help me a lot to learn Django-Oscar. I found too that other D-O projects are using the old stripe API, or decommissioned libraries (paypal), which is weird.

Does the webhook currently fully work for you? No error about Basket? If so, could you please do a pip freeze > thegrouch-requirements.txt and post it here?

j2l commented 5 months ago

Since I'm learning, and you seem to be a wiz with python (you've done it without knowing Django and Oscar, I couldn't have done this project from scratch with my knowledge), may I ask how you approached it? From the stripe API code (for flask)? From another project? From Oscar libraries? What were the steps to get to this result? I'm asking all this because I'm not sure I found the right way to approach these frameworks. Information is scattered or old, even doc is outdated.

j2l commented 5 months ago

Hello and sorry to bother you, does the webhook currently fully work for you? No error about Basket?

courtneypattison commented 5 months ago

Hey @j2l, sorry it has taken me so long to get back to you. I've been quite busy.

The webhook does currently work for me. Here's the log from me running it.

thegrouch/🐝🐈 ./manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 06, 2024 - 17:45:40
Django version 3.2.21, using settings 'thegrouch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[06/Feb/2024 17:45:44] "GET / HTTP/1.1" 302 0
[06/Feb/2024 17:45:44] "GET /catalogue/ HTTP/1.1" 200 11749
[06/Feb/2024 17:45:44] "GET /media/cache/fa/3b/fa3b21e01d67e8ed5d82d92b9b29d4f6.jpg HTTP/1.1" 200 29032
[06/Feb/2024 17:45:44] "GET /media/cache/fa/57/fa571b6bc263ec73c7811b6d878eaa98.jpg HTTP/1.1" 200 14641
[06/Feb/2024 17:45:44] "GET /static/oscar/css/styles.css HTTP/1.1" 200 294186
[06/Feb/2024 17:45:44] "GET /static/oscar/js/bootstrap4/bootstrap.bundle.min.js HTTP/1.1" 200 83376
[06/Feb/2024 17:45:44] "GET /static/oscar/webfonts/fa-solid-900.woff2 HTTP/1.1" 200 154840
[06/Feb/2024 17:45:47] "POST /basket/add/3/ HTTP/1.1" 302 0
[06/Feb/2024 17:45:47] "GET /catalogue/ HTTP/1.1" 200 14163
[06/Feb/2024 17:45:47] "GET /media/cache/49/58/49586e63f43191d1734b0fb695bf880c.jpg HTTP/1.1" 200 4609
[06/Feb/2024 17:45:49] "GET /basket/ HTTP/1.1" 200 14974
[06/Feb/2024 17:45:51] "GET /checkout/ HTTP/1.1" 302 0
[06/Feb/2024 17:45:52] "GET /accounts/login/?next=/checkout/ HTTP/1.1" 200 13358
[06/Feb/2024 17:45:59] "POST /accounts/login/?next=/checkout/ HTTP/1.1" 200 13740
[06/Feb/2024 17:46:16] "POST /accounts/login/?next=/checkout/ HTTP/1.1" 200 14044
[06/Feb/2024 17:46:34] "POST /accounts/login/?next=/checkout/ HTTP/1.1" 302 0
[06/Feb/2024 17:46:34] "GET /checkout/ HTTP/1.1" 302 0
[06/Feb/2024 17:46:34] "GET /checkout/shipping-address/ HTTP/1.1" 200 24248
[06/Feb/2024 17:46:42] "POST /checkout/shipping-address/ HTTP/1.1" 302 0
[06/Feb/2024 17:46:42] "GET /checkout/shipping-method/ HTTP/1.1" 302 0
[06/Feb/2024 17:46:42] "GET /checkout/payment-method/ HTTP/1.1" 302 0
[06/Feb/2024 17:46:42] "GET /checkout/payment-details/ HTTP/1.1" 200 3648
[06/Feb/2024 17:46:45] "POST /checkout/preview/ HTTP/1.1" 200 11618
[06/Feb/2024 17:46:49] "POST /checkout/preview/ HTTP/1.1" 302 0
[06/Feb/2024 17:48:25] "GET /checkout/thank-you/ HTTP/1.1" 302 0
[06/Feb/2024 17:48:25] "GET /catalogue/ HTTP/1.1" 200 12137
[06/Feb/2024 17:48:25] "GET /media/cache/fa/3b/fa3b21e01d67e8ed5d82d92b9b29d4f6.jpg HTTP/1.1" 200 29032
[06/Feb/2024 17:48:25] "GET /media/cache/fa/57/fa571b6bc263ec73c7811b6d878eaa98.jpg HTTP/1.1" 200 14641
[06/Feb/2024 17:48:25] "GET /static/oscar/webfonts/fa-regular-400.woff2 HTTP/1.1" 200 24028
[06/Feb/2024 17:48:29] "GET /static/oscar/js/bootstrap4/bootstrap.bundle.min.js.map HTTP/1.1" 404 1900
[06/Feb/2024 17:48:29] "GET /static/oscar/css/styles.css.map HTTP/1.1" 200 509158

thegrouch/🐝🐈 stripe trigger payment_intent.succeeded
A newer version of the Stripe CLI is available, please update to: v1.19.2
Setting up fixture for: payment_intent
Running fixture for: payment_intent
Trigger succeeded! Check dashboard for event details.

Also, here are my frozen requirements:

asgiref==3.7.2
Babel==2.12.1
bleach==6.0.0
certifi==2023.7.22
charset-normalizer==3.2.0
coverage==7.3.0
distlib==0.3.7
Django==3.2.20
django-extra-views==0.14.0
django-haystack==3.2.1
django-oscar==3.2
django-phonenumber-field==6.4.0
django-tables2==2.3.4
django-treebeard==4.7
django-widget-tweaks==1.5.0
djangorestframework==3.14.0
docutils==0.20.1
easy-thumbnails==2.8.5
factory-boy==3.2.1
Faker==19.3.1
filelock==3.12.3
idna==3.4
importlib-metadata==6.8.0
jaraco.classes==3.3.0
keyring==24.2.0
markdown-it-py==3.0.0
mdurl==0.1.2
mock==5.1.0
more-itertools==10.1.0
pbr==5.11.1
phonenumbers==8.13.19
Pillow==10.0.0
pkginfo==1.9.6
platformdirs==3.10.0
purl==1.6
Pygments==2.16.1
python-dateutil==2.8.2
pytz==2023.3
readme-renderer==41.0
requests==2.31.0
requests-toolbelt==1.0.0
rfc3986==2.0.0
rich==13.5.2
six==1.16.0
sqlparse==0.4.4
stevedore==5.1.0
twine==4.0.2
urllib3==2.0.4
virtualenv==20.24.3
virtualenv-clone==0.5.7
virtualenvwrapper==4.8.4
webencodings==0.5.1
zipp==3.16.2

I wish I had more time to help you debug the issue, but I have too much to do at the moment. If you do solve the problem, I'd really appreciate you making a PR to help someone else in the future.

Lol, I'm a beginner Python developer and also found the documentation frustrating.

  1. I went through the django-oscar building your own shop tutorial
  2. Read through the django-oscar-stripe-sca code
  3. Read the PaymentDetailsView source and found there was a RedirectRequired error
  4. Found a django-oscar-gocardless uses the RedirectRequired
  5. Did a mashup of django-oscar-stripe-sca and django-oscar-gocardless with stripe webhooks after reading more about the Stripe API from their documentation

Overall I found the comments in oscar-django's code more useful than the documentation!

j2l commented 5 months ago

Thank you so much @courtneypattison !! This is so detailed and helpful. I will study all this closely (tomorrow, it's 8pm in France) and let you know. I'm pretty sure I missed a tiny thing.

Good luck with all you have to do!

j2l commented 5 months ago

I updated, package by package (downgraded from Django 4.2.9 to 3.2.20, downgraded oscar, ...) flushed the db, made migrations, migrated, runserver, ... no luck so far but a couple of things:

It looks like there are 2 routes, depending whether you run (in a different bash, while server is running) stripe listen --forward-to 127.0.0.1:8000/stripe/webhook/ or not.

If you do, like me (to write events in stripe local webhook log), you should get the call to the webhook, followed by the basket error:

[07/Feb/2024 11:49:29] "POST /checkout/preview/ HTTP/1.1" 302 0
[07/Feb/2024 11:49:48] "POST /stripe/webhook/ HTTP/1.1" 200 0
...
oscar.apps.basket.models.Basket.DoesNotExist: Basket matching query does not exist.

If you don't run it, it is like your log:

[07/Feb/2024 11:53:36] "POST /checkout/preview/ HTTP/1.1" 302 0
[07/Feb/2024 11:53:58] "GET /checkout/thank-you/ HTTP/1.1" 302 0
[07/Feb/2024 11:53:58] "GET /catalogue/ HTTP/1.1" 200 10081

No error ... but no order passed (at least for me).

Can you check if any recent order was created for you in the dashboard (http://localhost:8000/dashboard/orders/) or django admin (http://localhost:8000/admin/order/order/)?

If not, there's something bypassing the webhook to go straight to the thank-you page (that doesn't exist, 302).

courtneypattison commented 5 months ago

@j2l You're right! I forgot about setting up the stripe listener. I'm getting the same error message. Looks like I never had it configured correctly. I have no time to get it working. Thank you for your help and sorry for the trouble.

j2l commented 5 months ago

OK, I understand. I don't want to bother you. When you have 5min: May I just ask how did you get the order in the first place?