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

时间:2018-12-27 17:01:38

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

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

2 个答案:

答案 0 :(得分:1)

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

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

要创建一个webhooks请求,可以使用如下代码-

mensajeAdvertencia["deposit_amount"] = "Pago Inicial"
mensajeAdvertencia["plan_reference"] = "Referencia Pago Inicial"

raise Warning(
    _("Por favor ingresar los siguientes campos: %s" 
      % sorted(mensajeAdvertencia.values()))
)

这将使您注册事件,事件将自动发送到您的端点 即$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);

然后,您可以使用上面的代码来验证Web钩子到达端点时的有效性。一旦您注册了事件并将webhooks发送到端点,就可以使用如下代码检索历史记录-

https://{yourserver.com}/{your path}/{your endpoint}

您可以像这样检索特定的Webhook-

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

其中$ webhookId是与您要检索的Webhook绑定的ID。您可以在名称空间中搜索以查看针对特定Webhook操作的其他方法调用。

答案 1 :(得分:1)

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

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

控制器:

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

路线:

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