1

我从我的 authorize.net 商家界面获得了 SIGNATURE_KEY。我正在使用 AuthnetJson 包。我是否必须将 128 十六进制 SIGNATURE_KEY 转换为二进制?如果答案是肯定的,那么我这样做了,但我的代码永远不会在 if ($webhook->isValid()){// 代码永远不会执行执行} 中执行。我做错了什么?

$webhook = new AuthnetWebhook('services.authorize.signature', $payload);
    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
        ));
        $user = User::find(1);
        $user->notify( new PasswordResetSuccess($response));
        /* 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
        */
    }

编辑:

<?php

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;
use App\Notifications\PasswordResetSuccess;
use App\Models\User;
use Log;
use \stdClass;
use App\Models\Anet;
class WebhookController extends Controller
{
    public function webhook(Request $request){

        $headers = getallheaders();
        $payloadraw = file_get_contents("php://input");
        $payloadEncoded = json_encode($payloadraw);
        $payloadDecoded = json_decode($payloadraw);
        $type = gettype($payloadraw);

        $webhook = new AuthnetWebhook('xxxxx8EF4B4186A3BC745B70637EA1Fxx091E1DD0706BF9A9D721982B882BE54192BD1BBCEAFC0415DF06E6xxxxxxxxx',$payloadEncoded, $headers);
        if ($webhook->isValid()) {
            // Get the transaction ID
            $transactionId = $webhook->payload->id;

            // Here you can get more information about the transaction
            $request  = AuthnetApiFactory::getJsonApiHandler('AUTHNET_LOGIN','AUTHNET_TRANSKEY');
            $response = $request->getTransactionDetailsRequest(array('transId' => $transactionId));

            $anet = new Anet();
            $anet->notification = $payloadraw ;
            $anet->payload = $payloadDecoded ;
            $anet->type = $type ;
            $anet->transaction_type = $response->transaction->transactionType;
            $anet->transactions_status = $response->transaction->transactionStatus;
            $anet->auth_code = $response->transaction->authCode;
            $anet->avs_response = $response->transaction->AVSResponse; 
            $anet->save();  
        }else{
            $anet = new Anet();
            $anet->notification = $payloadEncoded ;
            $anet->payload = $payloadDecoded ;
            $anet->type = $type ;
            $anet->transactions_status = '401';
            $anet->save();  
        }

    }   
}    
4

2 回答 2

1

您不需要将其转换为二进制。Authorize.Net 界面中显示的值是它应该如何在您的代码中使用:

例子:

$webhook = new AuthnetWebhook('14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE1752CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E', $payload);

或者,如果您使用config.inc.php库中的配置文件:

defined('AUTHNET_SIGNATURE') || define('AUTHNET_SIGNATURE', '14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE1752CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E');

并在您的代码中:

$webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload);
于 2018-12-28T13:47:05.887 回答
0

在不引用其余逻辑的情况下,您的 if 语句评估为不真实的原因是您正在对已经是 json 的数据进行 json 编码。当您调用getallheaders()内容类型时,已经定义为 json。所以替换这个:

    $headers = getallheaders();
    $payloadraw = file_get_contents("php://input");
    $payloadEncoded = json_encode($payloadraw);
    $payloadDecoded = json_decode($payloadraw);
    $type = gettype($payloadraw);

有了这个:

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

和这个:

  $webhook = new AuthnetWebhook($signature,$payload, $headers);
    if ($webhook->isValid()) {
      //logic goes here      
     } 

将评估为真,并且条件中包含的任何有效逻辑都将被执行。我建议在添加其他逻辑之前测试上面的代码以验证它是否有效(确实如此)。您可以像这样创建一个简单的日志文件:

$dump = print_r($payload,true);
$fp = file_put_contents( '

test.log', $dump );

如果您的目录在交付单个 webhook 后有一个名为 test.log 的文件,那么您就知道您有一个基线可供参考。如果你的 if 语句的其余部分存在无效逻辑,它可能会破坏整个事情。

并且要回答您已经正确回答的第一个问题,请不要将签名密钥转换为二进制。所以 $signature在上面的代码中是授权给你的签名密钥。

于 2018-12-29T23:27:13.377 回答