This section is designed for developers who are using Laravel, a popular PHP framework, to integrate webhook notifications from Ottu. We aim to provide clear and practical examples to help you effectively set up and handle webhook events within your Laravel applications.
In this section, we focus on setting up the necessary database models and migrations required to handle and store webhook notifications, both Payment Webhooks and Operation Notification, and related data in a Laravel application. Our primary goal is to capture data associated with both the webhook notifications and the Checkout API responses efficiently.
The Checkout model represents the data structure for storing information received from the Checkout API response. Here is how you can set up this model and its corresponding migration:
A Controller will need to be created. Below can be generated using the artisan command:
php artisan make:controller WebhookController
Below is how the WebhookController could be structured, translating the functionality from the Django view:
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useApp\Models\Webhook;useIlluminate\Support\Facades\Log;classWebhookControllerextendsController{publicfunctionhandle(Request $request) {Log::info('Webhook received: '. $request->getContent());if (!$this->verifySignature($request)) {returnresponse()->json(['detail'=>'Unable to verify signature'],401); }try { $processedData =$this->processData($request);$this->saveData($processedData);returnresponse()->json(['detail'=>'Success'],200); } catch (\Exception $e) {Log::error('Failed to process webhook: '. $e->getMessage());returnresponse()->json(['detail'=>'Failed to process webhook'],400); } }protectedfunctionverifySignature(Request $request) {// Assume `verify_signature` is a helper function you've set up as per the signing mechanism section:// https://docs.ottu.com/developer/webhooks/signing-mechanism#phpreturnverify_signature($request->getContent(), $request->header('Signature')); }protectedfunctionprocessData(Request $request) {// Extract data from $request and perform any necessary transformations to process the data sent by Ottureturnjson_decode($request->getContent(),true); }protectedfunctionsaveData(array $data) {// Use the data array to create or update your modelsWebhook::create(['session_id'=> $data['session_id'],'checkout_id'=> $data['checkout_id'] ??null,'payload'=> $data ]); }}
Key Points:
Signature Verification:
Implement the verify_signature function to confirm the authenticity of incoming webhooks. This involves comparing a signature calculated from the request payload with the signature provided in the webhook headers. For detailed instructions on how to perform this verification, refer to our Signing Mechanism section.
Integrating with Laravel Routes:
Define a route in Laravel that directs to this controller method by adding the following line to your routes file (typically located in routes/web.php):
Route::post('/webhook', [WebhookController::class, 'handle']);
Logging:
Log all incoming data from Ottu before processing. This is crucial for maintaining detailed records useful for troubleshooting and auditing purposes.
Response Status Codes:
Carefully manage the response status codes sent back to Ottu to ensure clear communication about the success or failure of the webhook handling.
Conclusion:
The example code provided above offers a foundational framework for developing your own webhook receiver in a Laravel environment. To enhance integration, it's important to adhere to the Key Points outlined above and incorporate the following best practices:
Response Status Codes: Exercise meticulous care in managing the response status codes returned to Ottu. This is critical for ensuring transparent communication regarding the success or failure of the webhook handling.
Endpoint Requirements: Gain a thorough understanding of all endpoint requirements to ensure complete compliance with Ottu's operational standards. Detailed information on these requirements is available on our Endpoint Requirements page.
These enhancements and best practices will optimize your webhook integration and ensure effective communication and compliance within the Laravel framework.