beyondcode / laravel-vouchers
允许用户兑换与模型绑定的优惠券。
Requires
- php: ^7.1|^8.0
- illuminate/config: ^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0|^8.0|^9.0
- phpunit/phpunit: ^8.0|^9.0|^10.0
README
此包可以将优惠券与您的 Eloquent 模型关联。如果您需要将优惠券代码与存储在 Eloquent 模型中的内容关联,这可能很有用。
以下是如何创建和兑换优惠券的示例
$videoCourse = VideoCourse::find(1); $voucher = $videoCourse->createVoucher(); auth()->user()->redeemVoucher($voucher);
安装
您可以通过 composer 安装此包
composer require beyondcode/laravel-vouchers
包将自动注册自己。
您可以使用以下命令发布迁移
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="migrations"
迁移发布后,您可以通过运行迁移来创建优惠券表
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="config"
这是发布配置文件的内容
<?php return [ /* * Database table name that will be used in migration */ 'table' => 'vouchers', /* * Database pivot table name for vouchers and users relation */ 'relation_table' => 'user_voucher', /* * List of characters that will be used for voucher code generation. */ 'characters' => '23456789ABCDEFGHJKLMNPQRSTUVWXYZ', /* * Voucher code prefix. * * Example: foo * Generated Code: foo-AGXF-1NH8 */ 'prefix' => null, /* * Voucher code suffix. * * Example: foo * Generated Code: AGXF-1NH8-foo */ 'suffix' => null, /* * Code mask. * All asterisks will be removed by random characters. */ 'mask' => '****-****', /* * Separator to be used between prefix, code and suffix. */ 'separator' => '-', /* * The user model that belongs to vouchers. */ 'user_model' => \App\User::class, ];
如果需要,您可以发布翻译文件进行进一步定制
php artisan vendor:publish --provider="BeyondCode\Vouchers\VouchersServiceProvider" --tag="translations"
您可以通过以下方式访问包的翻译:__('vouchers::validation.code_invalid')
。
使用方法
此包的基本概念是可以创建与特定模型关联的优惠券。例如,您可能有一个在线视频课程销售应用程序,优惠券将允许用户访问特定的视频课程。
将 BeyondCode\Vouchers\Traits\HasVouchers
特性添加到您想要与优惠券关联的所有 Eloquent 模型中。
namespace App\Models; use BeyondCode\Vouchers\Traits\CanRedeemVouchers; class User extends Authenticatable { use CanRedeemVouchers; # ... }
此外,将 BeyondCode\Vouchers\Traits\CanRedeemVouchers
特性添加到您的用户模型中。这样,用户可以轻松兑换优惠券代码,并且该包将负责在数据库中存储优惠券/用户关联。
namespace App\Models; use BeyondCode\Vouchers\Traits\HasVouchers; class VideoCourse extends Model { use HasVouchers; # ... }
创建优惠券
使用外观
您可以通过使用 Vouchers
外观创建一个或多个优惠券
$videoCourse = VideoCourse::find(1); // Create 5 vouchers associated to the videoCourse model. $vouchers = Vouchers::create($videoCourse, 5);
返回值是一个包含所有生成的 Voucher
模型的数组。
优惠券模型有一个名为 code
的属性,其中包含生成的优惠券代码。
使用 Eloquent 模型
此外,您还可以通过在关联模型上使用 createVouchers
方法来创建优惠券
$videoCourse = VideoCourse::find(1); // Returns an array of Vouchers $vouchers = $videoCourse->createVouchers(2); // Returns a single Voucher model instance $vouchers = $videoCourse->createVoucher();
具有附加数据的优惠券
将任意数据与您的优惠券关联可能很有用 - 例如,可能是创建优惠券的人的个人消息等。在创建优惠券时,您可以将数组作为第二个参数传递,然后可以在 Voucher 实例上稍后检索它。
$videoCourse = VideoCourse::find(1); $vouchers = $videoCourse->createVouchers(2, [ 'from' => 'Marcel', 'message' => 'This one is for you. I hope you like it' ]); $voucher = $user->redeem('ABC-DEF'); $from = $voucher->data->get('from'); $message = $voucher->data->get('message');
具有到期日期的优惠券
您还可以创建仅在特定日期之前可用的优惠券。用户之后无法兑换此代码。`createVouchers` 方法接受 Carbon 实例作为第三个参数。
$videoCourse = VideoCourse::find(1); $videoCourse->createVouchers(2, [], today()->addDays(7));
兑换优惠券
让您的用户兑换优惠券代码的最简单方法是使用用户模型上的 redeemCode
方法
$voucher = $user->redeemCode('ABCD-EFGH');
如果优惠券有效,该方法将返回与此代码关联的优惠券模型。
如果您想兑换现有的优惠券模型,您可以在用户模型上使用 redeemVoucher
方法
$user->redeemVoucher($voucher);
用户成功兑换优惠券后,此包将触发 BeyondCode\Vouchers\Events\VoucherRedeemed
事件。该事件包含用户实例和优惠券实例。您应该监听此事件,以便在用户兑换优惠券时执行应用程序的业务逻辑。
访问关联的优惠券模型
优惠券模型有一个指向关联 Eloquent 模型的 model
关系。
$voucher = $user->redeemCode('ABCD-EFGH'); $videoCourse = $voucher->model;
验证优惠券和优惠券代码
isValidCode
和 isValidVoucher
方法在 Vouchers
门面中允许您检查优惠券代码是否有效或优惠券模型是否有效。
Vouchers::isValidCode('ABCD-EFGH'); // true or false Vouchers::isValidVoucher($voucher); // true or false
错误处理
redeemCode
和 redeemVoucher
方法会抛出一些异常,您需要在应用程序中捕获并响应这些异常。
优惠券无效
如果用户尝试兑换无效的代码,程序将抛出以下异常:BeyondCode\Vouchers\Exceptions\VoucherIsInvalid
。
优惠券已兑换
所有生成的优惠券只能兑换一次。如果用户尝试第二次兑换优惠券,或者如果有其他用户已经兑换了此优惠券,程序将抛出以下异常:BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed::class
。
优惠券过期
如果用户尝试兑换过期的优惠券代码,程序将抛出以下异常:BeyondCode\Vouchers\Exceptions\VoucherExpired
。
测试
composer test
更新日志
有关最近更改的详细信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献指南。
安全性
如果您发现任何安全问题,请通过电子邮件marcel@beyondco.de联系,而不是使用问题跟踪器。
致谢
此包主要基于Zura Gabievi的Laravel Promocodes包。您可以在GitHub上找到代码。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。