Search code examples
phplaravelcoinpayments-api

hash_hmac() expects parameter 2 to be string, array given


Im getting this error:

[2019-04-04 05:00:04] local.ERROR: hash_hmac() expects parameter 2 to be
 string, array given {"exception":"[object] (ErrorException(code: 0): hash_hmac() 
expects parameter 2 to be string, array given at /home/domains/domain.com/smm/app/Http/Controllers/CoinPaymentsController.php:132)[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'hash_hmac() exp...', '/home/u37281288...', 132, Array)#1 

This is my code:

 public function ipn(Request $request)
    {
        if (!$request->filled('ipn_mode') || !$request->filled('merchant')) {
            activity('coinpayments')
                ->withProperties(['ip' => $request->ip()])
                ->log('Missing POST data from callback.');
            die();
        }

        if ($request->input('ipn_mode') == 'httpauth') {
            //Verify that the http authentication checks out with the users supplied information
            if ($request->server('PHP_AUTH_USER') != $this->merchantId || $request->server('PHP_AUTH_PW') != $this->secretKey) {
                activity('coinpayments')
                    ->withProperties(['ip' => $request->ip()])
                    ->log('Unauthorized HTTP Request');
                die();
            }

        } elseif ($request->input('ipn_mode') == 'hmac') {
            // Create the HMAC hash to compare to the recieved one, using the secret key.
// line 132 of the error...
            $hmac = hash_hmac("sha512", $request->all(), $this->secretKey);

            if ($hmac != $request->server('HTTP_HMAC')) {
                activity('coinpayments')
                    ->withProperties(['ip' => $request->ip()])
                    ->log('Unauthorized HMAC Request');
                die();
            }

        } else {
            activity('coinpayments')
                ->withProperties(['ip' => $request->ip()])
                ->log('Unauthorized HMAC Request');
            die();
        }

        // Passed initial security test - now check the status
        $status = intval($request->input('status'));
        $statusText = $request->input('status_text');

        if ($request->input('merchant') != $this->merchantId) {
            activity('coinpayments')
                ->withProperties(['ip' => $request->ip()])
                ->log('Mismatching merchant ID. MerchantID:' . $request->input('merchant'));
            die();
        }

I'm trying to add CoinPayments on my website.. and when I setup IPN URL I get this error.. The payment is coming to my account but the product isn't getting avaiable to download..


Solution

  • You need to learn read the error message properly before posting it in stackoverflow, $request->all() give you an array but hash_hmac() method expect a string that's all, If you want all data. then you can use implode() function for make a string from input array.

    $inputs = $request->all();
    $string = implode("",$inputs);
    $hmac = hash_hmac("sha512", $string, $this->secretKey);