Code Snippets
Free, reusable code snippets for developers. Save time with ready-to-use solutions.
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;
...
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;...
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;
...
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...
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...
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("...
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...
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">
...
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;
...
Code copied to clipboard!