# 📱 M-Pesa STK Push Demo — Daraja API Integration
> A production-ready M-Pesa STK Push integration using Safaricom's Daraja API. Enter a phone number and amount — an M-Pesa prompt fires on the customer's phone instantly.
## ✨ Features
- **Real STK Push** — Triggers actual M-Pesa prompts via Daraja sandbox
- **OAuth2 token generation** — Auto-fetches and uses Daraja access tokens
- **Secure password hashing** — `Base64(shortcode + passkey + timestamp)`
- **Callback URL handler** — Receives and processes Safaricom payment confirmations
- **Status polling** — Frontend polls every 3s until confirmed or failed
- **Phone number validation** — Accepts 07XX, 01XX, +254, 254 formats
- **Amount presets** — Quick KES 50 / 100 / 500 / 1K / 2.5K / 5K buttons
- **90s timeout** — Graceful expiry with retry option
- **Go-live ready** — Swap 4 env vars, zero code changes
## 🚀 Quick Start
### 1. Get Daraja credentials (free)
1. Go to developer.safaricom.co.ke
2. Create an account → Create App → Select **Lipa Na M-Pesa Sandbox**
3. Copy your **Consumer Key** and **Consumer Secret**
### 2. Clone & install
```bash
git clone
github.com
cd mpesa-demo
npm install
```
### 3. Configure environment
```bash
cp .env.local.example .env.local
```
Edit `.env.local`:
```env
MPESA_CONSUMER_KEY=your_consumer_key
MPESA_CONSUMER_SECRET=your_consumer_secret
MPESA_SHORTCODE=174379
MPESA_PASSKEY=bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919
NEXT_PUBLIC_BASE_URL=
your-app.vercel.app
```
### 4. Deploy to Vercel first (callback URL needs to be public)
```bash
git init && git add . && git commit -m "feat: M-Pesa STK Push integration"
git remote add origin
github.com
git push -u origin main
```
Then: **vercel.com → New Project → Add env vars → Deploy**
Update `NEXT_PUBLIC_BASE_URL` to your Vercel URL.
## 🏗️ Project Structure
```
mpesa-demo/
├── app/
│ ├── api/mpesa/
│ │ ├── token/route.ts …