#emnaruto07-webhooks

1 messages · Page 1 of 1 (latest)

static sky
#

Hi, is there a specific Event object I can check? evt_xxx?

livid mirage
#

I'm getting error with

#

Stripe_signature

#

'STRIPE_SIGNATURE'```
#

django

static sky
#

Can you share your full code?

livid mirage
#

wait

#
    webhook_secret = settings.WEBHOOK_KEY
    payload = request.body
    sig_header = request.headers['STRIPE_SIGNATURE']
    event = None

    try:
        event = stripe.Webhook.construct_event(
        payload, sig_header, webhook_secret
        )
    except ValueError as e:
        # Invalid payload
        return e
    except stripe.error.SignatureVerificationError as e:
        # Invalid signature
        return e

    if event['type'] == 'payment_intent.succeeded':
        session = event['data']['object']
        print(session)
        receipt_email=session["charges"]["data"][0]["billing_details"]["email"]
        print(receipt_email)


        #Creating a successfull copy
        Subscription.objects.create(
            stripe_payment_id=session["charges"]["data"][0]["payment_intent"],
            customer=session["customer"],
            payment_type=session["payment_method_types"],
            status=session["charges"]["data"][0]["paid"],
            email=session["charges"]["data"][0]["billing_details"]["email"],
            invoice=session["invoice"],
            customer_name=session["charges"]["data"][0]["billing_details"]["name"],
            plan_amount=session["charges"]["data"][0]["amount_captured"]
        )

        # send_mail(
        #     subject="Thankyou for your order on quickhighprotein.com",
        #     message=f"We are reviewing your order! If everything looks fine, Then your Advertisement will be shown on our website within 24hours. Otherwise, you will get a full refund. Thanks!",
        #     recipient_list=[receipt_email],
        #     from_email="shazeb@pwnremote.com"
        # )

    # Passed signature verification
    return HttpResponse(status=200)```
#

line number 4

#

and i'm using sining_secret

#

key

static sky
#

It's hyphenated (Stripe-Signature)

livid mirage
#

no its not

#
    event = None
    payload = request.data
    sig_header = request.headers['STRIPE_SIGNATURE']```
static sky
#

Stripe can optionally sign the webhook events it sends to your endpoints by including a signature in each event’s Stripe-Signature header. This allows you to verify that the events were sent by Stripe, not by a third party. You can verify signatures either using our official libraries, or manually using your own solution.

#

Specific Django example:
sig_header = request.META['HTTP_STRIPE_SIGNATURE']

livid mirage
#

i tried this as well

#

you can check the error yourself

long jetty
#

Hi! To test your webhook page, you need to actually get an event from Stripe. If you just visit the page, there will be no stripe signature set, so it's expected to see errors.

livid mirage
#

?

long jetty
#

If you click on https://quickhighprotein.com/webhooks/stripe/, then there will be no stripe signature set and request.META['HTTP_STRIPE_SIGNATURE'] will generate an error. So this is expected, you cannot test your webhook code like this.

livid mirage
#

so you mean, i need stripe cli in production as well?

long jetty
#

No you don't need it. I'm just saying that you can't test a webhook code by just visiting the URL.
To test your webhook code, you need to check if it can actually process an event.

#

But yes using the Stripe CLI could be a good idea to help you debug your code.

livid mirage
#

i know my code is working fine

#

because its working with cli

#

i want to make it live, what should i do?

long jetty
#

Great! Then you need to create a new webhook endpoint in livemode here: https://dashboard.stripe.com/webhooks
Then use the new webhook secret (whsec_xxx) in your code. And that's it!
Note that you need to make sure that the sig_header you retrieve is not being modified by your server/framework. Stripe needs the raw header to be able to verify the webhook.

#

Finally run some test events to make sure everything is working. If not, add some logs to your code to try to identify where is the issue.

livid mirage
#

but if i change the mode, i cant test live with test card

low kelp
#

You don't need to test in live mode. If you have tested in test mode, that should be enough. If all you want to do is make sure your live endpoint is functioning as expected, you can first connect it to your account as a test webhook endpoint

livid mirage
#

so i added webhook already right

low kelp
#

What's your question?

livid mirage
#

can i test with stripe cli?

low kelp