#_kirito_7800

1 messages · Page 1 of 1 (latest)

quaint abyssBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

lunar delta
#
import json
import os
import requests
import stripe


def lambda_handler(event, context):
    
    print(event)
    
    stripe.api_key = os.getenv("STRIPE_API_KEY")
    event_data = None
    payload = json.dumps(json.loads(event.get('body')))
    endpoint_secret = os.getenv("STRIPE_SIGNATURE")
    print(payload)
    
    try:
        event_data = json.loads(payload)
    except json.decoder.JSONDecodeError as e:
        print('⚠️  Webhook error while parsing basic request.' + str(e))
        return {
            'body': json.dumps({"status": False})
        }
    if endpoint_secret:
        sig_header = event.get("Stripe-Signature")
        print(sig_header)
        try:
            event_data = stripe.Webhook.construct_event(
                payload, sig_header, endpoint_secret
            )
        except stripe.error.SignatureVerificationError as e:
            print('⚠️  Webhook signature verification failed.' + str(e))
            return {
                'body': json.dumps({"status": False})
            }

    response_data = json.loads(event["body"])
    print(response_data)
    if response_data['type'] == 'checkout.session.completed':
        session = response_data['data']['object']
    elif response_data['type'] == 'subscription_schedule.aborted':
        subscription_schedule = response_data['data']['object']
    elif response_data['type'] == 'subscription_schedule.canceled':
        subscription_schedule = response_data['data']['object']
    elif response_data['type'] == 'subscription_schedule.created':
        subscription_schedule = response_data['data']['object']
    else:
        print('Unhandled event type {}'.format(response_data['type']))

    print("Success")
    return {
        'statusCode': 200,
        'body': json.dumps(response_data)
    }
ivory matrix
#

Hi there

lunar delta
#

Lambda Mapping Template for integration request:

{
  "body": "$util.escapeJavaScript($input.body)",
  "Stripe-Signature": "$input.params('Stripe-Signature')"
}
ivory matrix
#

As you can see from the log your body is not the raw body

#

It has already been JSONified

#

So you need to figure out how to receive the raw body in your endpoint

#

Likely your framework is messing with it

lunar delta
#

Okay.

#

Could you please verify if my Lambda Mapping Template for the integration request is correct? This might be the root cause of the issue.

// @ivory matrix

ivory matrix
#

Looks fine afaict

#

But really the issue is likely with ```payload = json.dumps(json.loads(event.get('body')))

#

That payload needs to be the raw body

#

You shouldn't manipulate it at all before passing it to stripe.Webhook.construct_event()

#

Ah also I misread what you stated above -- was just looking at your code. I'm not familiar with Lamda really so can't say about the Mapping Template

lunar delta
#

Initially, I was only using event.get('body'), but it didn't work. So, I thought encoding it into JSON and then converting it back to a string might work.

#

Okay.

#

Thanks for your help!