narpat-bishnoi/laravel-user-discounts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

narpat-bishnoi/laravel-user-discounts

Composer 安装命令:

composer require narpat-bishnoi/laravel-user-discounts

包简介

Deterministic, idempotent user-level discounts for Laravel.

README 文档

README

1. Publish (optional) and migrate:

composer require acme/laravel-user-discounts
php artisan vendor:publish --tag=config --provider="NarpatBishnoi\UserDiscounts\DiscountsServiceProvider"
php artisan migrate

2. Create discounts (seed or admin UI):

use NarpatBishnoi\UserDiscounts\Models\Discount;

$summer = Discount::create([
  'code' => 'SUMMER20',
  'name' => '20% off summer',
  'type' => 'percentage',
  'percent' => '20.00',
  'priority' => 10,
  'active' => true,
  'starts_at' => now(),
  'ends_at' => now()->addMonth(),
]);

3. Assign to user:

use NarpatBishnoi\UserDiscounts\Facades\UserDiscounts;

UserDiscounts::assign($user, $summer, usageLimit: 2);

4. Check eligibility:

$eligible = app('acme.user-discounts')->eligibleFor($user);
// -> Collection of Discount models

5. Apply to an order subtotal (in cents!) with an idempotency context key:

$result = app('acme.user-discounts')->apply($user, 4599 /* $45.99 */, 'order:98765');

/*
$result = [
  'application_uid' => '01J..ULID..',
  'context_key' => 'order:98765',
  'subtotal_before_cents' => 4599,
  'subtotal_after_cents' => 3899,
  'total_discount_cents' => 700,
  'lines' => [
      ['discount_id' => 1, 'amount_cents' => 200],
      ['discount_id' => 2, 'amount_cents' => 500],
  ],
]
*/

6. Revoke (soft by default):

UserDiscounts::revoke($user, $summer);          // sets revoked_at
UserDiscounts::revoke($user, $summer, true);     // hard delete pivot

How this meets your Acceptance Criteria

Assign → eligible → apply works with audits assign() creates/updates pivot; eligibleFor() filters active window + usage caps; apply() writes a row per discount into discount_audits with an application_uid to group lines; event DiscountApplied fires.

Expired/inactive excluded Discount::scopeActiveWindow() and active flag enforced; eligibleFor() and apply() both respect this.

Usage caps enforced user_discounts.used_count < usage_limit checked and rows locked via lockForUpdate() so concurrent apply can’t double-increment.

Stacking and rounding correct Configured type order + priority + ID tie-break; percentage lines rounded per discounts.rounding.mode; optional aggregate cap (e.g., max 80% off) with deterministic proportional scaling.

Revoked discounts not applied user_discounts.revoked_at excluded in queries.

Concurrency safe Transaction + lockForUpdate() on pivots + unique index on (user_id, discount_id, context_key) yields idempotent writes; duplicate concurrent inserts are handled and usage increments occur at most once.

Unit Test included tests/Feature/DiscountApplicationTest.php validates discount math, idempotency, and cap logic.

Notes / Options

Money representation: Inputs/outputs are integer cents to avoid float drift. If you use a Money library, adapt apply() signatures accordingly.

Stacking policies: Update config/discounts.php to switch order (e.g., apply fixed before percentage), or add further strategies (e.g., “best of”).

Global usage limits / product scopes: Extend the schema and eligibleFor() as needed.

Audits shape: Each discount line is individually auditable and replayable via (user_id, context_key) query.

narpat-bishnoi/laravel-user-discounts 适用场景与选型建议

narpat-bishnoi/laravel-user-discounts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「ecommerce」 「pricing」 「laravel」 「coupons」 「discounts」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 narpat-bishnoi/laravel-user-discounts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 narpat-bishnoi/laravel-user-discounts 我们能提供哪些服务?
定制开发 / 二次开发

基于 narpat-bishnoi/laravel-user-discounts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-01