tankonyako/promocodes
Composer 安装命令:
composer require tankonyako/promocodes
包简介
Promotional Codes Generator for Laravel 5
README 文档
README
Promocodes generator for Laravel 5.*. Trying to make the best package in this category. You are welcome to join the party, give me some advices 🎉 and make pull requests.
Table of Contents
What's new?
Installation
Install this package via Composer:
$ composer require tankonyako/promocodes
If you are using Laravel 5.5 or later, then installation is done. Otherwise follow the next steps.
Open config/app.php and follow steps below:
Find the providers array and add our service provider.
'providers' => [ // ... Gabievi\Promocodes\PromocodesServiceProvider::class ],
Find the aliases array and add our facade.
'aliases' => [ // ... 'Promocodes' => Gabievi\Promocodes\Facades\Promocodes::class ],
Configuration
Publish config & migration file using Artisan command:
$ php artisan vendor:publish
To create table for promocodes in database run:
$ php artisan migrate
Configuration parameters are well documented. There is no need to describe each parameter here.
Check
config/promocodes.phpand read comments there if you need.
Usage
Generate as many codes as you wish and output them without saving to database.
You will get array of codes in return:
Promocodes::output($amount = 1);
Parameters
| name | type | description | required? |
|---|---|---|---|
| $amount | number | Number of items to be generated | NO |
Create as many codes as you wish. Set reward (amount).
Attach additional data as array. Specify for how many days should this codes stay alive.
By default generated code will be multipass (several users will be able to use this code once).
They will be saved in database and you will get collection of them in return:
Promocodes::create($amount = 1, $reward = null, array $data = [], $expires_in = null, $quantity = null, $is_disposable = false);
If you want to create code that will be used only once, here is method for you.
Promocodes::createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null, $quantity = null);
Parameters
| name | type | description | default | required? |
|---|---|---|---|---|
| $amount | integer | Number of promocodes to generate | 1 | NO |
| $reward | float | Number of reward that user gets (ex: 30 - can be used as 30% sale on something) | null | NO |
| $data | array | Any additional information to get from promocode | [] | NO |
| $expires_in | integer | Number of days to keed promocode valid | null | NO |
| $quantity | integer | How many times can promocode be used? | null | NO |
| $is_disposable | boolean | If promocode is one-time use only | false | NO |
Check if given code exists, is usable and not yet expired.
Returns Promocode object if valid, or false if not.
Promocodes::check($code);
Parameters
| name | type | description | required? |
|---|---|---|---|
| $code | string | Code to be checked for validity | YES |
If you want to check if user tries to use promocode for second time you can call Promocodes::isSecondUsageAttempt and pass Promocode object as an argument. As an answer you will get boolean value
Redeem or apply code. Redeem is alias for apply method.
User should be authenticated to redeem code or this method will throw an exception (\Gabievi\Promocodes\Exceptions\UnauthenticatedException).
Also if authenticated user will try to apply code twice, it will throw an exception (\Gabievi\Promocodes\Exceptions\AlreadyUsedException)
Returns Promocode object if applied, or false if not.
Promocodes::redeem($code); Promocodes::apply($code);
Parameters
| name | type | description | required? |
|---|---|---|---|
| $code | string | Code to be applied by authenticated user | YES |
Get the collection of valid promotion codes.
Promocodes::all();
You can immediately expire code by calling disable function. Returning boolean status of update.
Promocodes::disable($code);
Parameters
| name | type | description | required? |
|---|---|---|---|
| $code | string | Code to be set as invalid | YES |
And if you want to delete expired, or non-usable codes you can erase them.
This method will remove redundant codes from database and their relations to users.
Promocodes::clearRedundant();
Promocodes can be related to users
If you want to use user relation open app/User.php and make it Rewardable as in example:
namespace App; use Illuminate\Notifications\Notifiable; use Gabievi\Promocodes\Traits\Rewardable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, Rewardable; // ... }
Redeem or apply code are same. redeemCode is alias of applyCode
Pass promotion code you want to be applied by current user.
User::redeemCode($code, $callback = null); User::applyCode($code, $callback = null);
Example (usage of callback):
$redeemMessage = $user->redeemCode('ABCD-DCBA', function ($promocode) use ($user) { return 'Congratulations, ' . $user->name . '! We have added ' . $promocode->reward . ' points on your account'; }); // Congratulations, Zura! We have added 10 points on your account
How to use additional data?
- Process of creation:
Promocodes::create(1, 25, ['foo' => 'bar', 'baz' => 'qux']);
- Getting data back:
Promocodes::redeem('ABC-DEF', function($promocode) { echo $promocode->data['foo']; }); // bar
or
User::redeemCode('ABC-DEF', function($promocode) { echo $promocode->data['foo']; }); // bar
Testing
Finally it's here. I've written some test to keep this package healthy and stable
License
laravel-promocodes is licensed under a MIT License.
tankonyako/promocodes 适用场景与选型建议
tankonyako/promocodes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「promo code」 「coupon code」 「discount code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tankonyako/promocodes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tankonyako/promocodes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tankonyako/promocodes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block to display a group of promo objects - a small card with an image, headline, short description, and link.
Promotional Codes Generator for Laravel 5.1
Promotional Codes Generator for Laravel 5
Alfabank REST API integration
The redeem code system used for creating redeem codes to compensate users, or giving rewards in events.
Promotional Codes Generator for Laravel 5
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-17
