Introduction

This comprehensive guide aims to provide a detailed and descriptive walkthrough of the process involved in integrating a payment gateway into the Martvill platform using Laravel. By following these meticulous steps, you will be able to seamlessly enable payment processing for your online store.

Step 1: Creating a Laravel Module

The integration process begins with the creation of a dedicated Laravel module for your chosen payment gateway. To accomplish this, utilize the following Artisan command:

php artisan module:make moduleName

Step 2: Adding Gateway Configuration

Within your module’s module.json configuration file, you must explicitly define that this module serves as a payment gateway. This is achieved by setting the “gateway” property to true:

{
    "name": "moduleName",
    "gateway": true,
    // ... other module settings
}

Step 3: Creating a Scope Class

To maintain separation and clarity in your code, it is advisable to create a scope class within your module. This class should bear the name of your chosen gateway (e.g., “StripeScope”) and must implement the Scope interface. The primary purpose of this class is to define the criteria that will be applied to queries related to your gateway. Below is a code example:

namespace Modules\Stripe\Scope;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class StripeScope implements Scope
{
    /**
     * Apply the Stripe gateway scope.
     *
     * @param Builder $builder
     * @param Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('gateway', 'stripe');
    }
}

Step 4: Creating a Custom Gateway Model

In this step, you will create a custom model for your payment gateway (e.g., “Stripe”). This model should extend the base “Gateway” class found in the “Modules\Gateway\Entities\Gateway” namespace. Configure the table name, appended attributes, and apply the previously created scope. Here is an example:

namespace Modules\Stripe\Entities;

use Modules\Stripe\Scope\StripeScope;
use Modules\Gateway\Entities\Gateway;

class Stripe extends Gateway
{
    protected $table = 'gateways'; // Define the table name

    protected $appends = ['image_url']; // Specify appended attributes

    /**
     * Apply the StripeScope as a global scope.
     *
     * @return void
     */
    protected static function booted()
    {
        static::addGlobalScope(new StripeScope());
    }
}

Step 5: Creating the Custom Gateway “GatewayNameBody” Model

To effectively manage your payment gateway’s specific attributes, you need to create a custom model named after your gateway (e.g., “StripeBody”). This model should reside within the “Modules\GatewayModuleName\Entities” namespace. It serves as a container for attributes such as client secret, publishable key, instructions, status, and sandbox configuration. Here is an example:

<?php
namespace Modules\Stripe\Entities;

use Modules\Gateway\Entities\GatewayBody;

class StripeBody extends GatewayBody
{
    public $clientSecret;
    public $publishableKey;
    public $instruction;
    public $status;
    public $sandbox;

    /**
     * StripeBody constructor.
     *
     * @param \Illuminate\Http\Request $request
     */
    public function __construct($request)
    {
        $this->clientSecret = $request->clientSecret;
        $this->publishableKey = $request->publishableKey;
        $this->instruction = $request->instruction;
        $this->status = $request->status;
        $this->sandbox = $request->sandbox;
    }
}
Additional Notes

Feel free to add any additional fields or attributes to the “GatewayModuleNameBody” model according to your payment gateway’s specific configuration requirements.

Step 6: Configuring the Payment Gateway Module

Configuration is crucial to ensure that your payment gateway module operates smoothly. Define the settings, validation rules, and field definitions for your gateway module. Here’s an example configuration for the Stripe payment gateway module:

return [
    'name' => 'Stripe', // Name of the payment gateway
    'alias' => 'stripe', // Alias for the gateway
    'logo' => 'Modules/Stripe/Resources/assets/stripe.png', // Path to the gateway logo
    // ... other settings
];

Step 7: Creating a Request File for Payment Gateway Credentials

A request file (e.g., “GateywayModuleNameRequest”) is necessary to handle the validation of payment gateway credentials during the storage process. This file should be placed within the appropriate namespace and directory. Below is a sample implementation:

<?php

namespace Modules\Stripe\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StripeRequest extends FormRequest
{
    // ... Define request validation rules and methods
}

Step 8: Adding Routes for Editing and Storing Payment Gateway Credentials

To allow users to edit and store payment gateway credentials, you need to define specific routes within your Laravel application. These routes should be specified in a routes file (e.g., “web.php”). Below is an example of how to structure these routes:

// Example routes for the Stripe payment gateway module
Route::group(['prefix' => 'gateway/stripe', 'as' => 'stripe.', 'namespace' => 'Modules\Stripe\Http\Controllers', 'middleware' => ['auth', 'permission', 'locale']], function () {
    Route::post('/store', 'StripeController@store')->name('store'); // Route for storing payment gateway credentials
    Route::get('/edit', 'StripeController@edit')->name('edit'); // Route for editing payment gateway credentials
});

Step 9: Adding Methods to the Controller

In this step, you will add two essential methods to your payment gateway controller: one for storing payment gateway settings and another for rendering the edit form.

Store Method – Storing Payment Gateway Settings
public function store(StripeRequest $request)
{
    // ... Implement logic to store gateway settings
}
Edit Method – Rendering the Edit Form
public function edit(Request $request)
{
    // ... Implement logic to render the edit form
}

Step 10: Creating a Payment View Class for Your Payment Gateway

To enhance modularity, create a payment view class within your payment module. This class should implement the PaymentViewInterface and provide a static method for rendering the payment view.

Step 11: Creating a Payment Blade View File

To deliver a seamless payment experience, create a

Blade view file (e.g., “pay.blade.php”) that showcases the payment interface for your payment gateway. Customize this view with the necessary form fields and elements as per your gateway’s requirements.

Step 12: Creating a Response Class for Stripe Payments

A response class (e.g., “StripeResponse”) is indispensable for handling and formatting responses from Stripe payments. This class should extend the base response class and implement the HasDataResponseInterface. It plays a pivotal role in processing payment outcomes.

Step 13: Creating a Payment Processor Class for Your Payment Gateway

In this final step, you will create a payment processor class (e.g., “StripeProcessor”) that takes charge of payment transactions within your payment module. This class implements various interfaces, including PaymentProcessorInterface, RequiresCallbackInterface, and RequiresCancelInterface. It orchestrates the entire payment process, ensuring that transactions are executed securely and efficiently.


By diligently following these detailed and descriptive steps, you will successfully integrate a payment gateway into your Martvill. This integration will enable your customers to make seamless and secure payments, enhancing their overall shopping experience.