#mxd3
1 messages · Page 1 of 1 (latest)
Can you be more specific on what you're trying to do?
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```
ill try that
I actually don't see anywhere in the above code where you set the stripe customer id in your database
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
You don't set it in the database there at all
stripe_customer_id = event['data']['object']['customer']
just sets a python variable
oh
user = User.query.filter_by(stripe_customer_id=stripe_customer_id).first() just queries the user table
So you're not setting it anywhere
right, so assign it this: stripe_customer_id = event['data']['object']['customer']
That's just a python variable
That isn't your database model
So is your question why is that python variable null?
yes, trying to get help with swapping that to the customer_id
stripe cli too or nah?
Just need to trace your code and figure out why it's not being set properly
if it helps
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