This project is a simple e-commerce checkout system that integrates Stripe Payment Elements for secure transactions. It enables users to select a book, proceed to checkout, and complete a payment seamlessly using Stripeโs API.
- โ Secure payment processing using Stripe Payment Intents API
- โ Dynamic total amount calculation & error handling
- โ Displays Payment Intent ID & Total Charged Amount
- โ Structured to easily extend with additional features
- ๐ง Installation & Setup
- โ๏ธ How It Works
- ๐๏ธ Project Structure
- ๐ฌ Technical Implementation
- ๐ง Challenges & Learnings
- ๐ฎ Future Enhancements
Ensure you have the following installed:
- Python 3.x
- Flask
- Stripe API Keys (Create an account at Stripe)
Clone the repository
git clone https://github.com/your-username/stripe-payment-integration.git
cd stripe-payment-integrationCreate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows use venv\Scripts\activateInstall dependencies
pip install -r requirements.txt- Rename
sample.envto.envin the project root. - Populate the file with your Stripe test API keys, for example:
STRIPE_SECRET_KEY=sk_test_xxxxxxxxxxxxxxxxx
STRIPE_PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxxxxxx
flask runThen, open http://127.0.0.1:5000/ in your browser to access the application.
-
User selects a book โ enter your email address and click "Pay".
-
Backend creates a Stripe Payment Intent โ
/create-payment-intent. -
Stripe renders a secure payment form via Elements API.
-
Enter Dummy Payment Info for Testing
For example, use Stripeโs test card:4242 4242 4242 4242with any future date for expiry and any 3-digit CVC.
-
User submits payment โ Payment is processed & confirmed.
-
Upon success, user is redirected to
/success, displaying:- โ Payment Intent ID
- โ Total Charged Amount
-
Verify the payment in Stripe Dashboard โ Check the transaction to ensure it was processed successfully.
stripe-payment-integration/
โโโ app.py # Main Flask application
โโโ templates/
โ โโโ index.html # Home page
โ โโโ checkout.html # Payment form
โ โโโ success.html # Payment confirmation
โโโ static/
โโโ .env.example # Environment variables
โโโ requirements.txt # Dependencies
โโโ README.md # Documentation
- Payment Intents API: Creates a payment intent before checkout.
- Payment Element: Renders a secure payment form.
@app.route('/create-payment-intent', methods=['POST'])
def create_payment_intent():
data = request.json
amount = data.get("amount")
try:
intent = stripe.PaymentIntent.create(
amount=amount,
currency='usd'
)
return jsonify({'clientSecret': intent.client_secret})
except stripe.error.StripeError as e:
return jsonify({'error': str(e)}), 500- Issue: Payment Intent not initializing โ Solution: Ensured
clientSecretwas properly returned from the backend. - Issue: Amount displayed as
25.00instead of25โ Solution: UsedparseInt(amount) / 100. - Issue: Stripe Elements not mounting โ Solution: Ensured
clientSecretwas passed before callingelements.create().
- โ Enable Webhooks โ Handle real-time payment updates.
- โ
Implement Refund Functionality โ Use
Refund API. - โ Store Transactions in a Database โ Track orders and receipts.
- โ Support Multiple Payment Methods โ Add Apple Pay, Google Pay, Grab Pay, etc.