PHPackages                             banulakwin/laravel-pricing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. banulakwin/laravel-pricing

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

banulakwin/laravel-pricing
==========================

Rule-based Laravel pricing engine with configurable coupon, tax, and shipping rules.

1.1.0(3w ago)07MITPHPPHP ^8.2CI passing

Since May 18Pushed 3w agoCompare

[ Source](https://github.com/banulalakwindu/laravel-pricing)[ Packagist](https://packagist.org/packages/banulakwin/laravel-pricing)[ Docs](https://github.com/banulalakwindu/laravel-pricing)[ RSS](/packages/banulakwin-laravel-pricing/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (6)Versions (3)Used By (0)

Laravel Pricing (`banulakwin/laravel-pricing`)
==============================================

[](#laravel-pricing-banulakwinlaravel-pricing)

[![Latest Version on Packagist](https://camo.githubusercontent.com/da33f7d06c28ee2a9439112d30951a68f09b1e0382628c63c498e3ce566f38ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62616e756c616b77696e2f6c61726176656c2d70726963696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/banulakwin/laravel-pricing)[![Tests](https://github.com/banulalakwindu/laravel-pricing/actions/workflows/tests.yml/badge.svg)](https://github.com/banulalakwindu/laravel-pricing/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/38a6b398ceec97d635161855a5d8061c6c4166c2641b69404ab3ccee2b7fd1f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62616e756c616b77696e2f6c61726176656c2d70726963696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/banulakwin/laravel-pricing)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Portable Laravel package: a small **rule-based pricing engine** with **config toggles**, **injectable coupon validation**, and defaults for **coupon (percent)**, **tax**, and **flat shipping**.

---

Requirements
------------

[](#requirements)

- PHP `^8.2`
- Laravel `^11.0|^12.0|^13.0`

---

Installation
------------

[](#installation)

Registration is automatic via Composer `extra.laravel.providers`:

- `Banulakwin\Pricing\PricingServiceProvider`

Optional facade alias: `Pricing` → `Banulakwin\Pricing\Facades\Pricing`.

Publish configuration (optional):

```
php artisan vendor:publish --tag=pricing-config
```

Publish migrations (optional):

```
php artisan vendor:publish --tag=pricing-migrations
php artisan migrate
```

---

Configuration
-------------

[](#configuration)

Config keyPurpose`pricing.rules`Booleans: `coupon`, `tax`, `shipping` (your app branches when building the engine).`pricing.user_model`Class string for the user model, used for coupon user restrictions and first-time validation.`pricing.user_orders_relation`Method name on the user model (e.g., `orders`) to verify `first_time_only` coupon eligibility.`pricing.user_orders_scope`Optional scope name (e.g., `captured`) applied to the orders query. Set to `null` to use the relation as-is.`pricing.tax.rate`Default tax rate for `TaxRule` (overridable in constructor).`pricing.shipping.flat`Default flat shipping for `ShippingRule` (overridable in constructor).`pricing.auto_rules`Optional list of rule class names to resolve with `app($class)` and attach to the engine.Environment: `PRICING_TAX_RATE`, `PRICING_SHIPPING_FLAT` (see `config/pricing.php`).

---

Usage
-----

[](#usage)

### Basic Pricing

[](#basic-pricing)

```
use Banulakwin\Pricing\Facades\Pricing;
use Banulakwin\Pricing\Rules\CouponRule;
use Banulakwin\Pricing\Rules\ShippingRule;
use Banulakwin\Pricing\Rules\TaxRule;

$engine = Pricing::engine();
// or: pricing()->engine();

$engine
    ->addRule(new CouponRule(['type' => 'percent', 'value' => 10, 'items' => [], 'users' => []]))
    ->addRule(new TaxRule)
    ->addRule(new ShippingRule);

$context = $engine->calculate($items, ['user_id' => auth()->id()]);

echo $context->subtotal;   // 250.00
echo $context->discount;   // 25.00
echo $context->tax;        // 22.50
echo $context->shipping;   // 10.00
echo $context->total;      // 257.50
```

### Line Item Format

[](#line-item-format)

```
$items = [
    ['price' => 100, 'qty' => 2, 'type' => 'product', 'id' => 1],
    ['price' => 50, 'qty' => 1, 'type' => 'product', 'id' => 2],
];
```

### Conditional Rule Registration

[](#conditional-rule-registration)

```
$rules = config('pricing.rules', []);
$engine = pricing()->engine();

if ($rules['coupon'] ?? false) {
    $engine->addRule(new CouponRule($coupon, ['couponable_type' => 'type', 'couponable_id' => 'id']));
}
if ($rules['tax'] ?? false) {
    $engine->addRule(new TaxRule);
}
if ($rules['shipping'] ?? false) {
    $engine->addRule(new ShippingRule);
}

$result = $engine->calculate($items, ['user_id' => auth()->id()]);
```

---

Architecture
------------

[](#architecture)

### Engine Flow

[](#engine-flow)

1. `calculate()` builds `PriceContext`, sets `subtotal` / initial `total` from line items (`price` × `qty`).
2. Each registered rule mutates `discount`, `tax`, `shipping`, and `total` in order.

**Rule order matters** — compose rules explicitly in your app.

### Contracts

[](#contracts)

- `Banulakwin\Pricing\Contracts\PriceRule` — `apply(PriceContext $context): void`
- `Banulakwin\Pricing\Contracts\RuleValidator` — `validate(PriceContext $context, array $data): bool` (optional for `CouponRule`)

---

Coupon Types
------------

[](#coupon-types)

### Global Coupon

[](#global-coupon)

Applies to all items in the cart (empty `items` relation).

### Targeted Coupon

[](#targeted-coupon)

Applies only to specific items via `coupon_items` pivot table.

### User-Restricted Coupon

[](#user-restricted-coupon)

Only redeemable by specific users via `coupon_users` pivot table.

### First-Time Only Coupon

[](#first-time-only-coupon)

Validates that the user has no prior orders via the configured `user_orders_relation`. Guest users are allowed through during preview — server-side checkout re-validates once authenticated.

### Once Per User Coupon

[](#once-per-user-coupon)

Prevents a user from redeeming the same coupon code more than once. Checks past orders for the coupon code via the configured `user_orders_relation`. Guest users are allowed through during preview — server-side checkout re-validates once authenticated.

### Configurable Order Scope

[](#configurable-order-scope)

When validating `first_time_only` or `once_per_user`, an optional scope can be applied to the orders query via `pricing.user_orders_scope` (e.g., `captured` to only count paid orders). Leave it `null` to use the relation as-is, or define your relation to return only the orders you want counted.

---

Testing
-------

[](#testing)

```
composer test          # Run PHPUnit
composer pint          # Fix code style
composer phpstan       # Static analysis
composer quality       # Run all (pint + phpstan + test)
```

---

Changelog
---------

[](#changelog)

See [CHANGELOG.md](CHANGELOG.md) for details.

---

Contributing
------------

[](#contributing)

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/your-feature`)
3. Run `composer quality` to ensure tests and style pass
4. Commit and push
5. Open a pull request

---

Package layout (reference)
--------------------------

[](#package-layout-reference)

```
src/
  PricingServiceProvider.php
  helpers.php
  Contracts/
    PriceRule.php
    RuleValidator.php
  Engine/
    PriceContext.php
    PricingEngine.php
  Facades/
    Pricing.php
  Managers/
    PricingManager.php
  Models/
    Coupon.php
    CouponItem.php
    CouponUser.php
  Rules/
    CouponRule.php
    ShippingRule.php
    TaxRule.php
config/
  pricing.php
database/
  migrations/
    2026_05_11_000000_create_coupons_table.php
    2026_05_11_000001_create_coupon_items_table.php
    2026_05_11_000002_create_coupon_users_table.php

```

---

License
-------

[](#license)

MIT — see [LICENSE](LICENSE) for details.

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance95

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

21d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/64958389?v=4)[Banula Lakwindu](/maintainers/banulalakwindu)[@banulalakwindu](https://github.com/banulalakwindu)

---

Top Contributors

[![banulalakwindu](https://avatars.githubusercontent.com/u/64958389?v=4)](https://github.com/banulalakwindu "banulalakwindu (2 commits)")

---

Tags

laravelengineshippingtaxpricingcoupondiscount

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/banulakwin-laravel-pricing/health.svg)

```
[![Health](https://phpackages.com/badges/banulakwin-laravel-pricing/health.svg)](https://phpackages.com/packages/banulakwin-laravel-pricing)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[frittenkeez/laravel-vouchers

Voucher system for Laravel 11+

5821.2k2](/packages/frittenkeez-laravel-vouchers)[wearepixel/laravel-cart

A cart implementation for Laravel

1355.6k](/packages/wearepixel-laravel-cart)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
