| Server IP : 91.108.98.166 / Your IP :
216.73.216.109 [
Web Server : LiteSpeed System : Linux nl-srv-web1124.main-hosting.eu 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64 User : u964240598 ( 964240598) PHP Version : 8.4.19 Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail Domains : 2 Domains MySQL : OFF | cURL : ON | WGET : ON | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /home/u964240598/domains/hofam.org/public_html/churchapp/application/libraries/ |
Upload File : |
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Pesapal v3 Library (CodeIgniter 3)
* ---------------------------------
* - Handles authentication
* - Creates payment orders
* - Verifies transactions (IPN)
*/
class Pesapal
{
private $CI;
private $baseUrl;
public function __construct()
{
$this->CI =& get_instance();
// --------------------------------------------------
// SELECT ENVIRONMENT
// --------------------------------------------------
$env = $this->CI->config->item('pesapal_env'); // 'live' or 'sandbox'
$this->baseUrl = ($env === 'live')
? 'https://pay.pesapal.com/v3/api'
: 'https://cybqa.pesapal.com/pesapalv3/api';
}
/* =========================================================
* CURL HELPER
* ========================================================= */
private function curl($url, $method = 'POST', $data = null, $headers = [])
{
$ch = curl_init($url);
$defaultHeaders = [
'Accept: application/json',
'Content-Type: application/json'
];
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array_merge($defaultHeaders, $headers),
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => 60,
];
if ($data !== null) {
$options[CURLOPT_POSTFIELDS] = json_encode($data);
}
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return [
'error' => [
'message' => $error
]
];
}
$decoded = json_decode($response, true);
// Fallback if Pesapal returns non-JSON
if ($decoded === null) {
return [
'error' => [
'message' => 'Invalid JSON response from Pesapal',
'raw' => $response
]
];
}
return $decoded;
}
/* =========================================================
* AUTHENTICATION
* ========================================================= */
public function authenticate()
{
$key = $this->CI->config->item('pesapal_key');
$secret = $this->CI->config->item('pesapal_secret');
if (!$key || !$secret) {
return null;
}
$res = $this->curl(
$this->baseUrl . '/Auth/RequestToken',
'POST',
[
'consumer_key' => $key,
'consumer_secret' => $secret,
]
);
return $res['token'] ?? null;
}
/* =========================================================
* CREATE ORDER
* ========================================================= */
public function createOrder(array $payload)
{
$token = $this->authenticate();
if (!$token) {
return [
'error' => [
'message' => 'Unable to authenticate with Pesapal'
]
];
}
// --------------------------------------------------
// ENSURE notification_id EXISTS
// --------------------------------------------------
if (empty($payload['notification_id'])) {
return [
'error' => [
'message' => 'Missing Pesapal notification_id (IPN ID)'
]
];
}
return $this->curl(
$this->baseUrl . '/Transactions/SubmitOrderRequest',
'POST',
$payload,
["Authorization: Bearer {$token}"]
);
}
/* =========================================================
* VERIFY TRANSACTION (IPN)
* ========================================================= */
public function verify($trackingId)
{
$token = $this->authenticate();
if (!$token) {
return [
'error' => [
'message' => 'Unable to authenticate with Pesapal'
]
];
}
return $this->curl(
$this->baseUrl . '/Transactions/GetTransactionStatus?orderTrackingId=' . urlencode($trackingId),
'GET',
null,
["Authorization: Bearer {$token}"]
);
}
}
Anon7 - 2022
AnonSec Team
