#pratikco

1 messages ยท Page 1 of 1 (latest)

keen waspBOT
mental helm
#

Hi there

eternal lichen
#

Hii

mental helm
#

Where are you setting the metadata exactly?

#

Are you setting it as subcription_data.metadata?

eternal lichen
#

which creating a session for checkout

mental helm
eternal lichen
#

checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[
{
"price": price["id"],
"quantity": 1,
}
],
metadata={
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},
mode="subscription",
customer=customer_id,
success_url='http://127.0.0.1:8000/success' + "?session_id={CHECKOUT_SESSION_ID}",
cancel_url='http://127.0.0.1:8000/failed/',
subscription_data=subscription_data,

)
#

like this

mental helm
#

Got it

#

So in that case, the metadata is only being set on the Checkout Session object

#

If you want the metadata to be on the Subscription itself, then you should set it as the subscription_data.metadata

#

That way, each time a payment is made, you can check the customer.subscription.updated webhook and you will see that metadata there

#

(Or you can retrieve the Subscription based on the PaymentIntent if you are listening to payment_intent.succeeded for instance)

eternal lichen
mental helm
#

Do you mean can I help you with the syntax?

eternal lichen
#

yeah

eternal lichen
mental helm
#

So you would move your metadata object into your subscription_data variable

eternal lichen
# eternal lichen so that I can write it in my this code

customer_id = stripe.Customer.create(
name= 'abcd,
)

    stripe.Customer.modify(
        customer_id,
        metadata={
            "user_id": request.user.id,
            "art_price": art_price,
            "amount": amount,
            "free_trial": free_trial,
        },
    )



    instance = request.user
    instance.stripe_customer_id = customer_id["id"]
    instance.save()

# create checkout session for recurring subscription
checkout_session = stripe.checkout.Session.create(
    payment_method_types=["card"],
    line_items=[
        {
            "price": price["id"],
            "quantity": 1,
        }
    ],
    
    mode="subscription",
    customer=customer_id,
    success_url='http://127.0.0.1:8000/success' + "?session_id={CHECKOUT_SESSION_ID}",
    cancel_url='http://127.0.0.1:8000/failed/',
    subscription_data=subscription_data,

)

So you means I should do it like this right?

mental helm
#

No that is putting the metadata at the Customer level. Which you can do if you want the metadata to be there. But doesn't really work if the Customer has multiple Subscriptions. Where is your subscription_data variable being created that you are passing? See: subscription_data=subscription_data in your code

eternal lichen
#

subscription_data = {
"trial_end": 1665269153,
metadata={
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},

    }
mental helm
#

Yep

#

There you go

eternal lichen
#

like this? but the syntax is not right

mental helm
#

Syntax is right

#

That looks good

eternal lichen
#

gettng error

mental helm
#

Oh wait

#

not metadata=

eternal lichen
mental helm
#

metadata: {

#

Like how you are doing trial_end:

eternal lichen
#

ohh ok got it

#

subscription_data = {
"trial_end": 1665269153,
'metadata':{
"user_id": request.user.id,
"art_price": art_price,
"amount": amount,
"free_trial": free_trial,
},

    }
#

now looks good

mental helm
#

๐Ÿ‘

eternal lichen
#

now how will I extract data in my webhook event
if event['type'] == 'checkout.session.completed':
data = session["subscription_data"]["metadata"]["amount"]
Like this?

mental helm
#

Nope

#

The metadata will now be on the Subscription object

#

so instead of using if event['type'] == 'checkout.session.completed':

#

You would use if event['type'] == 'customer.subscription.created':

#

And then you would handle the Subscription object instead of the Checkout Session

eternal lichen
#

so in every month when stripe make payment from same account it will trigger 'customer.subscription.created' this event on sucessful transaction?

mental helm
#

Nope, that is just on creation. On renewal you would use customer.subscription.updated

#

That details the different events you can use and what you might use them for

eternal lichen
#

I will receive this event every month when stipe successfully deducts the amount from the user account right?

plucky pine
#

Correct

eternal lichen
#

I tested this code at first I am only receiving checkout.session.completed not payment_intent.succeded

plucky pine
#

For a subscription?

#

Can you share the sub id?

eternal lichen
plucky pine
#

The event data will contain the subscription object, which will have the metadata attached

eternal lichen
#

how will I update subscription every time

#

because its a recurring job strip itself will deduct the amount

plucky pine
#

You don't have to update anything

#

That event is generated each billing cycle

#

Ah apologies. No it's not.