See the code behind this demo!

Checkout donations

You should normally never set your price on the client, but it does have its uses, such as variable amounts for donations. This will demonstrate how to create Checkout Sessions with a user-defined amount.



                                      
              fastify.post("/create-checkout-session", async (request, reply) => {
  
                // fail silently if the amount is less than $0.50, as that's 
                // the minimum Stripe can charge
                if (Number.isNaN(request.body.amount) || request.body.amount < 0.5) {
                  return reply.send({
                    error: 'Invalid amount sent',
                  });
                }

                const session = await stripe.checkout.sessions.create({
                  payment_method_types: ['card'],
                  line_items: [
                    {
                      price_data: {
                        currency: 'usd',
                        product_data: {
                          name: 'Tiny Demos donation',
                          images: ['https://i.imgur.com/EHyR2nP.png'],
                        },
                        unit_amount: request.body.amount * 100,
                      },
                      quantity: 1,
                    },
                  ],
                  mode: 'payment',
                  success_url: `${process.env.BASE_URI}?success=true`,
                  cancel_url: `${process.env.BASE_URI}/?cancel=true`,
                });

                reply.redirect(session.url);
              });
          
        

Experiment and learn

Make copies, remix, change and learn how to play with this tinydemo.


Any questions?

Reach out to us on any of our contact channels: