#victoria.fabris-client-secret
1 messages ยท Page 1 of 1 (latest)
Hi ๐ can you clarify where in the Subscription Schedule response that you're seeing a client_secret being provided, and what type of webhook event you're referring to?
hi, im going to paste my code here to be more clear
name=("Delivery"),
)
price = stripe.Price.create(
unit_amount=int((total_price / installments) * 100),
currency="brl",
recurring={
"interval": "month",
},
product=product["id"],
)
subscription_schedule = stripe.SubscriptionSchedule.create(
customer=customer_id,
phases=[
{
"items": [
{
"price": price["id"],
},
],
"iterations": installments,
},
],
start_date="now",
end_behavior="cancel",
expand=["subscription.latest_invoice.payment_intent"],
)
invoice = stripe.Invoice.finalize_invoice(
subscription_schedule.subscription.latest_invoice.id
)
payment = stripe.PaymentIntent.retrieve(invoice.payment_intent)
return payment.client_secret```
im doing in python
and in receiving a client secret like this one: pi_3L3LNNH2jrwTN4ex0TKNabmA_secret_qghr1rEOQvneVLcTE6OUOQWj0
payload = request.body
signature = request.META.get("HTTP_STRIPE_SIGNATURE")
if not signature:
return Response(status=status.HTTP_401_UNAUTHORIZED)
try:
event = stripe.Webhook.construct_event(
payload,
signature,
endpoint_secret,
)
except Exception:
return Response(status=status.HTTP_200_OK)
events_to_watch = ["invoice.payment_failed", "payment_intent.succeeded"]
if event["type"] not in events_to_watch:
return Response(status=status.HTTP_200_OK)
subscription_id = event["data"]["object"]["client_secret"]
delivery: Optional[Delivery] = Delivery.objects.filter(
payment_code=subscription_id
).first()
if not delivery:
return Response(status=status.HTTP_200_OK)```
later, im doing something like this, and checking in my database for this same client secret
i added my webhook endpoint in my dashboard listening for the events, this is alright, in getting the events. but i receive a http200
my doubt is where is this http200 is coming from, one way is coming if i couldnt find the object in the database by search with the client secret
so i want to be sure that this part of the code is ok and finding the client secret in the database
(im looking to the database and my client secret is there, im receiving it in this part, but im not sure if im receiving it in the webhook part)
Alright, there's a bit to unpack here. Your first snippet shows you creating a Subscription Schedule which is also creating multiple underlying objects (Subscription, Invoice, and Payment Intent). The client_secret that you're referencing is stored on the Payment Intent and will remain constant.
When you're receiving events though, your code is written to handle two different types of events. payment_intent.succeeded events will contain a Payment Intent in their object field, so you can reference the client_secret the way you are in your code. However, invoice.payment_failed events will contain an Invoice object in their object field, and the Invoice object doesn't have a client_secret field.
So if you receive an invoice event, you'll need to retrieve the associated Payment Intent and check the client_secret that it contains.
ok, so, im handling the event "payment_intent.succeeded" just fine, cause it receives a client secret, but it fails for "invoice.payment_failed" cause my event will not have a client secret in it?
what i have to do is check if the object i receive in the event is a Payment Intent or a Invoice object, right?
Correct.
and if the object is payment intent, just doing subscription_id = event["data"]["object"]["client_secret"] will get my client_secret?
Yup, data.object will contain the object that triggered the event. invoice.XXX events will contain an Invoice, payment_intent.XXX will contain a Payment Intent, etc:
https://stripe.com/docs/api/events/object#event_object-data-object
And the Payment Intent object contains a client_secret field:
https://stripe.com/docs/api/payment_intents/object#payment_intent_object-client_secret
For Invoices, you can retrieve their associated Payment Intent by using the ID in the payment_intent field:
https://stripe.com/docs/api/invoices/object#invoice_object-payment_intent
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Any time!