Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial release #1

Open
wants to merge 35 commits into
base: 0.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
71b2159
add comment
repl6669 Apr 5, 2024
11459e8
cleanup translation files
repl6669 Apr 5, 2024
9aa54ad
add laravel wallet package for managing reward points
repl6669 Apr 5, 2024
887649a
update test case
repl6669 Apr 5, 2024
fc5b6fe
update config groups
repl6669 Apr 5, 2024
7b6b5b9
update readme
repl6669 Apr 5, 2024
47a0c14
update config
repl6669 Apr 6, 2024
6e90a3d
add coupon code generator
repl6669 Apr 6, 2024
d127e16
add charge points action
repl6669 Apr 6, 2024
c995989
add create coupon from balance action
repl6669 Apr 6, 2024
dc09cd3
add deposit points action
repl6669 Apr 6, 2024
fa42605
add transfer points action
repl6669 Apr 6, 2024
79fcdc7
add reward points calculator
repl6669 Apr 6, 2024
c572d20
add reward value calculator
repl6669 Apr 6, 2024
ecf4ca3
add abstract points action
repl6669 Apr 6, 2024
fafb4c9
add contracts
repl6669 Apr 6, 2024
fc8f229
add reward data type
repl6669 Apr 6, 2024
1959ed2
add point balance manager
repl6669 Apr 6, 2024
4c3f1b4
add has reward points balance trait
repl6669 Apr 6, 2024
a017dfe
update lunar rewards
repl6669 Apr 6, 2024
dac75e2
add and update test stubs
repl6669 Apr 6, 2024
1d53e83
update creates testing models trait
repl6669 Apr 6, 2024
dac06c0
update test case
repl6669 Apr 6, 2024
b0ca3e6
update service provider
repl6669 Apr 6, 2024
d7e5ad1
update packages
repl6669 Apr 6, 2024
3a4a315
update test config files
repl6669 Apr 6, 2024
bb6036f
add example feature test
repl6669 Apr 6, 2024
79aaa97
update namespace alias
repl6669 Apr 6, 2024
e90f6c6
update variable names
repl6669 Apr 6, 2024
b5415fc
update readme with basic documentation
repl6669 Apr 6, 2024
4911dc8
fix
repl6669 Apr 6, 2024
9a4e4ed
update readme
repl6669 Apr 6, 2024
7503a53
add custom models
repl6669 Apr 7, 2024
31ed413
update wallet config
repl6669 Apr 7, 2024
aec6741
update readme
repl6669 Apr 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ jobs:

- name: Execute tests
run: composer test

# - name: Execute tests with hashids turned on
# run: composer test-hashids
173 changes: 168 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,33 @@
## What's going on here?

This is a reward system package for [Lunar](https://github.com/lunarphp/lunar)
which allows your users to earn points for their purchases and redeem them for discounts.
which allows you to add or subtract reward points from models (eg. `User`s).
You can give points to users for various actions like buying products, writing reviews, etc.
Your users can then spend these points on discounts, free products, etc.

Point balances are managed by the awesome [Laravel Wallet](https://github.com/021-projects/laravel-wallet) package.

### Example use cases

#### Getting rewards

```diff
+ Give points to a user for every paid order (can be calculated from order value, or fixed amount)
+ Give points to a user for writing a review
+ Give points to a user for referring a friend
+ Give points to a user for signing up
+ Give points to a user for completing a profile
```

#### Spending rewards

```diff
- Donating points to a charity (transfer points from user to a dedicated charity account)
- Redeeming points for a discount (applied on cart)
- Redeeming points for a coupon code with the value of points
- Moving users to a different customer group based on points
- Giving a free product for a certain amount of points
```

## Getting started guide

Expand All @@ -27,12 +53,15 @@ composer require dystcz/lunar-rewards

Publish config files

> You will probably need them pretty bad

```bash
php artisan vendor:publish --provider="Dystcz\LunarRewards\LunarRewardsServiceProvider" --tag="lunar-rewards"
php artisan vendor:publish --provider="Dystcz\LunarRewards\LunarRewardsServiceProvider" --tag="lunar-rewards.config"
```

This will publish two configuration files:

1. `config/lunar-rewards/rewards.php` - contains the rewards configuration
2. `config/wallet.php` - contains the wallet configuration

Publish migrations

> Only in case you want to customize the database schema
Expand All @@ -41,6 +70,140 @@ Publish migrations
php artisan vendor:publish --provider="Dystcz\LunarRewards\LunarRewardsServiceProvider" --tag="lunar-rewards.migrations"
```

### Configuration

If you want to dig deeper into the underlaying [laravel-wallet](https://github.com/021-projects/laravel-wallet) package configuration
please visit their [documentation](021-projects.github.io/laravel-wallet).
You might want to [configure the database table names](https://021-projects.github.io/laravel-wallet/8.x/configuration.html#table-names).

### Usage

#### Preparing your models

1. Implement the `Rewardable` interface in your model.
2. Add the `HasRewardPointsBalance` trait to your model.

```php
use Dystcz\LunarRewards\Domain\Rewards\Contracts\Rewardable;
use Dystcz\LunarRewards\Domain\Rewards\Traits\HasRewardPointsBalance;

class Model
class Model implements Rewardable
{
use HasRewardPointsBalance;
}
```

#### Depositing / Giving points to a model

```php
use Dystcz\LunarRewards\Domain\Rewards\Actions\DepositPoints;
use Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward;
use Dystcz\LunarRewards\Facades\LunarRewards;

(new DepositPoints)->handle(to: $model, points: new Reward(100));

// or by calling the facade
LunarRewards::deposit(to: $model, points: new Reward(1000));
```

#### Charging points from a model

```php
use Dystcz\LunarRewards\Domain\Rewards\Actions\ChargePoints;
use Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward;
use Dystcz\LunarRewards\Facades\LunarRewards;

(new ChargePoints)->handle(from: $model, points: new Reward(100));

// or by calling the facade
LunarRewards::charge(from: $model, points: new Reward(1000));
```

#### Transferring points

```php
use Dystcz\LunarRewards\Domain\Rewards\Actions\TransferPoints;
use Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward;
use Dystcz\LunarRewards\Facades\LunarRewards;

(new TransferPoints)->handle(from: $model, to: $model2, points: new Reward(100));

// or by calling the facade
LunarRewards::transfer(from: $model, to: $model2, points: new Reward(1000));
```

#### Getting model points balance

```php
use Dystcz\LunarRewards\Domain\Rewards\Managers\PointBalanceManager;
use Dystcz\LunarRewards\Facades\LunarRewards;

$balance = PointBalanceManager::of($model);

// Points Balance
$balance->getValue(); // int
$balance->getReward(); // Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward

// Get balance by calling the facade
LunarRewards::balance($model); // int

// All Sent Points
$balance->getSent(); // int
$balance->getSentReward(); // Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward

// All Received Points
$balance->getReceived(); // int
$balance->getReceivedReward(); // Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward
```

#### Getting model points transactions

```php
use Dystcz\LunarRewards\Domain\Rewards\Managers\PointBalanceManager;

$balance = PointBalanceManager::of($model);

// All Received Points
$balance->getTransactions(); // Illuminate\Support\Collection<\Dystcz\LunarRewards\Domain\Rewards\Models\Transaction>
$balance->getTransactionsQuery(); // Illuminate\Database\Eloquent\Builder

// Or simply by calling the facade
```

#### Validating balances

```php
use Dystcz\LunarRewards\Domain\Rewards\Managers\PointBalanceManager;
use Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward;

$balance = PointBalanceManager::of($model);

// Check if model has enough points
$balance->hasEnoughPoints(new Reward(1000)); // bool
```

#### Creating coupons from balance

```php
use Dystcz\LunarRewards\Domain\Rewards\Actions\CreateCouponFromBalance;
use Dystcz\LunarRewards\Domain\Rewards\DataTypes\Reward;

$currency = $order->currency; // Lunar\Models\Currency

// Create a coupon with the value from the whole balance
$coupon = App::make(CreateCouponFromBalance::class)->handle(model: $model, currency: $currency);

// Create a coupon only for provided points
$coupon = App::make(CreateCouponFromBalance::class)->handle(
model: $model,
currency: $currency,
points: new Reward(1000)
);
```

### Lunar API endpoints

### Testing

```bash
Expand All @@ -67,7 +230,7 @@ If you discover any security related issues, please email [email protected] instead of u

- [All Contributors](../../contributors)
- [Lunar](https://github.com/lunarphp/lunar) for providing awesome e-commerce package
- [Laravel JSON:API](https://github.com/laravel-json-api/laravel)
- [Laravel Wallet](https://github.com/021-projects/laravel-wallet) for the points transaction engine
which is a brilliant JSON:API layer for Laravel applications

## License
Expand Down
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"require": {
"php": "^8.2",
"021/laravel-wallet": "^8.2",
"illuminate/support": "^10.0",
"lunarphp/lunar": "^0.8"
},
Expand All @@ -31,14 +32,15 @@
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.13",
"driftingly/rector-laravel": "^0.17.0",
"dystcz/lunar-api": "^0.8",
"laravel-json-api/testing": "^2.1",
"laravel/facade-documenter": "dev-main",
"laravel/pint": "^1.7",
"dystcz/lunar-api": "^0.8",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"rector/rector": "^0.15.23",
"driftingly/rector-laravel": "^0.17.0",
"spatie/laravel-ray": "^1.32"
},
"autoload": {
Expand All @@ -56,7 +58,6 @@
"clear": "@php vendor/bin/testbench package:purge --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"test": "vendor/bin/pest",
"test-hashids": "vendor/bin/pest -c phpunit.hashids.xml",
"test-coverage": "vendor/bin/pest --coverage",
"analyse": "vendor/bin/phpstan analyse",
"format": "vendor/bin/pint"
Expand All @@ -78,5 +79,11 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"repositories": {
"facade-documenter": {
"type": "vcs",
"url": "[email protected]:laravel/facade-documenter.git"
}
}
}
Loading
Loading