61 lines
2.1 KiB
PHP
61 lines
2.1 KiB
PHP
<?php
|
|
$endpoint = "https://api.ikhokha.com/public-api/v1/api/payment";
|
|
$appID = "IKFLESZTKFM4HWWS76131L8HK9BYF96P";
|
|
$appSecret = "gfoQTvXRXuzq6ArPHUS2CBFxtHtH1bxM";
|
|
$requestBody = [
|
|
"entityID" => "4",
|
|
"externalEntityID" => "4",
|
|
"amount" => 1000,
|
|
"currency" => "ZAR",
|
|
"requesterUrl" => "https://beta.4wdcsa.co.za/requester",
|
|
"description" => "Test Description 1",
|
|
"paymentReference" => "4",
|
|
"mode" => "sandbox",
|
|
"externalTransactionID" => "5",
|
|
"urls" => [
|
|
"callbackUrl" => "https://beta.4wdcsa.co.za/callback",
|
|
"successPageUrl" => "https://beta.4wdcsa.co.za/success",
|
|
"failurePageUrl" => "https://beta.4wdcsa.co.za/failure",
|
|
"cancelUrl" => "https://beta.4wdcsa.co.za/cancel"
|
|
]
|
|
];
|
|
function escapeString($str) {
|
|
$escaped = preg_replace(['/[\\"\'\"]/u', '/\x00/'], ['\\\\$0', '\\0'], (string)$str);
|
|
$cleaned = str_replace('\/', '/', $escaped);
|
|
return $cleaned;
|
|
}
|
|
function createPayloadToSign($urlPath, $body) {
|
|
$parsedUrl = parse_url($urlPath);
|
|
$basePath = $parsedUrl['path'];
|
|
if (!$basePath) {
|
|
throw new Exception("No path present in the URL");
|
|
}
|
|
$payload = $basePath . $body;
|
|
$escapedPayloadString = escapeString($payload);
|
|
return $escapedPayloadString;
|
|
}
|
|
function generateSignature($payloadToSign, $secret) {
|
|
return hash_hmac('sha256', $payloadToSign, $secret);
|
|
}
|
|
$stringifiedBody = json_encode($requestBody);
|
|
$payloadToSign = createPayloadToSign($endpoint, $stringifiedBody);
|
|
$ikSign = generateSignature($payloadToSign, $appSecret);
|
|
// Initialize cURL session
|
|
$ch = curl_init($endpoint);
|
|
// Set cURL options
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $stringifiedBody);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"IK-APPID: $appID",
|
|
"IK-SIGN: $ikSign"
|
|
]);
|
|
// Execute cURL session
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
// Decode and output the response
|
|
$responseData = json_decode($response, true);
|
|
echo print_r($responseData, true);
|
|
?>
|