How I Built a Mental Health Platform with M-Pesa Payments
Behind the scenes of building SalusCare — a clinical psychology platform with AI-guided intake, encrypted messaging, and M-Pesa + Paystack payments in Next.js.
The Brief
SalusCare came to me with a clear vision: a platform where clients in Kenya could book psychology sessions, complete clinical assessments, and pay via M-Pesa — all in one place. The existing solution was WhatsApp messages and manual bank transfers.
The Challenge
Mental health platforms carry extra responsibility. We needed end-to-end encrypted messaging, validated clinical assessment tools (PHQ-9, GAD-7, PCL-5), and frictionless M-Pesa payments for a market that does not use credit cards.
AI-Guided Intake
export async function POST(req: Request) {
const { responses, clientId } = await req.json()
await db.intake.create({
data: {
clientId,
responses: JSON.stringify(responses),
urgencyScore: responses.urgency,
completedAt: new Date(),
}
})
if (responses.urgency >= 8) {
await flagForImmediateReview(clientId)
}
return NextResponse.json({ success: true })
}
Encrypted Messaging
import crypto from "crypto"
const ALGORITHM = "aes-256-gcm"
const KEY = Buffer.from(process.env.ENCRYPTION_KEY!, "hex")
export function encrypt(text: string): string {
const iv = crypto.randomBytes(16)
const cipher = crypto.createCipheriv(ALGORITHM, KEY, iv)
const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()])
const authTag = cipher.getAuthTag()
return [iv, authTag, encrypted].map(b => b.toString("hex")).join(":")
}
M-Pesa + Paystack Dual Payment
export async function POST(req: Request) {
const { method, amount, phone, email, sessionId } = await req.json()
if (method === "mpesa") {
const result = await triggerSTKPush({ phone, amount, reference: sessionId })
return NextResponse.json({ provider: "mpesa", checkoutRequestId: result.CheckoutRequestID })
}
if (method === "card") {
const reference = "SALUS-" + Date.now()
return NextResponse.json({ provider: "paystack", reference, amount, email })
}
}
Results
Session booking time reduced from 15 minutes to under 2 minutes. M-Pesa payments confirmed instantly with automatic session scheduling. If you are building a health or sensitive-data platform, reach out.
Work With James
Need help with your project?
Whether it’s M-Pesa integration, a full web application, or a performance audit — reach out and let’s build something great.
Get In Touch →