Skip to content

Latest commit

ย 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 

Repository files navigation

Stripe E-Commerce Payment Integration (SA Take-Home Project)

๐Ÿ† Project Overview

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.

โœจ Features

  • โœ… 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

๐Ÿ“Œ Table of Contents

๐Ÿ”ง Installation & Setup

1๏ธโƒฃ Prerequisites

Ensure you have the following installed:

  • Python 3.x
  • Flask
  • Stripe API Keys (Create an account at Stripe)

2๏ธโƒฃ Setup Instructions

Clone the repository

git clone https://github.com/your-username/stripe-payment-integration.git
cd stripe-payment-integration

Create a virtual environment

python -m venv venv
source venv/bin/activate  # On Windows use venv\Scripts\activate

Install dependencies

pip install -r requirements.txt

3๏ธโƒฃ Configure Environment Variables

  1. Rename sample.env to .env in the project root.
  2. Populate the file with your Stripe test API keys, for example:
STRIPE_SECRET_KEY=sk_test_xxxxxxxxxxxxxxxxx
STRIPE_PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxxxxxx

4๏ธโƒฃ Running the Application

flask run

Then, open http://127.0.0.1:5000/ in your browser to access the application.

โš™๏ธ How It Works

๐Ÿ“Œ Payment Flow

  1. User selects a book โ†’ enter your email address and click "Pay".

  2. Backend creates a Stripe Payment Intent โ†’ /create-payment-intent.

  3. Stripe renders a secure payment form via Elements API.

  4. Enter Dummy Payment Info for Testing
    For example, use Stripeโ€™s test card:

    4242 4242 4242 4242
    

    with any future date for expiry and any 3-digit CVC.

  5. User submits payment โ†’ Payment is processed & confirmed.

  6. Upon success, user is redirected to /success, displaying:

    • โœ… Payment Intent ID
    • โœ… Total Charged Amount
  7. Verify the payment in Stripe Dashboard โ†’ Check the transaction to ensure it was processed successfully.

๐Ÿ—๏ธ Project Structure

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

๐Ÿ”ฌ Technical Implementation

APIs Used

Key Backend Code (app.py)

@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

๐Ÿšง Challenges & Learnings

โœ… Challenges Faced & How They Were Solved

  • Issue: Payment Intent not initializing โ†’ Solution: Ensured clientSecret was properly returned from the backend.
  • Issue: Amount displayed as 25.00 instead of 25 โ†’ Solution: Used parseInt(amount) / 100.
  • Issue: Stripe Elements not mounting โ†’ Solution: Ensured clientSecret was passed before calling elements.create().

๐Ÿ”ฎ Future Enhancements

  • โœ… 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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors