1

I have been trying to generating stripe payment intent but I see this error of invalid Integer

Traceback (most recent call last):

  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 472, in thread_handler

    raise exc_info[1]

  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 42, in inner

    response = await get_response(request)

  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 253, in _get_response_async

    response = await wrapped_callback(

  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 435, in __call__

    ret = await asyncio.wait_for(future, timeout=None)

  File "/usr/local/lib/python3.8/asyncio/tasks.py", line 455, in wait_for

    return await fut

  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run

    result = self.fn(*self.args, **self.kwargs)

  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 476, in thread_handler

    return func(*args, **kwargs)

  File "/usr/local/lib/python3.8/contextlib.py", line 75, in inner

    return func(*args, **kwds)

  File "/usr/local/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view

    return view_func(*args, **kwargs)

  File "/usr/local/lib/python3.8/site-packages/django/views/generic/base.py", line 84, in view

    return self.dispatch(request, *args, **kwargs)

  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch

    response = self.handle_exception(exc)

  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception

    self.raise_uncaught_exception(exc)

  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception

    raise exc

  File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch

    response = handler(request, *args, **kwargs)

  File "/yacht-away/bookings/views.py", line 94, in post

    print(payment.create_payment_intent())

  File "/yacht-away/bookings/payments.py", line 14, in create_payment_intent

    payment = stripe.PaymentIntent.create(

  File "/usr/local/lib/python3.8/site-packages/stripe/api_resources/abstract/createable_api_resource.py", line 22, in create

    response, api_key = requestor.request("post", url, params, headers)

  File "/usr/local/lib/python3.8/site-packages/stripe/api_requestor.py", line 122, in request

    resp = self.interpret_response(rbody, rcode, rheaders)

  File "/usr/local/lib/python3.8/site-packages/stripe/api_requestor.py", line 399, in interpret_response

    self.handle_error_response(rbody, rcode, resp.data, rheaders)

  File "/usr/local/lib/python3.8/site-packages/stripe/api_requestor.py", line 159, in handle_error_response

    raise err

stripe.error.InvalidRequestError: Request req_4TngiwH4P1ztKp: Invalid integer: {:"0"=>"2400"}

I have been following this documentation and wrote the payment class like this -

class Payments:
    def __init__(self, amount):
        self.amount = amount,
        self.currency = "sgd",
        self.automatic_payment_methods = {"enabled": True}

        stripe.api_key = "sk_test_51LAvboSE9yL96I7XGzEIe7GQyAPWbVHMBULDK4yw0raYGn2NF4ksodn8nF7V5HKtKNiPMnOPOCd1neNcqolGXPjG00vCJUTtuX"

    def create_payment_intent(self):
        payment = stripe.PaymentIntent.create(
            payment_method_types=["paynow"],
            payment_method_data={"type": "paynow"},
            amount=self.amount,
            currency=self.currency,
        )
        print(payment)

And this is how I have been invoking the payment intent method -

payment = Payments(
            amount=int(bill["payable_amount"]) * 100,
        )

Where am I going wrong? The code looks good to me and I am using the same api_key as given in the documentation.

Jeet Patel
  • 1,140
  • 18
  • 51
  • attach your code, and take a closer look to `/yacht-away/bookings/payments.py", line 14` and `/yacht-away/bookings/views.py", line 94` – oruchkin Jun 18 '22 at 11:20

1 Answers1

1

At the bottom of your question, you shared the request id req_4TngiwH4P1ztKp which can be loaded directly in your Stripe Dashboard here: https://dashboard.stripe.com/test/logs/req_4TngiwH4P1ztKp

The error message is also trying to show what you did wrong as it reads

Invalid integer: {:"0"=>"2400"}

This usually means that you're not passing the parameter the way the API expects it. Somewhere, it expects an integer but instead you passed a hash where the integer is associated with the 0 key. The only parameter your code is sending that should be an integer is amount. This means that your self.amount right now, instead of being 2400 is { 0: '2400'} likely because of the way you send it from your client. This is the part you want to debug.

I also recommend carefully reading Stripe's documentation on error handling as they cover the basics of their API in all languages. Catching the error cleanly in your code would allow you to immediately see the error and where it comes from.

koopajah
  • 23,792
  • 9
  • 78
  • 104