#diezler

1 messages · Page 1 of 1 (latest)

mental ginkgoBOT
#

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.

wary verge
#

Do you have an Event ID for the Event you're looking for the client_reference_id in?

cursive sable
#

Hey, sorry what is the event id?

wary verge
#

It looks like this --> evt_abc123

#

Each webhook has one

cursive sable
#

where can i find it?

wary verge
#

event.id

cursive sable
#

yeah there is the i

#

id*

wary verge
#

Can you copy/paste it here?

cursive sable
#

evt_1Ocy3GGpk89JeyEs613tIwJD

wary verge
#

That's an invoice.updated event, but you're talking about the client_reference_id which would only come through on Checkout Session events like checkout.session.completed or checkout.session.expired

cursive sable
#

also payment_failed?

wary verge
cursive sable
#

ah, okay i get it. In my case I should move the logic accessing the id into the methods that actually use it right?

#

btw iam using subscriptions*

wary verge
#

I'm not sure I understand the question. What you should be doing is detecting what type of event is coming through and programming different logic for each type of event, that way you aren't looking for client_reference_id in Invoice events where it doesn't exist

cursive sable
#

This is my full webhook, i now moved the logic accessing the id into the methods that have it


@router.post("/webhook")
async def stripe_webhook(db: db_dependency, event: dict, request: Request, stripe_signature=Header(None)):
    webhook_secret = os.getenv("WEBHOOK_SECRET")
    raw_body = await request.body()
    try:
        event = stripe.Webhook.construct_event(
            raw_body, stripe_signature, webhook_secret
        )

    except ValueError as e:
        # Invalid payload
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid payload.")

    except stripe.error.SignatureVerificationError as e:
        # Invalid signature

        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid signature.")

    # Handle the checkout.session.completed event
    session = event["data"]["object"]

    if not session:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid session.")

    if event["type"] == "checkout.session.completed":
        handle_session_completed(session, user, db)

    if event["type"] == "checkout.session.paid":
        handle_session_paid(session, user, db)

    if event["type"] == "checkout.session.payment_failed":
        handle_session_payment_failed(session, user, db)

    return "Success"
#

eg


def handle_session_completed(session, db):
    user = get_user_by_id(session.client_reference_id, db)

    customer_id, subscription_id = session.customer, session.subscription
    customer = db.query(StripeCustomer).where(StripeCustomer.customer_id == customer_id).first()

    if not customer:
        create_stripe_customer_model = StripeCustomer(
            user=user,
            customer_id=customer_id,
            subscription_id=subscription_id,
            subscription_status="active"
        )

        db.add(create_stripe_customer_model)
        db.commit()
    else:
        customer.subscription_status = "active"
wary verge
#

Is that working?

cursive sable
#

till now yes, just noticed that i confused invoice.payment_failed with checkout.session.xxxx
I will change that and it should work then

wary verge
#

Sounds good!

cursive sable
wary verge
#

You should store your Checkout Session ID alongside your Invoice ID in your database for quick lookup, because it only exists on Checkout Session objects

cursive sable
#

so if i get for example payment failed, i compare the invoice id with the one in my db and update the sub status?

wary verge
#

That's a way to go, sure. Not sure how that relates to client_reference_id, but that seems like a normal use-case for webhooks all the same

cursive sable
#

I guess I was on the wrong way with the reference id