Metadata-Version: 2.4
Name: ecommerce-package
Version: 0.1.0
Summary: Ecommerce site package for the Core boilerplate: catalog with filtering, cart, checkout/orders with PDF invoices, wallet, a pluggable payment gateway (mock by default), a support-ticket system, and client/seller/super-admin panels. Single-store or multi-vendor marketplace, switchable via ECOMMERCE_MULTI_VENDOR — same codebase either way. Depends on core-package for auth (including OTP login), RBAC, and the shared User model — it does not define its own.
Author-email: puzzllium <puzzllium@gmail.com>
Maintainer-email: puzzllium <puzzllium@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/puzzelchannel/ecommerce-package
Project-URL: Issues, https://github.com/puzzelchannel/ecommerce-package/issues
Keywords: django,ecommerce,shop,marketplace,cart,django-ninja
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: core-package>=0.2.0
Requires-Dist: django-ninja>=1.7.0
Requires-Dist: django-unfold>=0.81.0
Requires-Dist: Pillow>=10.0
Requires-Dist: reportlab>=4.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: pytest-django>=4.14; extra == "dev"
Requires-Dist: pytest-asyncio>=1.4; extra == "dev"
Dynamic: license-file

# ecommerce-package

An ecommerce site package for the [Core](https://pypi.org/project/core-package/) boilerplate: catalog
with filtering, cart, checkout/orders with PDF invoices, a wallet, a
pluggable payment gateway (mock by default — no real gateway credentials
exist for this project), a support-ticket system, and three panels: client,
seller, and super-admin. It depends on `core-package` for auth
(including OTP login — see Core's `apps/core/sms.py` and
`apps/users/services.py`), RBAC, and the shared `User` model — **it never
defines its own user/account model.**

## Single-store or multi-vendor — one setting, one codebase

`ECOMMERCE_MULTI_VENDOR` (env-driven, see `ecommerce/conf.py`) switches the
whole package between:

- **Multi-vendor** (default): any authenticated user can `POST
  /sellers/apply`; an admin approves (`POST /admin/sellers/{id}/approval`)
  before they can list products. Many `Seller` rows can exist.
- **Single-store**: `POST /sellers/apply` is disabled (`403`), and exactly
  one `Seller` row is auto-provisioned on first access
  (`services.sellers.get_default_seller`, `user=None` — nobody individually
  owns it) that admins manage directly through the admin-panel endpoints.

Every model/service/endpoint is identical either way — single-store is the
multi-vendor model with exactly one seller, not a second code path. Set it
in the host's `.env`:

```
ECOMMERCE_MULTI_VENDOR=True
ECOMMERCE_PAYMENT_GATEWAY=mock
ECOMMERCE_DEFAULT_COMMISSION_RATE=10.00
```

## Payments

No real gateway credentials exist for this project, so the only backend
shipped is `MockPaymentGateway` (`ecommerce/gateways.py`) — it "succeeds"
immediately with a fake reference, so wallet top-up and order payment work
end-to-end in dev/tests with zero external accounts. A real gateway
(Zarinpal, Stripe, ...) plugs in as a second `PaymentGateway` subclass
registered in `get_gateway()`, selected via `ECOMMERCE_PAYMENT_GATEWAY` —
nothing else in this package changes. `POST /payments/{id}/callback` is
where a real (redirect-based) gateway posts back; the mock backend never
needs it since it resolves synchronously.

## Wallet

One `Wallet` per user — both clients (top up, pay for orders, receive
refunds) and sellers (receive sale proceeds minus commission, request
payouts) use the same model. `Wallet.balance` is a materialized total kept
in sync with the `WalletTransaction` ledger, the actual audit trail
(`services.wallet.apply_transaction`, atomic via an `F()` expression).

## Invoices

Generated on demand as a PDF (`ecommerce/pdf.py`, via `reportlab` — same
pure-Python choice as the `company` package's CV generator, no system
libraries needed) from the `Invoice` row + `OrderItem` snapshots, never
stored as a file — always in sync, can't go stale.

See Core's `AGENT.md` §10 ("Package architecture") for the full contract this
package follows.

## Install

```bash
pip install ecommerce-package
```

Requires Python 3.10+ and Django 5.2 (via core-package).

## Local development

```bash
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest
```

## Installing into a Core-based project

1. `pip install ecommerce-package` into the host project's venv.
2. Add `"ecommerce"` to `INSTALLED_APPS` under `# site packages`.
3. Mount its router in `config/api.py`:
   ```python
   from ecommerce.api import router as ecommerce_router
   ...
   api.add_router("/shop", ecommerce_router)
   ```
4. Add `ECOMMERCE_MULTI_VENDOR`/`ECOMMERCE_PAYMENT_GATEWAY`/
   `ECOMMERCE_DEFAULT_COMMISSION_RATE` to `.env` (see above; all have safe
   defaults if omitted).
5. `python manage.py migrate`.

## Endpoints

Mounted at whatever prefix the host chooses (`/shop` in the example above).

**Catalog (public reads; category/attribute writes admin-only)**
- `GET /categories`, `GET /attributes`
- `GET /products` — paginated, filterable: `?category=`, `?seller_id=`,
  `?search=`, `?min_price=`/`?max_price=`, `?in_stock=`,
  `?attributes=color:red,size:l`, `?sort=newest|price_asc|price_desc|name`
- `GET /products/by-slug/{slug}`
- `POST/PATCH/DELETE /categories`, `POST /attributes` — admin-only

**Cart (logged in)**
- `GET /cart`, `POST /cart/items`, `PATCH /cart/items/{product_id}`,
  `DELETE /cart/items/{product_id}`, `DELETE /cart`

**Orders (logged in, own orders only)**
- `POST /orders/checkout` — splits the cart into one order per seller
- `GET /orders`, `GET /orders/{id}`
- `POST /orders/{id}/pay-with-wallet`, `POST /orders/{id}/cancel`
- `GET /orders/{id}/invoice` — PDF download, only once paid

**Wallet / Payments (logged in)**
- `GET /wallet`, `GET /wallet/transactions`
- `POST /payments/wallet/topup`, `POST /payments/orders/{order_id}`
- `POST /payments/{payment_id}/callback` — `auth=None` (gateway calls this)

**Tickets (logged in, own tickets only)**
- `POST /tickets`, `GET /tickets`, `GET /tickets/{id}`,
  `POST /tickets/{id}/messages`, `POST /tickets/{id}/close`

**Seller panel** (`POST /sellers/apply` needs only login; everything else
under `/sellers/*` needs an approved `Seller`, via the `require_seller`
decorator in `ecommerce/permissions.py`)
- `POST /sellers/apply`, `GET/PATCH /sellers/me`,
  `PUT /sellers/me/translations/{language}`, `POST /sellers/me/logo`
- `GET/POST /sellers/products`, `PATCH/DELETE /sellers/products/{id}`,
  `PUT /sellers/products/{id}/translations/{language}`,
  `PUT /sellers/products/{id}/attributes/{attribute_id}`
- `GET /sellers/orders`, `GET /sellers/orders/{id}`,
  `POST /sellers/orders/{id}/status`, `POST /sellers/orders/{id}/refund`
- `GET /sellers/wallet`, `GET /sellers/wallet/transactions`,
  `POST /sellers/wallet/payout`

**Super-admin panel** (`require_role("admin")`, from Core — every route)
- `GET /admin/sellers`, `POST /admin/sellers/{id}/approval`
- `GET /admin/products`, `POST /admin/sellers/{seller_id}/products`,
  `PATCH/DELETE /admin/products/{id}`
- `GET /admin/orders`, `GET /admin/orders/{id}`,
  `POST /admin/orders/{id}/status`, `POST /admin/orders/{id}/refund`
- `GET /admin/payments` (read-only oversight)
- `GET /admin/tickets`, `GET /admin/tickets/{id}`,
  `POST /admin/tickets/{id}/messages`, `POST /admin/tickets/{id}/assign`,
  `POST /admin/tickets/{id}/close`

## Known simplifications (deliberate, not oversights)

- Single-SKU products only — no size/color variant matrix with per-variant
  stock; `ProductAttribute`/`ProductAttributeValue` are filterable metadata,
  not inventory-affecting combinations.
- No shipping-cost calculation or tax engine — `Order.total` is the item
  subtotal only.
- No coupon/discount-code system.
- Stock is decremented at checkout (order creation), not at payment —
  placing an order reserves inventory; `cancel_order` restores it, but
  there's no automatic expiry/release for an order left unpaid forever.
- Refunds reverse the client's full payment and the seller's net proceeds,
  but not the commission line separately (documented in
  `services.orders.refund_order`).
- Invoice numbering has a narrow race under concurrent checkouts on the
  same day (documented in `services.orders._issue_invoice`) — fine for this
  boilerplate's scope, not something to trust under real concurrent load
  without backing it with a DB sequence.
