0

我正在尝试在 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,
4

2 回答 2

1

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

控制器方法必须有参数,路由不Request $request应该。POSTGET

控制器:

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

路线:

Route::post('authnet-webhook-listener', 'AuthnetWebhookController@webhookListener');
于 2020-11-07T10:17:08.987 回答
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 操作的其他方法调用。

于 2018-12-28T22:36:53.770 回答