InfyOm Blog

latest-post
Payments gateways are very useful components of any e-commerce store. One of the popular payment gateways is Stripe. it's becoming more popular nowadays. 

Stripe's simple definition is : 

We bring together everything that’s required to build websites and apps that accept payments and send payouts globally. Stripe’s products power payments for online and in-person retailers, subscription businesses, software platforms and marketplaces, and everything in between.  ~ Stripe

To begin this laravel tutorial, I hope you already have fresh laravel repo.

Stripe Configuration with Laravel

Run the following command to install stripe :

composer require stripe/stripe-php
if you don't have a Stripe account, you'll want to set that up and add your API keys. Add the following to your .env file.

STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret

Publish Migrations Files From Stripe

php artisan vendor:publish --tag="cashier-migrations" 

And Run migrations by hitting the following command

php artisan migrate 

Setup Stripe Controller

Now create a stripe controller by hitting the following command:

php artisan make:controller StripeController
namespace App\Http\Controllers;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Stripe\Checkout\Session;
use Stripe\Exception\ApiErrorException;
/**
 * Class FeaturedCompanySubscriptionController
 */
class StripeControlle extends AppBaseController
{
    public function createSession(Request $request)
    {
        setStripeApiKey();
        $session = Session::create([
            'payment_method_types' => ['card'],
            'customer_email'       => $userEmail,
            'line_items'           => [
                [
                    'price_data'  => [
                        'product_data' => [
                            'name' => 'Make '.$company->user->first_name.' as featured Company',
                        ],
                        'unit_amount'  => 100 * 100,
                        'currency'     => 'USD',
                    ],
                    'quantity'    => 1,
                    'description' => '',
                ],
            ],
            'client_reference_id'  => '1234',
            'mode'                 => 'payment',
            'success_url'          => url('payment-success').'?session_id={CHECKOUT_SESSION_ID}',
            'cancel_url'           => url('failed-payment?error=payment_cancelled'),
        ]);
        $result = [
            'sessionId' => $session['id'],
        ];
        return $this->sendResponse($result, 'Session created successfully.');
    }
    public function paymentSuccess(Request $request)
    {
        $sessionId = $request->get('session_id');
        // 
    }
   
    public function handleFailedPayment()
    {
        // 
    }
}

Define Routes

    Route::post('stripe-charge', 'StripeController@createSession');
    Route::get('payment-success', 'StripeController@paymentSuccess');
    Route::get('failed-payment',  'StripeController@handleFailedPayment');

Setup From View file


Here we are going to create stripe session from backend and redirect to stripe checkout page once we will receive the sessionId from backend.
Assume that makePaymentURL is something like "APP_URL/stripe-charge".
Now let's say when you hit the submit form of stripe it will call MakePaymentURL and that URL returns your session ID which we will use to redirect to the stripe checkout page. 

 $(document).on('click', '#makePayment', function () {
           
        $(this).addClass('disabled');
        $.post(makePaymentURL, payloadData).done((result) => {
            let sessionId = result.data.sessionId;
            stripe.redirectToCheckout({
                sessionId: sessionId,
            }).then(function (result) {
                $(this).html('Make Featured').removeClass('disabled');
                manageAjaxErrors(result);
            });
        }).catch(error => {
            $(this).html('Make Featured').removeClass('disabled');
            manageAjaxErrors(error);
        });
    });
That's it, after entering proper details into stripe you will get a success callback to a related route, where you can perform related actions. 
Invalid date2 minutesuserVishal Ribdiya

Posts

post

Recently, we have started working on one CRM System for client, where we offer a user to create an account and then can upload his excel file of Contracts and we will create contracts in the database, and then these contracts will be emailed to both the parties of the contract.

For this, the functionality we want is, we do not let users wait to do these all things, because this process may take longer based on the number of records the user has. Also, this process contains lots of computation like,

  1. Upload an Excel file
  2. Do field mappings
  3. Validate the record of contract
  4. Create a contract in the database along with all other information
  5. Create a PDF of contract (which take significant time)
  6. Email that PDF to both the parties.

Just imagine, if the user has uploaded a file with 10K records in a file it will take an hour to do this in a single process.

Here is where we used the Laravel Jobs & Queues where we can let use relax and we do our processing in backend with multiple Jobs running at the same time.

So we came up with architecture with Several Jobs in place with the following things in mind.

  1. We will have multiple Jobs to perform multiple things to have SRP (Single Responsibility Principle)
  2. When the one Job completes its duty, it will dispatch the next Job with the data that next job needs.
  3. The next job will do that same till the process ends

Another advantage we got here is, we can execute different jobs on different machines/servers based on the resources required. For e.g. We run a Job of Creating PDF on a powerful machine then sending an email.

Also, we can set different priorities for different Job Listeners. For e.g. Sending an Email Job will have a lower priority than others.

So here is the architecture that we came up with and used. I tried to architect the Jobs structure only and haven't mentioned small details of updating and creating records.

Implementing Efficient and Fast Data Import with Laravel Jobs & Queues

Hope it will help you to architect you a better Jobs and Queues architecture where you need to perform multiple things on the backend and do not let your users wait to finish those long-running processes.

Happy & Safe Queuing. :)

August 14, 20202 minutesauthorMitul Golakiya
post

While hosting on the Laravel project on cPanel, the traditional problem that lots of developers get is, /public path is appended to the URL. Because in most cases, we put a project directly into the public_html folder, so public_html is our root for the website and that's where our laravel application is also placed.

But to run the Laravel application, we need to point our domain root to the public folder of the laravel. It is possible to do with cPanel but you need to go through some steps which are not known by most of the people and also the tedious process. So to make it simple, what you can do is, there is a way we can do it via .htaccess file in our root folder.

We can copy .htaccess file from our public folder and then make modifications to it to work with the direct root folder and route every request to the public folder.

Here is the final .htaccess file,

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization MemberHeader
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Remove public URL from the path
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,QSA]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

By adding the above file to the root folder we can use laravel project without the public path. Check out the following two lines:

  RewriteCond %{REQUEST_URI} !^/public/
  RewriteRule ^(.*)$ /public/$1 [L,QSA]

These two lines make magic and our application will work without public path in URL.

August 01, 20201 minuteauthorMitul Golakiya
post

Recently we use Spatie laravel-multitenancy package in our of our client's CRM project along with spatie/laravel-medialibrary.

The default behavior of the media library package is, it generates a folder for each media with its ID in our configured disk, like a public folder or s3 or whatever disk we configured.

This works really great when you are dealing with a single-tenant application. But while using multi-tenant, you got a media table in each of your tenant databases. so this pattern simply doesn't work. Because then you will end up having multiple media files under the same folder from different tenants.

So what we want to do is, instead of having a structure like,

public
-- media
---- 1
------ file.jpg
---- 2
------ file.jpg
...

What we want to achieve is, we want to have a folder structure where media of every tenant will be in a separate folder with tenant unique id,

public
-- media
---- abcd1234 // tenant1 Id
------ 1
-------- file.jpg
------ 2
-------- file.jpg
---- efgh5678 // tenant2 Id
------ 1
-------- file.jpg
------ 2
-------- file.jpg
...

Spatie Media library is very configurable out of the box where you can write your own media library path generator. That is documented very well over here: https://docs.spatie.be/laravel-medialibrary/v8/advanced-usage/using-a-custom-directory-structure

So what we did is, we wrote our own Path Generator, which can store files into tenants folder. Here is how it looks like,

<?php

namespace App\MediaLibrary;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator;

class InfyCRMMediaPathGenerator extends DefaultPathGenerator
{
    /*
     * Get a unique base path for the given media.
     */
    protected function getBasePath(Media $media): string
    {
        $currentTenant = app('currentTenant');

        return $currentTenant->unique_id.DIRECTORY_SEPARATOR.$media->getKey();
    }
}

What we did here is, we just simply extended the Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator of Media Library and override the function getBasePath. We attached the prefix of the tenant's unique_id to the path. so instead of 1/file.png, it will return abcd1234/1/file.png.

All you need to make sure is, whenever you are uploading a media, your application should be tenant aware. Otherwise, it will not able to get the current tenant.

Hope this helps while using spatie media library along with spatie multi-tenant.

Even if you are not using a spatie multi-tenant, still you can create your own PathGenerator and use it your own way to have a media structure you want.

July 30, 20202 minutesauthorMitul Golakiya
post

Recently Spatie released a brand new package for multi-tenancy called laravel-multitenancy.

It comes with great support to work out of the box with sub-domains like,

https://zluck.infychat.com

https://infyom.infychat.com

https://vasundhara.infychat.com

It identified the tenant based on the sub-domain and sets a database runtime for your tenant-specific models.

Recently, we used it in one of our client for Snow Removal CRM. Here we have two models,

  1. Freemium Model - with no sub-domain (application will work on main domain only)
  2. Premium Model - where the tenant will get its subdomain

And user can convert his account from Freemium to Premium at any point of time by just subscribing to the plan.

So what we want is, on the backend, we want to have a separate database for each tenant, but the application should run on main as well as a sub-domain.

So what we want to have is the ability to extend/customize the tenant detection mechanism. And Spatie does a very good job at there where you can customize the logic.

You can create your own TenantFinder class and configure it in the config file of the package. And there is very good documentation for that here: https://docs.spatie.be/laravel-multitenancy/v1/installation/determining-current-tenant/

To do that, what we did is, we have a field called tenant_id in our users table. All of our users are stored into the main database since we may have user access across the tenant.

And when any user does a login, we listen for the event Illuminate\Auth\Events\Login which is documented in Laravel Docs over here.

When a user does a login, our listener will be called and will create a cookie called tenant on the browser with the tenant id of the user. So our listener will look like,

<?php

namespace App\Listeners;

use App\Models\User;
use Cookie;
use Illuminate\Auth\Events\Login;

class LoginListener
{
    public function handle(Login $event)
    {
        /** @var User $authUser */
        $authUser = $event->user;

        Cookie::forget('tenant');
        Cookie::queue(Cookie::forever('tenant', encrypt($authUser->tenant_id)));
    }
}

Also, we encrypt the cookie on our end, so we do not want Laravel to encrypt it again, so we added tenant cookie into except array of EncryptCookies middleware as per documentation here. so our middleware looks like,

<?php

namespace App\Http\Middleware;

use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;

class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        'tenant'
    ];
}

Now at the last point, we extended our logic to find a tenant and get it to work on the main domain as well as sub-domain.

We have created our own custom class called InfyChatTenantFinder, which looks like,

<?php

namespace App\TenantFinder;

use App\Models\Account;
use App\Models\SubDomain;
use Illuminate\Http\Request;
use Spatie\Multitenancy\Models\Concerns\UsesTenantModel;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\TenantFinder\TenantFinder;

class InfyChatTenantFinder extends TenantFinder
{
    use UsesTenantModel;

    public function findForRequest(Request $request): ?Tenant
    {
        $host = $request->getHost();

        list($subDomain) = explode('.', $host, 2);

        // Get Tenant by subdomain if it's on subdomain
        if (!in_array($subDomain, ["www", "admin", "infychat"])) {
            return $this->getTenantModel()::whereDomain($host)->first();
        }

        // Get Tenant from user's account id if it's main domain
        if (in_array($subDomain, ["www", "infychat"])) {

            if ($request->hasCookie('tenant')) {
                $accountId = $request->cookie('tenant');

                $accountId = decrypt($accountId);

                $account = $this->getTenantModel()::find($accountId);

                if (!empty($account)) {
                    return $account;
                }

                \Cookie::forget('tenant');
            }
        }

        return null;
    }
}

So basically, first we check if the sub-domain is there, then find tenant from the sub-domain.

If the domain is the main domain then get the tenant id from the cookie and return the account (tenant) model.

So this is how you can customize the logic the way you want to have a custom tenant identification system.

July 24, 20203 minutesauthorMitul Golakiya
post

Sometimes we need to load a large amount of data into memory. Like all the models we have in the database.

For e.g. PDF Printing, Perform some global updates, etc.

So the general practices we use in Laravel is to write the following code,

$users = User::all();

Just imagine I have 10,000 users in the database and when I load all the users in one shot.

But it takes a really high amount of memory to load all the records and prepare Laravel Model class objects. And sometimes we also load them in chunks to save the memory, but in some use cases, chunking can not be the option.

Here is the screenshot of mine when I load 10,000 users into memory with the above code.


10k Models


It's using 37MB memory. Also, imagine the required memory if we are loading some relationships as well with these 10,000 records.

The Eloquent model is a great way to handle operations with lots of features like Mutators, Relationships, and much more.

But we really do not use these features all the time. We simply output or use the direct values which are stored in the table. So ideally, we do not need an eloquent model at all, if we are not going to use these features.

In those cases, Laravel also has a handy function toBase(). By calling this function it will retrieve the data from the database but it will not prepare the Eloquent models, but will just give us raw data and help us to save a ton of memory.

So my revised code will look something like this,

$users = User::toBase()->get();

Check the revised memory screenshot after adding the toBase function.


10k Models toBase


So it almost saves 50% of the memory. It's reduced from 35MB to 20MB and the application also works much much faster, because it doesn't need to spend time in preparing 10,000 Eloquent models.

So if you are not really going to use features of Eloquent and loading a large amount of data, then the toBase function can be really useful.

Here you can find a full video tutorial for the same.

June 21, 20202 minutesauthorMitul Golakiya
post

Recently, I've started working on one project where we follow modules pattern and for the same, we have different assets folder for the different modules and the folder named common for assets which are common across all the modules.

So our public folder looks like the following,

Module Asset Functions

The problem that I started facing was everywhere I need to give a full path to import/include any of the files from any of the folders. For e.g.

<img src="{{ asset('assets/tasks/images/delete.png') }}" alt="Delete Task">

Even if we have some folder in images to group similar images then it was even becoming longer. For e.g.

<img src="{{ asset('assets/tasks/images/social/facebook.png') }}" alt="Facebook">

The workaround that I used is, I created one file called helpers.php and created dedicated asset functions for each of the modules. For e.g., for tasks,

if (!function_exists('tasks_asset')) {
    /**
     * Generate an asset path for tasks module folder.
     *
     * @param  string  $path
     * @param  bool|null  $secure
     * @return string
     */
    function tasks_asset($path, $secure = null)
    {
        $path = "assets/tasks/".$path;
        return app('url')->asset($path, $secure);
    }
}

With this function, I can use,

<img src="{{ tasks_asset('images/delete.png') }}" alt="Delete Task">

Other advantages it gives are,

  1. if in future if the path of tasks folder changed, then I do not need to go and update every single import/include.
  2. I (or any new developer) do not need to remember the long paths and can always use direct function names for modules.

Even I like this pattern so much, so I went further and created dedicated image function as well,

if (!function_exists('tasks_image')) {
    /**
     * Generate an asset path for tasks module images folder.
     *
     * @param  string  $path
     * @param  bool|null  $secure
     * @return string
     */
    function tasks_image($path, $secure = null)
    {
        $path = "images/".$path;
        return tasks_asset($path, $secure);
    }
}

So I can use it as,

<img src="{{ tasks_image('delete.png') }}" alt="Delete Task">

Simple and handy functions to use everywhere.

May 16, 20202 minutesauthorMitul Golakiya
post

In chapter 5 of Gita, Karma-Sanyas-Yog, Lord shree Krishna said in verse 8,

યોગયુક્ત તત્વને જાણનારો મનુષ્ય જુએ, સાંભળે, સ્પર્શ કરે, સૂંઘે, ખાય, ચાલે, ઊંઘે, શ્વાસ લે, બોલે, મળત્યાગ કરે, ગ્રહણ કરે, નેત્ર ઉઘાડે તથા મીંચે તો પણ "ઇન્દ્રિયોના વિષયોમાં ઇન્દ્રિયો વર્તે છે" એમ સમજી "હું કાંઈ જ કરતો નથી" એમ મને છે.

The wise person knows, just like breathing, smelling, opening and closing eyelid, eat, sleep happens automatically, just like that He believes that everything is happening by itself. I’m just doing nothing.

Same in business, do not try to micro-manage things. Some things happen by itself. And let them happen in their own way. If you will spend your time in micro-managing those small things then you will never able to focus on other important things.

The another important thing is,

જ્ઞાનીઓ વિદ્યાવિનયયુક્ત બ્રાહ્મણમાં, ગાયમાં, હાથીમાં, કૂતરામાં તથા ચાંડાલમાં પણ સમાન દ્રષ્ટિ રાખવાવાળા હોય છે.

Sometimes people only respect certain people. Like some people only give respect to rich and financially sound people but they don’t pay attention to middle or lower income group people. Such behavior is not proper. Same way in office as well, we give attention to some people and we might not give attention to the office boy, pun, watchman, cleaner, etc. or Senior Developer vs Junior Developer. One must consider each and everyone with equality. Each and everyone should be respected. Just say them Hi or Hello and see the Joy that you and they get.

You can also listen to the full podcast here.

February 07, 20201 minuteauthorMitul Golakiya
post

In Last Podcast and Blog Post, we discussed how we need to create 4 departments in our business and become A-Karta in our organization.

But just becoming A-Karta doesn't work. So in verse 7 & 8, Lord Shree Krishna said and this is one of the most popular verses of Bhagavad Gita,

यदा यदा हि धर्मस्य ग्लानिर्भवति भारत |

अभ्युत्थानमधर्मस्य तदात्मानं सृजाम्यहम् || 7 ||

परित्राणाय साधूनां विनाशाय च दुष्कृताम् |

धर्मसंस्थापनार्थाय सम्भवामि युगे युगे || 8 ||

હે ભારત! જયારે જયારે ધર્મ ને હાનિ થાય છે અને અધર્મનું જોર વધે છે, ત્યારે ત્યારે હું જન્મ ધારણ કરું છું. સત્પુરુષોનાં રક્ષણ માટે, દુષ્ટોના વિનાશ માટે અને ધર્મની સારી રીતે સ્થાપના કરવા માટે હું યુગે યુગે પ્રકટું છું.

Whenever there is an increase in unrighteousness, Lord appears on earth age by age, to protect the righteous, to annihilate the wicked, and to re-establish the principles of dharma.

Same way when our 4 created departments do not do their work or perform their duties or do not produce desired results, then we also need to come in the picture time by time.

Whenever there is an increase in unrighteousness - People do not perform their duties or you do not get results

Lord appears on earth age by age - You need to come in the picture time by time

protect the righteous, to annihilate the wicked - to protect your culture & company & its people

and to re-establish the principles of dharma - to re-establish the balance and processes of the company.

You can also listen to the full podcast here.

February 01, 20201 minuteauthorMitul Golakiya