function verifyWebhookEvent(string $requestBody , array $headers) { /** * This is one way to receive the entire body that you received from PayPal webhook. This is one of the way to retrieve that information. * Just uncomment the below line to read the data from actual request. */ /** @var String $requestBody */ /** * Receive the entire body that you received from PayPal webhook. * Just uncomment the below line to read the data from actual request. */ /** @var String $bodyReceived */ /** * Receive HTTP headers that you received from PayPal webhook. * Just uncomment the below line to read the data from actual request. */ /** * In Documentions https://developer.paypal.com/docs/api/webhooks/#verify-webhook-signature_post * All header keys as UPPERCASE, but I recive the header key as the example array, First letter as UPPERCASE */ $headers = array_change_key_case($headers, CASE_UPPER); foreach ($headers as $key => $header) { if (is_array($header)) { $headers[$key] = $header[0]; } } // Log::info('Paypal Verify Header:' . json_encode($headers)); $signatureVerification = new VerifyWebhookSignature(); $signatureVerification->setAuthAlgo($headers['PAYPAL-AUTH-ALGO']); $signatureVerification->setTransmissionId($headers['PAYPAL-TRANSMISSION-ID']); $signatureVerification->setCertUrl($headers['PAYPAL-CERT-URL']); // Note that the Webhook ID must be a currently valid Webhook that you created with your client ID/secret. $signatureVerification->setWebhookId(env('PAYPAL_WEBHOOK_ID')); $signatureVerification->setTransmissionSig($headers['PAYPAL-TRANSMISSION-SIG']); $signatureVerification->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']); $signatureVerification->setRequestBody($requestBody); try { /** @var \PayPal\Api\VerifyWebhookSignatureResponse $output */ $output = $signatureVerification->post($this->apiContext); } catch (\Exception $ex) { Log::error('Paypal Verify Exception : ' . $ex->getMessage()); // ResultPrinter::printError("Validate Received Webhook Event", "WebhookEvent", null, $request->toJSON(), $ex); return false; } // ResultPrinter::printResult("Validate Received Webhook Event", "WebhookEvent", $output->getVerificationStatus(), $request->toJSON(), $output); return $output->getVerificationStatus() === 'SUCCESS'; }
... View more