#dalel.eth - Checkout Metadata

1 messages ยท Page 1 of 1 (latest)

random basin
#

Metadata isn't automatically copied between objects in most cases. That metadata will either be on the checkout session itself if you set it via the metadata parameter or the PaymentIntent/SetupIntent/Subscription if you set it via the payment_intent_data, setup_intent_data, or subscription_data parameters

#

Where would you like this metadata to be set?

#

If you want it somewhere other than those four objects, we can find the appropriate time and place to copy it on to whichever object you like

rose pagoda
#

Hey @random basin ! Sorry had to step away for a little while there

echo ivy
#

Hi ๐Ÿ‘‹ I'm taking over for @random basin .

rose pagoda
echo ivy
#

Okay, so the metadata you are showing here is for the Checkout Session. That's the object it will be appended to.

rose pagoda
#

hmm alright

#

I need to append it to the customer so that it displays inmy customer dashboard

#

I'm using the customer.created webhook to do this

#

but it doesn't appear to be working

#

does this make sense to you @echo ivy ?

echo ivy
#

Yes I see what you mean

#

So what part of this isn't working?

#

I use Django but I've never used DjStripe. To me it looks like you are saving to your local Customer object. Is this supposed to update the Stripe object also?

rose pagoda
#

Hey @echo ivy - yes I'm trying to figure out the best way to update the Stripe object

#

in the checkout session - I want to provide the metadat during the checkout session so that it saves and I can view on Stripe dashboard

echo ivy
#

Yes I remember. I'm curious what is happening in the line from your code with customer.save(). I haven't used the DjStripe plugin.

#

But in my Django integrations that would only save to the local database object.

rose pagoda
#

DjStripe syncs with Stripe

#

allows me to keep a record of Stripe models / data in my local db

echo ivy
#

Okay so does it modify the save() method on the local models? What method does it call?

rose pagoda
#

I'm initializing that metadata that I expect in stripe though, in the checkout session

echo ivy
#

Right, and that is attached to the Checkout Session object. So you are trying to migrate it to the Customer, correct?

rose pagoda
#

I'm not sure I even need to migrate it to the Customer manually that way - that's just something I was trying out

echo ivy
#

Webhooks listen for data. You can use them to sync your local DB to Stripe data but not the other way around.

#

You would need to be calling a stripe.Customer.modify(metadata=metadata) at the time you save the customer

rose pagoda
#

oh okay - I'll give that a try

#

so maybe on the checkout session completed webhook?

echo ivy
#

Sorry that was inaccurate. Stripe uses webhooks to provide data to you. Events trigger webhook requests which are sent to the endpoints you set up on your server. However, it's a one way transmission.

rose pagoda
#

gotcha that's what I was thinking ๐Ÿ™‚

#

so I can save the customer using that modify() method at the time the checkout session is completed

#

or customer created maybe? what do you think

#

only problem with that is that the metadata I need is coming from the request object

echo ivy
#

To be clear, the modify method is using the Stripe library for Python.

rose pagoda
#

gotcha

echo ivy
#

As for the webhook event, I would walk through completing a Checkout Session in Test mode and see all the events that fire. You can check them in your dashboard here:
https://dashboard.stripe.com/test/events

rose pagoda
#

"TypeError: modify() missing 1 required positional argument: 'sid'"

#

so looks like it's working and the function is being called

#

but is missing something

rose pagoda
#

but the metadata still not on Stripe with that event

echo ivy
#

Okay, again the metadata should be appended to the session object you create with the stripe.checkout.Session.create() call.

#

What do you expect your application to do?

rose pagoda
#

I want the application to create the checkout session and pass the metadata to stripe and append the metadata to the customer object

rose pagoda
#

session.append(metadata=metadata)

#

or can I do something like the above?

echo ivy
#

No you want to keep it in the checkout Session create call

rose pagoda
echo ivy
#

But you have to do the adding of it to the Customer when the webhook sends the session object back.

rose pagoda
#

ohhh I see

#

customer = session["customer"]

#

and then use stripe.Customer.modify(metadata=metadata)

echo ivy
#

Yup. Then you should see a POST request to Stripe updating your Customer record in Stripe's data.

rose pagoda
#

in this function call?

echo ivy
#

You would need the ID

#

cus_XXXX

#

It's in the session

rose pagoda
#

right - but how to actually write it?

#

stripe.Customer.modify(metadata=metadata, id=cus_XXXX)

#

that doesn't seem quite right either ๐Ÿค”

echo ivy
#

ID first, without the named parameter e.g. stripe.Customer.modify(cus_ZZZ, metdata=metadata)

rose pagoda
#

gotcha

#

hmmm

#

"customer = session["customer"]
KeyError: 'customer'"

#

how to access customer from the session object?

echo prawn
#

are you sure you have something in session right now?

#

session.customer is the way though

echo ivy
#

@rose pagoda is this in the webhook handler code you shared earlier?

rose pagoda
rose pagoda
#

I've updated it - just a sec I'll share it

echo ivy
#

Okay this is the customer.created event. That means the object that is coming back is the customer itself.

rose pagoda
#

ah -so there is probably a better webhook event to utilize for this

#

checkout session completed

echo ivy
#

Bingo!

#

And that will have the session.customer attribute with the ID you can use to update your customer. Also it will have the metadata you appended when you created the session

rose pagoda
#

so in theory this should work?

echo ivy
#

Well. you'll need to use session["customer"] because it's a dict, same with 'customer'

echo prawn
#

I do think session.customer just works and it shouldn't be an issue

rose pagoda
#

hmmm still no metadata

#

on Stripe dash

echo prawn
#

I don't really understand your code right now unfortunately ๐Ÿ˜ฆ

#

You're also mixing multiple things, like is the customer update call even being made?

#

Step 1 is to manually test things and hardcode values stripe.customer.modify('cus_123', metadata={'order_id': '6735'}) and confirm that works, then do the same in the webhook with clear logs too

rose pagoda
#

okay let's see...

rose pagoda
#

but going to see if that hardcoding worked

rose pagoda
#

I'm just trying to figure out how to get the metadata to be set when my customer is created

echo prawn
#

I'm sorry for being dense, you say "no metadata" but without any info

#

like are you still hardcoding this straight in a webhook handler that might not be running at all?

#

Or have your done a one-off python script that creates and then updates a customer to set metadata and familiarize yourself with that part of our API?

rose pagoda
echo prawn
#

what's the customer id?

rose pagoda
#

that's what I'm trying to get from the session object

#

so not sure - I was trying to log that but since I'm unable to access the customer object here I'm not sure

#

want me to go through the user flow though and send the customer ID that is created? even if there is no metadata?

echo prawn
#

you must have an id already (or many) since you log it in the picture of the code you shared above

rose pagoda
#

it's failing

#

"customer = session.customer
AttributeError: 'dict' object has no attribute 'customer'"

#

failing just before the logs

#

ah it actually did work with session["customer"]

rose pagoda
#

still no metadata set, though

echo prawn
#

sure but you can quickly debug this and check the Customer in the Dashboard and confirm you never made that update call

rose pagoda
#

ahh I see

echo prawn
#

I'm sorry, I'm flying a bit blind here, you keep saying "doesn't work" but there isn't much to go on
Can you move away from the webhook entirely for a few minutes, work on end to end code that will properly update an existing customer, make that work, and once that work, move that code to your webhook handler (with actionable logs) to confirm it's running. With also proper error handling https://stripe.com/docs/api/errors

rose pagoda
#

Sure I'll try that approach

echo prawn
#

The problem with the webhook is that sometimes you think you are running the code but you aren't and it's hard to clearly debug since it's all async without a direct console output

rose pagoda
#

yeah it's hard to tell - I assumed the code was running since my logs are working on my server

#

is email an accessible field on a customer object?

#

customer.email or customer["email"] - one of these should work?

echo prawn
#

yes

#

you can also just log the entire object

rose pagoda
#

" stripe.customer.modify(customer.id, metadata={"djstripe_subscriber": "40"})
AttributeError: module 'stripe.api_resources.customer' has no attribute 'modify'"

#

I'm thinking this might just be because it is a lowercase "c"?

#

Successfully updated customer metadata ๐Ÿ™‚

#

now to get it working with my data...this might be a quick fix now

echo prawn
#

yay

#

always moving the code to a simple script where you can repro

rose pagoda
#

stripe.error.InvalidRequestError: Request req_IvMyr3NPtbW2mF: Not a valid URL

do you guys recognize this?

#

getting this now trying to create a checkout session...this is a separate issue so maybe we create a separate thread?

#

or happy to discuss in here - whatever is best for y'all

dense ravine
#

jumping in as koopajah had to step away! we can totally continue discussing in this thread

rose pagoda
#

sweet sweet thank you

dense ravine
#

when you get an error message like what you pasted, a good place to start would be to take a look at what was sent to Stripe in that request. If you login to your Dashboard, you can view what was sent in the POST request in logs : https://dashboard.stripe.com/test/logs/iar_IvMyr3NPtbW2mF

#

if you take a look at what was sent, it looks like your app has concatenated some other stuff into the success_url, so that's not a valid URL

rose pagoda
#

ah - that's the goal actually

#

the success URL relies on two parameters

dense ravine
rose pagoda
#

For the purposes of my application it relies on two parameters - userId and communityId both of which I'm passing from my frontend

#

assuming communityId of 1 and userId of 38

#

so should be a valid URL if I am not mistaken

#

it is very interesting to look at the logs though - I see what you mean @dense ravine !

#

looks like the communityId is not being accessed properly and so it is in fact an invalid URL...

#

@dense ravine here's how the success URL is being interpreted...

dense ravine
#

yeah, it's totally not a valid URL

#

you'd want to get that fixed

rose pagoda
#

activeCommunityId is what I need within params

#

here's how I'm currently building the success url - do you see anything wrong with this?

#

this is where community_id comes from

#

that second one being on the frontend

dense ravine
#

i would log out every section which you're concatenating there to figure out where's the issue

#

for example, console.log(FRONTEND_URL), console.log(request.data["community_id"]), etc

rose pagoda
#

Wow - looks like the issue is on my frontend after all

#

don't think this has anything to do with Stripe so I'll take care of it. thanks for your help though!

dense ravine
#

you're welcome, feel free to reach out again if you have other questions! ๐Ÿ˜„