Welcome to the guide on how to implement Stripe in your Python application using a Lazy template. This template is designed to help you integrate a custom Stripe payment page into your application with ease. It includes a backend service set up using FastAPI, which handles the creation and retrieval of Stripe checkout sessions, and a frontend script that you can embed into your Python application to initiate the payment process.
To begin using this template, click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy or paste any code manually.
Before testing the application, you need to set up the required environment secrets:
To add these environment secrets, navigate to the Environment Secrets tab within the Lazy Builder and enter the values for each secret.
Once you have set up the environment secrets, press the "Test" button to deploy the app. Lazy will handle the deployment, and you won't need to worry about installing libraries or setting up your environment.
If the template requires user input through the CLI, you will be prompted to provide it after pressing the "Test" button. Follow the instructions in the Lazy CLI to enter the necessary information.
After deployment, Lazy will provide you with a dedicated server link to use the API. If the template uses FastAPI, you will also receive a link to the API documentation. Use these links to interact with your deployed backend service.
To integrate the Stripe payment functionality into your frontend, you will need to add the provided frontend script to your application. Here's how:
Here is the script you need to add to your frontend:
<div id="checkout"></div>
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe("PUBLISHABLE STRIPE API KEY");
initialize();
function getUTMParameters() {
const params = new URLSearchParams(window.location.search);
return {
utm_source: params.get('utm_source') || '',
utm_medium: params.get('utm_medium') || '',
utm_campaign: params.get('utm_campaign') || '',
utm_term: params.get('utm_term') || '',
utm_content: params.get('utm_content') || ''
};
}
async function initialize() {
const months = 1;
const utmParams = getUTMParameters();
const response = await fetch("LAZY SERVER LINK/create-checkout-session", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
months,
price: 'PRICE_ID',
...utmParams
})
});
const { clientSecret } = await response.json();
const checkout = await stripe.initEmbeddedCheckout({
clientSecret,
});
checkout.mount('#checkout');
}
</script>
By following these steps, you will have successfully integrated a Stripe payment page into your Python application using the Lazy template. If you encounter any issues or need further assistance, refer to the Stripe API documentation or reach out to Lazy's customer support.