PHP code example of devrkb21 / czbd-courier

1. Go to this page and download the library: Download devrkb21/czbd-courier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devrkb21 / czbd-courier example snippets


   use CourierChecker;

   $report = CourierChecker::check('01712345678');
   

use CourierChecker;

$report = CourierChecker::check('01712345678');

use Czbd\CourierChecker\CourierCheckerManager;

class OrderController
{
    public function show(CourierCheckerManager $manager)
    {
        return $manager->check('01712345678');
    }
}

use Czbd\CourierChecker\Services\PathaoService;
use Czbd\CourierChecker\Services\SteadfastService;
use Czbd\CourierChecker\Services\RedxService;
use Czbd\CourierChecker\Services\PaperflyService;
use Czbd\CourierChecker\Services\CarrybeeService;

$result = app(PathaoService::class)->getDeliveryStats('01712345678');

use CourierChecker;

class CheckoutController
{
    public function placeOrder(Request $request)
    {
        $report = CourierChecker::check($request->input('phone'));

        $ratio = $report['aggregate']['success_ratio'];
        $totalOrders = $report['aggregate']['total_deliveries'];

        // Only apply the rule once there's enough history to judge -
        // a brand-new customer with 0 orders isn't "risky", just unknown.
        if ($totalOrders >= 3 && $ratio < 50.0) {
            return back()->withErrors([
                'phone' => 'This number has a high cancellation rate. COD is unavailable; please pay online.',
            ]);
        }

        // ...proceed with order creation
    }
}

[
    'steadfast' => [
        'success' => 10,
        'cancel' => 2,
        'total' => 12,
        'success_ratio' => 83.33,
    ],
    'pathao' => [
        'success' => 5,
        'cancel' => 1,
        'total' => 6,
        'success_ratio' => 83.33,
        'customer_rating' => 'excellent_customer', // Pathao-only, may be null
    ],
    'redx' => [
        'success' => 20,
        'cancel' => 5,
        'total' => 25,
        'success_ratio' => 80.0,
    ],
    'paperfly' => [
        'success' => 3,
        'cancel' => 1,
        'total' => 4, // total record count from Paperfly, may exceed success+cancel if some records are unclassified
        'success_ratio' => 75.0,
    ],
    'carrybee' => [
        'success' => 8,
        'cancel' => 2,
        'total' => 10,
        'success_ratio' => 80.0,
    ],
    'aggregate' => [
        'total_success' => 46,
        'total_cancel' => 11,
        'total_deliveries' => 57,
        'success_ratio' => 80.7,
        'cancel_ratio' => 19.3,
    ],
]

// Invalid input, courier auth failure, unexpected/malformed response, etc.
[
    'error' => 'Failed to authenticate with Pathao',
    'status' => 401, // present when caused by an HTTP response
]

// Courier not configured at all (missing credentials)
[
    'error' => 'pathao is not configured',
    'message' => 'No valid credentials configured for Pathao. Set at least one account.',
]

[
    'error' => 'Service unavailable or failed to process',
    'message' => '<exception message>',
]

foreach (['steadfast', 'pathao', 'redx', 'paperfly', 'carrybee'] as $courier) {
    if (isset($report[$courier]['error'])) {
        // handle/log the per-courier failure
    }
}
bash
php artisan vendor:publish --provider="Czbd\\CourierChecker\\CourierCheckerServiceProvider" --tag="courier-checker-config"
bash
php artisan vendor:publish --tag="config"