Logo Lanfrica

HumphreyTuva/mpesa-stk-push-php

Domain:

digital infrastructuresocioeconomic

Record type:

software
Creator:
Hum
Host:
A full PHP stack for M-Pesa STK Push payments — plain-PHP REST API backend + server-rendered PHP frontend, no framework or Node build step required. # M-Pesa STK Push — PHP A full-stack M-Pesa payment integration built entirely in PHP. It provides a checkout page that triggers an STK Push prompt via Safaricom's Daraja API, polls for the payment result in real time, and records every transaction in SQLite. No Node.js, no React, no build step — just PHP on both the backend and frontend. ## Features - STK Push checkout with live status polling (pending → processing → successful/failed) - Kenyan phone number validation and normalization (07xx, 01xx, +254, 254 formats) - M-Pesa callback handling with transaction persistence (SQLite) - Legal/policy pages (Terms of Service, Privacy Policy, Refund Policy, Acceptable Use Policy) - Zero external dependencies — runs on PHP's built-in server, no framework required ## Tech Stack | Layer | Technology | |------------|----------------------------------------------| | Backend | PHP 8.1+, PDO/SQLite, cURL (Daraja API client)| | Frontend | PHP (server-rendered), vanilla JS, Tailwind CSS (CDN) | | Storage | SQLite (auto-created on first run) | ## Project Structure ``` . ├── backend-php/ REST API │ ├── public/ Front controller (index.php) │ ├── src/ Payment model, MPesaProvider, validators, services │ ├── schema.sql SQLite schema │ └── .env.example Environment variable template │ └── frontend-php/ Checkout UI + legal pages ├── public/ Entry pages + assets (JS/CSS) └── includes/ Shared layout and config ``` ## Prerequisites - PHP 8.1 or later, with the `pdo_sqlite` and `curl` extensions enabled - A Safaricom Daraja account with sandbox (or production) API credentials ## Getting Started ### 1. Backend (API) ```bash cd backend-php cp .env.example .env # Windows: copy .env.example .env ``` Edit `.env` and add your Daraja credentials: ```env MPESA_CONSUMER_KEY=your_consumer_key MPESA_CONSUMER_SECRET=your_consumer_secret MP …

Languages