shahariar-ahmad/courier-fraud-checker-bd
Installation:
composer require shahariar-ahmad/courier-fraud-checker-bd
Add the service provider to config/app.php (Laravel 5.4+ auto-discovers it).
Configure credentials:
Add to .env:
PATHAO_USER=your_pathao_email
PATHAO_PASSWORD=your_pathao_password
STEADFAST_USER=your_steadfast_email
STEADFAST_PASSWORD=your_steadfast_password
First use case: Check a customer’s fraud risk during order placement:
$fraudRisk = CourierFraudCheckerBd::check($customerPhone);
$isHighRisk = $fraudRisk['pathao']['cancel'] / ($fraudRisk['pathao']['total'] ?? 1) > 0.5;
Order Validation Middleware:
// app/Http/Middleware/CheckFraudRisk.php
public function handle($request, Closure $next) {
$phone = $request->user()->phone;
$risk = CourierFraudCheckerBd::check($phone);
if ($this->isHighRisk($risk)) {
return redirect()->route('fraud-review');
}
return $next($request);
}
Customer Model Observer:
// app/Observers/CustomerObserver.php
public function saved(Customer $customer) {
if ($customer->is_first_order) {
$risk = CourierFraudCheckerBd::check($customer->phone);
$customer->update(['fraud_risk_score' => $this->calculateScore($risk)]);
}
}
Batch Processing:
// Process 100 customers in bulk (avoid rate limits)
$phones = Customer::whereNull('fraud_risk_score')->limit(100)->pluck('phone');
foreach ($phones as $phone) {
$risk = CourierFraudCheckerBd::check($phone);
// Update DB
}
$risk = Cache::remember("fraud_risk_{$phone}", now()->addHours(1), function() use ($phone) {
return CourierFraudCheckerBd::check($phone);
});
CheckFraudJob::dispatch($customer->phone);
API Credentials:
.env and validate them in config/courier.php:
'require_credentials' => env('COURIER_CHECK_ENABLED', true),
PATHAO_USER is empty, the package silently skips Pathao checks.Phone Validation:
018xx). Add validation:
use ShahariarAhmad\CourierFraudCheckerBd\Helpers\PhoneValidator;
if (!PhoneValidator::isValidBdNumber($phone)) {
throw new \InvalidArgumentException("Invalid phone number");
}
Rate Limits:
try {
$result = CourierFraudCheckerBd::check($phone);
} catch (\ShahariarAhmad\CourierFraudCheckerBd\Exceptions\RateLimitException $e) {
sleep(10); // Retry after 10 seconds
retry();
}
Enable Logging:
Add to config/courier.php:
'debug' => env('COURIER_DEBUG', false),
Logs API responses to storage/logs/courier_debug.log.
Mock Responses: For testing, override the API client:
// tests/TestCase.php
protected function getPackageProviders($app) {
return [
\ShahariarAhmad\CourierFraudCheckerBd\CourierFraudCheckerBdServiceProvider::class,
\ShahariarAhmad\CourierFraudCheckerBd\Testing\CourierFraudCheckerBdTestServiceProvider::class,
];
}
Custom Risk Rules:
Override the default risk calculation in app/Providers/CourierFraudCheckerBdServiceProvider.php:
public function boot() {
CourierFraudCheckerBd::extend(function ($risk) {
$risk['custom_score'] = $this->calculateCustomScore($risk);
return $risk;
});
}
Add New Couriers:
Extend the CourierChecker interface:
class NewCourierChecker implements CourierChecker {
public function check($phone) { /* ... */ }
}
Register it in the service provider’s register() method.
Webhook Integration: Listen for courier webhooks to update fraud scores in real-time:
Route::post('/courier/webhook', function (Request $request) {
$data = $request->validate(['courier' => 'required', 'order_id' => 'required', 'status' => 'required']);
// Update DB and recalculate risk
});
How can I help you explore Laravel packages today?