Md Obydullah Howdy, I am Md Obydullah. I love to write the article which may help others. It's my passion.

Laravel Skrill Payment Gateway Integration with LaraSkrill

Last modified on July 17, 2019 6 min read

Today, I’m going to show how to integrate Skrill payment gateway in Laravel with LaraSkrill package. By using this package we are able to make payment and do refund. So, let’s start:

Table of Contents

  1. Install LaraSkrill Package
  2. Make Migration, Model and Controller
  3. Register Routes
  4. Create Blade Templates
  5. Configure LaraSkrill
  6. Test Our Project
  7. Note

Step 1 : Install LaraSkrill Package

At first, let’s install LaraSkrill package by typing this command:

composer require obydul/laraskrill

Now we have to register LaraSkrill provider. Go to config >> app.php and find the providers & add this:

app.php
'providers' => [
    // ...
    Obydul\LaraSkrill\LaraSkrillServiceProvider::class,
]

Run the following command to publish configuration:

php artisan vendor:publish

Then choose ‘Obydul\LaraSkrill\LaraSkrillServiceProvider’.

The provider is in number 8. Type 8, hit enter.

Step 2 : Make Migration, Model and Controller

We are going to create migration, model and controller for Skrill payment. Run this artisan command to create the three things:

php artisan make:model SkrillPayment -mcr

The migration file is for Skrill IPN. Like PayPal, Skrill provides Instant Payment Notification (IPN) too. To save payment information, we use IPN. If you don’t need to save payment information in your database, then you don’t this need.

Go to database>migrations and open the newly created migration file. Then paste this code:

create_skrill_payments_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSkrillPaymentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('skrill_payments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('transaction_id');
            $table->string('mb_transaction_id');
            $table->string('invoice_id');
            $table->string('order_from');
            $table->string('customer_id');
            $table->string('customer_email');
            $table->string('biller_email');
            $table->string('amount');
            $table->string('currency');
            $table->string('status');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('skrill_payments');
    }
}

Now open SkrillPaymentController from app>Http>Controllers and paste this code:

SkrillPaymentController.php
<?php

namespace App\Http\Controllers;

use App\SkrillPayment;
use Illuminate\Http\Request;
use Obydul\LaraSkrill\SkrillClient;
use Obydul\LaraSkrill\SkrillRequest;
use Redirect;

class SkrillPaymentController extends Controller
{
    /**
     * Make Skrill Payment
     */
    public function makePayment()
    {
        // Create object instance of SkrillRequest
        $request = new SkrillRequest();
        $request->transaction_id = 'MNPTTX0001'; // generate transaction id
        $request->amount = '10.26';
        $request->currency = 'USD';
        $request->language = 'EN';
        $request->prepare_only = '1';
        $request->merchant_fields = 'site_name, customer_email';
        $request->site_name = 'Your Website';
        $request->customer_email = 'customer@mynotepaper.com';
        $request->detail1_description = 'Product ID:';
        $request->detail1_text = '101';

        // Create object instance of SkrillClient
        $client = new SkrillClient($request);
        $sid = $client->generateSID(); //return SESSION ID

        // handle error
        $jsonSID = json_decode($sid);
        if ($jsonSID != null && $jsonSID->code == "BAD_REQUEST")
            return $jsonSID->message;

        // do the payment
        $redirectUrl = $client->paymentRedirectUrl($sid); //return redirect url
        return Redirect::to($redirectUrl); // redirect user to Skrill payment page
    }

    /**
     * Do Refund
     */
    public function doRefund()
    {
        // Create object instance of SkrillRequest
        $prepare_refund_request = new SkrillRequest();
        $prepare_refund_request->transaction_id = 'MNPTTX0001';
        $prepare_refund_request->amount = '5.56';
        $prepare_refund_request->refund_note = 'Product no longer in stock';
        $prepare_refund_request->merchant_fields = 'site_name, customer_email';
        $prepare_refund_request->site_name = 'Your Website';
        $prepare_refund_request->customer_email = 'customer@example.com';

        // do prepare refund request
        $client_prepare_refund = new SkrillClient($prepare_refund_request);
        $refund_prepare_response = $client_prepare_refund->prepareRefund(); // return sid or error code

        // refund requests
        $refund_request = new SkrillRequest();
        $refund_request->sid = $refund_prepare_response;

        // do refund
        $client_refund = new SkrillClient($refund_request);
        $do_refund = $client_refund->doRefund();
        var_dump($do_refund); // display response
    }

    /**
     * Instant Payment Notification (IPN) from Skrill
     */
    public function ipn(Request $request)
    {
        // skrill data - get more fields from Skrill Quick Checkout Integration Guide 7.9 (page 23)
        $transaction_id = $request->input('transaction_id');
        $mb_transaction_id = $request->input('mb_transaction_id');
        $invoice_id = $request->input('invoice_id'); // custom field
        $order_from = $request->input('order_from'); // custom field
        $customer_email = $request->input('customer_email'); // custom field
        $biller_email = $request->input('pay_from_email');
        $customer_id = $request->input('customer_id');
        $amount = $request->input('amount');
        $currency = $request->input('currency');
        $status = $request->input('status');

        // status message
        if ($status == '-2') {
            $status_message = 'Failed';
        } else if ($status == '2') {
            $status_message = 'Processed';
        } else if ($status == '0') {
            $status_message = 'Pending';
        } else if ($status == '-1') {
            $status_message = 'Cancelled';
        }

        // now store data to database
        $skrill_ipn = new SkrillPayment();
        $skrill_ipn->transaction_id = $transaction_id;
        $skrill_ipn->mb_transaction_id = $mb_transaction_id;
        $skrill_ipn->invoice_id = $invoice_id;
        $skrill_ipn->order_from = $order_from;
        $skrill_ipn->customer_email = $customer_email;
        $skrill_ipn->biller_email = $biller_email;
        $skrill_ipn->customer_id = $customer_id;
        $skrill_ipn->amount = $amount;
        $skrill_ipn->currency = $currency;
        $skrill_ipn->status = $status_message;
        $skrill_ipn->created_at = Date('Y-m-d H:i:s');
        $skrill_ipn->updated_at = Date('Y-m-d H:i:s');
        $skrill_ipn->save();
    }
}

In the SkrillPaymentController, I’ve created three functions.

  • makePayment(): to make a payment
  • doRefund(): to send refund
  • ipn(): to receive payment information from Skrill

Step 3 : Register Routes

Let’s define the routes for our project. Open web.php from routes folder and paste this routes:

SkrillPaymentController.php
<?php

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

Route::get('make-payment', 'SkrillPaymentController@makePayment');
Route::get('do-refund', 'SkrillPaymentController@doRefund');
Route::post('ipn', 'SkrillPaymentController@ipn');

Route::get('payment-completed', function () {
    return view('payment-completed');
});
Route::get('payment-cancelled', function () {
    return view('payment-cancelled');
});

Step 4 : Create Blade Templates

We have defined 3 views on the routes. Let’s make the blade flies. Go to resources/views folder make 4 blade files like below:

layout.blade.php
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>@yield('title')</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body style="margin-top: 40px;">

<div class="text-center">
    <img src="https://i.imgur.com/hHZjfUq.png"><br>
    <span class="text-secondary">Laravel Skrill Payment Gateway Integration with LaraSkrill</span>
</div>

<div class="container" style="margin-top: 40px;">
    @yield('content')
</div>

</body>
</html>
home.blade.php
@extends('layout')

@section('title', 'MyNotePaper - Laravel Skrill Payment Gateway Integration with LaraSkrill')

@section('content')
    <div class="text-center" style="margin-bottom: 25px;">
        <a href="{{url('make-payment')}}" class="btn btn-info">Make Payment</a> <a href="{{url('do-refund')}}"
                                                                                   class="btn btn-danger">Do
            Refund</a>
    </div>
    @php
        $skrill_payments = DB::table('skrill_payments')->orderBy('id', 'desc')->get();
    @endphp
    <table class="table table-bordered">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">MB Transaction ID</th>
            <th scope="col">Amount</th>
            <th scope="col">Customer Email</th>
            <th scope="col">Created At</th>
        </tr>
        </thead>
        <tbody>
        @if(!empty($skrill_payments))
            @foreach($skrill_payments as $skrill_payment)
                <tr>
                    <td>{{$skrill_payment->id}}</td>
                    <td>{{$skrill_payment->mb_transaction_id}}</td>
                    <td>{{$skrill_payment->amount}} ({{$skrill_payment->currency}})</td>
                    <td>{{$skrill_payment->customer_email}}</td>
                    <td>{{$skrill_payment->created_at}}</td>
                </tr>
            @endforeach
        @endif
        </tbody>
    </table>
@endsection
payment-completed.blade.php
@extends('layout')

@section('title', 'Payment Completed')

@section('content')
    <div class="alert alert-success" role="alert">
        Payment Successfully Completed
    </div>

    <div class="text-center">
        <a href="{{url('/')}}" class="btn btn-info">&lt;Home</a>
    </div>
@endsection
payment-cancelled.blade.php
@extends('layout')

@section('title', 'Payment Cancelled')

@section('content')
    <div class="alert alert-danger" role="alert">
        Payment Has Been Cancelled
    </div>

    <div class="text-center">
        <a href="{{url('/')}}" class="btn btn-info">&lt;Home</a>
    </div>
@endsection

Step 5 : Configure LaraSkrill

We are about to finish. Navigate to config folder and open laraskrill.php. Please configure this file correctly.

laraskrill.php
return [
    'merchant_email' => 'demoqco@sun-fish.com',
    'api_password' => 'MD5 API/MQI password', // required for refund option only.
    'return_url' => 'https://laravel.dev/payment-completed',
    'cancel_url' => 'https://laravel.dev/payment-cancelled',
    'status_url' => 'your-email@example.com', // url or email
//    'status_url' => 'http://laravel.dev/ipn', // url or email
    'status_url2' => 'your-email2@example.com', // url or email
    'refund_status_url' => 'IPN URL or Email', // url or email
    'logo_url' => 'https://i.imgur.com/BYBiIZX.png',
];

Replace return_url, cancel_url and status_url with your project’s URL. Skrill sends instant payment notification status_url. If you set your email, you will get email notification. If you set IPN URL, you will get payment data in POST request.

Note: IPN doesn’t work in localhost. For localhost, please test with email.

Step 6 : Test Our Project

We have completed all the steps. It’s time to test our project. My test project URL is https://laravel.dev/.

Skrill test card VISA number: 4000001234567890

Run the project and visit make payment route:

https://laravel.dev/make-payment

If everything is correct, you will be redirected to this page:

Enter the test card and fill-up the rest fields with any data. Please enter correct email to receive payment receipt. Then click “PAY NOW” button.

I’ve seen the success message. You can click continue button or it will automatically redirect to your return URL.

I’ve set IPN and received data from Skrill. I’ve stored the data in the database.

** To do refund, you have to add your IP in white-list. The option is in Skrill settings. You need to generate MQI/API password too.

We have successfully integrated Skrill payment gateway in Laravel using LaraSkrill. You can download this project from GitHub.

Step 7 : Note

I’ve created this LaraSkrill package 😛 . You will find more information on GitHub. If you notice any errors, please write in the comment section. Thank you. 🙂

You're welcome to suggest any article to write!

Md Obydullah Howdy, I am Md Obydullah. I love to write the article which may help others. It's my passion.

Leave a Reply

Your email address will not be published. Required fields are marked *