public function StripeWebhookController::handle in Stripe 8
Same name and namespace in other branches
- 2.x src/Controller/StripeWebhookController.php \Drupal\stripe\Controller\StripeWebhookController::handle()
Handle webhook.
1 string reference to 'StripeWebhookController::handle'
File
- src/
Controller/ StripeWebhookController.php, line 57
Class
- StripeWebhookController
- Controller routines for book routes.
Namespace
Drupal\stripe\ControllerCode
public function handle(Request $request) {
$config = \Drupal::config('stripe.settings');
Stripe::setApiKey($config
->get('apikey.' . $config
->get('environment') . '.secret'));
$environment = $config
->get('environment');
$payload = @file_get_contents("php://input");
$signature = $request->server
->get('HTTP_STRIPE_SIGNATURE');
$secret = $config
->get("apikey.{$environment}.webhook");
try {
if (!empty($secret)) {
$event = Webhook::constructEvent($payload, $signature, $secret);
}
else {
$data = json_decode($payload, TRUE);
$jsonError = json_last_error();
if ($data === NULL && $jsonError !== JSON_ERROR_NONE) {
$msg = "Invalid payload: {$payload} (json_last_error() was {$jsonError})";
throw new \UnexpectedValueException($msg);
}
if ($environment == 'live') {
$event = Event::retrieve($data['id']);
}
else {
$event = Event::constructFrom($data, NULL);
}
}
} catch (\UnexpectedValueException $e) {
return new Response('Invalid payload', Response::HTTP_BAD_REQUEST);
} catch (SignatureVerification $e) {
return new Response('Invalid signature', Response::HTTP_BAD_REQUEST);
}
// Dispatch the webhook event.
$this->eventDispatcher
->dispatch(StripeEvents::WEBHOOK, new StripeWebhookEvent($event));
return new Response('OK', Response::HTTP_OK);
}