by Lazy Sloth
Create Charge with Stripe API
import os
from flask import Flask, jsonify, render_template_string, request
# Set the Stripe API key from environment variables
stripe_api_key = os.environ.get('STRIPE_API_KEY')
if not stripe_api_key:
raise ValueError("The STRIPE_API_KEY environment variable is not set.")
import stripe
stripe.api_key = stripe_api_key
app = Flask(__name__)
# HTML content for creating a charge and displaying the result
HTML_CONTENT = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create a Stripe Charge</title>
</head>
<body>
Created: | Last Updated:
Introduction to the Stripe Charge Creation Template
Welcome to the Stripe Charge Creation Template! This template is designed to help you quickly set up a web application that can create charges using the Stripe API. It's built with Flask, a lightweight web framework for Python, and includes a simple frontend form to collect payment details and a backend endpoint to process the charge. This article will guide you through the steps needed to get this template up and running on the Lazy platform.
Getting Started
To begin using this template, click on "Start with this Template" in the Lazy Builder interface. This will pre-populate the code in the Lazy Builder, so you won't need to copy or paste any code manually.
Initial Setup
Before you can test and use the application, you need to set up an environment secret for the Stripe API key. Follow these steps to configure your environment:
- Log in to your Stripe account and navigate to the API section to find your API keys.
- Copy your Stripe secret key. Be sure to keep this key secure and do not share it publicly.
- In the Lazy Builder interface, go to the Environment Secrets tab.
- Create a new secret with the key as
STRIPE_API_KEY
and paste your Stripe secret key as the value.
With the environment secret set, your application will be able to authenticate with Stripe's services.
Test
Press the "Test" button in the Lazy Builder. This will deploy your application and launch the Lazy CLI. No additional user input through the CLI is required at this stage since the necessary configuration is done through environment secrets.
Using the App
Once the application is deployed, Lazy will provide you with a dedicated server link. Use this link to access the web interface of your application. Here's how to create a charge:
- Open the provided server link in your web browser to view the form.
- Enter the amount in cents, currency (e.g., "usd"), and source (e.g., "tok_visa") in the respective fields.
- Click the "Submit Payment" button to send the payment information to your Flask backend.
- The result of the charge creation will be displayed on the same page under the form.
If you encounter any issues, check the Stripe documentation for troubleshooting and ensure that your Stripe API key is correctly set up in the environment secrets.
Integrating the App
If you wish to integrate this charge creation functionality into another service or frontend, you can use the server link provided by Lazy as the endpoint for your API calls. Ensure that you secure your API and only allow authorized calls to the /create_charge
endpoint.
For example, if you're integrating with a frontend application, you would send a POST request to the /create_charge
endpoint with the payment details. Here's a sample request you might use in your external application:
fetch('YOUR_LAZY_SERVER_LINK/create_charge', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
amount: 1000, // Amount in cents
currency: 'usd',
source: 'tok_visa' // Replace with a valid token
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Replace YOUR_LAZY_SERVER_LINK
with the server link provided by Lazy. The response will contain the result of the charge attempt.
By following these steps, you should now have a fully functional Stripe charge creation application running on the Lazy platform. Happy building!