unicodeveloper / laravel-paystack

:credit_card: :package: :moneybag: Laravel 6, 7, 8, 9, 10 and 11 Package for Paystack
https://paystack.co
MIT License
605 stars 312 forks source link

Non-static method Unicodeveloper\Paystack\Paystack::genTranxRef() should not be called statically #68

Closed olayemii closed 5 years ago

olayemii commented 5 years ago

I keep getting this error Non-static method Unicodeveloper\Paystack\Paystack::genTranxRef() should not be called statically when i try setting up the library according to its doc This is what gives the error

<input type="hidden" name="reference" value="{{ Unicodeveloper\Paystack\Paystack::genTranxRef() }}">

Am trying this on Laravel 5.7

aeadedoyin commented 5 years ago

Hi Olayemii, don't call the method (Paystack::genTranxRef()) in your blade, do it in your controller. i.e. Remove this line from your blade :- <input type="hidden" name="reference" value="{{ Unicodeveloper\Paystack\Paystack::genTranxRef() }}">

Then do this.

  1. In your controller make sure you are importing. use Paystack; and not use Unicodeveloper\Paystack\Paystack;

  2. Then in your pay method i.e

    public function redirectToGateway(Request $request)
    {
        :
        :
        $request->reference = Paystack::genTranxRef();
        :
        return Paystack::getAuthorizationUrl()->redirectNow();
    }
olayemii commented 5 years ago

Hi Olayemii,

  1. In your controller make sure you are importing. use Paystack; and not use Unicodeveloper\Paystack\Paystack;
  2. Then in your pay method i.e
    public function redirectToGateway(Request $request)
    {
        :
        :
        $request->reference = Paystack::genTranxRef();
        :
        return Paystack::getAuthorizationUrl()->redirectNow();
    }

using use Paystack causes Class 'Paystack' not found

aeadedoyin commented 5 years ago

Alright, it means you didn't register the Facade and Provider To do this, open the app.php file in config\ folder, scroll down to aliases section and append like so:

'providers' => [
    ...
    Unicodeveloper\Paystack\PaystackServiceProvider::class,
    ...
],

'aliases' => [
    ...
    'Paystack' => Unicodeveloper\Paystack\Facades\Paystack::class,
    ...
]
olayemii commented 5 years ago

Alright, it means you didn't register the Facade. To do this, open the app.php file in config\ folder, scroll down to aliases section and append like so:

'aliases' => [
    ...
    'Paystack' => Unicodeveloper\Paystack\Facades\Paystack::class,
    ...
]

Adding that and changing all use Unicodeveloper\Paystack\Facades\Paystack to use Paystack gives class laravel-paystack not found. the docs says that step should be skipped for laravel versions above 5.5 ..or not?

aeadedoyin commented 5 years ago

Ignore the doc. It shouldn't be skipped.

If you use Unicodeveloper\Paystack\Paystack; then you would have to create an instance whenever you need to call the Paystack class i.e $paystack = new Paystack(); because the methods are not static.

However you can avoid the whole instance thing, (that's one of the reason for the "Facade" keys) by adding the alias as I have sent it. Then in anywhere you can call

Paystack::functionName()

instead of

$paystack = new Paystack(); $paystack->functionName();

olayemii commented 5 years ago

Ignore the doc. It shouldn't be skipped.

If you use Unicodeveloper\Paystack\Paystack; then you would have to create an instance whenever you need to call the Paystack class i.e $paystack = new Paystack(); because the methods are not static.

However you can avoid the whole instance thing, (that's one of the reason for the "Facade" keys) by adding the alias as I have sent it. Then in anywhere you can call

Paystack::functionName()

instead of

$paystack = new Paystack(); $paystack->functionName();

Fixed it using your method, i just needed to register the provider. Having a whole new different error though.

aeadedoyin commented 5 years ago

Feel free to drop it. Always happy to help.