Created
January 3, 2025 09:23
-
-
Save tarikmanoar/9ab3f198a9611487fc0915557fe50ecf to your computer and use it in GitHub Desktop.
Courier Webhook Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api\V2; | |
use App\Models\Order; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use App\Actions\Order\DeliveryStatus; | |
class WebhookController extends Controller | |
{ | |
public function __construct() {} | |
public function steadfast(Request $request) | |
{ | |
$validatedData = $request->validate([ | |
'consignment_id' => 'required|integer|exists:orders,consignment_id', | |
'invoice' => 'required|string', | |
'status' => 'required|string|in:pending,delivered,partial_delivered,cancelled,unknown', | |
'cod_amount' => 'required|numeric', | |
'delivery_charge' => 'required|numeric', | |
'updated_at' => 'required|date' | |
]); | |
if ($request->header('Authorization') !== 'Bearer 0NrlqWvGVotLxMcmcdqlrOClGe9Fc6qhcSqK3p5j') { | |
return response()->json([ | |
'status' => 'error', | |
'message' => 'Unauthorized' | |
], 401); | |
} | |
$order = Order::where('consignment_id', $validatedData['consignment_id'])->where('delivery_channel', 'steadfast')->firstOrFail(); | |
// return $order->user; | |
try { | |
DeliveryStatus::update($order, $validatedData['status']); | |
return response()->json([ | |
'status' => 'success', | |
'message' => 'Webhook received successfully' | |
]); | |
} catch (\Exception $e) { | |
return $e->getMessage(); | |
return response()->json([ | |
'status' => 'error', | |
'message' => 'Order not found' | |
], 404); | |
} | |
} | |
public function pathao(Request $request) | |
{ | |
$validatedData = $request->validate([ | |
'consignment_id' => 'nullable|string', | |
'event' => 'nullable|string', | |
]); | |
if ($request->consignment_id && $request->header('X-PATHAO-Signature') !== '0NrlqWvGVotLxMcmcdqlrOClGe9Fc6qhcSqK3p5j') { | |
$status = match ($validatedData['event']) { | |
'order.delivered' => 'delivered', | |
'order.assigned-for-delivery' => 'assigned_for_delivery', | |
'order.returned' => 'cancelled', | |
'order.in-transit' => 'in_transit', | |
'order.picked' => 'picked', | |
'order.partial-delivery' => 'partial_delivered', | |
default => 'unknown' | |
}; | |
$order = Order::where('consignment_id', $validatedData['consignment_id'])->where('delivery_channel', 'pathao')->first(); | |
if ($order) { | |
DeliveryStatus::update($order, $status); | |
} | |
} | |
return response()->json([ | |
'message' => 'Greetings from ' . env('APP_NAME', 'MANOAR') . '!', | |
], 202)->header('X-Pathao-Merchant-Webhook-Integration-Secret', '__YOUR_SECRET__'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment