patinthehat / laravel-vouchers
允许用户兑换与模型绑定的代金券。
Requires
- php: ^7.1.3
- illuminate/config: ^5.7
- illuminate/database: ^5.7
- illuminate/support: ^5.7
Requires (Dev)
- larapack/dd: ^1.0
- mockery/mockery: ^1.1
- phpunit/phpunit: ^7.0
This package is not auto-updated.
Last update: 2024-09-29 05:27:57 UTC
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, ];
用法
此包的基本概念是您可以为特定模型创建关联的代金券。例如,您可能有一个在线视频课程销售应用程序,代金券可以为用户提供访问一个特定视频课程的权限。
将 BeyondCode\Vouchers\Traits\HasVouchers
特性添加到您想要与代金券关联的所有 Eloquent 模型。
此外,将 BeyondCode\Vouchers\Traits\CanRedeemVouchers
特性添加到您的用户模型中。这样用户可以轻松兑换代金券代码,而包会负责在数据库中存储代金券/用户关联。
创建代金券
使用外观
您可以通过使用 Vouchers
外观创建一个或多个代金券
$videoCourse = VideoCourse::find(1); // Create 5 vouchers associated to the videoCourse model. $vouchers = Vouchers::create($videoCourse, 5);
返回值是一个包含所有生成的 Voucher
模型的数组。
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
事件。该事件包含用户实例和代金券实例。您应该监听此事件,以便在用户兑换代金券时执行应用程序的业务逻辑。
访问关联的代金券模型
Voucher
模型有一个 model
关联,该关联将指向关联的 Eloquent 模型
$voucher = $user->redeemCode('ABCD-EFGH'); $videoCourse = $voucher->model;
处理错误
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)。有关更多信息,请参阅许可文件。