#dr3w1792

1 messages · Page 1 of 1 (latest)

next patioBOT
manic plinth
#

What's the event ID?

wet horizon
#

evt_1NhmMRKkCMehlZf7LYlm2HaF

manic plinth
#

Are you using the webhook secret from Dashboard or CLI?

wet horizon
#

dashboard, I switched from the cli one when I was testing locally

manic plinth
#

Is your webhook endpoint's server time correct?

wet horizon
#

i am using aws so I would assume so but do you know how I could check that?

manic plinth
#

No, maybe you want to check with AWS?

wet horizon
#

ok i verified that it is the correct time

manic plinth
#

OK. Can you share with me your code related webhook handling?

wet horizon
#

sure

#
@csrf_exempt
def stripe_webhook(request):
    event = None
    payload = request.body
    sig_header = request.META.get('HTTP_STRIPE_SIGNATURE', '')
    endpoint_secret = 'whsec_wuqPPGrRyPxQFtylWdNFMG2NTD87fn2z'
    
    try:
        event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
    except ValueError as e:
        # Invalid payload
        raise e
    except stripe.error.SignatureVerificationError as e:
        # Invalid signature
        raise e

    # Handle the event
    if event['type'] == 'checkout.session.completed':
        checkout = event['data']['object']
        biz=Business.objects.get(id=checkout['metadata']['biz_id'])
        type=checkout['metadata']['type']
        if type == 'premium':
            biz.set_premium(checkout.subscription)
        elif type == 'badge':
            badge_list = json.loads(checkout['metadata']['badge_list'])
            for badge_id in badge_list:
                badge = Badge.objects.get(id=badge_id)
                biz.badges.add(badge)
            biz.save()

    elif event['type'] == 'customer.subscription.deleted':
        subscription = event['data']['object']
        subscription_id = subscription['id']
        business = Business.objects.filter(subscription_id=subscription_id).first()

        business.subscription_status = 'canceled'
        business.subscription_end = datetime.now(pytz.utc)

    else:
      print('Unhandled event type {}'.format(event['type']))

    return JsonResponse({'success': True})
manic plinth
#

I think you should get the signature header from stripe-signature

wet horizon
#

instead of HTTP_STRIPE_SIGNATURE?

manic plinth
wet horizon
#

ah ok, I am doing a quick deploy to see if that fixed the issue

wet horizon
#

still getting the same error

manic plinth
#

Change
sig_header = request.META.get('HTTP_STRIPE_SIGNATURE', '')
to
sig_header = request.headers.get('stripe-signature')

wet horizon
#

so request.headers is a flask thing, does not work in django

#

I was getting errors when I had that initally

manic plinth
#

I see, perhaps you want to find out what's the equivalent function to get all headers in Django?

wet horizon
manic plinth
#

print the content of this META dict and see if stripe-signature is inside it.

wet horizon
#

it is interesting it is working locally, but not when deployed to production

next patioBOT
wet horizon
#

should I be seeing the stripe_signature without http before it when running locally?

vagrant whale
#

i'm not too familiar with python but you should be able to check / verify from our sample

wet horizon
#

So when checking locally I see http_stripe_signature (the one i had)

#

but not stripe_signature

#

im wondering if because locally its an http request, while the production webhook is https?

vagrant whale
#

like what jack mentioned, you should log all the headers and see what you can find

wet horizon
#

i just did and am telling you what i see

#

locally i see http_stripe_signature

#

not stripe_signature

vagrant whale
#

alright, and what about in production? the problem is in production right?

wet horizon
#

ya I just verifying before deploying for 10 minutes that I may get a different header name in production vs local

#

but if its unkown ill deploy and see