Posted on Leave a comment

Get Your Daily Stripe Balance Using Laravel Task Scheduler

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

Laravel Task Scheduler

I create many apps that use the Uber business model for paying out contractors using Stripe, and I make money by using application fees. I like to get my daily account available and pending balances from Stripe. Using Laravel’s built in task scheduler this is trivial. Laravel provides a way to define all of your application’s scheduled jobs within Laravel itself, so that you only have to manage one cron tab * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 it’s pretty awesome!

Install Dependencies

I will assume you have created a new Laravel install. I  need the Stripe PHP composer module for this to work. Go to the root of your application and type the following command composer require stripe/stripe-php now you have all the necessary components installed.

Define Schedule

All schedules are defined in schedule() method of the App\Console\Kernel located at app/console/kernel.php. I want to make a stripe call to retrieve my balance, both available and pending.

<?php
namespace App\Console;
use Mail;
use Carbon\Carbon;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\Inspire::class,
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
        \Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
       $balance = \Stripe\Balance::retrieve();
       $emailText = "As of {Carbon::today()->toDayDateTimeString()}  your available balance is {$balance->available->amount} and your pending balance is {$balance->pending->amount}";
Mail::raw('Text to e-mail', function ($message) {
    //
    
 $message->from('financial@app.com', 'Your Application');
            $message->to(env('EMAIL_ADDRESS'),env('EMAIL_NAME'))->subject('Your Daily Stripe Balance!');
}); })->daily();  } }

With this piece of code I am now getting daily emails with the date, my available balance, and my pending balance. If you liked this please share and subscribe to my blog posts via email to the right!

Posted on Leave a comment

API Authentication In Laravel 5.2

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

Hours Saved!

Token based API authentication is something you are inevitably going to encounter if you plan on working with web/mobile apps (unless you are coding under a rock). The benefits are great, in fact here are six of them; However it can be a nightmare if you need to create your own token authentication server. As of Laravel 5.2, there is a new auth guard called conveniently api this guard allows us to check an api_token parameter against an api_token field on our users table in our database.

API Configuration

Almost everything is configured right out of the box, open up config/auth.php and you will see this block of code

  /*
 |--------------------------------------------------------------------------
 | Authentication Guards
 |--------------------------------------------------------------------------
 |
 | Next, you may define every authentication guard for your application.
 | Of course, a great default configuration has been defined for you
 | here which uses session storage and the Eloquent user provider.
 |
 | All authentication drivers have a user provider. This defines how the
 | users are actually retrieved out of your database or other storage
 | mechanisms used by this application to persist your user's data.
 |
 | Supported: "session", "token"
 |
 */
 'guards' => [
 'web' => [
 'driver' => 'session',
 'provider' => 'users',
 ],
 'api' => [
 'driver' => 'token',
 'provider' => 'users',
 ],
 ],

By default Laravel 5.2 uses the web guard, which is the traditional session based model. This model is great for a traditional web application but not suitable for API architecture. The next guard is the api this guard is suitable for routes that need to be consumed by mobile apps and the like. The reason why you would want to do this is because when you are making request from your client-side app, each request is mutually exclusive of the requests before and after it, thus each one requires authentication. In the traditional session based flow, user data is saved on the server and that session data is used in every request, not only is this approach not scalable, it isn’t efficient or as secure as one might think. API token authentication is faster, scalable, and more secure. The biggest benefit of all is that you can now use this token in other applications to access your own API if you so choose. If you would like to see an example leave a comment of what kind of app you would like to see this implemented in and I will write it, record it, and blog it!