I have a Swift iOS app that works well communicating with a local server on my laptop to make Stripe payments using the below server.js code
const stripe = require('stripe')('sk_test_');
const express = require('express');
const app = express();
app.use(express.json());
app.post('/prepare-payment-sheet', async (req, res) => {
const customer = await stripe.customers.create();
const ephemeralKey = await stripe.ephemeralKeys.create({customer: customer.id},
{apiVersion: '2024-04-10'});
const paymentIntent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
customer: customer.id,
automatic_payment_methods: {
enabled: true,
},
});
res.json({
paymentIntentID: paymentIntent.id,
clientSecret: paymentIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
publishableKey: 'pk_test_'
});
});
app.post('/update-payment-sheet', async (req, res) => {
const paymentIntent = await stripe.paymentIntents.update(
req.body.paymentIntentID,
{
amount: req.body.amount,
}
);
console.log(req.body)
console.log(res.body)
res.json({});
});
However I cannot transfer this code to AWS Lambda and get the app to communicate between the app and the function like it does on the local server on my laptop.
Below is the code for the Lambda function that is not working in the index.mjs file. The function is setup as a Node.js 20.x file within AWS.
//export const handler = async (event) => {
// TODO implement
const stripe = require('stripe')('sk_test_');
const express = require('express');
const app = express();
app.use(express.json());
app.post('/prepare-payment-sheet', async (req, res) => {
const customer = await stripe.customers.create();
const ephemeralKey = await stripe.ephemeralKeys.create({customer: customer.id},
{apiVersion: '2024-04-10'});
const paymentIntent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'usd',
customer: customer.id,
automatic_payment_methods: {
enabled: true,
},
});
res.json({
paymentIntentID: paymentIntent.id,
clientSecret: paymentIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
publishableKey: 'pk_test_'
});
});
app.post('/update-payment-sheet', async (req, res) => {
const paymentIntent = await stripe.paymentIntents.update(
req.body.paymentIntentID,
{
amount: req.body.amount,
}
);
console.log(req.body)
console.log(res.body)
res.json({});
});
//const response = {
// statusCode: 200,
// body: JSON.stringify('Hello from Lambda!'),
// };
// return response;
//};
Below is the Swift code to call the URL of the local server as well as the AWS Lambda function
//private let backendtUrl = URL(string: "http://localhost:4242")!
private let backendtUrl = URL(string: "https://i7phb...q0houbc.lambda-url.us-east-2.on.aws/")!
I’ve been stuck here for days and could really use some assistance on how to convert the server.js file that works on the local server to the index.mjs file in the Lambda function