#RafaCitec-webhook

1 messages · Page 1 of 1 (latest)

sturdy kernel
#

hi! what's the question exactly?

idle trellis
#

hello

#

def handle_event_webhook(payload, event, request) -> None:
"""Handle webhook events Stripe.

Args:
    payload: body RAW of event without treating
    event: request send of Stripe in json type

Returns:
    None
"""
print(payload)
print(event)
print(request.headers)
if webhook_secret:
    # Only verify the event if there is an endpoint secret defined
    # Otherwise use the basic event deserialized with json
    # Retrieve the event by verifying the signature using the
    # raw body and secret if webhook signing is configured.

    signature = request.headers.get("stripe-signature")

    try:
        event = stripe.Webhook.construct_event(
            payload=payload,
            sig_header=signature,
            secret=webhook_secret,
        )
        data = event["data"]
    except stripe.error.SignatureVerificationError as e:
        print("Webhook signature verification failed,  " + str(e))
    event_type = event["type"]
else:
    data = payload["data"]
    event_type = payload["type"]

if event_type == "invoice.paid":
    customer_email = data.object.customer_email
    timestamp_expiration_data = data.object.lines.data[0].period.end
    admin_patch_user(
        customer_email=customer_email,
        user_data={
            "user_metadata": {
                "expiration_date": datetime.fromtimestamp(
                    timestamp_expiration_data
                ).strftime("%Y-%m-%d"),
                "timestamp_expiration_date": timestamp_expiration_data,
                "stripe_customer_id": data.object.customer,
            }
        },
    )
#

I have this function in Python

#

and I am do unit testing

#

but I don't know like mock the object request

#

that Stripe send to webhook

#

can you have examples?

#

o can send me the object in python language?

sturdy kernel
#

easiest way would be to just copy it from a real event. Like do some testing in test mode, and save the value of payload (you're already printing it), which you can then use for unit tests later

sturdy kernel
# idle trellis that Stripe send to webhook

payload is exactly what we send to you(it's the raw HTTP request body), so you could just copy that from a real event and then set up your test to replay that in a request to your code.

idle trellis
#

yes

#

problem is with request object

#

print(request) not print the object

sturdy kernel
#

what does it print instead?

idle trellis
sturdy kernel
#

yeah, it's the request object from whatever web framework you're using.

#

I wouldn't know how to mock that for you, that's nothing to do with Stripe.

What I would suggest is you refactor the code. So move the Stripe part into a new function that takes stripe_webhook(payload,signature) . Refactor the handle_event_webhook so that it pulls out the signature header from the request and passes it though. That way your stripe_webhook can be easily unit tested by passing it just simple strings.