#Phoenix_
1 messages · Page 1 of 1 (latest)
What's the event ID?
This is a payment_method.attached, I believe you should listen to checkout.session.completed event in order to get the metadata from a checkout session object
So webhook is wrong?
@app.route('/webhook', methods=['POST'])
def webhook():
event = None
payload = request.data
sig_header = request.headers['STRIPE_SIGNATURE']
try:
event = stripe.Webhook.construct_event(
payload, sig_header, 'whsec_45e053f0b777e02850555bdae985d3c594f095eb0b00e4c29ed083b5614cb0eb'
)
except ValueError as e:
# Invalid payload
raise e
except stripe.error.SignatureVerificationError as e:
# Invalid signature
raise e
# Handle the event
if event['type'] == 'payment_intent.succeeded':
print(event, '(*************)')
payment_intent = event['data']['object']
print(payment_intent, 'OOOOOOOOOOOOOOOO')
# ... handle other event types
else:
print('Unhandled event type {}'.format(event['type']))
return jsonify(success=True)
This is my webhook.
If you're listening for payment_intent.* events as opposed to checkout.session.* events, you should use this metadata param: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Would you like to provide some sample code to send metadata?
I still don't get metadata.
What have you tried? Where are you expecting to see the metadata?
I want to see metadata in webhook response.
And sending metadata when creating checkout.
@app.route('/create-checkout-session', methods=['POST'])
def create_checkout_session():
metadata = request.json["metadata"]
price_id = request.json["price_id"]
print(metadata, price_id)
try:
checkout_session = stripe.checkout.Session.create(
line_items=[
{
# Provide the exact Price ID (for example, pr_1234) of the product you want to sell
'price': price_id,
'quantity': 1
},
],
metadata = metadata,
mode='subscription',
success_url='http://localhost:3000/settings' + '?success=true',
cancel_url='http://localhost:3000/settings' + '?canceled=true',
)
except Exception as e:
print(e)
return str(e)
# return redirect(checkout_session.url, code=303)
print(checkout_session.url)
return jsonify({'redirectUrl': checkout_session.url}), 200
Did you try using the other parameter I shared above in your session creation code?
The metadata parameter sets on the Checkout Session object, and therefore only on checkout.session.* events (which you're not listening to)
I am looking this. But as I have no much experience, I didn't get the point.
So I can use another parameters such as custom_fields?
No, payment_intent_data.metadata: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Then your payment_intent.succeeded events will include those fields
You can not pass payment_intent_data in subscription mode.
I am getting this error.
Ok, yep. I missed you were using mode: 'subscription'. Why specifically do you need metadata on the Payment Intent events?
I want to send user_id in backend.
Then use checkout.session.completed events instead and your existing code will work
Looks like you're using the CLI? stripe listen?
You likely need to add the checkout.session.completed event to your command: stripe listen --events=checkout.session.completed --forward-to=https://xxx