| 1 |
<?php |
| 2 |
|
| 3 |
namespace Stripe; |
| 4 |
|
| 5 |
abstract class Webhook |
| 6 |
{ |
| 7 |
const DEFAULT_TOLERANCE = 300; |
| 8 |
|
| 9 |
/** |
| 10 |
* Returns an Event instance using the provided JSON payload. Throws an |
| 11 |
* Exception\UnexpectedValueException if the payload is not valid JSON, and |
| 12 |
* an Exception\SignatureVerificationException if the signature |
| 13 |
* verification fails for any reason. |
| 14 |
* |
| 15 |
* @param string $payload the payload sent by Stripe |
| 16 |
* @param string $sigHeader the contents of the signature header sent by |
| 17 |
* Stripe |
| 18 |
* @param string $secret secret used to generate the signature |
| 19 |
* @param int $tolerance maximum difference allowed between the header's |
| 20 |
* timestamp and the current time |
| 21 |
* |
| 22 |
* @throws Exception\UnexpectedValueException if the payload is not valid JSON, |
| 23 |
* @throws Exception\SignatureVerificationException if the verification fails |
| 24 |
* |
| 25 |
* @return Event the Event instance |
| 26 |
*/ |
| 27 |
public static function constructEvent($payload, $sigHeader, $secret, $tolerance = self::DEFAULT_TOLERANCE) |
| 28 |
{ |
| 29 |
WebhookSignature::verifyHeader($payload, $sigHeader, $secret, $tolerance); |
| 30 |
|
| 31 |
$data = \json_decode($payload, true); |
| 32 |
$jsonError = \json_last_error(); |
| 33 |
if (null === $data && \JSON_ERROR_NONE !== $jsonError) { |
| 34 |
$msg = "Invalid payload: {$payload} " |
| 35 |
. "(json_last_error() was {$jsonError})"; |
| 36 |
|
| 37 |
throw new Exception\UnexpectedValueException($msg); |
| 38 |
} |
| 39 |
|
| 40 |
return Event::constructFrom($data); |
| 41 |
} |
| 42 |
} |
| 43 |
|