定制 hipstersg-demo/laravel-user-discounts-package 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

  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_orderpriority | 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

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-09-26