adeela/user-discounts

There is no license information available for the latest version (v1.0.0) of this package.

A Laravel package for user-level discounts

Maintainers

Package info

github.com/adeelagit/Laravel-user-discount-package

pkg:composer/adeela/user-discounts

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2025-09-28 06:26 UTC

This package is auto-updated.

Last update: 2026-03-09 13:39:15 UTC


README

A Laravel 8+ package to manage user-level discounts with deterministic stacking, usage caps, and audit logging.

✨ Features

  • Assign and revoke discounts to specific users
  • Validate user discount eligibility
  • Apply discounts with stacking and rounding logic
  • Enforce per-user usage limits
  • Log and audit discount usage
  • Fire events on discount assignment, revocation, and application

🧩 Installation

  1. Install via Composer

    composer require adeela/user-discounts
  2. Register Service Provider (if not auto-discovered)

    In config/app.php:

    'providers' => [
        Adeela\UserDiscounts\UserDiscountsServiceProvider::class,
    ],
  3. Publish Config File

    php artisan vendor:publish --provider="Adeela\UserDiscounts\UserDiscountsServiceProvider" --tag="config"

    This creates config/user-discounts.php.

  4. Run Migrations (if provided)

    php artisan migrate

⚙️ Configuration

Sample config/user-discounts.php:

```php
return [
    'stacking_order' => 'highest_first',
    'max_percentage_cap' => 50,
    'rounding' => 'round',
];
```

🧠 Usage Examples

1. Create a Discount

use Adeela\UserDiscounts\Models\Discount;

$discount = Discount::create([
    'code' => 'Save20',
    'name' => 'Save 20%',
    'type'       => 'percentage',
    'value'      => 20,
    'active'     => 1,
    'starts_at'  => now()->subDay(),
    'ends_at'    => now()->addDay(),
]);

2. Assign Discount to a User

$service = new \Adeela\UserDiscounts\Services\DiscountService();

$user = User::find(1);
$discount = Discount::find(34);

$service->assign($user, $discount);

4. Apply Discount

$service = new \Adeela\UserDiscounts\Services\DiscountService();
$amount = 100;
$orderRef = 'ORDER-12345';
$user = User::find(1);

$finalAmount = $service->apply($user, $amount, $orderRef);
dd("Final amount after discounts: {$finalAmount}");

5. Revoke Discount

$service = new \Adeela\UserDiscounts\Services\DiscountService();

$user = User::find(1);
$discount = Discount::find(34);

$service->revoke($user, $discount);

🔔 Events

Event Description
DiscountAssigned Triggered when a discount is assigned
DiscountRevoked Triggered when a discount is revoked
DiscountApplied Triggered when a discount is applied

🧪 Testing

vendor/bin/phpunit