hipstersg-demo / laravel-user-discounts-package
There is no license information available for the latest version (1.0.0) of this package.
interview task
Package info
github.com/vishaljagani08/hipstersg-demo-laravel-user-discounts-package
pkg:composer/hipstersg-demo/laravel-user-discounts-package
1.0.0
2025-09-28 01:04 UTC
Requires
- php: >=8.1
- illuminate/database: ^12.0
- illuminate/support: ^12.0
Requires (Dev)
- orchestra/testbench: ^8.0
README
A reusable Laravel 12 package for deterministic user-level discounts with stacking, caps, audits, and concurrency safety.
π¦ Installation
-
Install via Composer
composer require hipstersg-demo/laravel-user-discounts-package
-
Publish Config & Migrations
php artisan vendor:publish --provider="Vendor\\UserDiscounts\\DiscountServiceProvider" --tag=config php artisan migrate -
(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_firstmax_total_percentageβ cap on total % discountroundingβ precision + mode (half_up,half_down,up,down)default_per_user_capβ fallback if discount doesnβt define onerequire_idempotency_keyβ enforce unique apply calls
π¨βπ» Developer
- Vishal Jagani
- π§ vish2patel08@gmail.com
- π +91 90995 46953