Code Monkey home page Code Monkey logo

laravel-promocodes's Introduction

laravel-promocodes

Packagist Packagist license Scrutinizer Code Quality Build Status

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 zgabievi/promocodes

If you are using Laravel 5.5, 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.php and 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);

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);

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);

Check if given code exists, is usable and not yet expired.

This code may throw \Gabievi\Promocodes\Exceptions\InvalidPromocodeException if there is not such promocode in database, with give code.

Returns Promocode object if valid, or false if not.

Promocodes::check($code);

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);

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

Promocodes::disable($code);

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?

  1. Process of creation:
Promocodes::create(1, 25, ['foo' => 'bar', 'baz' => 'qux']);
  1. 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

laravel-promocodes tests

License

laravel-promocodes is licensed under a MIT License.

laravel-promocodes's People

Contributors

abhij89 avatar oleksiibrylin avatar zgabievi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.