PHP code example of zenopay / zenopay-php

1. Go to this page and download the library: Download zenopay/zenopay-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zenopay / zenopay-php example snippets



// config.php

// Your ZenoPay API key
define('ZP_API_KEY', 'YOUR_API_KEY_HERE');

// Base URL for ZenoPay endpoints
define('ZP_BASE_URL', 'https://zenoapi.com/api/payments');


erData = [
    'order_id'    => uniqid('', true),
    'buyer_email' => '[email protected]',
    'buyer_name'  => 'John Doe',
    'buyer_phone' => '0744963858', // Tanzanian format
    'amount'      => 1000,         // Amount in TZS
    'webhook_url' => 'https://your-domain.com/webhook', // Optional
];

$ch = curl_init(ZP_BASE_URL . '/mobile_money_tanzania');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'x-api-key: ' . ZP_API_KEY,
    ],
    CURLOPT_POSTFIELDS     => json_encode($orderData),
]);

$response = curl_exec($ch);
if ($response === false) {
    file_put_contents('error_log.txt', date('[Y-m-d H:i:s] ') . 'cURL Error: ' . curl_error($ch) . PHP_EOL, FILE_APPEND);
    exit('Request failed. Check error_log.txt');
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200) {
    file_put_contents('error_log.txt', date('[Y-m-d H:i:s] ') . "HTTP Error: {$httpCode} - {$response}" . PHP_EOL, FILE_APPEND);
    exit("HTTP {$httpCode}: {$response}");
}

$data = json_decode($response, true);
if ($data['status'] === 'success') {
    echo "✅ Order created! Order ID: {$data['order_id']}\n";
} else {
    echo "❌ Error: {$data['message']}\n";
}


erId = '3rer407fe-3ee8-4525-456f-ccb95de38250';

$url = ZP_BASE_URL . '/order-status?order_id=' . urlencode($orderId);

$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'x-api-key: ' . ZP_API_KEY,
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    exit('cURL Error: ' . curl_error($ch));
}

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
    exit("HTTP {$httpCode}: {$response}");
}

$data = json_decode($response, true);
if (!empty($data['data']) && $data['result'] === 'SUCCESS') {
    foreach ($data['data'] as $order) {
        echo "🔎 Order ID: {$order['order_id']}\n";
        echo "   Status: {$order['payment_status']}\n";
        echo "   Amount: {$order['amount']} TZS\n";
        echo "   Reference: {$order['reference']}\n";
    }
} else {
    echo "❌ Error: {$data['message']}\n";
}


$_SERVER['HTTP_X_API_KEY'] !== ZP_API_KEY) {
    http_response_code(403);
    exit('Invalid API key');
}

$payload = file_get_contents('php://input');

file_put_contents(
    'weblogs.txt',
    date('[Y-m-d H:i:s] ') . $payload . PHP_EOL,
    FILE_APPEND
);

$data = json_decode($payload, true);

// Example payload:
// {
//   "order_id":"677e43274d7cb",
//   "payment_status":"COMPLETED",
//   "reference":"1003020496",
//   "metadata":{
//     "product_id":"12345",
//     "color":"blue",
//     "size":"L",
//     "custom_notes":"Please gift-wrap this item."
//   }
// }

// TODO: update your DB, send email, etc.

http_response_code(200);
echo 'OK';