Skip to content

Commit

Permalink
migration update and multi-use codes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabiezur committed Aug 29, 2017
1 parent 7ee247c commit f51536f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.0.1] - 2017-08-29
### Fixed
- Migration file, that was not updated.

### Added
- Ability to create multi-use or one-time promotion codes.

## [1.0.0] - 2017-08-29
### Added
- Ability to set expiration date of promocode while creating.
Expand All @@ -24,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Ability of user, that they could create promocodes assigned to them.

[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...HEAD
[1.0.1]: https://github.com/zgabievi/laravel-promocodes/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/zgabievi/laravel-promocodes/compare/v0.5.4...v1.0.0

[#12]: https://github.com/zgabievi/laravel-promocodes/issues/12
Expand Down
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Generate as many codes as you wish and output them without saving to database.
You will get array of codes in return:

```php
Promocodes::output($amount = 1)
Promocodes::output($amount = 1);
```

---
Expand All @@ -84,10 +84,18 @@ 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:

```php
Promocodes::create($amount = 1, $reward = null, array $data = [], $expires_in = null)
Promocodes::create($amount = 1, $reward = null, array $data = [], $expires_in = null);
```

If you want to create code that will be used only once, here is method for you.

```php
Promocodes::createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null);
```

---
Expand All @@ -99,7 +107,7 @@ This code may throw `\Gabievi\Promocodes\Exceptions\InvalidPromocodeExceprion` i
Returns `Promocode` object if valid, or `false` if not.

```php
Promocodes::check($code)
Promocodes::check($code);
```

---
Expand All @@ -113,16 +121,16 @@ Also if authenticated user will try to apply code twice, it will throw an except
Returns `Promocode` object if applied, or `false` if not.

```php
Promocodes::redeem($code)
Promocodes::apply($code)
Promocodes::redeem($code);
Promocodes::apply($code);
```

---

You can imediately expire code by calling *disable* function. Returning boolean status of update.

```php
Promocodes::disable($code)
Promocodes::disable($code);
```

---
Expand All @@ -132,7 +140,7 @@ 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.

```php
Promocodes::clearRedundant()
Promocodes::clearRedundant();
```

---
Expand Down Expand Up @@ -162,8 +170,8 @@ Redeem or apply code are same. *redeemCode* is alias of *applyCode*
Pass promotion code you want to be applied by current user.

```php
User::redeemCode($code, $callback = null)
User::applyCode($code, $callback = null)
User::redeemCode($code, $callback = null);
User::applyCode($code, $callback = null);
```

Example (usage of callback):
Expand Down
23 changes: 21 additions & 2 deletions src/Promocodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ public function output($amount = 1)
/**
* Save promocodes into database
* Successful insert returns generated promocodes
* Fail will return NULL.
* Fail will return empty collection.
*
* @param int $amount
* @param null $reward
* @param array $data
* @param int|null $expires_in
* @param bool $is_disposable
*
* @return \Illuminate\Support\Collection
*/
public function create($amount = 1, $reward = null, array $data = [], $expires_in = null)
public function create($amount = 1, $reward = null, array $data = [], $expires_in = null, $is_disposable = false)
{
$records = [];

Expand All @@ -82,6 +83,7 @@ public function create($amount = 1, $reward = null, array $data = [], $expires_i
'reward' => $reward,
'data' => json_encode($data),
'expires_at' => $expires_in ? Carbon::now()->addDays($expires_in) : null,
'is_disposable' => $is_disposable,
];
}

Expand All @@ -92,6 +94,23 @@ public function create($amount = 1, $reward = null, array $data = [], $expires_i
return collect([]);
}

/**
* Save one-time use promocodes into database
* Successful insert returns generated promocodes
* Fail will return empty collection.
*
* @param int $amount
* @param null $reward
* @param array $data
* @param int|null $expires_in
*
* @return \Illuminate\Support\Collection
*/
public function createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null)
{
return $this->create($amount, $reward, $data, $expires_in, true);
}

/**
* Check promocode in database if it is valid.
*
Expand Down
18 changes: 14 additions & 4 deletions src/migrations/2016_05_17_221000_create_promocodes_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ public function up()
Schema::create(config('promocodes.table', 'promocodes'), function (Blueprint $table) {
$table->increments('id');

$table->integer('user_id')->unsigned()->nullable();

$table->string('code', 32)->unique();
$table->double('reward', 10, 2)->nullable();

$table->json('data')->nullable();
$table->text('data')->nullable();

$table->boolean('is_disposable')->default(false);
$table->timestamp('expires_at')->nullable();
});

Schema::create(config('promocodes.relation_table', 'promocode_user'), function (Blueprint $table) {
$table->unsignedInteger('user_id');
$table->unsignedInteger('promocode_id');

$table->timestamp('used_at');

$table->boolean('is_used')->default(false);
$table->primary(['user_id', 'promocode_id']);

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('promocode_id')->references('id')->on(config('promocodes.table', 'promocodes'));
});
}

Expand All @@ -36,5 +45,6 @@ public function up()
public function down()
{
Schema::drop(config('promocodes.table', 'promocodes'));
Schema::drop(config('promocodes.relation_table', 'promocode_user'));
}
}

0 comments on commit f51536f

Please sign in to comment.