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

Confliction With Laravel Cashier #530

Closed
HilalLko opened this issue Jul 26, 2022 · 16 comments
Closed

Confliction With Laravel Cashier #530

HilalLko opened this issue Jul 26, 2022 · 16 comments
Assignees
Labels
bug Something isn't working duplicate This issue or pull request already exists question Further information is requested

Comments

@HilalLko
Copy link

HilalLko commented Jul 26, 2022

Describe the bug
Already handling subscriptions with Laravel Cashier, so as per Laravel Cashier my User Model contains Cahier's Billable trait

use Laravel\Cashier\Billable; 
class User extends Authenticatable
{
   use Billable;
    ....
    ....
}

Now, need to use Wallet's Purchases feature So Changed my User model to this

use Laravel\Cashier\Billable;
use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Traits\CanPay;
use Bavix\Wallet\Interfaces\Customer;
class User extends \TCG\Voyager\Models\User implements Customer
{
    use Billable, HasApiTokens, HasFactory, Notifiable, SoftDeletes, HasWallet;
    use CanPay;
    .......
    .....

But now I am getting below error and even unable to serve.
**PHP Fatal error: Trait method Bavix\Wallet\Traits\CanPay::pay has not been applied as App\Models\User::pay, because of collision with Laravel\Cashier\Billable::pay in /var/www/html/better_cloud/app/Models/User.php on line 20

Symfony\Component\ErrorHandler\Error\FatalError

Trait method Bavix\Wallet\Traits\CanPay::pay has not been applied as App\Models\User::pay, because of collision with Laravel\Cashier\Billable::pay**

image

@HilalLko HilalLko added bug Something isn't working question Further information is requested labels Jul 26, 2022
@rez1dent3 rez1dent3 added the duplicate This issue or pull request already exists label Jul 26, 2022
@rez1dent3
Copy link
Member

rez1dent3 commented Jul 26, 2022

Hello. duplicate #87
You need to use the functionality of Multi Wallets.

Model example: https://github.com/bavix/laravel-wallet/blob/77b781e2ccef1a4638d5fb414a5a1f5570ba5777/tests/Infra/Models/UserCashier.php

{
/** @var UserCashier $user */
$user = UserCashierFactory::new()->create();
$default = $user->wallet;
self::assertSame($default->balanceInt, 0);
$transaction = $default->deposit(100);
self::assertSame($transaction->type, Transaction::TYPE_DEPOSIT);
self::assertSame($transaction->amountInt, 100);
self::assertSame($default->balanceInt, 100);
$newWallet = $user->createWallet([
'name' => 'New Wallet',
]);
$transfer = $default->transfer($newWallet, 100);
self::assertSame($default->balanceInt, 0);
self::assertSame($newWallet->balanceInt, 100);
self::assertSame($transfer->withdraw->type, Transaction::TYPE_WITHDRAW);
self::assertSame($transfer->withdraw->amountInt, -100);
self::assertSame($transfer->deposit->type, Transaction::TYPE_DEPOSIT);
self::assertSame($transfer->deposit->amountInt, 100);

@HilalLko HilalLko changed the title Confliction Laravel Cashier Confliction With Laravel Cashier Jul 27, 2022
@HilalLko
Copy link
Author

Hey @rez1dent3
Thanks for the prompt response, the wallet is working as expected with cashier until and unless am not adding "CanPay trait and Customer interface to your User model."

use Bavix\Wallet\Traits\CanPay;
use Bavix\Wallet\Interfaces\Customer;

class User extends Model implements Customer
{
    use CanPay;
    ....
    ....

No collision with Cashier if am not using CanPay Trait along with Customer interface.

@HilalLko
Copy link
Author

Hello. duplicate #87 You need to use the functionality of Multi Wallets.

Model example: https://github.com/bavix/laravel-wallet/blob/77b781e2ccef1a4638d5fb414a5a1f5570ba5777/tests/Infra/Models/UserCashier.php

{
/** @var UserCashier $user */
$user = UserCashierFactory::new()->create();
$default = $user->wallet;
self::assertSame($default->balanceInt, 0);
$transaction = $default->deposit(100);
self::assertSame($transaction->type, Transaction::TYPE_DEPOSIT);
self::assertSame($transaction->amountInt, 100);
self::assertSame($default->balanceInt, 100);
$newWallet = $user->createWallet([
'name' => 'New Wallet',
]);
$transfer = $default->transfer($newWallet, 100);
self::assertSame($default->balanceInt, 0);
self::assertSame($newWallet->balanceInt, 100);
self::assertSame($transfer->withdraw->type, Transaction::TYPE_WITHDRAW);
self::assertSame($transfer->withdraw->amountInt, -100);
self::assertSame($transfer->deposit->type, Transaction::TYPE_DEPOSIT);
self::assertSame($transfer->deposit->amountInt, 100);

In reference of suggested way to implement desired functionality I have changed my User model to be like this

use Bavix\Wallet\Traits\HasWallets;
use Bavix\Wallet\Traits\MorphOneWallet;
use Bavix\Wallet\Traits\CanPay;
use Bavix\Wallet\Interfaces\Customer;
use Laravel\Cashier\Billable;

class User extends \TCG\Voyager\Models\User implements Customer
{
    use Billable, HasApiTokens, HasFactory, Notifiable, SoftDeletes;
    use HasWallets;
    use MorphOneWallet;
    use CanPay;

But still am facing the same problem
image

Can see in the above image am even unable to make the server up i.e, php artisan serve itself fails with the above error.

@rez1dent3
Copy link
Member

So it will not work, because. Packages use methods with the same name. You need to use multi wallets, they have all the necessary functionality

@rez1dent3
Copy link
Member

I mean, CanPay is already inside the wallet and there is no need to mark it in the model

@HilalLko
Copy link
Author

So @rez1dent3
Need to change this

use Bavix\Wallet\Traits\HasWallets;
use Bavix\Wallet\Traits\MorphOneWallet;
use Bavix\Wallet\Traits\CanPay;
use Bavix\Wallet\Interfaces\Customer;
use Laravel\Cashier\Billable;

class User extends \TCG\Voyager\Models\User implements Customer
{
    use Billable, HasApiTokens, HasFactory, Notifiable, SoftDeletes;
    use HasWallets;
    use MorphOneWallet;
    use CanPay;

to this

use Bavix\Wallet\Traits\HasWallets;
use Bavix\Wallet\Traits\MorphOneWallet;
use Laravel\Cashier\Billable;

class User extends \TCG\Voyager\Models\User 
{
    use Billable, HasApiTokens, HasFactory, Notifiable, SoftDeletes;
    use HasWallets;
    use MorphOneWallet;

@rez1dent3
Copy link
Member

Yes

@HilalLko
Copy link
Author

Changing the User Model to look like this

use Bavix\Wallet\Traits\HasWallets;
use Bavix\Wallet\Traits\MorphOneWallet;
use Laravel\Cashier\Billable;

class User extends \TCG\Voyager\Models\User 
{
    use Billable, HasApiTokens, HasFactory, Notifiable, SoftDeletes;
    use HasWallets;
    use MorphOneWallet;

can serve now.
Let me tinker and test Purchases flow now

@rez1dent3 Thanks mate (y)

@HilalLko
Copy link
Author

Furthermore, just to clear silly doubt
$user->balance gives Error with exception as
LogicException with message 'App\Models\User::balance must return a relationship instance.'

Need to use full syntax in place of shorthand syntax right?

$user->wallet->balance

@HilalLko
Copy link
Author

@rez1dent3
Now with the wallet's Purchases module
Whenever am doing this

$item = Item::first();
$item->getAmountProduct($user);

am receiving an error saying ** TypeError: App\Models\Order::getAmountProduct(): Argument #1 ($customer) must be of type Bavix\Wallet\Interfaces\Customer, App\Models\User given on line 1 **
image

@rez1dent3
Copy link
Member

purchases come from the wallet. you need to get the wallet and pass it to the method

@HilalLko
Copy link
Author

So need to change

$item = Item::first();
$item->getAmountProduct($user);

with this

$item = Item::first();
$item->getAmountProduct($user->wallet);

@rez1dent3
Copy link
Member

if the purchase was this wallet, then yes

@HilalLko
Copy link
Author

Well as per your suggestion am using multi wallet now but am not creating any new wallet rather than working with default wallet as of now
So yes the purchase needs to use User's default wallet which we can get by $user->wallet or we need to use something else?

@HilalLko
Copy link
Author

Maxim @rez1dent3
Thanks for the quick support as well as such an awesome digital wallet.

@rez1dent3
Copy link
Member

yes, if you only use one wallet, then you need to transfer it. I wrote this for general understanding.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working duplicate This issue or pull request already exists question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants