|string $payload raw JSON string or already-parsed array * * @return Event * * @throws Exception\UnexpectedValueException if the payload is not valid JSON or is a v2 thin event */ private static function buildV1Event($payload) { $data = self::parsePayload($payload); if (isset($data['object']) && 'v2.core.event' === $data['object']) { throw new Exception\UnexpectedValueException( 'You passed a thin event notification to a method that expects a webhook body. Use the corresponding parseEventNotification* method instead.' ); } return Event::constructFrom($data); } /** * Internal helper for parsing a JSON string into an associative array, or returns the input as-is if already an array. * * @param array|string $payload raw JSON string or already-parsed array * * @return array * * @throws Exception\UnexpectedValueException if the payload is a string and not valid JSON */ public static function parsePayload($payload) { if (\is_array($payload)) { return $payload; } $data = \json_decode($payload, true); if (\JSON_ERROR_NONE !== \json_last_error()) { throw new Exception\UnexpectedValueException( 'Invalid JSON payload: ' . \json_last_error_msg() ); } return $data; } /** * Internal helper to extract the inner event data from an AWS EventBridge or Azure Event Grid envelope, * or returns the parsed payload as-is if it is already a raw Stripe event. * * @param string $payload the JSON payload * * @return array the Stripe event, if available * * @throws Exception\UnexpectedValueException if the payload is not valid JSON or not recognized */ public static function maybeExtractFromCloudProviderEnvelope($payload) { $data = self::parsePayload($payload); // Could add as many checks as we want here, but we'll start simple if (\array_key_exists('detail', $data)) { // AWS // https://docs.stripe.com/event-destinations/eventbridge#event-structure return $data['detail']; } if (\array_key_exists('specversion', $data) && \array_key_exists('data', $data)) { // Azure // https://docs.stripe.com/event-destinations/eventgrid#event-structure return $data['data']; } if (isset($data['object']) && ('event' === $data['object'] || 'v2.core.event' === $data['object'])) { // Raw Stripe event: return as-is return $data; } throw new Exception\UnexpectedValueException( 'Unrecognized event format. The payload must be an AWS EventBridge/Azure Event Grid event envelope or a Stripe webhook (thin event notification or snapshot).' ); } }