I'm new using stripe payments, so I decided to make it easy and implemented redirectToCheckout and stripe webhooks from the stripe documentation, I followed the steps, first I created a product from stripe's dashboard, then I added some metadata key and value, finally I wrote my code on Angular and everything was working great until I realized that i wasn't getting the expected metadata, actually it was empty.
I am using Firebase cloud functions as backend and Angular framework as front,this is my code:
Angular
stripe.redirectToCheckout({
lineItems: [{ price: itemSku, quantity: 1}],
mode: 'payment',
customerEmail: this.userEmail,
successUrl: 'http://localhost:4200/purchase/success',
cancelUrl: 'http://localhost:4200/purchase/failed'
})
Firebase cloud function
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.rawBody, sig, endpointSecret);
} catch (err) {
response.status(404).end()
}
const intent = event.data.object
switch (event.type) {
case constants.INTENT_SUCCESS:
// it prints the object with empty metadata
console.log('Success object:', intent); <- metadata:{}
break;
case constants.INTENT_FAILED:
console.log('Failed:', intent.id);
break;
}
response.json({received: true});
response.sendStatus(200)
});
Each object in Stripe will have different metadata. It sounds like metadata was added to the Product
object which will not be copied to the PaymentIntent
.
I would listen for the checkout.session.completed
webhook notification event type, then fetch the Checkout Session and expanding it's related line_items
, price
, and product
.
const session = await stripe.checkout.sessions.retrieve(
"cs_test_xxx", {
expand: ["line_items.data.price.product"]
}
)
console.log(res.line_items.data[0].price.product);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With