Page 1 of 1

Braintree on OpenCart 3.x with PHP 7.2 -- SOLVED

Posted: Mon Feb 18, 2019 3:56 am
by BajaMod
There are many post here but I could not find a resolution. Has anyone found a fix that works for the two errors
PHP Unknown: Function create_function() is deprecated in /var/www/storage/vendor/braintree/braintree_php/lib/Braintree/Util.php on line 205
PHP Unknown: Function create_function() is deprecated in /var/www/storage/vendor/braintree/braintree_php/lib/Braintree/Util.php on line 172

I see many post here but their resolutions seems to be to downgrade PHP. Has anyone gotten this to work on PHP 7.2?

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Mon Feb 18, 2019 4:52 am
by straightlight
In system/storage/vendor/Braintree/Braintree_php/lib/Braintree/Util.php file,

find:

Code: Select all

$callback = create_function('$matches', 'return strtoupper($matches[1]);');
replace with:

Code: Select all

$callback[$delimiter] = function($matches) use ($delimiter) {
				return $delimiter . strtolower($matches[1]);
			};
Then, find:

Code: Select all

$callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
replace with:

Code: Select all

$callbacks[$delimiter] = function($matches) use ($delimiter) {
				return $delimiter . strtolower($matches[1]);
			};
Then, in system/storage/vendor/Braintree/Braintree_php/tests/integration/Error/ValidationErrorCollectionTest.php file,

find:

Code: Select all

$codes = array_map(create_function('$validationError', 'return $validationError->code;'), $validationErrors);
replace with:

Code: Select all

$codes = function ($validationErrors) {
			return strtolower($validationErrors[1]);
		};

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Tue Feb 19, 2019 9:36 am
by BajaMod
I had previously uninstalled when I couldn't get the extension when I could not get it to work.
I re-installed.
I performed the above steps.
I refreshed the Extensions>Modifications Refresh
I Refreshed the Dashboard Theme and SaSS

Now I get error when going to edit setting of PayPal powered by Braintree in the Extensions Menu.

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in /var/www/opencart/upload/system/storage/vendor/braintree/braintree_php/lib/Braintree/Util.php on line 186




Here is that area of the file...
// so use a static variable to avoid adding a new function to memory
// every time this function is called.
static $callback = null;
if ($callback === null) {
$callback[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};

return preg_replace_callback('/' . $delimiter . '(\w)/', $callback, $string);
}

/**
* convert alpha-beta-gamma to alpha_beta_gamma
*
* @access public
* @param string $string
* @return string modified string
*/
public static function delimiterToUnderscore($string)
{
return preg_replace('/-/', '_', $string);
}

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Tue Feb 19, 2019 10:15 pm
by straightlight
You removed a closing bracket after this line:

Code: Select all

return $delimiter . strtolower($matches[1]);
};
You need to put it back:

Code: Select all

}

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Wed Feb 20, 2019 12:33 am
by BajaMod
Ok, now similar error at line 214.
Parse error: syntax error, unexpected 'return' (T_RETURN), expecting function (T_FUNCTION) or const (T_CONST) in /var/www/opencart/upload/system/storage/vendor/braintree/braintree_php/lib/Braintree/Util.php on line 214

Looks like similar issue but I am not seeing anything missing?


Here is my current code:

if (!isset($callbacks[$delimiter])) {
return $delimiter . strtolower($matches[1]);
}
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
}

return preg_replace_callback('/([A-Z])/', $callbacks[$delimiter], $string);
}

public static function delimiterToCamelCaseArray($array, $delimiter = '[\-\_]')
{
$c

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Wed Feb 20, 2019 12:43 am
by BajaMod
Here is a better view of the trouble area...

Original

Code: Select all

           $callbacks[$delimiter] = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
        }

        return preg_replace_callback('/([A-Z])/', $callbacks[$delimiter], $string);
    }


Current

Code: Select all

       $callbacks[$delimiter] = function($matches) use ($delimiter) {
                                return $delimiter . strtolower($matches[1]);
                        };
}

        return preg_replace_callback('/([A-Z])/', $callbacks[$delimiter], $string);
    }

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Wed Feb 20, 2019 2:18 am
by straightlight

Code: Select all

if (!isset($callbacks[$delimiter])) {
return $delimiter . strtolower($matches[1]);
}
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
}
should simply be:

Code: Select all

if (!isset($callbacks[$delimiter])) {
$callbacks[$delimiter] = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
}

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Wed Feb 20, 2019 3:55 am
by BajaMod
Ok, getting close. I can see the Braintree edit settings page again. Now I get this on the edit settings page:

Warning: preg_replace_callback(): Requires argument 2, 'Array', to be a valid callback in /var/www/opencart/upload/system/storage/vendor/braintree/braintree_php/lib/Braintree/Util.php on line 177Warning: preg_replace_callback(): Requires argument 2, 'Array', to be a valid callback in /var/www/opencart/upload/system/storage/vendor/braintree/braintree_php/lib/Braintree/Util.php on line 177

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Wed Feb 20, 2019 6:18 am
by straightlight
In accordance of the GitHub source for this package: https://github.com/braintree/braintree_php/issues/191 , this issue has been fixed. See this link to download the newest package of Braintree: https://github.com/braintree/braintree_ ... tag/3.26.0 . I have also looked at the specific files you've described as they contain the fix as well.

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Fri Feb 22, 2019 6:14 am
by BajaMod
Thanks for your help with this. I am surprised how old the code is in OpenCart 3.0.3.1. This code that you are telling me to use is several versions old also. Before I go down this path I want to make sure you want me to use 3.26.0 that you sent in the link. The GitHub doesn't really explain how to install this in OpenCart. Should I just replace all of the files from GitHub into the appropriate folder in OpenCart? Or should I only replace the Util.php? Test.php?

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Fri Feb 22, 2019 6:56 am
by straightlight
BajaMod wrote:
Fri Feb 22, 2019 6:14 am
Thanks for your help with this. I am surprised how old the code is in OpenCart 3.0.3.1. This code that you are telling me to use is several versions old also. Before I go down this path I want to make sure you want me to use 3.26.0 that you sent in the link. The GitHub doesn't really explain how to install this in OpenCart. Should I just replace all of the files from GitHub into the appropriate folder in OpenCart? Or should I only replace the Util.php? Test.php?
The best way is to make the comparison on your FTP between your extracted ZIP file and your system/storage/vendor/Braintree folder. Once you see both sides with identical readings, then you know you're on the right location to extract your files from your host console's file manager by uploading the ZIP file and use the extract button on the same folder location. The extract button will replace the old files in seconds instead of waiting a long time for the upload from FTP to complete.

Once the files replaced, clear your OC cache: viewtopic.php?f=176&p=739789#p718325 and try a transaction again with Braintree (Site Maintenance set to 'On' recommended if your site is running in production).

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Mon Mar 04, 2019 4:16 am
by BajaMod
I just wanted to follow up. I finally found the time to get back in here and replace the files and everything with Braintree is working.
Opencart 3.0.3.1 and PHP 7.2

Thank you straightlight for all your help. I really appreciate it.

Re: Braintree on OpenCart 3.x with PHP 7.2

Posted: Mon Mar 04, 2019 4:51 am
by straightlight
Excellent. Thanks for getting back mentioning this solution has solved the issue.