hipstersg-demo/laravel-user-discounts-package
最新稳定版本:1.0.0
Composer 安装命令:
composer require hipstersg-demo/laravel-user-discounts-package
包简介
interview task
README 文档
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
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-09-26