0

In Authorize.Net created the subscription and recurring events handling using webhook. When I test using the web hook account authentication works but in actual webhook notification always failed authentication

if (isset($this->header['x-anet-signature'])) {
        $json = Json::encode($this->body);
        if ($json) {
            //To check the header and signature is true
         if (hash_equals(strtolower($this->header['x-anet-signature']),
                    'sha512=' . hash_hmac('sha512',$json, $secret))
            ) { 


             }else{
                yii::info($json,'webhookhNotifications');
                throw new \yii\web\ServerErrorHttpException('Authentication failed in Webhook');
              return false;

            } 
        }
    }

Webhook JSON

{
   "notificationId":"4bbba8fb-1d32-46b6-a513-a9ca2fed885c",
   "eventType":"net.authorize.customer.subscription.created",
   "eventDate":"2019-11-27T06:20:36.3621687Z",
   "webhookId":"a2929d59-147e-4400-a2bb-b3bd25a0311d",
   "payload":{
      "name":"Test subscription",
      "amount":290.00,
      "status":"active",
      "profile":{
         "customerProfileId":1921894828,
         "customerPaymentProfileId":1834842681,
         "customerShippingAddressId":1879009509
      },
      "entityName":"subscription",
      "id":"6168233"
   }
}

Secret key

F7B582AFFA9372866965456CFAC0D1B1219258F955FD5266D1A96BF9BE3C85F7D54C7CDFF9EF3EE7D3916EACB5EE920167F557BBB307288C17FBD169F0257AB4

x-anet-signature

sha512=FDE5518801C115C4886311877B4C37F6C26ABACE01ADB973EF372FB51C8F1E5321A83717161AD7DEFFD46F5013900E68B6220F3B25E9302A4208A9C673D32749
4
  • Can you explain how it is failing? Do you get an error? If so, what error do you get? Please more information as there is not enough to help you.
    – John Conde
    Nov 27, 2019 at 12:55
  • Please the body part that coming form webhook end {"notificationId":"4bbba8fb-1d32-46b6-a513-a9ca2fed885c","eventType":"net.authorize.customer.subscription.created","eventDate":"2019-11-27T06:20:36.3621687Z","webhookId":"a2929d59-147e-4400-a2bb-b3bd25a0311d","payload":{"name":"Test subscription","amount":290.00,"status":"active","profile":{"customerProfileId":1921894828,"customerPaymentProfileId":1834842681,"customerShippingAddressId":1879009509},"entityName":"subscription","id":"6168233"}} Always failing the hash equal part. Nov 27, 2019 at 13:07
  • Can you post the value of $this->header['x-anet-signature'] and $secret so we can try to reproduce the issue? You will need to change your secret key after you do so for security's sake.
    – John Conde
    Nov 27, 2019 at 14:40
  • Please see the secret key and signature . Secret key :F7B582AFFA9372866965456CFAC0D1B1219258F955FD5266D1A96BF9BE3C85F7D54C7CDFF9EF3EE7D3916EACB5EE920167F557BBB307288C17FBD169F0257AB4 $this->header['x-anet-signature'] = sha512=FDE5518801C115C4886311877B4C37F6C26ABACE01ADB973EF372FB51C8F1E5321A83717161AD7DEFFD46F5013900E68B6220F3B25E9302A4208A9C673D32749 Nov 28, 2019 at 5:10

1 Answer 1

1

Your code should work. I simplified it a bit for testing purposes but using the values you provided above it does indeed validate successfully:

$signature = 'sha512=FDE5518801C115C4886311877B4C37F6C26ABACE01ADB973EF372FB51C8F1E5321A83717161AD7DEFFD46F5013900E68B6220F3B25E9302A4208A9C673D32749';
$json = '{"notificationId":"4bbba8fb-1d32-46b6-a513-a9ca2fed885c","eventType":"net.authorize.customer.subscription.created","eventDate":"2019-11-27T06:20:36.3621687Z","webhookId":"a2929d59-147e-4400-a2bb-b3bd25a0311d","payload":{"name":"Test subscription","amount":290.00,"status":"active","profile":{"customerProfileId":1921894828,"customerPaymentProfileId":1834842681,"customerShippingAddressId":1879009509},"entityName":"subscription","id":"6168233"}}';
$secret = 'F7B582AFFA9372866965456CFAC0D1B1219258F955FD5266D1A96BF9BE3C85F7D54C7CDFF9EF3EE7D3916EACB5EE920167F557BBB307288C17FBD169F0257AB4';

if (hash_equals(strtolower($signature), 'sha512=' . hash_hmac('sha512', $json, $secret))) {
    echo 'valid';
}else{
    echo 'invalid';
}

Demo

I think your error is you are encoding the JSON which is already JSON. So change this line:

$json = Json::encode($this->body);

new code:

if (isset($this->header['x-anet-signature'])) {
        $json = $this->body;
        if ($json) {
            //To check the header and signature is true
         if (hash_equals(strtolower($this->header['x-anet-signature']),
                    'sha512=' . hash_hmac('sha512',$json, $secret))
            ) { 


             }else{
                yii::info($json,'webhookhNotifications');
                throw new \yii\web\ServerErrorHttpException('Authentication failed in Webhook');
              return false;

            } 
        }
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.