Code Snippets

Free, reusable code snippets for developers. Save time with ready-to-use solutions.

Reset

5 snippets found

PHP

Daraja API - STK Push (Lipa Na M-Pesa Online)

Complete PHP implementation of Safaricom Daraja API STK Push for Lipa Na M-Pesa Online. Includes access token generation and payment processing.

<?php
/**
 * Daraja API - STK Push (Lipa Na M-Pesa Online)
 * Complete implementation with error handling
 */

class MpesaSTKPush {
    private $consumerKey;
    private $consumerSecret;
    ...
daraja mpesa safaricom payment stkpush api
PHP

Daraja API - C2B (Customer to Business)

Implement Customer to Business payments with validation and confirmation URLs

<?php
/**
 * Daraja API - C2B (Customer to Business)
 * Register URLs and process payments
 */

class MpesaC2B {
    private $consumerKey;
    private $consumerSecret;
    private $shortCode;...
daraja mpesa c2b payment api
PHP

Daraja API - B2C (Business to Customer)

Send money from business to customer (B2C) - withdrawals, refunds, salary payments

<?php
/**
 * Daraja API - B2C (Business to Customer)
 * Send money to customers (withdrawals, refunds, salary)
 */

class MpesaB2C {
    private $consumerKey;
    private $consumerSecret;
   ...
daraja mpesa b2c withdrawal payment
PHP

Daraja API - Callback Handler

Handle STK Push callbacks and process payment notifications

<?php
/**
 * Daraja API - Callback Handler
 * Process STK Push callbacks
 */

// mpesa-callback.php
header("Content-Type: application/json");

// Get the callback data
$callbackData = file_g...
daraja mpesa callback webhook stkpush
JavaScript

JavaScript Fetch API

Modern way to make HTTP requests

// GET request
fetch("https://api.example.com/data")
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error("Error:", error));

// POST request
fetch("...
api fetch ajax
Code copied to clipboard!
Need assistance? Ask me anything