faraztanveer / restaurant_vouchers
允许用户兑换与模型绑定的代金券。
Requires
- php: ^7.1
- illuminate/config: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0
- illuminate/database: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0
- phpunit/phpunit: ^7.0|^8.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, ];
用法
此包的基本概念是您可以为特定模型创建关联的代金券。例如,您可能有一个销售在线视频课程的程序,代金券可以为用户提供访问特定视频课程的机会。
将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();
具有附加数据的代金券
将任意数据与您的代金券关联可能很有用——例如,创建代金券的人的个性化信息等。在创建代金券时,您可以传递一个数组作为第二个参数,稍后可以在代金券实例上检索该数组。
$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
模型有一个指向关联Eloquent模型的model
关系。
$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)。有关更多信息,请参阅许可文件。