Posted on Leave a comment

Creating A POS System With Lumen and Ionic – Part 1

[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]

Your Very Own POS

I use Stripe all the time in my web and mobile applications to accept payments. It’s great I love it, I don’t have to worry about PCI compliance and they have a kick ass API. In this new programming series I will be showing you how to create your own full stack solution for accepting payments with Stripe. In order to accomplish this it requires a Lumen back end micro service, and an Ionic mobile app, ready? Good! LET’S GET STARTED!!
 

Part 1 – Lumen Back End

If you haven’t read my past Lumen tutorial take the time to read it….or not completely up to you, in either case I am assuming you have the environment to run the following command:

 lumen new StripeServer

before we do anything else, we need to install our dependencies. Kindly input the following commands into your terminal:

composer require stripe/stripe-php palanik/lumen-cors illuminate/mail guzzlehttp/guzzle

The first dependency is for Stripe (So we can take payments), the other is for CORS (we will need this for our mobile app consumption), the last two are for email alerts.
Next we need to create controllers to handle our requests create two controllers: ChargeController.php and WebHookController.php. Inside of ChargeController.php paste the following code:


<?php

namespace AppHttpControllers;

 

use IlluminateHttpRequest;


class ChargeController extends Controller
{
public function charge(Request $request){
StripeStripe::setApiKey(env('STRIPE_KEY'));
StripeCharge::create(array(
"amount" => $request->input('amount'),
"currency" => "usd",
"source" => $request->token,
"description" => $request->input('description')
));
return response()->json(['success'=>'true']);
}}

This controller only has one method. See? It’s not that scary. We are making use of the Stripe Charge API to create a credit card charge, then returning a json response letting us know everything is ok. Now let’s move on to the WebHookController :


<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesMail;
class WebHookController extends Controller
{
public function handleStripe(Request $request){
switch($request->type){
case 'charge.succeeded':
$number = $request->data['object']['amount'] / 100;
Mail::raw('Charge succeeded in amount of $'. money_format('%i', $number) . "n",
function($msg) {
$msg->to([env('PHONE_ADDRESS')]);
$msg->from([[env('MAIL_FROM_ADDRESS')]);
});
break;
case 'charge.failed':
$number = $request->data['object']['amount'] / 100;
Mail::raw('Charge failed in amount of $'. money_format('%i', $number) . "n",
function($msg) {
$msg->to([[env('PHONE_ADDRESS')]);
$msg->from([[env('MAIL_FROM_ADDRESS')]);
});
break;
}
}
}

Here we are sending text message alerts when we get pinged by one of Stripe’s webhook events. Here we are only looking for 2 Stripe events but feel free to add whatever you want. For a more in depth tutorial on sending text messages with Laravel click here. Lastly we need edit the routes file, open routes.php and add the following routes:


$app->post('stripe', 'WebHookController@handleStripe');
$app-> post('charge',['middleware' => 'cors', 'uses' => 'ChargeController@charge']);

Back end is finished, just be sure to edit your .env file. You can view the source code here.

Posted on 6 Comments

Create A Real-Time Microservice With Lumen

Laravel 6 Is Out Which Means New Lumen!

When I was at Laracon 2019 I interviewed Taylor Otwell and one of the things we talked about was new Lumen updates. I have switched to a primarily micro service mentality and I’m excited to write up scalable robust production ready microservices.

Laracon 2019 I interviewed Taylor Otwell

What Is Lumen?

Lumen is microframework provided by Laravel. I use it all the time when I need to create standalone APIs and microservices. Today I will show you how to create a generic real time microservice that can be used for later mobile back end tutorials . The microservice will take messages and push them out to all clients listening setting the ground for push notifications. Without further ado, let’s get started!

Getting Started

Because I want to focus on development, and not system administration, please follow this guide to getting your environment up and running, after doing so, go to your terminal, cd into an empty directory and type in this command or clone the repo:

lumen new real-time-microservice-skeleton

cd into your newly created app. First thing we are going to do is define our events. This microservice will give us real-time updates from those who sent messages to it. In order to provide this, we will leverage the broadcasting paradigm included in the Event class. If you are interested in a deeper understanding on how broadcasting works check here, but in short you tell the event what channel to broadcast on and you give it a driver (Redis, Pusher, log). We will being using Redis with a Node.js implementation. Before we can do any of that, we have to add a dependency or two:

For Composer:

composer require predis/predis

For NPM:

npm install --save socket.io-client

Now open up app/Providers/EventServiceProvider.php and change  the array to this:

protected $listen = [
        'App\Events\NewMessageEvent' => [
            'App\Listeners\NewMessageListener',
        ],
    ];

Event & Listener

Lumen doesn’t have generators so we have to do things manually. Now create those corresponding classes in the app/Events and app/Listeners directories respectively. In the NewMessageEvent add this code:

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class NewMessageEvent extends Event implements ShouldBroadcast
{
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($channel, $message)
{
//
$this->message = $message;
}
public function broadcastOn()
{
return new PrivateChannel('example-channel');
}
}

In the NewMessageListener it can just have the default listener code

<?php
namespace App\Listeners;
use App\Events\NewMessageEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class NewMessageListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    /**
     * Handle the event.
     *
     * @param  NewMessageEvent  $event
     * @return void
     */
    public function handle(NewMessageEvent $event)
    {
        //
    }
}

Routing

For the lumen skeleton I will just add two addition routes to the web.php file one GET and one POST that do the same thing. Pass a message parameter to a controller method.

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) {
    return $router->app->version();
});
$router->get('/message', 'MessageController@sendMessage');
$router->post('/message', 'MessageController@sendMessage');

Controller

The controller will have one method that calls a new event and passes the message as the parameter.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessageController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    public function sendMessage(Request $request){
      event(new \App\Events\NewMessageEvent($request->message));
    }
    //
}

Socket-IO Client

We will use Laravel Echo to interact with out redis pub/sub mechanism. Create a new file called socket-client.js and input the following

import Echo from "laravel-echo"
window.io = require('socket.io-client');
window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});
Echo.private('example-channel')
    .listen('NewMessageEvent', (e) => {
        console.log(e.message);
    });

As you can see we are listening for a private channel with the same name that we broadcasted on in the Event class. Now the only thing needed is an actual socket.io server. I won’t get into that in this tutorial but the official Laravel documentation recommends that you use this github package. In the next step of the tutorial I will add authentication to secure channels.