Skip to content

Commit d1ec6f6

Browse files
authored
Merge pull request #88 from bavix/cashier
Add support Cashier
2 parents e72d839 + 6ca9c42 commit d1ec6f6

File tree

9 files changed

+132
-28
lines changed

9 files changed

+132
-28
lines changed

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ matrix:
1313

1414
before_script:
1515
- composer install
16-
- wget https://github.com/infection/infection/releases/download/0.13.0/infection.phar
17-
- wget https://github.com/infection/infection/releases/download/0.13.0/infection.phar.asc
18-
- gpg --keyserver hkps.pool.sks-keyservers.net --recv-keys 493B4AA0
19-
- gpg --with-fingerprint --verify infection.phar.asc infection.phar
20-
- chmod +x infection.phar
2116

2217
script:
23-
- ./infection.phar --min-msi=48 -j$(nproc)
18+
- ./vendor/bin/infection --min-msi=60 -j$(nproc)

changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [3.1.6] - 2019-08-08
10+
### Added
11+
- Add support laravel cashier #87 @imhuso
12+
913
## [3.1.5] - 2019-08-07
1014
### Fixed
1115
- Fixed math rounding (mantissa) #85 @anthoz69
@@ -380,7 +384,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
380384
- Exceptions: AmountInvalid, BalanceIsEmpty.
381385
- Models: Transfer, Transaction.
382386

383-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.5...HEAD
387+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.6...HEAD
388+
[3.1.6]: https://github.com/bavix/laravel-wallet/compare/3.1.5...3.1.6
384389
[3.1.5]: https://github.com/bavix/laravel-wallet/compare/3.1.4...3.1.5
385390
[3.1.4]: https://github.com/bavix/laravel-wallet/compare/3.1.3...3.1.4
386391
[3.1.3]: https://github.com/bavix/laravel-wallet/compare/3.1.2...3.1.3

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
"ramsey/uuid": "^3.0"
3131
},
3232
"require-dev": {
33+
"infection/infection": "^0.13",
3334
"orchestra/testbench": "^3.7|^4.0",
3435
"phpstan/phpstan": "^0.11",
35-
"phpunit/phpunit": "^7.5|^8.2"
36+
"phpunit/phpunit": "^7.5|^8.2",
37+
"laravel/cashier": "*"
3638
},
3739
"suggest": {
3840
"bavix/laravel-wallet-swap": "Addition to the laravel-wallet library for quick setting of exchange rates"

src/Traits/HasWallet.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
*
2222
* @package Bavix\Wallet\Traits
2323
*
24-
* @property-read WalletModel $wallet
2524
* @property-read Collection|WalletModel[] $wallets
2625
* @property-read int $balance
2726
*/
2827
trait HasWallet
2928
{
3029

30+
use MorphOneWallet;
31+
3132
/**
3233
* The input means in the system
3334
*
@@ -201,22 +202,4 @@ public function transfers(): MorphMany
201202
->morphMany(config('wallet.transfer.model'), 'from');
202203
}
203204

204-
/**
205-
* Get default Wallet
206-
* this method is used for Eager Loading
207-
*
208-
* @return MorphOne|WalletModel
209-
*/
210-
public function wallet(): MorphOne
211-
{
212-
return ($this instanceof WalletModel ? $this->holder : $this)
213-
->morphOne(config('wallet.wallet.model'), 'holder')
214-
->where('slug', config('wallet.wallet.default.slug'))
215-
->withDefault([
216-
'name' => config('wallet.wallet.default.name'),
217-
'slug' => config('wallet.wallet.default.slug'),
218-
'balance' => 0,
219-
]);
220-
}
221-
222205
}

src/Traits/HasWallets.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ public function createWallet(array $data): WalletModel
9191

9292
/**
9393
* Create a default wallet
94+
* @var $walletModel WalletModel
9495
*/
95-
$this->getBalanceAttribute();
96+
$walletModel = $this->wallet;
97+
$walletModel->getBalanceAttribute();
9698

9799
return $wallet;
98100
}

src/Traits/MorphOneWallet.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Traits;
4+
5+
use Bavix\Wallet\Models\Wallet as WalletModel;
6+
use Illuminate\Database\Eloquent\Relations\MorphOne;
7+
8+
/**
9+
* Trait MorphOneWallet
10+
* @package Bavix\Wallet\Traits
11+
* @property-read WalletModel $wallet
12+
*/
13+
trait MorphOneWallet
14+
{
15+
16+
/**
17+
* Get default Wallet
18+
* this method is used for Eager Loading
19+
*
20+
* @return MorphOne|WalletModel
21+
*/
22+
public function wallet(): MorphOne
23+
{
24+
return ($this instanceof WalletModel ? $this->holder : $this)
25+
->morphOne(config('wallet.wallet.model'), 'holder')
26+
->where('slug', config('wallet.wallet.default.slug'))
27+
->withDefault([
28+
'name' => config('wallet.wallet.default.name'),
29+
'slug' => config('wallet.wallet.default.slug'),
30+
'balance' => 0,
31+
]);
32+
}
33+
34+
}

tests/Models/UserCashier.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test\Models;
4+
5+
use Bavix\Wallet\Traits\HasWallets;
6+
use Bavix\Wallet\Traits\MorphOneWallet;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Laravel\Cashier\Billable;
9+
10+
/**
11+
* Class User
12+
*
13+
* @package Bavix\Wallet\Test\Models
14+
* @property string $name
15+
* @property string $email
16+
*/
17+
class UserCashier extends Model
18+
{
19+
use Billable, HasWallets, MorphOneWallet;
20+
21+
/**
22+
* @return string
23+
*/
24+
public function getTable(): string
25+
{
26+
return 'users';
27+
}
28+
}

tests/MultiWalletTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Bavix\Wallet\Exceptions\AmountInvalid;
66
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
7+
use Bavix\Wallet\Models\Transaction;
78
use Bavix\Wallet\Models\Transfer;
89
use Bavix\Wallet\Test\Models\Item;
10+
use Bavix\Wallet\Test\Models\UserCashier;
911
use Bavix\Wallet\Test\Models\UserMulti;
1012
use Illuminate\Database\QueryException;
1113
use function compact;
@@ -406,4 +408,35 @@ public function testPay(): void
406408
$this->assertEquals($b->balance, $product->getAmountProduct());
407409
}
408410

411+
/**
412+
* @return void
413+
*/
414+
public function testUserCashier(): void
415+
{
416+
/**
417+
* @var UserCashier $user
418+
*/
419+
$user = factory(UserCashier::class)->create();
420+
$default = $user->wallet;
421+
422+
$this->assertEquals($default->balance, 0);
423+
424+
$transaction = $default->deposit(100);
425+
$this->assertEquals($transaction->type, Transaction::TYPE_DEPOSIT);
426+
$this->assertEquals($transaction->amount, 100);
427+
$this->assertEquals($default->balance, 100);
428+
429+
$newWallet = $user->createWallet(['name' => 'New Wallet']);
430+
431+
$transfer = $default->transfer($newWallet, 100);
432+
$this->assertEquals($default->balance, 0);
433+
$this->assertEquals($newWallet->balance, 100);
434+
435+
$this->assertEquals($transfer->withdraw->type, Transaction::TYPE_WITHDRAW);
436+
$this->assertEquals($transfer->withdraw->amount, -100);
437+
438+
$this->assertEquals($transfer->deposit->type, Transaction::TYPE_DEPOSIT);
439+
$this->assertEquals($transfer->deposit->amount, 100);
440+
}
441+
409442
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Bavix\Wallet\Test\Models\UserCashier;
4+
use Faker\Generator as Faker;
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Model Factories
9+
|--------------------------------------------------------------------------
10+
|
11+
| This directory should contain each of the model factory definitions for
12+
| your application. Factories provide a convenient way to generate new
13+
| model instances for testing / seeding your application's database.
14+
|
15+
*/
16+
17+
$factory->define(UserCashier::class, function (Faker $faker) {
18+
return [
19+
'name' => $faker->name,
20+
'email' => $faker->unique()->safeEmail,
21+
];
22+
});

0 commit comments

Comments
 (0)