adeela/user-discounts
最新稳定版本:v1.0.0
Composer 安装命令:
composer require adeela/user-discounts
包简介
A Laravel package for user-level discounts
README 文档
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
-
Install via Composer
composer require adeela/user-discounts
-
Register Service Provider (if not auto-discovered)
In
config/app.php:'providers' => [ Adeela\UserDiscounts\UserDiscountsServiceProvider::class, ],
-
Publish Config File
php artisan vendor:publish --provider="Adeela\UserDiscounts\UserDiscountsServiceProvider" --tag="config"
This creates
config/user-discounts.php. -
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
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-09-28