This application serves as an API server that incorporates functionalities. It includes an endpoint that allows you to create a Stripe checkout session, for payment processing. Additionally it provides another endpoint to retrieve the status of a checkout session. The application allows for origin resource sharing (CORS) and generates logs with information level details regarding sent requests.
Welcome to the Stripe Checkout Session API Template! This template has been specifically designed to assist you in setting up an API server. Its primary purpose is to enable the creation of Stripe checkout sessions for processing payments. Alongside this functionality it features endpoints that facilitate the retrieval of checkout session statuses and ensures CORS integration. Moreover logging capabilities at the information level have been incorporated into this template. Follow this guide to easily deploy and run this template on the Lazy platform.
To commence using this template simply click on the "Start with this Template" button found within the Lazy Builder interface. By doing all necessary code will be automatically populated in the Lazy Builder without requiring any copying, pasting or deletion.
Before conducting any testing or utilizing the APIs features there are an environment secrets that need to be set up within the Lazy Builder. These secrets play a role in establishing communication, between your application and Stripe while defining parameters for your checkout sessions.
To set up the necessary environment secrets, for your Stripe integration follow these steps;
1. Access your Stripe dashboard and navigate to Developers > API keys to find your Stripe key.
2. Determine the domain of your frontend application as this will be the domain for your customers after they complete a payment.
3. Create a price ID for the product or service you are selling in your Stripe dashboard under Products.
4. Open the Environment Secrets tab in the Lazy Builder. Add each of the keys as secrets entering their corresponding values.
5. Save all changes made.
6. To test the API simply click on the "Test" button, within the Lazy Builder. This will deploy the app. Launch the Lazy CLI.
Once you've clicked on "Test" Lazy will provide you with a server link to access and utilize the API. Additionally since this template utilizes FastAPI you will receive a link to access API documentation that outlines how to interact with endpoints and understand request and response formats.
To set up a Stripe checkout session you need to send a request, to the /create checkout session endpoint with all the required information. Below is an example of how the request should look;
POST /create-checkout-session
Content-Type: application/json
{
"months": 6,
"utm_source": "newsletter",
"utm_medium": "email",
"utm_campaign": "spring_sale",
"utm_term": "shoes",
"utm_content": "ad_content"
}
Upon completion you will receive a response that looks like this;
{
"clientSecret": "cs_test_a1b2c3d4e5f6g7h8i9j0kExample"
}
The clientSecret is crucial for redirecting users to the Stripe Checkout page on your website.
To integrate this functionality into your frontend application you need to establish a connection with the API. Utilize the provided server link by Lazy to make API requests, from your frontend code. When a user decides to make a purchase initiate the /create checkout session endpoint from your frontend. Then use the received clientSecret to redirect them to the Stripe Checkout page.
To initiate the checkout process you'll need to add the following code snippet to the section of your frontend application. Make sure to replace "YOUR_SERVER_LINK" with the server link provided by Lazy;
```javascript
fetch('YOUR_SERVER_LINK/create checkout session' {
method; 'POST'
headers; {
'Content Type'; 'application/json'
}
body; JSON.stringify({
months; 6
utm_source; 'newsletter'
utm_medium; 'email'
utm_campaign; 'spring_sale'
utm_term; 'shoes
utm_content; 'ad_content'
})
})
.then(response => response.json())
.then(data => {
// Redirect to Stripe Checkout using the clientSecret
stripe.redirectToCheckout({ session_id; data.clientSecret });
})
.catch(error => {
console.error('Error;' error);
});
```
By following these steps you'll be able to integrate the Stripe Checkout Session API into your application using the Lazy platform.