# M-Pesa E-Ticketing System
A complete serverless e-ticketing system with M-Pesa STK Push integration using Safaricom's Daraja API and Firebase Firestore.
## Features
- M-Pesa STK Push payment integration
- Real-time payment callback handling
- Automatic ticket generation
- Firebase Firestore database
- Serverless architecture (Vercel)
- Clean, responsive UI with vanilla JavaScript
## Project Structure
```
mpesa-eticketing/
├── api/
│ ├── initiatePayment.js # Triggers M-Pesa STK Push
│ └── callback.js # Handles M-Pesa payment callbacks
├── public/
│ ├── index.html # Frontend UI
│ ├── script.js # Frontend JavaScript
│ └── style.css # Styling
├── firebase.js # Firebase Admin SDK initialization
├── package.json # Dependencies
├── vercel.json # Vercel configuration
└── .env.example # Environment variables template
```
## Setup Instructions
### 1. Prerequisites
- Node.js (v14 or higher)
- Firebase project
- Safaricom Daraja API account
- Vercel account
### 2. Firebase Setup
1. Go to Firebase Console
2. Create a new project or use existing one
3. Go to Project Settings > Service Accounts
4. Click "Generate New Private Key"
5. Save the JSON file (you'll need this for environment variables)
6. Enable Firestore Database in your project
### 3. Daraja API Setup
1. Go to Daraja Portal
2. Create an account and log in
3. Create a new app to get credentials
4. For testing, use sandbox credentials
5. Note down your Consumer Key and Consumer Secret
### 4. Environment Variables
1. Copy `.env.example` to `.env`:
```bash
cp .env.example .env
```
2. Fill in the environment variables:
- `FIREBASE_SERVICE_ACCOUNT`: Paste your entire Firebase service account JSON as a single line
- `DARAJA_CONSUMER_KEY`: Your Daraja consumer key
- `DARAJA_CONSUMER_SECRET`: Your Daraja consumer secret
- `BUSINESS_SHORTCODE`: Your paybill/till number (use 174379 for sandbox) …