AuthnetWebhooksResponse   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 184
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 3 1
A getNotificationHistory() 0 9 3
A getWebhooksId() 0 3 1
A getDeliveryStatus() 0 3 1
A __toString() 0 11 1
A getNotificationId() 0 3 1
A __construct() 0 5 2
A getStatus() 0 3 1
A getEventTypes() 0 11 3
A getEventDate() 0 3 1
A getEventType() 0 3 1
A getWebhooks() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the AuthnetJSON package.
7
 *
8
 * (c) John Conde <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Authnetjson;
15
16
use Authnetjson\Exception\AuthnetInvalidJsonException;
17
18
/**
19
 * Adapter for the Authorize.Net Webhooks API
20
 *
21
 * @package   AuthnetJSON
22
 * @author    John Conde <[email protected]>
23
 * @copyright 2015 - 2023 John Conde <[email protected]>
24
 * @license   http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
25
 * @link      https://github.com/stymiee/authnetjson
26
 * @see       https://developer.authorize.net/api/reference/
27
 */
28
class AuthnetWebhooksResponse
29
{
30
    /**
31
     * @var object  SimpleXML object representing the API response
32
     */
33
    private $response;
34
35
    /**
36
     * @var string  JSON string that is the response sent by Authorize.Net
37
     */
38
    private $responseJson;
39
40
    /**
41
     * Creates the response object with the response json returned from the API call
42
     *
43
     * @param string $responseJson Response from Authorize.Net
44
     * @throws AuthnetInvalidJsonException
45
     */
46 2
    public function __construct(string $responseJson)
47
    {
48 2
        $this->responseJson = $responseJson;
49 2
        if (($this->response = json_decode($this->responseJson, false)) === null) {
50 1
            throw new AuthnetInvalidJsonException('Invalid JSON returned by the API');
51
        }
52 1
    }
53
54
    /**
55
     * Outputs the response JSON in a human-readable format
56
     *
57
     * @return string  HTML table containing debugging information
58
     */
59 1
    public function __toString(): string
60
    {
61 1
        $output = '<table id="authnet-response">' . "\n";
62 1
        $output .= '<caption>Authorize.Net Webhook Response</caption>' . "\n";
63 1
        $output .= '<tr><th colspan="2"><b>Webhook Response JSON</b></th></tr>' . "\n";
64 1
        $output .= '<tr><td colspan="2"><pre>' . "\n";
65 1
        $output .= $this->responseJson . "\n";
66 1
        $output .= '</pre></td></tr>' . "\n";
67 1
        $output .= '</table>';
68
69 1
        return $output;
70
    }
71
72
    /**
73
     * Gets a response variable from the API response
74
     *
75
     * net.authorize.customer.created
76
     * net.authorize.customer.deleted
77
     * net.authorize.customer.updated
78
     * net.authorize.customer.paymentProfile.created
79
     * net.authorize.customer.paymentProfile.deleted
80
     * net.authorize.customer.paymentProfile.updated
81
     * net.authorize.customer.subscription.cancelled
82
     * net.authorize.customer.subscription.created
83
     * net.authorize.customer.subscription.expiring
84
     * net.authorize.customer.subscription.suspended
85
     * net.authorize.customer.subscription.terminated
86
     * net.authorize.customer.subscription.updated
87
     * net.authorize.payment.authcapture.created
88
     * net.authorize.payment.authorization.created
89
     * net.authorize.payment.capture.created
90
     * net.authorize.payment.fraud.approved
91
     * net.authorize.payment.fraud.declined
92
     * net.authorize.payment.fraud.held
93
     * net.authorize.payment.priorAuthCapture.created
94
     * net.authorize.payment.refund.created
95
     * net.authorize.payment.void.created
96
     *
97
     * @return array   Array of event types supported by Webhooks API
98
     */
99 2
    public function getEventTypes(): array
100
    {
101 2
        $events = [];
102 2
        if (isset($this->response->eventTypes)) {
103 1
            foreach ($this->response->eventTypes as $event) {
104 1
                $events[] = $event;
105
            }
106
        } else {
107 1
            $events = array_column((array) $this->response, 'name');
108
        }
109 2
        return $events;
110
    }
111
112
    /**
113
     * Gets the webhooks ID
114
     *
115
     * @return string  Webhooks ID
116
     */
117 1
    public function getWebhooksId(): string
118
    {
119 1
        return $this->response->webhookId;
120
    }
121
122
    /**
123
     * Gets the status of the Webhooks
124
     *
125
     * @return string  Status of the webhooks [active|inactive]
126
     */
127 1
    public function getStatus(): string
128
    {
129 1
        return $this->response->status;
130
    }
131
132
    /**
133
     * Gets the URL the Webhooks API will use for these Webhooks
134
     *
135
     * @return string
136
     */
137 1
    public function getUrl(): string
138
    {
139 1
        return $this->response->url;
140
    }
141
142
    /**
143
     * Gets a list of webhooks
144
     *
145
     * @return array
146
     * @throws AuthnetInvalidJsonException
147
     */
148 1
    public function getWebhooks(): array
149
    {
150 1
        $webhooks = [];
151 1
        foreach ($this->response as $webhook) {
152 1
            $webhooks[] = new AuthnetWebhooksResponse(json_encode($webhook));
153
        }
154 1
        return $webhooks;
155
    }
156
157
    /**
158
     * Gets a list of webhooks
159
     *
160
     * @return array
161
     * @throws AuthnetInvalidJsonException
162
     */
163 1
    public function getNotificationHistory(): array
164
    {
165 1
        $notifications = [];
166 1
        if (count($this->response->notifications)) {
167 1
            foreach ($this->response->notifications as $notification) {
168 1
                $notifications[] = new AuthnetWebhooksResponse(json_encode($notification));
169
            }
170
        }
171 1
        return $notifications;
172
    }
173
174
    /**
175
     * Gets the notification ID of a notification
176
     *
177
     * @return string
178
     */
179 1
    public function getNotificationId(): string
180
    {
181 1
        return $this->response->notificationId;
182
    }
183
184
    /**
185
     * Gets the delivery status of a notification
186
     *
187
     * @return string
188
     */
189 1
    public function getDeliveryStatus(): string
190
    {
191 1
        return $this->response->deliveryStatus;
192
    }
193
194
    /**
195
     * Gets the event type of notification
196
     *
197
     * @return string
198
     */
199 1
    public function getEventType(): string
200
    {
201 1
        return $this->response->eventType;
202
    }
203
204
    /**
205
     * Gets the event date of a notification
206
     *
207
     * @return string
208
     */
209 1
    public function getEventDate(): string
210
    {
211 1
        return $this->response->eventDate;
212
    }
213
}
214