#mxd3-pricingtable-reference

1 messages ยท Page 1 of 1 (latest)

frail galeBOT
regal kestrel
#

mxd3-pricingtable-reference

frail galeBOT
tranquil isle
#

ok testing it out now,

#
HTTP status code
400 (Bad Request)

{
  "message": "No user_id found in event metadata",
  "status": "failure"
}```
ancient knot
#

๐Ÿ‘‹ stepping in here as koopajah needed to step away

#

Is the above coming from your Webhook handler?

tranquil isle
#

yes I just click the 'start trial' filled out the credit card form, and this is from: dashboard stripe /test/webhooks/

ancient knot
#

Are you listening for checkout_session.completed here?

tranquil isle
#

ok let me see

#

am I listening for it in the webhook function?

ancient knot
#

I'm not sure what you mean by that.

#

Going to need a lot more details about exactly what you are doing in order to be able to help!

tranquil isle
#

so i did the {{client-reference-id="{{ current_user.id }}" and the number is 7

#

for the pricing table

#

then submitted the form

#

and here are the results after adding that client-reference-id: ```HTTP status code
400 (Bad Request)

{
"message": "No user_id found in event metadata",
"status": "failure"
}```

ancient knot
#

Where do you see this error exactly?

tranquil isle
#

in the stripe dashboard

#

so basically

ancient knot
#

Are you looking at an Event and that is the Response body?

#

Also are you working with a third party integration here?

tranquil isle
#

i have a mysql table, with a field called stripe_customer_id, and its currently NULL, and after paying, im trying to get it to flip to: cus_OG3syY0HXU7Xzg from NULL. and trying to associate the user of id=7

#

so the pricing page has the client-reference-id which takes the current logged in user id which is 7

ancient knot
#

Yep and are you listening for Webhooks to then ingest this data that way?

tranquil isle
#

can I paste the webhook?

ancient knot
#

Yep if you can show me your webhook code that would help

tranquil isle
#
@app.route('/webhook', methods=['POST'])
def webhook():
    ...

    user_id = None
    stripe_customer_id = None

    if 'user_id' in event['data']['object']['metadata']:
        if event['type'] == 'customer.created':
            user_id = event['data']['object']['metadata']['user_id']
            stripe_customer_id = event['data']['object']['id']
        elif event['type'] in ['customer.subscription.created', 'customer.subscription.updated', 'customer.subscription.deleted', 'checkout.session.completed']:
            user_id = event['data']['object']['metadata']['user_id']
            stripe_customer_id = event['data']['object']['customer']
    else:
        return jsonify({'status': 'failure', 'message': 'No user_id found in event metadata'}), 400


    user = User.query.filter_by(id=user_id).first()

    if not user:
        return jsonify({'status': 'failure', 'message': 'No user found for this id'}), 400

    user.stripe_customer_id = stripe_customer_id
    db.session.commit()

    stripe_subscription_id = event['data']['object']['id']
    stripe_product_id = event['data']['object']['plan']['product'] if 'plan' in event['data']['object'] else None

    subscription_record = Subscription.query.filter_by(stripe_subscription_id=stripe_subscription_id).first()
    if subscription_record:
        subscription_record.user_id = user.id
        subscription_record.active = event['type'] != 'customer.subscription.deleted'
        subscription_record.stripe_product_id = stripe_product_id
    else:
        subscription_record = Subscription(user_id=user.id, stripe_subscription_id=stripe_subscription_id,
                                        active=event['type'] != 'customer.subscription.deleted', stripe_product_id=stripe_product_id)
        db.session.add(subscription_record)

    db.session.commit()

    return jsonify({'status': 'success'}), 200```
ancient knot
#

Okay cool. So see how you have if 'user_id' in event['data']['object']['metadata']:

#

That will never work

tranquil isle
#

yeah i tried this before getting the idea to do the client-reference-id thing

ancient knot
#

What you want to do is something like if event and event['type'] == 'checkout_session.completed': user_id = event['data']['object'][client_reference_id]

tranquil isle
#

does client_reference_id show up in the Request?

ancient knot
#

What is "the Request"?

tranquil isle
#
HTTP status code
400 (Bad Request)

{
  "message": "No user_id found in event metadata",
  "status": "failure"
}

Request

{
  "id": "evt_1Z",
  "object": "event",
  "api_version": "2022-08-01",
  "created": 1689621266,
  "data": {
    "object": {
      "id": "sub_1Q",
      "object": "subscription",
      "application": null,
      "application_fee_percent": null,
      "automatic_tax": {
        "enabled": false
      },```
ancient knot
#

Yes, it would be a property there within data.object but that is a Subscription object so that is the wrong Event type. It shows up on the Checkout Session object.

#

Which is why you need the checkout_session.completed Event.

tranquil isle
#

I see it: "after_expiration": null, "allow_promotion_codes": true, "amount_subtotal": 0, "amount_total": 0, "automatic_tax": { "enabled": false, "status": null }, "billing_address_collection": "required", "cancel_url": "https://stripe.com", "client_reference_id": "7",

#

i did the resend on checkout.session.completed

ancient knot
#

๐Ÿ‘

tranquil isle
#

now I will try the if event and event['type']

ancient knot
#

Sounds good!

tranquil isle
#

thats what it started off looking like

#

do you think the webhook code is poor? and I should rewrite it? is that what you are recommending?

ancient knot
#

Well I don't think you are doing signature verification above. But maybe you removed that part

tranquil isle
#

yea

#

i took it out for it to fit

ancient knot
#

Okay then yeah you probably just need to clean up your code. I didn't look at it too closely

#

Just saying that our webhook quickstart is a good clean example to refer to

tranquil isle
#

oh ok

ancient knot
#

Which it sounds like you are already doing

tranquil isle
#

I got this now : ```checkout.session.completed

Response
HTTP status code
200 (OK)
{
"status": "success"
}```

ancient knot
#

๐ŸŽ‰

#

Sounds like it is working

tranquil isle
#

thank you very much, I have no further questions