Completed
Branch master (2b4924)
by John
01:42 queued 18s
created

AuthnetWebhook::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
/**
4
 * This file is part of the AuthnetJSON package.
5
 *
6
 * (c) John Conde <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace JohnConde\Authnet;
13
14
/**
15
 * Handles a Webhook notification from the Authorize.Net Webhooks API
16
 *
17
 * @package     AuthnetJSON
18
 * @author      John Conde <[email protected]>
19
 * @copyright   John Conde <[email protected]>
20
 * @license     http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
21
 * @link        https://github.com/stymiee/authnetjson
22
 * @see         https://developer.authorize.net/api/reference/
23
 */
24
class AuthnetWebhook
25
{
26
    /**
27
     * @var     object  SimpleXML object representing the Webhook notification
28
     */
29
    private $webhook;
30
31
    /**
32
     * @var     string  JSON string that is the Webhook notification sent by Authorize.Net
33
     */
34
    private $webhookJson;
35
36
    /**
37
     * @var     array  HTTP headers sent with the notification
38
     */
39
    private $headers;
40
41
    /**
42
     * @var     string  Authorize.Net Signature Key
43
     */
44
    private $signature;
45
46
    /**
47
     * Creates the response object with the response json returned from the API call
48
     *
49
     * @param   string     $signature       Authorize.Net Signature Key
50
     * @param   string     $payload         Webhook Notification sent by Authorize.Net
51
     * @param   array      $headers         HTTP headers sent with the Webhook notification
52
     * @throws  \JohnConde\Authnet\AuthnetInvalidJsonException
53
     */
54
    public function __construct($signature, $payload, Array $headers)
55
    {
56
        $this->signature   = $signature;
57
        $this->webhookJson = $payload;
58
        $this->headers     = $headers;
59
        if (($this->webhook = json_decode($this->webhookJson)) === null) {
60
            throw new AuthnetInvalidJsonException('Invalid JSON sent in the Webhook notification');
61
        }
62
    }
63
64
    /**
65
     * Outputs the response JSON in a human readable format
66
     *
67
     * @return  string  HTML table containing debugging information
68
     */
69
    public function __toString()
70
    {
71
        $output  = '';
72
        $output .= '<table summary="Authorize.Net Webhook" id="authnet-webhook">' . "\n";
73
        $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Response HTTP Headers</b></th>' . "\n" . '</tr>' . "\n";
74
        $output .= '<tr><td colspan="2"><pre>' . "\n";
75
        $output .= var_export($this->headers) . "\n";
76
        $output .= '</pre></td></tr>' . "\n";
77
        $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Response JSON</b></th>' . "\n" . '</tr>' . "\n";
78
        $output .= '<tr><td colspan="2"><pre>' . "\n";
79
        $output .= $this->webhookJson . "\n";
80
        $output .= '</pre></td></tr>' . "\n";
81
        $output .= '</table>';
82
83
        return $output;
84
    }
85
86
    /**
87
     * Gets a response variable from the Webhook notification
88
     *
89
     * @param   string  $var
90
     * @return  string          requested variable from the API call response
91
     */
92
    public function __get($var)
93
    {
94
        return $this->webhook->{$var};
95
    }
96
97
    /**
98
     * Validates a webhook signature to determine if the webhook is valid
99
     *
100
     * @return bool
101
     */
102
    public function isValid()
103
    {
104
        $hashedBody = strtoupper(hash_hmac('sha512', $this->webhookJson, $this->signature));
105
        return (isset($this->headers['X-ANET-Signature']) && strtoupper(explode('=', $this->headers['X-ANET-Signature'])[1]) === $hashedBody);
106
    }
107
108
    /**
109
     * Validates a webhook signature to determine if the webhook is valid
110
     *
111
     * @return string
112
     */
113
    public function getRequestId()
114
    {
115
        return (isset($this->headers['X-Request-Id'])) ? $this->headers['X-Request-Id'] : null;
116
    }
117
}