Authorize.net Webhook Webhook通知中发送的JSON无效



我正在尝试在Laravel项目上实现Authorize.net webhook。从商家界面,我添加了一个webhook端点。但当我尝试检索事件时,它会抛出无效的JSON错误。我在下面的代码中做错了什么?

namespace AppHttpControllersApiAnet;
use IlluminateHttpRequest;
use netauthorizeapicontractv1 as AnetAPI;
use netauthorizeapicontroller as AnetController;
use AppHttpControllersController;
use JohnCondeAuthnetAuthnetWebhook;
class xxxController extends Controller
{
public function webhook(){
$headers = getallheaders();
$payload = file_get_contents("php://input");
$webhook = new AuthnetWebhook(hex2bin('XXXXXD4FF0A6060E23DBCD9AE507E20XXXXX'), $payload, $headers);
if ($webhook->isValid()) {
// Get the transaction ID
$transactionId = $webhook->payload->id;
// Here you can get more information about the transaction
$request  = AuthnetApiFactory::getJsonApiHandler('services.authorize.login', 'services.authorize.key');
$response = $request->getTransactionDetailsRequest(array(
'transId' => $transactionId
));
/* You can put these response values in the database or whatever your business logic dictates.
$response->transaction->transactionType
$response->transaction->transactionStatus
$response->transaction->authCode
$response->transaction->AVSResponse
*/
}     
}
}

错误:

"message": "Invalid JSON sent in the Webhook notification",
"exception": "JohnConde\Authnet\AuthnetInvalidJsonException",
"file": "/var/www/html/staging/vendor/stymiee/authnetjson/src/authnet/AuthnetWebhook.php",
"line": 67,

您的问题是没有收到webhook通知。您使用的代码用于验证webhook通知,而不是发出webhook请求。你必须提出一个请求才能获得一个webhook。

当您设置端点时,您可以使用该代码(尽管我认为不需要hex2bin())来验证webhook,然后从中提取信息。

要创建一个webhooks请求,您可以使用这样的代码-

$webhooksArray = array(' net.authorize.payment.authorization.created',' 
net.authorize.payment.authcapture.created',' 
net.authorize.payment.capture.created');
$webhooksUrl = 'https://{yourserver.com}/{your path}/{your endpoint}';
$webhook = new AuthnetAPIFactory();
$handler = $webhook->getWebhooksHandler($login,$transId);
$createWebhooks = $handler->createWebhooks($webhooksArray,$webhooksUrl);

这将为您注册事件,这些事件将自动发送到您的端点即CCD_ 1。

然后,当webhook到达您的端点时,您可以使用上面的代码来验证它们。一旦你注册了事件,并且webhook被发送到你的端点,你就可以使用这样的代码检索历史记录-

$webhook = new AuthnetAPIFactory();
$handler = $webhook->getWebhooksHandler($login,$transId);
$history = $handler->getNotificationHistory();
print_r($history);

你可以检索这样一个特定的webhook-

$webhook = new AuthnetAPIFactory();
$handler = $webhook->getWebhooksHandler($login,$transId);
$webhook = $handler->getWebhook($webhookId);

其中$webhookId是绑定到要检索的webhook的id。您可以在命名空间中搜索,以查看针对特定webhook操作的其他方法调用。

我知道,对这个问题的回答太晚了。但我最近遇到了这个问题。我为我的一个项目解决了这个问题。

控制器方法必须具有Request $request参数,并且路由应该是POST而不是GET

控制器:

class AuthnetWebhookController extends Controller
{
public function webhookListener(Request $request)
{
.....
}
// other methods
}

路线:

Route::post('authnet-webhook-listener', 'AuthnetWebhookController@webhookListener');

最新更新