laravel example
About Us Blog Contact Us Request a quote

Globalpayment gateway in Laravel 12 | Laravel globalpayment gateway

Globalpayment gateway in Laravel 12

Install Laravel 12 App

Run below this command


composer create-project laravel/laravel example-app

Install global payment package

Run below this command


composer require globalpayments/php-sdk

Add Route

routes/web.php like as below code


<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PaymentController;

/*
 * |--------------------------------------------------------------------------
 * | Web Routes
 * |--------------------------------------------------------------------------
 * |
 * | Here is where you can register web routes for your application. These
 * | routes are loaded by the RouteServiceProvider and all of them will
 * | be assigned to the "web" middleware group. Make something great!
 * |
 */

Route::get('/', function () {
    return view('welcome');
});

Route::controller(PaymentController::class)->group(function () {
    Route::get('/payment', 'payment')->name('payment');
    Route::get('/get-json-file', 'getJsonFile')->name('getJsonFile');
});

Create controller file

Run below this command to create controller


<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GlobalPayments\Api\Entities\Address;
use GlobalPayments\Api\Entities\Enums\AddressType;
use GlobalPayments\Api\Entities\Enums\RemittanceReferenceType;
use GlobalPayments\Api\PaymentMethods\BankPayment;
use GlobalPayments\Api\ServiceConfigs\Gateways\GpEcomConfig;
use GlobalPayments\Api\HostedPaymentConfig;
use GlobalPayments\Api\Entities\HostedPaymentData;
use GlobalPayments\Api\Entities\Enums\HppVersion;
use GlobalPayments\Api\Entities\Exceptions\ApiException;
use GlobalPayments\Api\Services\HostedService;

class PaymentController extends Controller
{

    public function payment()
    {
        return view('payment');
    }

    public function getJsonFile()
    {
        $config = new GpEcomConfig();
        $config->merchantId = env('GP_MERC_ID');
        $config->accountId = "internet";
        $config->sharedSecret = env('GP_SECRET');
         //this is sandbax url
        $config->serviceUrl ="https://pay.sandbox.realexpayments.com/pay";
        $config->enableBankPayment = true;
        $config->hostedPaymentConfig = new HostedPaymentConfig();
        $config->hostedPaymentConfig->version = HppVersion::VERSION_2;
        $service = new HostedService($config);

        // Add 3D Secure 2 Mandatory and Recommended Fields
        $hostedPaymentData = new HostedPaymentData();
        $hostedPaymentData->customerEmail = "james.mason@example.com";
        $hostedPaymentData->customerPhoneMobile = "44|07123456789";
        $hostedPaymentData->addressesMatch = false;

        $hostedPaymentData->customerCountry = 'GB';
        $hostedPaymentData->customerFirstName = 'James';
        $hostedPaymentData->customerLastName = 'Mason';
        $hostedPaymentData->transactionStatusUrl = 
        $_SERVER['HTTP_REFERER'].'/examples/hpp/response-endpoint.php';
        $hostedPaymentData->merchantResponseUrl = 
        $_SERVER['HTTP_REFERER'] .'/examples/hpp/response-endpoint.php';
        $hostedPaymentData->presetPaymentMethods = [
            'cards'
        ];

        $billingAddress = new Address();
        $billingAddress->streetAddress1 = "Flat 123";
        $billingAddress->streetAddress2 = "House 456";
        $billingAddress->streetAddress3 = "Unit 4";
        $billingAddress->city = "Halifax";
        $billingAddress->postalCode = "W5 9HR";
        $billingAddress->country = "826";

        $shippingAddress = new Address();
        $shippingAddress->streetAddress1 = "Flat 123";
        $shippingAddress->streetAddress2 = "House 456";
        $shippingAddress->streetAddress3 = "Unit 4";
        $shippingAddress->city = "Halifax";
        $shippingAddress->postalCode = "W5 9HR";
        $shippingAddress->country = "826";


        try {
            $hppJson = $service->charge(19.99)
              ->withCurrency("GBP")
              ->withHostedPaymentData($hostedPaymentData)
              ->withAddress($billingAddress, AddressType::BILLING)
              ->withAddress($shippingAddress, AddressType::SHIPPING)
              ->withRemittanceReference(RemittanceReferenceType::TEXT, 
              'Nike Bounce Shoes')
              ->serialize();
           // with this, we can pass our json to the client side
            echo $hppJson;
        } catch (ApiException $e) {
            print_r($e);
            // TODO: Add your error handling here
        }
    }

    public function responseEndPointFile()
    {
        // configure client settings
        $config = new GpEcomConfig();
        $config->merchantId = "openbankingsandbox";
        $config->accountId = "internet";
        $config->sharedSecret = "sharedsecret";
        $config->serviceUrl = "https://pay.sandbox.realexpayments.com/pay";

        $service = new HostedService($config);

        $responseJson = $_POST['hppResponse'];

        try {
          // create the response object from the response JSON
          $parsedResponse = $service->parseResponse($responseJson, true);

          $orderId = $parsedResponse->orderId; // GTI5Yxb0SumL_TkDMCAxQA
          $responseCode = $parsedResponse->responseCode; // 00
           // [ test system ] Authorised
          $responseMessage = $parsedResponse->responseMessage;
          // get values accessible by key
          $responseValues = $parsedResponse->responseValues; 
           
          echo "Response Code : $responseCode \n";
          echo "Response Message : $responseMessage \n";
          echo "Response Values : ";
          print_r($responseValues);
        } catch (ApiException $e) {
          print_r($e);
          // For example if the SHA1HASH doesn't match what is expected
          // TODO: add your error handling here
        }
    }
}

Create blade file

resources/views/emails/payment.blade.php like as below code


<!DOCTYPE html>
<html>
<body>
<title>www.nidhit.in</title>

<button id="payButtonId">Click here to pay</button>

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script src="{{asset('js/rxp-hpp.js')}}"></script>
<script>
$(document).ready(function() {
$.getJSON("{{route('getJsonFile')}}", function(jsonFromRequestEndpoint) {
console.log(jsonFromRequestEndpoint);
RealexHpp.setHppUrl("https://pay.sandbox.realexpayments.com/pay");            
RealexHpp.lightbox.init("payButtonId", "{{route('responseEndPointFile')}}", 
jsonFromRequestEndpoint);
});
});
</script>
</body>
</html>

Setup mysql database and add global payment credentials

update .env file


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel12app //database name
DB_USERNAME=root
DB_PASSWORD=

GP_MERC_ID=openbankingsandbox
GP_SSC_TEST=sharedsecret
GP_URL_TEST=https://pay.sandbox.realexpayments.com/pay

Run below this command to migrate database


php artisan migrate

Run Laravel App:

Now Run your laravel 12 app after run these below commands


php artisan serve

Go to your web browser, hit this URL http://localhost:8000/payment and see your laravel app output:




https://www.laravelexample.com/

https://www.laravelexample.com/

https://www.laravelexample.com/

Recent Posts