#victoria.fabris-client-secret

1 messages ยท Page 1 of 1 (latest)

crisp seal
#

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?

unborn minnow
#

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)```
unborn minnow
#

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

unborn minnow
crisp seal
#

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.

unborn minnow
#

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?

crisp seal
#

Correct.

unborn minnow
#

and if the object is payment intent, just doing subscription_id = event["data"]["object"]["client_secret"] will get my client_secret?

crisp seal
unborn minnow
#

ok, nice!

#

thank you so much!!

crisp seal
#

Any time!