Code Snippets

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

Reset

9 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
PHP

PHP PDO Connection

Secure database connection using PDO

<?php
$host = "localhost";
$dbname = "database";
$user = "username";
$pass = "password";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ER...
database pdo mysql
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
Python

Python List Comprehension

Efficient way to create lists

# Basic list comprehension
squares = [x**2 for x in range(10)]
print(squares)  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# With condition
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(eve...
list comprehension python
HTML

HTML5 Boilerplate

Basic HTML5 template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Page description">
 ...
html5 template boilerplate
CSS

CSS Flexbox Center

Center elements with Flexbox

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.child {
    width: 200px;
    height: 200px;
    background: #0A7B7C;
    color: white;
   ...
flexbox center css
Code copied to clipboard!
Need assistance? Ask me anything