PHP code example of limetools / swedbank

1. Go to this page and download the library: Download limetools/swedbank library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

limetools / swedbank example snippets


use LimeTools\Swedbank\SwedbankPaymentApi;

class PaymentController extends Controller
{
    public function __construct(
        protected SwedbankPaymentApi $swedbankApi
    ) {}
}

use LimeTools\Swedbank\Facades\SwedbankPayment;

class PaymentController extends Controller
{
    public function initiatePayment()
    {
        $providers = SwedbankPayment::getPaymentProviders(
            'LT',
            config('swedbank.production.client_id'),
            config('swedbank.production.private_key')
        );
    }
}

$swedbankApi = app(LimeTools\Swedbank\SwedbankPaymentApi::class);

'channels' => [
    // ...
    'swedbank' => [
        'driver' => 'daily',
        'path' => storage_path('logs/swedbank.log'),
        'level' => env('SWEDBANK_LOG_LEVEL', 'info'),
        'days' => 14,
    ],
],

use App\Http\Controllers\SwedbankPaymentController;

Route::get('/pay/{order}', [SwedbankPaymentController::class, 'start'])->name('swedbank.pay');
Route::get('/pay/{order}/callback', [SwedbankPaymentController::class, 'callback'])->name('swedbank.callback');



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use LimeTools\Swedbank\SwedbankPaymentApi;

class SwedbankPaymentController extends Controller
{
    public function __construct(
        protected SwedbankPaymentApi $swedbankApi
    ) {}

    public function start(Request $request, int $orderId)
    {
        // Replace this with your real order fetch
        $amount = '100.00'; // EUR

        $clientId = config('swedbank.production.client_id') ?? config('swedbank.sandbox.client_id');
        $privateKey = config('swedbank.production.private_key') ?? config('swedbank.sandbox.private_key');

        $paymentData = [
            'amount' => [
                'currency' => 'EUR',
                'value' => $amount,
            ],
            'creditor' => [
                'name' => config('app.name'),
                'iban' => config('swedbank.merchant_iban', 'LT123456789012345678'),
                'bic' => config('swedbank.merchant_bic', 'HABALT22'),
            ],
            'debtor' => [
                'name' => $request->user()->name ?? 'Customer',
            ],
            'remittanceInformationUnstructured' => 'Order #' . $orderId,
            'redirectUri' => route('swedbank.callback', ['order' => $orderId]),
            'state' => (string) $orderId,
        ];

        $redirectUrl = $this->swedbankApi->createPaymentInitiation(
            $paymentData,
            $clientId,
            $privateKey
        );

        return redirect($redirectUrl);
    }

    public function callback(Request $request, int $orderId)
    {
        // In a real app you would typically store and use a status URL
        $statusUrl = $request->query('statusUrl') ?? $request->query('paymentId');

        if (! $statusUrl) {
            return redirect()->route('home')->with('error', 'Missing payment status URL');
        }

        $clientId = config('swedbank.production.client_id') ?? config('swedbank.sandbox.client_id');
        $privateKey = config('swedbank.production.private_key') ?? config('swedbank.sandbox.private_key');

        $status = $this->swedbankApi->getPaymentStatus(
            $statusUrl,
            $clientId,
            $privateKey
        );

        if (($status['status'] ?? null) === 'EXECUTED') {
            // Mark your order as paid here
            return redirect()->route('home')->with('success', 'Payment successful');
        }

        return redirect()->route('home')->with('error', 'Payment not completed');
    }
}

$providers = $this->swedbankApi->getPaymentProviders(
    country: 'LT',
    clientId: config('swedbank.production.client_id'),
    privateKey: config('swedbank.production.private_key')
);

// Returns an array of providers with:
// - id
// - name
// - country
// - bic
// - logo
// - url

$paymentData = [
    'amount' => [
        'currency' => 'EUR',
        'value' => '100.00'
    ],
    'creditor' => [
        'name' => 'Your Company Name',
        'iban' => 'LT123456789012345678',
        'bic' => 'HABALT22'
    ],
    'debtor' => [
        'name' => 'Customer Name'
    ],
    'remittanceInformationUnstructured' => 'Order #12345',
    'redirectUri' => 'https://your-domain.com/payment/callback',
    'state' => 'order_12345' // Optional: for tracking
];

$redirectUrl = $this->swedbankApi->createPaymentInitiation(
    paymentData: $paymentData,
    clientId: config('swedbank.production.client_id'),
    privateKey: config('swedbank.production.private_key')
);

// Redirect user to $redirectUrl to complete payment
return redirect($redirectUrl);

// The status URL is typically returned in the payment initiation response
$statusUrl = 'https://pi.swedbank.com/public/api/v3/transactions/...';

$status = $this->swedbankApi->getPaymentStatus(
    statusUrl: $statusUrl,
    clientId: config('swedbank.production.client_id'),
    privateKey: config('swedbank.production.private_key')
);

// Check payment status
if ($status['status'] === 'EXECUTED') {
    // Payment successful
}

$paymentData = [
    'amount' => '100.00',
    'currency' => 'EUR',
    'description' => 'Order #12345',
    'redirectUrl' => 'https://your-domain.com/payment/callback',
    'notificationUrl' => 'https://your-domain.com/payment/webhook',
    'locale' => 'lt',
];

$formData = $this->swedbankApi->getPaymentInitiationForm(
    bic: 'HABALT22',
    paymentData: $paymentData,
    clientId: config('swedbank.production.client_id'),
    privateKey: config('swedbank.production.private_key')
);

// Use $formData['urls']['redirect'] to redirect user
$redirectUrl = $formData['urls']['redirect'];



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use LimeTools\Swedbank\SwedbankPaymentApi;

class PaymentController extends Controller
{
    public function __construct(
        protected SwedbankPaymentApi $swedbankApi
    ) {}

    public function initiatePayment(Request $request)
    {
        $order = $request->user()->orders()->findOrFail($request->order_id);
        
        $clientId = config('swedbank.production.client_id');
        $privateKey = config('swedbank.production.private_key');

        // Get providers
        $providers = $this->swedbankApi->getPaymentProviders('LT', $clientId, $privateKey);

        // Create payment initiation
        $paymentData = [
            'amount' => [
                'currency' => 'EUR',
                'value' => number_format($order->total, 2, '.', '')
            ],
            'creditor' => [
                'name' => config('app.name'),
                'iban' => config('swedbank.merchant_iban'),
                'bic' => config('swedbank.merchant_bic')
            ],
            'debtor' => [
                'name' => $order->customer_name
            ],
            'remittanceInformationUnstructured' => 'Order #' . $order->id,
            'redirectUri' => route('payment.callback', ['order' => $order->id]),
            'state' => (string) $order->id
        ];

        $redirectUrl = $this->swedbankApi->createPaymentInitiation(
            $paymentData,
            $clientId,
            $privateKey
        );

        // Store status URL for later use
        $order->update([
            'payment_status_url' => $redirectUrl
        ]);

        return redirect($redirectUrl);
    }

    public function handleCallback(Request $request)
    {
        $order = $request->user()->orders()->findOrFail($request->order_id);
        
        $clientId = config('swedbank.production.client_id');
        $privateKey = config('swedbank.production.private_key');

        // Get status URL from order or request
        $statusUrl = $order->payment_status_url ?? $request->input('status_url');

        $status = $this->swedbankApi->getPaymentStatus(
            $statusUrl,
            $clientId,
            $privateKey
        );

        if ($status['status'] === 'EXECUTED') {
            $order->update(['status' => 'paid']);
            return redirect()->route('payment.success');
        }

        return redirect()->route('payment.failed');
    }
}

return [
    'environment' => env('EVERYPAY_ENVIRONMENT', 'demo'), // demo or production

    'demo' => [
        'api_username' => env('EVERYPAY_DEMO_API_USERNAME'),
        'api_secret'   => env('EVERYPAY_DEMO_API_SECRET'),
        'account_name' => env('EVERYPAY_DEMO_ACCOUNT_NAME'),
        'base_url'     => 'https://igw-demo.every-pay.com/api',
    ],

    'production' => [
        'api_username' => env('EVERYPAY_PRODUCTION_API_USERNAME'),
        'api_secret'   => env('EVERYPAY_PRODUCTION_API_SECRET'),
        'account_name' => env('EVERYPAY_PRODUCTION_ACCOUNT_NAME'),
        'base_url'     => 'https://pay.every-pay.eu/api',
    ],
];

use LimeTools\Swedbank\EveryPayApi;

class EveryPayController extends Controller
{
    public function __construct(
        protected EveryPayApi $everyPay,
    ) {}

    public function start(Request $request)
    {
        // Replace with your own order fetch
        $amount = 10.00;
        $orderReference = 'ORDER-' . $request->user()->id . '-' . now()->timestamp;

        $response = $this->everyPay->createOneOffPayment([
            'amount'          => $amount,
            'order_reference' => $orderReference,
            'customer_url'    => route('everypay.callback'),
            // optional extras:
            // 'email'        => $request->user()->email,
            // 'customer_ip'  => $request->ip(),
            // 'payment_description' => 'Order #' . $orderReference,
        ]);

        // Redirect the user to the EveryPay hosted page
        return redirect($response['payment_link']);
    }

    public function callback(Request $request)
    {
        $paymentReference = $request->query('payment_reference');

        if (! $paymentReference) {
            return redirect()->route('home')->with('error', 'Missing payment reference');
        }

        $status = $this->everyPay->getPaymentStatus($paymentReference);

        // Example status handling
        if (($status['payment_state'] ?? null) === 'settled') {
            // Mark your order as paid here
            return redirect()->route('home')->with('success', 'Payment successful');
        }

        return redirect()->route('home')->with('error', 'Payment not completed');
    }
}

$everyPay = app(\LimeTools\Swedbank\EveryPayApi::class);
// or
$everyPay = app('everypay.api');

'logging' => [
    'enabled' => env('SWEDBANK_LOGGING_ENABLED', true),
    'level' => env('SWEDBANK_LOG_LEVEL', 'info'),
    'channel' => env('SWEDBANK_LOG_CHANNEL', 'swedbank'),
],

'channels' => [
    // ...
    'swedbank' => [
        'driver' => 'daily',
        'path' => storage_path('logs/swedbank.log'),
        'level' => env('SWEDBANK_LOG_LEVEL', 'info'),
        'days' => 14,
    ],
],

try {
    $redirectUrl = $this->swedbankApi->createPaymentInitiation($paymentData, $clientId, $privateKey);
} catch (\Exception $e) {
    // Handle error
    logger()->error('Payment initiation failed', ['error' => $e->getMessage()]);
    return back()->withErrors(['payment' => 'Failed to initiate payment']);
}
bash
php artisan vendor:publish --tag=swedbank-config
bash
php artisan vendor:publish --tag=swedbank-config