#squirez - Webhook Endpoint

1 messages ยท Page 1 of 1 (latest)

rain abyss
#

Hello!

#

What are you specific questions?

dim frost
#

hello

#

so the question is this

#

ive set up a webhook using stripe CLI

#

and its working perfectly fine

#

now i trying to set up a webhook using a https url

#

but i keep getting this error message

#

AttributeError at /payments/webhook/
'NoneType' object has no attribute 'type'

#

im using python / Django

#

i understand the error message

#

but im not sure why im getting it

#

as this is the same code i've used

#

when i use the webhook via the stripe CLI

rain abyss
#

Can you share the code that's throwing that error?

dim frost
#

sure

#

once second

#

@csrf_exempt
@api_view(['POST'])
@permission_classes([AllowAny])
def webhook(request):
payload = request.body
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None

try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secert
)

except ValueError as e:

Invalid payload

return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "invaild Payload"})
except stripe.error.SignatureVerificationError as e:

Invalid signature

return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "invaild Payload"})
except AttributeError as e:

pass

return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "Attribute Error {}".format(e)})

rain abyss
#

Can you edit your message and put that in a code block with indentation? You can use three backticks (```) to start and end a code block.

dim frost
#

sure

#

@csrf_exempt
@api_view(['POST'])
@permission_classes([AllowAny])
def webhook(request):
payload = request.body
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None

try:
    event = stripe.Webhook.construct_event(
        payload, sig_header, endpoint_secert
    )

except ValueError as e:
    # Invalid payload
    return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "invaild Payload"})
    
except stripe.error.SignatureVerificationError as e: 
    # Invalid signature
    return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "invaild Payload"})
    
except AttributeError as e:
    # pass
    return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "Attribute Error {}".format(e)})
#

is that better

#

?

rain abyss
#

Yep, that's easier to read.

dim frost
#

ok

#

sorry i dont know how to use the three back tickets

rain abyss
#

On your keyboard, the key to the left of the 1 key. If you type that three times you can then write multiline code.

#

For example, like this:

@csrf_exempt
@api_view(['POST'])
@permission_classes([AllowAny])
#

This is how I wrote that:

\`\`\`python
@csrf_exempt
@api_view(['POST'])
@permission_classes([AllowAny])
#

Err, without the backslashes.

dim frost
#

oh ok

rain abyss
#

Which line of that code you shared is throwing the "AttributeError at /payments/webhook/ 'NoneType' object has no attribute 'type'" error?

dim frost
#

so at the moment i'm handling that error message

#

by using the

#
except AttributeError as e:
        # pass
        return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": "Attribute Error {}".format(e)})
#

without this line

#

that error message will appear

rain abyss
#

Can you log out the values for payload and sig_header and make sure they're being set correctly?

dim frost
#

ok sure

#

let me try that

#

this might take me a few minutes as i need to re-upload code into the cloud

dim frost
#

hello again

#

i've got the output

#

'''python

#
2021-10-25T22:45:01.727206842Z Bad Request: /payments/webhook/
2021-10-25T22:45:01.727731845Z payload value is b'{\n "created": 1326853478,\n "livemode": false,\n "id": "evt_00000000000000",\n "type": "payment_intent.created",\n "object": "event",\n "request": null,\n "pending_webhooks": 1,\n "api_version": "2020-08-27",\n "data": {\n "object": {\n "id": "pi_00000000000000",\n "object": "payment_intent",\n "amount": 1000,\n "amount_capturable": 0,\n "amount_received": 0,\n "application": null,\n "application_fee_amount": null,\n "canceled_at": null,\n "cancellation_reason": null,\n "capture_method": "automatic",\n "charges": {\n "object": "list",\n "data": [\n\n ],\n "has_more": false,\n "url": "/v1/charges?payment_intent=pi_1J5AKyImr5PkOfF3q0jGpbTX"\n },\n "client_secret": "pi_1J5AKyImr5PkOfF3q0jGpbTX_secret_lYouigHyrDmSEd0kA9XsU4hQK",\n "confirmation_method": "automatic",\n "created": 1624371144,\n "currency": "pln",\n "customer": null,\n "description": null,\n "invoice": null,\n "last_payment_error": null,\n "livemode": false,\n "metadata": {\n },\n "next_action": null,\n "on_behalf_of": null,\n "payment_method": null,\n "payment_method_options": {\n "card": {\n "installments": null,\n "network": null,\n "request_three_d_secure": "automatic"\n }\n },\n "payment_method_types": [\n "card"\n ],\n "receipt_email": "test@example.com",\n "review": null,\n "setup_future_usage": null,\n "shipping": null,\n "statement_descriptor": null,\n "statement_descriptor_suffix": null,\n "status": "requires_payment_method",\n "transfer_data": null,\n "transfer_group": null\n }\n }\n}โ€™

#
2021-10-25T22:45:01.727789345Z sig_header value is t=1635201900,v1=42e96dd5c3a92de2f1a8c373995a04b1653300b5ee4042073ce0a728747d043d,v0=24b39147b87808bdc30612207c0d12f77fd8666cba1cc19cf4a94f3e2aa755e6
2021-10-25T22:45:01.727797245Z payload value is b'{\n "created": 1326853478,\n "livemode": false,\n "id": "evt_00000000000000",\n "type": "payment_intent.created",\n "object": "event",\n "request": null,\n "pending_webhooks": 1,\n "api_version": "2020-08-27",\n "data": {\n "object": {\n "id": "pi_00000000000000",\n "object": "payment_intent",\n "amount": 1000,\n "amount_capturable": 0,\n "amount_received": 0,\n "application": null,\n "application_fee_amount": null,\n "canceled_at": null,\n "cancellation_reason": null,\n "capture_method": "automatic",\n "charges": {\n "object": "list",\n "data": [\n\n ],\n "has_more": false,\n "url": "/v1/charges?payment_intent=pi_1J5AKyImr5PkOfF3q0jGpbTX"\n },\n "client_secret": "pi_1J5AKyImr5PkOfF3q0jGpbTX_secret_lYouigHyrDmSEd0kA9XsU4hQK",\n "confirmation_method": "automatic",\n "created": 1624371144,\n "currency": "pln",\n "customer": null,\n "description": null,\n "invoice": null,\n "last_payment_error": null,\n "livemode": false,\n "metadata": {\n },\n "next_action": null,\n "on_behalf_of": null,\n "payment_method": null,\n "payment_method_options": {\n "card": {\n "installments": null,\n "network": null,\n "request_three_d_secure": "automatic"\n }\n },\n "payment_method_types": [\n "card"\n ],\n "receipt_email": "test@example.com",\n "review": null,\n "setup_future_usage": null,\n "shipping": null,\n "statement_descriptor": null,\n "statement_descriptor_suffix": null,\n "status": "requires_payment_method",\n "transfer_data": null,\n "transfer_group": null\n }\n }\n}'
2021-10-25T22:45:01.727923446Z sig_header value is t=1635201900,v1=42e96dd5c3a92de2f1a8c373995a04b1653300b5ee4042073ce0a728747d043d,v0=24b39147b87808bdc30612207c0d12f77fd8666cba1cc19cf4a94f3e2aa755e6
#

thats the output from the object that been populated with data

worldly cedar
#

So we can't do much with the payload, we can't verify it for you. Rubeus was simply trying to make sure you had values in them

dim frost
#

ok

worldly cedar
#

Unfortunately, this means something is changing the data/payload somewhere in your stack. It can be extremely painful to debug this but that's what you need to track down

dim frost
#

oh right

#

so its on my side then

worldly cedar
#

it's hard to say. What is your stack? Usually it means something on your end is "tampering" with the raw body we send you

dim frost
#

so im using azure webapp

#

to host my python django rest framework app

worldly cedar
#

Have you first made this work locally?

dim frost
#

yes

worldly cedar
#

okay so it works fine locally and doesn't on your host?

dim frost
#

i was using stripe cli

#

and it works fine

#

now i've tried setting up the webhook using the https url

#

im getting this error message

worldly cedar
#

hum that's strange if it's the same code, it should just work. But like I was saying it can be really painful to debug ๐Ÿ˜ฆ

dim frost
#

i think i may know what the problem is

worldly cedar
#

oh that's good to hear!

dim frost
#

Hi

#

so i've fixed it

#

you was right

#

there was a problem on my end

#

i was using the Signing secret that the CLI had provided me

worldly cedar
#

ohhhhhh

dim frost
#

which was wrong

#

lol

worldly cedar
#

nice that it wasn't a "raw payload issue" and was just the secret, I should have asked you to double check

#

it's so hard to debug those ๐Ÿ˜ฆ

dim frost
#

yeah

#

once i was able to output the error message properly

#

i could see the problem

#

Thank you for the support

worldly cedar
#

Glad you got unblocked, congrats ๐Ÿ™‚