Laravel Cybersource

Laravel Cybersource

Latest Stable Version Total Downloads License
View Package

Laravel Cybersource
This Laravel package allows you to integrate Cybersource payment solutions into your Laravel application seamlessly. It provides a simple, fluent API to handle payments and transactions with Cybersource.

Installation


You can install the package via composer:

composer require dipesh79/laravel-cybersource

Configuration

After installation, publish the configuration file:

php artisan vendor:publish

Pubilsh "Dipesh79\LaravelCybersource\LaravelCybersourceServiceProvider"

After publishing the configuration file, you can find it at `config/cybersource.php`. Here you can set your Cybersource API credentials and other settings.

Add following variables in .env file

CYBERSOURCE_SECRET_KEY=""
CYBERSOURCE_ACCESS_KEY=""
CYBERSOURCE_PROFILE_ID=""
CYBERSOURCE_URL="https://testsecureacceptance.cybersource.com/pay" #staging URL provided by Cybersource

You can get all these details from Cybersource Dashboard.

Usage


<?php
use Dipesh79\LaravelCybersource\LaravelCybersource;

$transaction_uuid = Uuid::uuid4()->toString();
$reference_number = 1234; // Generate unique reference number
//store this is db for future use
Payment::create([
    'reference_id'=>$reference_number,
    'transaction_uuid'=>'$transaction_uuid
]);
$paymentData = [
    'transaction_uuid' => $transaction_uuid,
    'reference_number' => $reference_number,
    'amount' => 1
];

$cybersource = new LaravelCybersource();
$data = $cybersource->generateFormData($paymentData);
return view('welcome', compact('data'));

Here you will get data array which you will use in form on blade file.

<form id="frm-nicasia" action="{{config('cybersource.url')}}" method="post">
    @csrf
    <div class="form-group">
        @foreach($data as $key=>$value)
            <label for="{{$key}}">{{$key}}</label>
            <input type="text" name="{{$key}}" id="{{$key}}" value="{{$value}}" class="form-control">

        @endforeach

        <button type="submit" class="btn btn-success m-3">Pay</button>
    </div>
</form>

Now,  when this form is submitted then user will be redirected to payment page.

Callback & Cancel Url

You have to add cancel url and callback url to cybersource dashboard.

Callback Response

<?php
use Dipesh79\LaravelCybersource\LaravelCybersource;

Route::post('/cybersource/callback', function (Request $request) {
    $cybersource = new LaravelCybersource();
    $data = $cybersource->checkPaymentStatus($request->all());
    dd($data);
});

On $data you will get following response

array:5 [▼ // routes/web.php:12
  "reference_number" => "1473"
  "transaction_uuid" => "42848dd8-3ba5-4fbe-897c-362b635a7553"
  "amount" => "1"
  "payment_status" => true
  "message" => "Request was processed successfully."
]

Cancel Callback

<?php
use Dipesh79\LaravelCybersource\LaravelCybersource;

Route::post('/cybersource/cancel', function (Request $request) {
    $cybersource = new LaravelCybersource();
    $data = $cybersource->getCancelledData($request->all());
    dd($data);
});

On $data you will get following response

array:5 [▼ // routes/web.php:17
  "reference_number" => "2818"
  "transaction_uuid" => "8051f60e-ded7-4488-911a-faf80666c002"
  "amount" => "1"
  "payment_status" => false
  "message" => "The consumer cancelled the transaction"
]

Update Payment Table

<?php
//Check Payment Status and update the database
// find the payment record using $data['reference_number']
$payment = Payment::where('reference_id',$data['reference_number'])->first();
if($data['payment_status'] === true)
{
   $payment->status = 'Success';
   $payment->save();
}
else
{
   $payment->status = 'Failed';
   $payment->save();
}


License: MIT

Author: @Dipesh79

Support: For support, email dipeshkhanal79[at]gmail[dot]com.


This post is licensed under CC BY 4.0 by the author.