1. Go to this page and download the library: Download mycoolpay/php-sdk 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/ */
mycoolpay / php-sdk example snippets
yCoolPay\Logging\Logger;
use MyCoolPay\MyCoolPayClient;
define("MCP_PUBLIC_KEY", "<Add your public key here>"); // Your API public key
define("MCP_PRIVATE_KEY", "<Add your private key here>"); // Your API private key
$logger = new Logger('app.log', __DIR__);
$mycoolpay = new MyCoolPayClient(MCP_PUBLIC_KEY, MCP_PRIVATE_KEY, $logger, true);
use Exception;
try {
$response = $mycoolpay->paylink([
"transaction_amount" => 100,
"transaction_currency" => "XAF",
"transaction_reason" => "Bic pen",
"app_transaction_ref" => "order_123",
"customer_phone_number" => "699009900",
"customer_name" => "Bob MARLEY",
"customer_email" => "[email protected]",
"customer_lang" => "en",
]);
$transaction_ref = $response->get("transaction_ref"); // You can store this reference to order in your database
$payment_url = $response->get("payment_url"); // Redirect customer to this url
} catch (Exception $exception) {
$logger->logException($exception);
}
paylink(array $data): MyCoolPay\Http\Response
use Exception;
try {
$response = $mycoolpay->payin([
"transaction_amount" => 100,
"transaction_currency" => "XAF",
"transaction_reason" => "Bic pen",
"app_transaction_ref" => "order_123",
"customer_phone_number" => "699009900",
"customer_name" => "Bob MARLEY",
"customer_email" => "[email protected]",
"customer_lang" => "en",
]);
$transaction_ref = $response->get("transaction_ref"); // You can store this reference to order in your database
$action = $response->get("action"); // This tells you what to do next
if ($action === "REQUIRE_OTP") {
// Ask your user to provide OTP received by SMS
// Then perform OTP request (see next section)
} elseif ($action === "PENDING") {
$ussd = $response->get("ussd"); // Tell user to dial this USSD code on his phone
} else {
throw new Exception("Unknown action '$action' in Payin response");
}
} catch (Exception $exception) {
$logger->logException($exception);
}
payin(array $data): MyCoolPay\Http\Response
use Exception;
try {
$response = $mycoolpay->authorizePayin([
"transaction_ref" => "f4fd89ea-e647-462c-9489-afc0aeb90d5f",
"code" => "123456",
]);
$action = $response->get("action"); // This tells you what to do next
if ($action === "PENDING") {
$ussd = $response->get("ussd"); // Tell user to dial this USSD code on his phone
} else {
throw new Exception("Unknown action '$action' in OTP response");
}
} catch (Exception $exception) {
$logger->logException($exception);
}
use Exception;
use MyCoolPay\Http\Http;
try {
$response = $mycoolpay->payout([
"transaction_amount" => 500,
"transaction_currency" => "XAF",
"transaction_reason" => "Customer refund",
"transaction_operator" => "CM_OM",
"app_transaction_ref" => "refund_123",
"customer_phone_number" => "699009900",
"customer_name" => "Bob MARLEY",
"customer_email" => "[email protected]",
"customer_lang" => "en",
]);
$transaction_ref = $response->get("transaction_ref"); // You can store this reference to payout in your database
$status_code = $response->getStatusCode(); // HTTP status code
$message = $response->getMessage();
if ($status_code === Http::OK) {
// Successful payout
} elseif ($status_code === Http::ACCEPTED) {
// Payout is pending in background
} else {
throw new Exception($message, $status_code);
}
} catch (Exception $exception) {
$logger->logException($exception);
}
payout(array $data): MyCoolPay\Http\Response
use Exception;
try {
$response = $mycoolpay->checkStatus("f4fd89ea-e647-462c-9489-afc0aeb90d5f");
$status = $response->get("transaction_status");
// Do whatever you need with $status
// Possible values are PENDING, SUCCESS, CANCELED and FAILED
} catch (Exception $exception) {
$logger->logException($exception);
}
use Exception;
try {
$response = $mycoolpay->getBalance();
$balance = $response->get("balance");
// Do whatever you need with $balance
} catch (Exception $exception) {
$logger->logException($exception);
}
getBalance(): MyCoolPay\Http\Response
use Exception;
try {
// Adapt this to the framework you are using to get requester IP address
$remote_ip = $_SERVER['REMOTE_ADDR'];
if ($mycoolpay->isVerifiedIp($remote_ip)) {
// Process callback
}
} catch (Exception $exception) {
$logger->logException($exception);
}
isVerifiedIp(string $ip): true
use Exception;
try {
// Adapt this to the framework you are using to get REST JSON data
$callback_data = json_decode(file_get_contents('php://input'), true);
if ($mycoolpay->checkCallbackIntegrity($callback_data)) {
// Process callback
}
} catch (Exception $exception) {
$logger->logException($exception);
}