Background: I am migrating our sales pages to a serverless CDN using Gatsby -> 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. Landing Page
Stripe's documentation is very straightforward, but the documentation assumes one is running a server.
The below code is the server implementation snippet from their documentation.
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 => {
// 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) => {
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, () => console.log('Node server listening on port 4242!'));
Here's Stripe's payment flow.
Stripe's Payment Processing Flow
Challenge: 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.
Netlify describes using serverless functions on their platform here.
I was able to run this silly little thing. But I know how request that output in my React app if I wanted to...
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})
};
}
I know I'm displaying my level of skill here, but as my Dad would say: better shame than pain.
Question:/Ask Can someone help me understand how to think about this?
I don't know if this would be a help, but here's my git repository.
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.
Thanks much!
John