nashtech-garage / yas

YAS: Yet Another Shop, a sample microservices project in Java
MIT License
1.58k stars 414 forks source link

[Delivery] Proposed approach for implementing delivery services #1254

Open mochacr0 opened 1 week ago

mochacr0 commented 1 week ago

Overview

Shipping is crucial to the e-commerce process, beginning when an order is placed and continuing through all steps required to deliver the package to the customer.

The shipping process starts when the user initiates checkout. At this point, they can select their preferred delivery provider, service type, and receive delivery time and fee estimates.

Once the order is confirmed, the shop owner prepares the shipment, including packaging the item, creating a shipping request, and scheduling a pickup time for the carrier. The carrier then manages the fulfillment process to deliver the package.

Throughout this journey, both the buyer and the shop owner can track the package status using a tracking number provided by the carrier.

YAS Delivery Idea

This section outlines the general approach for implementing delivery services into YAS. Since this is still a concept, further discussion is needed to finalize the solution and workflow.

Delivery Providers & Services

We plan to support multiple shipment providers (e.g., FedEx, UPS). Each provider offers a unique list of service types, each with varying rates and delivery times.

During checkout, the system retrieves all available delivery service types from enabled providers that can accommodate the current order details (buyer address, items to ship). Buyers will then select their preferred service type for the order.

Order & Shipment Packages

An order may consist of multiple shipment packages. This is necessary due to our support for multi-warehouse functionality, where products may be located in different warehouses and pickup locations. Since we cannot create a single package for items from various pickup locations, multiple packages will be created, one for each warehouse storing the products.

To illustrate how our system decides on package creation for an order, consider the following stock availability and order scenarios:

Samsung Iphone
Warehouse A 10 5
Warehouse B 5 20
Total 15 25

When a user wants to place an order containing both Samsung and iPhone with different quantities, the system processes it as follows:

Scenario 1: Samsung x1, iPhone x1

Since both products are available in both warehouses, the system can select either Warehouse A or B based on proximity to the buyer. However, estimating the distance may be complex for now. We can use a simpler method to choose the warehouse by sorting the names in alphabetical order, for example.

Scenario 2: Samsung x6, iPhone x6

In this case, only Warehouse A has enough stock for Samsung, while Warehouse B has sufficient stock for iPhone. Therefore, the shop owner must create two different packages:

Scenario 3: Samsung x11, iPhone x21

Here, the ordered quantities exceed the available stock in each warehouse, but the total is still manageable. Consequently, two packages must be created:

In this scenario, we prioritize allocation from Warehouse A first because its name comes first alphabetically.

This package-splitting logic is also applied when estimating delivery times and fees to provide accurate information to customers.

Arrange a Shipment

Arranging a shipment occurs after the buyer places an order and the shop owner accepts it. At this stage, shop owners must create a shipment package request for the buyer's chosen provider. The following actions can be performed:

Package Status Updating & Tracking

Once the shipment package is arranged and picked up, its status is continuously updated by the delivery providers via webhooks. Each time there is a change in the package's status—such as being picked up, moved to storage, out for delivery, or delivered—providers send update events to our system through pre-configured endpoints. Our system is responsible for receiving these updates and reflecting the accurate status of the package.

Additionally, each package is assigned a unique tracking number, allowing us to retrieve the latest status and details from the provider's website.

Note: Webhooks are only available for deployed and public environments. They are not sent in development environments.

Shipping Provider API Specifications

FedEx

  1. Get Rates (Delivery Times and Fees)
  2. Create Shipment
  3. Cancel Shipment
  4. Get Pickup Rates
  5. Schedule Pickup
  6. Cancel Pickup
  7. Tracking API

Get Rates (Delivery Times and Fees)

Retrieves available delivery services, estimated delivery times, and associated fees based on shipment details such as addresses, package weight, and pickup type.

Required Information:

Response Includes:


Create Shipment

Initiates a shipment request.

Required Information:

Response Includes:


Cancel Shipment

Cancels an existing shipment request.

Required Information:

Response Includes:


Tracking API

Provides the latest status and history of a shipment

Required Information:

Response Includes:


Get Pickup Rates (Optional)

Retrieves available pickup rates.

Required Information:

Response Includes:


Schedule Pickup (Optional)

Schedules a package pickup. This API works in conjunction with the Create Shipment API, as after a shipment is created, shippers can utilize this API to schedule the pickup of their packages, ensuring that their items are picked up at the desired time and location. Note that the pickup request is separate from the create shipment request, as a single pickup may collect multiple shipment packages at one time.

Required Information:

Response Includes:


Cancel Pickup (Optional)

Cancels a previously scheduled pickup.

Required Information:

Response Includes:


Notes

duylv27 commented 2 days ago

Testing FedEx API

Rates - Transit Times (estimate fee, delivery date) & Shipment APIs (create shipment)

Make sure request & response consistent (estimated fee, delivery date). Perform some tests on get rates and create shipments APIs to identify any fields in request that may lead to inconsistencies between these two APIs.

Assume that, these 2 APIs require certain fields to be identical for consistency.