This project is a Laravel application that handles sending parcel data to courier services. It is designed for future enhancements and the addition of multiple courier integrations.
-
Clone this repository:
git clone <repository_url> cd online-shop
-
Install dependencies:
composer install
-
Copy the example environment file and configure:
cp .env.example .env
-
Set the sender address in the
.env
file:SENDER_ADDRESS="Your Sender Address Here"
-
Generate an application key:
php artisan key:generate
-
Serve the application:
php artisan serve
To send parcel data, make a POST request to /send-parcel
with the following fields:
width
(numeric): Width of the parcel.height
(numeric): Height of the parcel.length
(numeric): Length of the parcel.weight
(numeric): Weight of the parcel.customer_name
(string): Name of the recipient.phone_number
(string): Phone number of the recipient.email
(email): Email address of the recipient.delivery_address
(string): Delivery address for the parcel.
{
"width": 10,
"height": 20,
"length": 30,
"weight": 5,
"customer_name": "John Doe",
"phone_number": "+380960000000",
"email": "john.doe@example.com",
"delivery_address": "Prorizna St, 2, Kyiv, Ukraine, 01601"
}
curl -X POST http://localhost:8000/send-parcel \
-H "Content-Type: application/json" \
-d '{
"width": 10,
"height": 20,
"length": 30,
"weight": 5,
"customer_name": "John Doe",
"phone_number": "+380950000000",
"email": "john.doe@example.com",
"delivery_address": "Prorizna St, 2, Kyiv, Ukraine, 01601"
}'
-
Create a new implementation of
CourierServiceInterface
:<?php namespace App\Services; use Illuminate\Support\Facades\Http; /** * Class OtherCourierService * @package App\Services * * Implementation of the CourierServiceInterface for another courier. */ class OtherCourierService implements CourierServiceInterface { /** * @inheritDoc */ public function send(array $data): bool { $response = Http::post('https://othercourier.test/api/delivery', $data); return $response->successful(); } }
-
Update the service provider to use the new courier:
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\CourierServiceInterface; use App\Services\NovaPoshtaService; use App\Services\OtherCourierService; /** * Class CourierServiceProvider * @package App\Providers * * Service provider for binding the courier service interface to its implementation. */ class CourierServiceProvider extends ServiceProvider { /** * Register services. * * @return void */ public function register() { $this->app->bind(CourierServiceInterface::class, function ($app) { $courier = config('courier.default'); switch ($courier) { case 'other': return new OtherCourierService(); case 'novaposhta': default: return new NovaPoshtaService(); } }); } /** * Bootstrap services. * * @return void */ public function boot() { // } }
-
Add courier configuration in
config/courier.php
:return [ 'default' => env('COURIER_SERVICE', 'novaposhta'), ];
-
Update the
.env
file:COURIER_SERVICE=novaposhta