1

我使用stymiee/authnetjson库在沙盒环境中使用和验证 authorize.net webhook。

我可以验证我的标题包括

X-ANET-Signature: sha512=C3CC15F7801AA304C0840C85E4F0222A15338827EE3D922DC13A6BB99DF4BFE7D8E235A623480C0EAF3151F7B008E4DFBFDC6E9F493A6901961C5CFC10143289

我的 json 身体是

{"notificationId":"c5933ec1-2ef6-4962-a667-10552d19c481","eventType":"net.authorize.payment.authcapture.created","eventDate":"2018-01-20T20:36:54.9163559Z","webhookId":"66cf7109-f42f-45b9-bf36-f1ade83eac48","payload":{"responseCode":1,"authCode":"37ATT8","avsResponse":"Y","authAmount":550.00,"entityName":"transaction","id":"60038744863"}}

我设置了一个签名密钥,这一切似乎都是正确的,并且与库测试中的类似

我的代码如下所示:

$signature_key = "...";
$headers = getallheaders();
$payload = file_get_contents("php://input");

$webhook = new JohnConde\Authnet\AuthnetWebhook($signature_key, $payload, $headers);

if ( ! $webhook->isValid() ) {
    error_log("Payload not valid");
}

我也尝试$headers从构造函数 args 中删除参数

当我执行测试事务时,它是无效的,所以我在日志中得到“有效负载无效”。我该如何调试这个问题?

谢谢!NFV

4

1 回答 1

1

此问题是由X-ANET-Signature区分大小写引起的。我最初正在寻找它,就像你看到的那样,但是另一个用户向我提出了同样的问题,但他们得到X-Anet-Signature的却是代码没有预料到的。当我进行调试时,我看到了同样的问题,并认为我在第一次编码时出现了错误,或者 Authnet 进行了更改,我需要适应。

显然这不是问题。我不确定为什么我们会看到这个标题不一致的情况,但我会联系 Authorize.Net 看看我是否能找出这个故事是什么。

但修复很简单:更新库以使用版本 3.1.4,这在检查此标头的值时不区分大小写。

为了回答这个问题的字面意思,这里有一个用于调试这个问题的示例脚本:

<?php

namespace myapplication;

use JohnConde\Authnet\AuthnetWebhook;

// Include a configuration file with the Authorize.Net API credentials
require('./config.inc.php');

// Include my application autoloader
require('./vendor/autoload.php');

$errorCode = null;
$errorText = null;
$isValid   = null;

try {
    $headers = getallheaders();
    $payload = file_get_contents("php://input");
    $webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload, $headers);
    $isValid = 'false';
    if ($webhook->isValid()) {
        $isValid = 'true';
    }
    $hashedBody = strtoupper(hash_hmac('sha512', $payload, AUTHNET_SIGNATURE));
    $hash = explode('=', $headers['X-Anet-Signature'])[1];
    $valid = strtoupper(explode('=', $headers['X-Anet-Signature'])[1]) === $hashedBody;
}
catch (\Exception $e) {
    $errorCode = $e->getCode();
    $errorText = $e->getMessage();
}
finally {
    ob_start(); 
    var_dump([
        'errorCode'  => $errorCode,
        'errorText'  => $errorText,
        'isValid'    => $isValid,
        'headers'    => $headers,
        'payload'    => $payload,
        'hashedBody' => $hashedBody,
        'hash'       => $hash,
        'valid'      => $valid
    ]);
    $dump = ob_get_clean();
    file_put_contents('webhooks.txt', $dump, FILE_APPEND | LOCK_EX);
}
于 2018-01-22T03:14:07.697 回答