Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe v3 setup instructions #389

Open
RyanHope opened this issue Nov 10, 2023 · 11 comments
Open

Stripe v3 setup instructions #389

RyanHope opened this issue Nov 10, 2023 · 11 comments

Comments

@RyanHope
Copy link

The Stripe v3 provider documentation is lacking some key details to get setup. Nowhere does it say where to obtain the endpoint secret. From other similar questions on stack overflow I was able to figure out that I need to setup a webhook on Stripe's website. This process requires specifying a url for the endpoint. What is this url? Nothing in the docs say what to specify here. Also, what events does this endpoint need to listen to for the v3 provider to work properly?

@RyanHope
Copy link
Author

Ahh, maybe there should be a link in the Stripe provider section to this: https://django-payments.readthedocs.io/en/latest/webhooks.html

@RyanHope
Copy link
Author

Even after reading the webooks page linked above there are still questions about the endpoint.

Make sure to replace https://your-app.com/payments/stripe/ with the actual URL for your Stripe webhook endpoint.

What is providing the "payments/stripe" url? The only urls.py file is see in this project repo is this:

urlpatterns = [
    path("process/<uuid:token>/", process_data, name="process_payment"),
    re_path(
        r"^process/(?P<variant>[a-z-]+)/$",
        static_callback,
        name="static_process_payment",
    ),
]

with no mention of stripe.

@Ivan-Vinitskyi
Copy link

I am on the same problem now. And besides V3 isn't implemented yet on the last release version (2.0.0)

@mariofix
Copy link
Member

@RyanHope you are right, there is no where in the docs explaining where and how the Webhook URL is created, i assigned myself to correct that.

In the meantime the webhook URL is made from 3 settings:

  • PAYMENT_USES_SSL
  • PAYMENT_HOST
  • and the index from PAYMENT_VARIANTS

if you have these settings

PAYMENT_USES_SSL=true
PAYMENT_HOST="example.com"
PAYMENT_VARIANTS= {"default": ("payments.stripe.StripeProviderV3", {}),}

the webhook URL for that particular provider is https://example.com/payments/default

As for the endpoint secret, Stripe has a very comprehensive documentation regarding their systems, you can find instructions here https://stripe.com/docs/webhooks#secure-webhook

@RyanHope
Copy link
Author

I have my project setup to use StripeV3 as directed above. When I go to click on my Stripe payment button (which was working with the older stripe module), I get this error from Django "Request req_Hih7ydsuAFVjrG: Not a valid URL". In the Stripe developer logs I see this error:
image

I am assuming something needs to change in my Payment model and the payment_success handler but the answer is not immediately obvious.

@Ivan-Vinitskyi
Copy link

image

Try to save real url path (it's a pitty but it should be a full path, and I can't setup relative one) for success and failed url fields in the Payment instance.
For testing I setup two views with urls for
"\success" and "\fail" with simple htmls. But in production our FE team setup urls to be redirected to in real time.

@RyanHope
Copy link
Author

image

Try to save real url path (it's a pitty but it should be a full path, and I can't setup relative one) for success and failed url fields in the Payment instance. For testing I setup two views with urls for "\success" and "\fail" with simple htmls. But in production our FE team setup urls to be redirected to in real time.

I have no idea what you are telling me to do.

@RyanHope
Copy link
Author

RyanHope commented Jan 26, 2024

Here is the stacktrace from django...

[26/Jan/2024 16:27:48] "GET /invoice/cart/ HTTP/1.1" 200 11429
Internal Server Error: /invoice/cart/
Traceback (most recent call last):
  File ".venv\lib\site-packages\payments\stripe\providers.py", line 141, in create_session
    return stripe.checkout.Session.create(**session_data)
  File ".venv\lib\site-packages\stripe\api_resources\abstract\createable_api_resource.py", line 16, in create
    return cls._static_request(
  File ".venv\lib\site-packages\stripe\api_resources\abstract\api_resource.py", line 139, in _static_request
    response, api_key = requestor.request(method_, url_, params, headers)
  File ".venv\lib\site-packages\stripe\api_requestor.py", line 122, in request
    resp = self.interpret_response(rbody, rcode, rheaders)
  File ".venv\lib\site-packages\stripe\api_requestor.py", line 399, in interpret_response
    self.handle_error_response(rbody, rcode, resp.data, rheaders)
  File ".venv\lib\site-packages\stripe\api_requestor.py", line 159, in handle_error_response
    raise err
stripe.error.InvalidRequestError: Request req_6p7s8xHEDDVOrc: Not a valid URL

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File ".venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ".venv\lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File ".venv\lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File ".venv\lib\site-packages\django\views\decorators\cache.py", line 62, in _wrapper_view_func
    response = view_func(request, *args, **kwargs)
  File ".venv\lib\site-packages\django\contrib\auth\mixins.py", line 73, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File ".venv\lib\site-packages\django\views\generic\base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "website-django\invoice\views.py", line 126, in post
    payment.get_form()
  File ".venv\lib\site-packages\payments\models.py", line 144, in get_form
    return provider.get_form(self, data=data)
  File ".venv\lib\site-packages\payments\stripe\providers.py", line 103, in get_form
    raise pe
  File ".venv\lib\site-packages\payments\stripe\providers.py", line 100, in get_form
    session = self.create_session(payment)
  File ".venv\lib\site-packages\payments\stripe\providers.py", line 144, in create_session
    raise PaymentError(e) from e
payments.PaymentError: Request req_6p7s8xHEDDVOrc: Not a valid URL
[26/Jan/2024 16:27:52] "POST /invoice/cart/ HTTP/1.1" 500 202538

@RyanHope
Copy link
Author

Making progress... I understand the previous comment. my success and failure handlers were returning relative paths so I switched them up like this:

    def get_failure_url(self):
        url = f"{settings.PAYMENT_HOST}{reverse('payment-failure', args=[self.pk])}"
        if settings.PAYMENT_USES_SSL:
            return f"https://{url}"
        return f"http://{url}"

    def get_success_url(self):
        url = f"{settings.PAYMENT_HOST}{reverse('payment-success', args=[self.pk])}"
        if settings.PAYMENT_USES_SSL:
            return f"https://{url}"
        return f"http://{url}"

and now I am at the new stripe payment portal.

@RyanHope
Copy link
Author

the webhook URL for that particular provider is https://example.com/payments/default

this info appears to be wrong, it seems the correct answer is

https://example.com/payments/process/default

@Ivan-Vinitskyi
Copy link

Actually Stripe webhook could be set to any of your home urls. It should be static, something like
"... process<your custom variant name>"
but by default it is "...payments\process\stripe"
and variant name is taken from
Payment instance field "variant".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants