Hi,
I have a view where I have been asked to implement some PHP code.
checkout.blade.php
@section('main')
<h1>Checkout</h1>
@php
// Key
$key = ENV('PAYFAST_KEY');
// Gateway URL
$url = ENV('PAYFAST_URL');
if (!isset($_POST['responseCode'])) {
// Send request to gateway
// Request
$req = array(
'merchantID' => 'xxxxxx',
'action' => 'SALE',
'type' => 1,
'countryCode' => 826,
'currencyCode' => 826,
'amount' => 1001,
'orderRef' => 'Test purchase 101',
'transactionUnique' => uniqid(),
'redirectURL' => url('dashboard/checkout/redirect'),
);
// Create the signature using the function called below.
$req['signature'] = createSignature($req, $key);
echo '<form action="' . htmlentities($url) . '" method="post">' . PHP_EOL;
foreach ($req as $field => $value) {
echo ' <input type="hidden" name="' . $field . '" value="' . htmlentities($value) . '">' . PHP_EOL;
}
echo ' <input type="submit" value="Pay Now">' . PHP_EOL;
echo '</form>' . PHP_EOL;
} else {
// Handle the response posted back
$res = $_POST;
// Extract the return signature as this isn't hashed
$signature = null;
if (isset($res['signature'])) {
$signature = $res['signature'];
unset($res['signature']);
}
// Check the return signature
if (!$signature || $signature !== createSignature($res, $key)) {
// You should exit gracefully
die('Sorry, the signature check failed');
}
// Check the response code
if ($res['responseCode'] === "0") {
echo "<p>Thank you for your payment.</p>";
} else {
echo "<p>Failed to take payment: " . htmlentities($res['responseMessage']) . "</p>";
}
}
// Function to create a message signature
function createSignature(array $data, $key)
{
// Sort by field name
ksort($data);
// Create the URL encoded signature string
$ret = http_build_query($data, '', '&');
// Normalise all line endings (CRNL|NLCR|NL|CR) to just NL (%0A)
$ret = str_replace(array('%0D%0A', '%0A%0D', '%0D'), '%0A', $ret);
// Hash the signature string and the key together
return hash('SHA512', $ret . $key);
}
@endphp
@endsection
With the above code I get:
Call to undefined function createSignature() (View: /home/vagrant/code/goldstarcompetitions.com/resources/views/dashboard/checkout.blade.php
I have tried in both places (no luck):
$this->createSignature($req, $key);
Thank you for your time and advice!