title: How do I write a Lambda function for my Stripe integration?
body: <p><strong>Background:</strong> I am migrating our sales pages to a serverless CDN using Gatsby -&gt; Netlify, and am trying to implement a Stripe customized payment flow because we want to customize our checkout form. I implemented the Stripe Checkout on one of our landing pages here, but it's not what we want. <a href="https://14-day-filmmaker.netlify.app/" rel="nofollow noreferrer">Landing Page</a></p>
  <p><a href="https://stripe.com/docs/payments/integration-builder" rel="nofollow noreferrer">Stripe's documentation</a> is very straightforward, but the documentation assumes one is running a server.</p>
  <p>The below code is the server implementation snippet from their documentation.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require("express");
  const app = express();
  // This is your real test secret API key.
  const stripe = require("stripe")("sk_test_51J085aDDSnMNt7v1ZO3n5fxobP6hhEhf1uC2SDmkHGIX8fCnxFiSMrxITKu06ypYUHYAMHVZ1lhc5Fqm7UoVa6dx00XvV5PZzG");
  
  app.use(express.static("."));
  app.use(express.json());
  
  const calculateOrderAmount = items =&gt; {
    // Replace this constant with a calculation of the order's amount
    // Calculate the order total on the server to prevent
    // people from directly manipulating the amount on the client
    return 1400;
  };
  
  app.post("/create-payment-intent", async (req, res) =&gt; {
    const { items } = req.body;
    // Create a PaymentIntent with the order amount and currency
    const paymentIntent = await stripe.paymentIntents.create({
      amount: calculateOrderAmount(items),
      currency: "usd"
    });
  
    res.send({
      clientSecret: paymentIntent.client_secret
    });
  });
  
  app.listen(4242, () =&gt; console.log('Node server listening on port 4242!'));</code></pre>
  </div>
  </div>
  </p>
  <p>Here's Stripe's payment flow.</p>
  <p><a href="https://i.stack.imgur.com/yc09l.png" rel="nofollow noreferrer">Stripe's Payment Processing Flow</a></p>
  <p><strong>Challenge:</strong> Since I am moving our content to Netlify, which is a serverless CDN, I need to use Lambda functions to interact with the Stripe API instead of the express server implementation in their example.</p>
  <p>Netlify describes using <a href="https://docs.netlify.com/functions/build-with-javascript/" rel="nofollow noreferrer">serverless functions</a> on their platform here.</p>
  <p>I was able to run this silly little thing. But I know how request that output in my React app if I wanted to...</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>exports.handler = async function(event, context, callback) {
      const { STRIPE_PUBLISHABLE_KEY } = process.env;
      console.log("I am the Hello World Lambda function.")
      return {
          statusCode: 200,
          body: JSON.stringify({message: STRIPE_PUBLISHABLE_KEY})
      };
  }</code></pre>
  </div>
  </div>
  </p>
  <p>I know I'm displaying my level of skill here, but as my Dad would say: better shame than pain.</p>
  <p><strong>Question:/Ask</strong> Can someone help me understand how to think about this?</p>
  <p>I don't know if this would be a help, but here's my <a href="https://github.com/fadethegap/fdfm-1" rel="nofollow noreferrer">git repository</a>.</p>
  <p>Any help would be incredibly appreciated. And if any of you have a few spare cycles and would consider mentoring me on this over a zoom meeting. I will absolutely pay you for your time.</p>
  <p>Thanks much!</p>
  <p>John</p>