【问题标题】:Authorize.net web hook Invalid JSON sent in the Webhook notificationAuthorize.net web hook Webhook 通知中发送的 JSON 无效
【发布时间】:2019-05-25 16:24:32
【问题描述】:

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

namespace App\Http\Controllers\Api\Anet;

use Illuminate\Http\Request;
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
use App\Http\Controllers\Controller;
use JohnConde\Authnet\AuthnetWebhook;

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,

【问题讨论】:

  • $payload的值是多少?
  • 您能否发布第 67 行或突出显示这是哪一行?似乎不完整,没有它就很难给出建议。猜测这些让我感到紧张。我会尝试输出请求的响应,而不是调用任何期望有效 JSON 的东西来查看我得到的输出,然后从那里找出原因。
  • 如果你能做一个var_dump($payload); 来向我们展示 JSON 的样子,这会很有帮助。
  • @JohnConde file_get_contents("php://input");返回空值。
  • @weaver 那么这显然是问题所在。构造函数期望该参数包含有效的 JSON,而不是空值。你需要弄清楚为什么触发 webhook 的东西没有发送 json

标签: php laravel payment-gateway authorize.net authorize.net-webhooks


【解决方案1】:

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

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

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

$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);

这会将您注册到事件中,这些事件将自动发送到您的端点 即https://{yourserver.com}/{your path}/{your endpoint}

然后,您可以使用上面的代码在 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 操作的其他方法调用。

【讨论】:

    【解决方案2】:

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

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

    控制器:

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

    路线:

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

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 2021-07-31
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      相关资源
      最近更新 更多