Markus Proctor
Markus Proctor

Reputation: 435

How to execute an external function in Laravel 5.2?

I have a controller with the following function

public function completeRegistration(Request $request)
{
        $yy = $request->exp_year;
        $mm = $request->exp_month;
        $cardnum = $request->cardnum;
        $cvc = $request->cvc;
}

and I want to be able to do something like this

public function completeRegistration(Request $request)
{
        $yy = $request->exp_year;
        $mm = $request->exp_month;
        $cardnum = $request->cardnum;
        $cvc = $request->cvc;

    if(billUser($cardnum, $mm, $yy, $cvc))
    {
        echo "charge success";
    }
    else 
    {
        echo "uh something was wrong";
    }
}

What are my options since 5.2 no longer has Commands and Handlers? Other controllers will need to be able to interact with my payment gateway's API. I'll need a list of payment related functions that I can easily call into any controller.

Upvotes: 0

Views: 616

Answers (1)

Hammerbot
Hammerbot

Reputation: 16344

I think that you need to make a service:

# app/Services/Payment/Payment.php

<?php
namespace App\Services\Payment;

class Payment 
{
    public function billUser($cardnum, $mm, $yy, $cvc)
    {
        // Your logic here
    }
}

Then, if you want to integrate this Service "the Laravel way", you would need to create a Service Provider:

# app/Services/Payment/PaymentServiceProvider.php

<?php
namespace App\Services\Payment;

use Illuminate\Support\ServiceProvider;

class PaymentServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('Payment', function($app) {
            return new Payment();
        });
    }
}

Then register this Service Provider in config/app.php:

return [

    ...

    'providers' => [

        ...

        App\Services\Payment\PaymentServiceProvider::class,

        ...

    ],

    ...

];

You can then access it in your controller:

<?php
namespace App\Http\Controllers;

public function completeRegistration(Request $request)
{
    $yy = $request->exp_year;
    $mm = $request->exp_month;
    $cardnum = $request->cardnum;
    $cvc = $request->cvc;

    if(app('Payment')->billUser($cardnum, $mm, $yy, $cvc))
    {
        echo "charge success";
    }
    else 
    {
        echo "uh something was wrong";
    }
}

You can then go further and create a Facade for your Service:

# app/Services/Payment/PaymentFacade.php

<?php
namespace App\Services\Payment;

use Illuminate\Support\Facades\Facade;

class PaymentFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'Payment';
    }
}

To use it, you also need to register it into your app/config.php file:

return [

    ...

    'aliases' => [

        ....

        'Payment' => App\Services\Payment\PaymentFacade::class,
    ],

];

And use it anywhere in your code:

<?php
namespace App\Http\Controllers;

use Payment;

public function completeRegistration(Request $request)
{
    $yy = $request->exp_year;
    $mm = $request->exp_month;
    $cardnum = $request->cardnum;
    $cvc = $request->cvc;

    if(Payment::billUser($cardnum, $mm, $yy, $cvc))
    {
        echo "charge success";
    }
    else 
    {
        echo "uh something was wrong";
    }
}

I hope this helps!

Upvotes: 1

Related Questions