1 <?php
2
3 namespace PayPal\Api;
4
5 use PayPal\Common\PayPalResourceModel;
6 use PayPal\Validation\ArgumentValidator;
7 use PayPal\Api\VerifyWebhookSignatureResponse;
8 use PayPal\Rest\ApiContext;
9 use PayPal\Validation\UrlValidator;
10
11 /**
12 * Class VerifyWebhookSignature
13 *
14 * Verify webhook signature.
15 *
16 * @package PayPal\Api
17 *
18 * @property string auth_algo
19 * @property string cert_url
20 * @property string transmission_id
21 * @property string transmission_sig
22 * @property string transmission_time
23 * @property string webhook_id
24 * @property \PayPal\Api\WebhookEvent webhook_event
25 */
26 class VerifyWebhookSignature extends PayPalResourceModel
27 {
28 /**
29 * The algorithm that PayPal uses to generate the signature and that you can use to verify the signature. Extract this value from the `PAYPAL-AUTH-ALGO` response header, which is received with the webhook notification.
30 *
31 * @param string $auth_algo
32 *
33 * @return $this
34 */
35 public function setAuthAlgo($auth_algo)
36 {
37 $this->auth_algo = $auth_algo;
38 return $this;
39 }
40
41 /**
42 * The algorithm that PayPal uses to generate the signature and that you can use to verify the signature. Extract this value from the `PAYPAL-AUTH-ALGO` response header, which is received with the webhook notification.
43 *
44 * @return string
45 */
46 public function getAuthAlgo()
47 {
48 return $this->auth_algo;
49 }
50
51 /**
52 * The X.509 public key certificate. Download the certificate from this URL and use it to verify the signature. Extract this value from the `PAYPAL-CERT-URL` response header, which is received with the webhook notification.
53 *
54 * @param string $cert_url
55 * @throws \InvalidArgumentException
56 * @return $this
57 */
58 public function setCertUrl($cert_url)
59 {
60 UrlValidator::validate($cert_url, "CertUrl");
61 $this->cert_url = $cert_url;
62 return $this;
63 }
64
65 /**
66 * The X.509 public key certificate. Download the certificate from this URL and use it to verify the signature. Extract this value from the `PAYPAL-CERT-URL` response header, which is received with the webhook notification.
67 *
68 * @return string
69 */
70 public function getCertUrl()
71 {
72 return $this->cert_url;
73 }
74
75 /**
76 * The ID of the HTTP transmission. Contained in the `PAYPAL-TRANSMISSION-ID` header of the notification message.
77 *
78 * @param string $transmission_id
79 *
80 * @return $this
81 */
82 public function setTransmissionId($transmission_id)
83 {
84 $this->transmission_id = $transmission_id;
85 return $this;
86 }
87
88 /**
89 * The ID of the HTTP transmission. Contained in the `PAYPAL-TRANSMISSION-ID` header of the notification message.
90 *
91 * @return string
92 */
93 public function getTransmissionId()
94 {
95 return $this->transmission_id;
96 }
97
98 /**
99 * The PayPal-generated asymmetric signature. Extract this value from the `PAYPAL-TRANSMISSION-SIG` response header, which is received with the webhook notification.
100 *
101 * @param string $transmission_sig
102 *
103 * @return $this
104 */
105 public function setTransmissionSig($transmission_sig)
106 {
107 $this->transmission_sig = $transmission_sig;
108 return $this;
109 }
110
111 /**
112 * The PayPal-generated asymmetric signature. Extract this value from the `PAYPAL-TRANSMISSION-SIG` response header, which is received with the webhook notification.
113 *
114 * @return string
115 */
116 public function getTransmissionSig()
117 {
118 return $this->transmission_sig;
119 }
120
121 /**
122 * The date and time of the HTTP transmission. Contained in the `PAYPAL-TRANSMISSION-TIME` header of the notification message.
123 *
124 * @param string $transmission_time
125 *
126 * @return $this
127 */
128 public function setTransmissionTime($transmission_time)
129 {
130 $this->transmission_time = $transmission_time;
131 return $this;
132 }
133
134 /**
135 * The date and time of the HTTP transmission. Contained in the `PAYPAL-TRANSMISSION-TIME` header of the notification message.
136 *
137 * @return string
138 */
139 public function getTransmissionTime()
140 {
141 return $this->transmission_time;
142 }
143
144 /**
145 * The ID of the webhook as configured in your Developer Portal account.
146 *
147 * @param string $webhook_id
148 *
149 * @return $this
150 */
151 public function setWebhookId($webhook_id)
152 {
153 $this->webhook_id = $webhook_id;
154 return $this;
155 }
156
157 /**
158 * The ID of the webhook as configured in your Developer Portal account.
159 *
160 * @return string
161 */
162 public function getWebhookId()
163 {
164 return $this->webhook_id;
165 }
166
167 /**
168 * The webhook notification, which is the content of the HTTP `POST` request body.
169 * @deprecated Please use setRequestBody($request_body) instead.
170 * @param \PayPal\Api\WebhookEvent $webhook_event
171 *
172 * @return $this
173 */
174 public function setWebhookEvent($webhook_event)
175 {
176 $this->webhook_event = $webhook_event;
177 return $this;
178 }
179
180 /**
181 * The webhook notification, which is the content of the HTTP `POST` request body.
182 *
183 * @return \PayPal\Api\WebhookEvent
184 */
185 public function getWebhookEvent()
186 {
187 return $this->webhook_event;
188 }
189
190 /**
191 * The content of the HTTP `POST` request body of the webhook notification you received as a string.
192 *
193 * @param string $request_body
194 *
195 * @return $this
196 */
197 public function setRequestBody($request_body)
198 {
199 $this->request_body = $request_body;
200 return $this;
201 }
202
203 /**
204 * The content of the HTTP `POST` request body of the webhook notification you received as a string.
205 *
206 * @return string
207 */
208 public function getRequestBody()
209 {
210 return $this->request_body;
211 }
212
213 /**
214 * Verifies a webhook signature.
215 *
216 * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials.
217 * @param PayPalRestCall $restCall is the Rest Call Service that is used to make rest calls
218 * @return VerifyWebhookSignatureResponse
219 */
220 public function post($apiContext = null, $restCall = null)
221 {
222 $payLoad = $this->toJSON();
223
224 $json = self::executeCall(
225 "/v1/notifications/verify-webhook-signature",
226 "POST",
227 $payLoad,
228 null,
229 $apiContext,
230 $restCall
231 );
232 $ret = new VerifyWebhookSignatureResponse();
233 $ret->fromJson($json);
234 return $ret;
235 }
236
237 public function toJSON($options = 0)
238 {
239 if (!is_null($this->request_body)) {
240 $valuesToEncode = $this->toArray();
241 unset($valuesToEncode['webhook_event']);
242 unset($valuesToEncode['request_body']);
243
244 $payLoad = "{";
245 foreach ($valuesToEncode as $field => $value) {
246 $payLoad .= "\"$field\": \"$value\",";
247 }
248 $payLoad .= "\"webhook_event\": $this->request_body";
249 $payLoad .= "}";
250 return $payLoad;
251 } else {
252 $payLoad = parent::toJSON($options);
253 return $payLoad;
254 }
255 }
256 }
257