hipstersg-demo/laravel-user-discounts-package

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

interview task

Maintainers

Package info

github.com/vishaljagani08/hipstersg-demo-laravel-user-discounts-package

pkg:composer/hipstersg-demo/laravel-user-discounts-package

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2025-09-28 01:04 UTC

This package is auto-updated.

Last update: 2026-03-28 02:05:37 UTC


README

A reusable Laravel 12 package for deterministic user-level discounts with stacking, caps, audits, and concurrency safety.

πŸ“¦ Installation

  1. Install via Composer

    composer require hipstersg-demo/laravel-user-discounts-package
  2. Publish Config & Migrations

    php artisan vendor:publish --provider="Vendor\\UserDiscounts\\DiscountServiceProvider" --tag=config
    php artisan migrate
  3. (Optional) Publish for Customization

    php artisan vendor:publish --provider="Vendor\\UserDiscounts\\DiscountServiceProvider" --tag=migrations
    php artisan vendor:publish --provider="Vendor\\UserDiscounts\\DiscountServiceProvider" --tag=seeders

πŸ›  Usage

βž• Assigning a Discount

use Vendor\UserDiscounts\Models\Discount;
use Vendor\UserDiscounts\Facades\Discounts;

$discount = Discount::create([
    'code' => 'WELCOME10',
    'type' => 'percentage',
    'value' => 10,
    'active' => true,
]);

Discounts::assign($user, $discount);

πŸ” Checking Eligibility

if (Discounts::eligibleFor($user, $discount)) {
    echo "User can use this discount.";
}

πŸ’° Applying Discounts

$result = Discounts::apply($user, 1500.00, [
    'idempotency_key' => 'order-1001',
    'meta' => ['order_id' => 1001],
]);

echo "Final amount: {$result['amount']}";

❌ Revoking Discounts

Discounts::revoke($user, $discount);

πŸ“‘ Events

The package fires events that you can listen for in your app:

  • DiscountAssigned($user, Discount $discount, array $meta)
  • DiscountRevoked($user, Discount $discount)
  • DiscountApplied($user, Collection $discounts, DiscountAudit $audit)

Example Listener

namespace App\Listeners;

use Vendor\UserDiscounts\Events\DiscountApplied;

class SendDiscountNotification
{
    public function handle(DiscountApplied $event)
    {
        // Access: $event->user, $event->discounts, $event->audit
        // Example: send email or log analytics
    }
}

Register in EventServiceProvider:

protected $listen = [
    DiscountApplied::class => [SendDiscountNotification::class],
];

βš™οΈ Configuration

See config/discounts.php for options:

  • stacking_order β†’ priority | percentage_first | fixed_first
  • max_total_percentage β†’ cap on total % discount
  • rounding β†’ precision + mode (half_up, half_down, up, down)
  • default_per_user_cap β†’ fallback if discount doesn’t define one
  • require_idempotency_key β†’ enforce unique apply calls

πŸ‘¨β€πŸ’» Developer