WooCommerce plugin
A WordPress shop takes M-Pesa, Mixx by Yas, Airtel Money and HaloPesa without writing code: customers pay on a PigaPay page and orders are marked paid automatically.
Install in 3 steps
- WordPress: Plugins > Add New > Upload Plugin, choose
pigapay-woocommerce.zip, then Activate. - PigaPay: API Keys > Create key with "Create payments" (and "Refund customers"). Copy the API key and the webhook secret.
- WordPress: WooCommerce > Settings > Payments > PigaPay: paste both, tick Enable, Save. The shop currency must be TZS. Try with a
pp_test_key first.
PigaPay PHP library
One file, no other packages. Create payments and checkout pages, and check webhook signatures.
1. Send the customer to a PigaPay payment page
The easiest way: PigaPay asks for the phone number, sends the prompt and returns the customer to your site.
<?php
require __DIR__ . '/PigaPay.php';
$pigapay = new PigaPay\Client(getenv('PIGAPAY_KEY')); // pp_test_... while building
$checkout = $pigapay->createCheckout([
'amount' => 25000,
'reference' => 'ORDER-1001',
'description' => 'School fees, term 1',
'success_url' => 'https://yourshop.co.tz/paid',
'cancel_url' => 'https://yourshop.co.tz/cart',
]);
header('Location: ' . $checkout['url']);2. Or send the prompt from your own screen
$payment = $pigapay->createPayment([
'amount' => 5000,
'customer' => ['phone' => '0712345678', 'name' => 'Asha Juma'],
'reference' => 'ORDER-1002',
]);
// $payment['status'] === 'forwarded': the customer has the prompt on the phone3. Receive the result
try {
$event = PigaPay\Webhook::fromRequest(getenv('PIGAPAY_WEBHOOK_SECRET'));
} catch (UnexpectedValueException $e) {
http_response_code(401); // not from PigaPay, or too old
exit;
}
if ($event['event'] === 'payment.completed') {
// mark $event['reference'] as paid (once per $event['event_id'])
}
http_response_code(200);Other languages
The REST API works from any language. Copy these examples; official libraries for more languages are on our list.
// Node.js 18+ (built-in fetch)
const res = await fetch('https://pay.pigatech.com/api/v1/checkout', {
method: 'POST',
headers: { 'Authorization': `Bearer ${process.env.PIGAPAY_KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ amount: 25000, reference: 'ORDER-1001', success_url: 'https://yourshop.co.tz/paid' }),
});
const { data } = await res.json();
// redirect the customer to data.url# Python 3 with requests (pip install requests)
import os, requests
r = requests.post('https://pay.pigatech.com/api/v1/checkout',
headers={'Authorization': 'Bearer ' + os.environ['PIGAPAY_KEY']},
json={'amount': 25000, 'reference': 'ORDER-1001', 'success_url': 'https://yourshop.co.tz/paid'},
timeout=30)
data = r.json()['data']
# redirect the customer to data['url']curl -X POST https://pay.pigatech.com/api/v1/checkout \
-H "Authorization: Bearer $PIGAPAY_KEY" \
-H "Content-Type: application/json" \
-d '{"amount": 25000, "reference": "ORDER-1001", "success_url": "https://yourshop.co.tz/paid"}'Check a webhook signature
Signature header: PigaPay-Signature: t=<time>,v1=<HMAC-SHA256 of "t.body" with your webhook secret>. Refuse events older than 5 minutes.
const crypto = require('crypto');
// Express: app.post('/pigapay/webhook', express.raw({ type: 'application/json' }), handler)
function verifyPigaPay(rawBody, header, secret) {
const parts = Object.fromEntries(String(header).split(',').map(p => p.split('=')));
if (!parts.t || !parts.v1 || Math.abs(Date.now() / 1000 - Number(parts.t)) > 300) return false;
const expected = crypto.createHmac('sha256', secret).update(parts.t + '.' + rawBody).digest('hex');
const a = Buffer.from(expected), b = Buffer.from(parts.v1);
return a.length === b.length && crypto.timingSafeEqual(a, b); // a wrong length is a wrong signature
}import hmac, hashlib, time
def verify_pigapay(raw_body: bytes, header: str, secret: str) -> bool:
parts = dict(p.split('=', 1) for p in header.split(',') if '=' in p)
if 't' not in parts or 'v1' not in parts or abs(time.time() - int(parts['t'])) > 300:
return False
expected = hmac.new(secret.encode(), parts['t'].encode() + b'.' + raw_body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, parts['v1'])Developer guide
The full step-by-step guide for PHP, Node.js, Python, Java and C#, in your dashboard.
Sign in to open