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.