#mxd3

1 messages · Page 1 of 1 (latest)

arctic nimbusBOT
rapid bough
#

Can you be more specific on what you're trying to do?

latent geode
#

yes

#
400 (Bad Request)

{
  "message": "No user associated with the given Stripe customer ID",
  "status": "failure"
}```
#

stripe_customer_id = NULL (in the database) , should be after user subscribes something like cus_OG3syY0HXU7Xzg

#
def webhook():
    event = None
    payload = request.data
    sig_header = request.headers['STRIPE_SIGNATURE']

    try:
        event = stripe.Webhook.construct_event(
            payload, sig_header, endpoint_secret
        )
    except ValueError as e:
        return str(e), 400
    except stripe.error.SignatureVerificationError as e:
        return str(e), 400


    stripe_customer_id = None
    if event['type'] in ['customer.subscription.created', 'customer.subscription.updated', 'customer.subscription.deleted', 'checkout.session.completed']:
        stripe_customer_id = event['data']['object']['customer']


    user = None
    if stripe_customer_id:
        user = User.query.filter_by(stripe_customer_id=stripe_customer_id).first()

    if not user:
        # Handle the case when no user is associated with the given Stripe customer ID
        return jsonify({'status': 'failure', 'message': 'No user associated with the given Stripe customer ID'}), 400


    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```
rapid bough
#

So what's the problem

#

Your code is not storing the customer id properly?

latent geode
#

yeah the webhook isn't swapping the NULL value to the customer id

#

any advice

rapid bough
#

Have you tried using a debugger?

#

To see where stripe_customer_id is set?

latent geode
#

ill try that

rapid bough
#

I actually don't see anywhere in the above code where you set the stripe customer id in your database

latent geode
#
    stripe_customer_id = None
    if event['type'] in ['customer.subscription.created', 'customer.subscription.updated', 'customer.subscription.deleted', 'checkout.session.completed']:
        stripe_customer_id = event['data']['object']['customer']

    # Look up the corresponding user in your database
    user = None
    if stripe_customer_id:
        user = User.query.filter_by(stripe_customer_id=stripe_customer_id).first()

    if not user:
        # Handle the case when no user is associated with the given Stripe customer ID
        return jsonify({'status': 'failure', 'message': 'No user associated with the given Stripe customer ID'}), 400
rapid bough
#

You don't set it in the database there at all

#

stripe_customer_id = event['data']['object']['customer']
just sets a python variable

latent geode
#

oh

rapid bough
#

user = User.query.filter_by(stripe_customer_id=stripe_customer_id).first() just queries the user table

#

So you're not setting it anywhere

latent geode
#

right, so assign it this: stripe_customer_id = event['data']['object']['customer']

rapid bough
#

That's just a python variable

#

That isn't your database model

#

So is your question why is that python variable null?

latent geode
#

yes, trying to get help with swapping that to the customer_id

rapid bough
#

Got it

#

So yeah use a debugger

#

Or print statements

latent geode
#

stripe cli too or nah?

rapid bough
#

Just need to trace your code and figure out why it's not being set properly

rapid bough
#

But the issue here is your code

#

off-hand, stripe_customer_id will always be null if the event type isn't in this list: ['customer.subscription.created', 'customer.subscription.updated', 'customer.subscription.deleted', 'checkout.session.completed']

#

But the real issue could also be something else, so recommend you debug

latent geode
#

alright, thank you for your help!

#

that is all for me