Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

P-Torres's avatar

PHP code in .blade view - Call to undefined function.

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!

0 likes
1 reply
tykus's avatar

I would have expected that function in in the global namespace? So it would be called using createSignature($req, $key);, however, the larger problem is where you are doing this work; it is not the responsibility of the view to do this work.

Ideally, we would separate the view layer from services; to this end, Laravel allows you to inject a service into a view https://laravel.com/docs/5.8/blade#service-injection This would allow you to define the function (and other implementation detail) in an appropriate class.

Depending on how you arrive to this view, a Service class might be appropriate, but it might also be argued that you could handle this in a Controller action(s)

Please or to participate in this conversation.