#diezler
1 messages · Page 1 of 1 (latest)
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.
- diezler, 1 day ago, 23 messages
Do you have an Event ID for the Event you're looking for the client_reference_id in?
Hey, sorry what is the event id?
where can i find it?
event.id
Can you copy/paste it here?
evt_1Ocy3GGpk89JeyEs613tIwJD
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
also payment_failed?
There's no Checkout Session event for failed payments, unless you're talking about delayed payment methods like ACH or SEPA: https://stripe.com/docs/api/events/types#event_types-checkout.session.async_payment_failed
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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*
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
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"
Is that working?
till now yes, just noticed that i confused invoice.payment_failed with checkout.session.xxxx
I will change that and it should work then
Sounds good!
just a question, how can i get the client on invoice failed? is the way to go the customer_id?
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
so if i get for example payment failed, i compare the invoice id with the one in my db and update the sub status?
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
I guess I was on the wrong way with the reference id