简体   繁体   中英

Webhook class not found when trying to retrieve and parse authorize.net webhook in php file

I keep getting class not found. - Class 'authnet\\AuthnetWebhook' not found

I do not have composer so have simply copied the files downloaded from - https://php-download.com/package/stymiee/authnetjson

I have copied the folder stymiee under folder vendor.

I have replaced $signaturekey with original key.

Here is my code

use authnet\AuthnetWebhook as AuthnetWebhook;
require 'vendor/stymiee/authnetjson/src/autoload.php';


$headers = getallheaders();
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook($signaturekey, $payload, $headers);
use authnet\AuthnetWebhook as AuthnetWebhook;
require 'vendor/stymiee/authnetjson/src/autoload.php';

You want to swap those lines around. First, you need to require autoload file, so then you have an access to desired namespace.

require 'vendor/stymiee/authnetjson/src/autoload.php';
use authnet\AuthnetWebhook as AuthnetWebhook;

If you use Composer to manage your dependencies the simplest and best way to manage your autoloading to is to load the autoload file generated by Composer and not the autoload files included in each package:

require __DIR__.'/vendor/autoload.php';

This will include the autoloader for all of your dependencies managed by Composer. Then you can call your use statement and the rest of the code. I use __DIR__ to ensure I get the correct relative path when including that file. You may need to tweak your path to match your setup.

require __DIR__.'/vendor/autoload.php';
use authnet\AuthnetWebhook as AuthnetWebhook;

$headers = getallheaders();
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook($signaturekey, $payload, $headers);

If you are not using Composer to manage your dependencies you will need to refer to the autoloader of each package directly like you are currently doing. But you still need to load it before you reference the code in that package.

require 'vendor/stymiee/authnetjson/src/autoload.php';
use authnet\AuthnetWebhook as AuthnetWebhook;

$headers = getallheaders();
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook($signaturekey, $payload, $headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM