1 <?php
2
3 namespace PayPal\Api;
4
5 use PayPal\Common\PayPalResourceModel;
6 use PayPal\Validation\ArgumentValidator;
7 use PayPal\Api\WebhookList;
8 use PayPal\Rest\ApiContext;
9 use PayPal\Validation\UrlValidator;
10
11 /**
12 * Class Webhook
13 *
14 * One or more webhook objects.
15 *
16 * @package PayPal\Api
17 *
18 * @property string id
19 * @property string url
20 * @property \PayPal\Api\WebhookEventType[] event_types
21 */
22 class Webhook extends PayPalResourceModel
23 {
24 /**
25 * The ID of the webhook.
26 *
27 * @param string $id
28 *
29 * @return $this
30 */
31 public function setId($id)
32 {
33 $this->id = $id;
34 return $this;
35 }
36
37 /**
38 * The ID of the webhook.
39 *
40 * @return string
41 */
42 public function getId()
43 {
44 return $this->id;
45 }
46
47 /**
48 * The URL that is configured to listen on `localhost` for incoming `POST` notification messages that contain event information.
49 *
50 * @param string $url
51 * @throws \InvalidArgumentException
52 * @return $this
53 */
54 public function setUrl($url)
55 {
56 UrlValidator::validate($url, "Url");
57 $this->url = $url;
58 return $this;
59 }
60
61 /**
62 * The URL that is configured to listen on `localhost` for incoming `POST` notification messages that contain event information.
63 *
64 * @return string
65 */
66 public function getUrl()
67 {
68 return $this->url;
69 }
70
71 /**
72 * A list of up to ten events to which to subscribe your webhook. To subscribe to all events including new events as they are added, specify the asterisk (`*`) wildcard. To replace the `event_types` array, specify the `*` wildcard. To see all supported events, [list available events](#available-event-type.list).
73 *
74 * @param \PayPal\Api\WebhookEventType[] $event_types
75 *
76 * @return $this
77 */
78 public function setEventTypes($event_types)
79 {
80 $this->event_types = $event_types;
81 return $this;
82 }
83
84 /**
85 * A list of up to ten events to which to subscribe your webhook. To subscribe to all events including new events as they are added, specify the asterisk (`*`) wildcard. To replace the `event_types` array, specify the `*` wildcard. To see all supported events, [list available events](#available-event-type.list).
86 *
87 * @return \PayPal\Api\WebhookEventType[]
88 */
89 public function getEventTypes()
90 {
91 return $this->event_types;
92 }
93
94 /**
95 * Append EventTypes to the list.
96 *
97 * @param \PayPal\Api\WebhookEventType $webhookEventType
98 * @return $this
99 */
100 public function addEventType($webhookEventType)
101 {
102 if (!$this->getEventTypes()) {
103 return $this->setEventTypes(array($webhookEventType));
104 } else {
105 return $this->setEventTypes(
106 array_merge($this->getEventTypes(), array($webhookEventType))
107 );
108 }
109 }
110
111 /**
112 * Remove EventTypes from the list.
113 *
114 * @param \PayPal\Api\WebhookEventType $webhookEventType
115 * @return $this
116 */
117 public function removeEventType($webhookEventType)
118 {
119 return $this->setEventTypes(
120 array_diff($this->getEventTypes(), array($webhookEventType))
121 );
122 }
123
124 /**
125 * Subscribes your webhook listener to events. A successful call returns a [`webhook`](/docs/api/webhooks/#definition-webhook) object, which includes the webhook ID for later use.
126 *
127 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
128 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
129 * @return Webhook
130 */
131 public function create($apiContext = null, $restCall = null)
132 {
133 $payLoad = $this->toJSON();
134 $json = self::executeCall(
135 "/v1/notifications/webhooks",
136 "POST",
137 $payLoad,
138 null,
139 $apiContext,
140 $restCall
141 );
142 $this->fromJson($json);
143 return $this;
144 }
145
146 /**
147 * Shows details for a webhook, by ID.
148 *
149 * @param string $webhookId
150 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
151 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
152 * @return Webhook
153 */
154 public static function get($webhookId, $apiContext = null, $restCall = null)
155 {
156 ArgumentValidator::validate($webhookId, 'webhookId');
157 $payLoad = "";
158 $json = self::executeCall(
159 "/v1/notifications/webhooks/$webhookId",
160 "GET",
161 $payLoad,
162 null,
163 $apiContext,
164 $restCall
165 );
166 $ret = new Webhook();
167 $ret->fromJson($json);
168 return $ret;
169 }
170
171 /**
172 * Retrieves all Webhooks for the application associated with access token.
173 *
174 * @deprecated Please use Webhook#getAllWithParams instead.
175 *
176 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
177 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
178 * @return WebhookList
179 */
180 public static function getAll($apiContext = null, $restCall = null)
181 {
182 return self::getAllWithParams(array(), $apiContext, $restCall);
183 }
184
185 /**
186 * Lists all webhooks for an app.
187 *
188 * @param array $params
189 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
190 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
191 * @return WebhookList
192 */
193 public static function getAllWithParams($params = array(), $apiContext = null, $restCall = null)
194 {
195 ArgumentValidator::validate($params, 'params');
196 $payLoad = "";
197 $allowedParams = array(
198 'anchor_type' => 1,
199 );
200 $json = self::executeCall(
201 "/v1/notifications/webhooks?" . http_build_query(array_intersect_key($params, $allowedParams)),
202 "GET",
203 $payLoad,
204 null,
205 $apiContext,
206 $restCall
207 );
208 $ret = new WebhookList();
209 $ret->fromJson($json);
210 return $ret;
211 }
212
213 /**
214 * Replaces webhook fields with new values. Pass a `json_patch` object with `replace` operation and `path`, which is `/url` for a URL or `/event_types` for events. The `value` is either the URL or a list of events.
215 *
216 * @param PatchRequest $patchRequest
217 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
218 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
219 * @return Webhook
220 */
221 public function update($patchRequest, $apiContext = null, $restCall = null)
222 {
223 ArgumentValidator::validate($this->getId(), "Id");
224 ArgumentValidator::validate($patchRequest, 'patchRequest');
225 $payLoad = $patchRequest->toJSON();
226 $json = self::executeCall(
227 "/v1/notifications/webhooks/{$this->getId()}",
228 "PATCH",
229 $payLoad,
230 null,
231 $apiContext,
232 $restCall
233 );
234 $this->fromJson($json);
235 return $this;
236 }
237
238 /**
239 * Deletes a webhook, by ID.
240 *
241 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
242 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
243 * @return bool
244 */
245 public function delete($apiContext = null, $restCall = null)
246 {
247 ArgumentValidator::validate($this->getId(), "Id");
248 $payLoad = "";
249 self::executeCall(
250 "/v1/notifications/webhooks/{$this->getId()}",
251 "DELETE",
252 $payLoad,
253 null,
254 $apiContext,
255 $restCall
256 );
257 return true;
258 }
259
260 }
261