PHPackages                             olssonm/swish-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. olssonm/swish-php

ActiveLibrary

olssonm/swish-php
=================

Swish API-wrapper. Compatible with Laravel

v3.8(2mo ago)3227.5k↑35.8%3MITPHPPHP ^8.2CI passing

Since Jun 30Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/olssonm/swish-php)[ Packagist](https://packagist.org/packages/olssonm/swish-php)[ Docs](https://github.com/olssonm/swish-php)[ Fund](https://marcusolsson.me/kontakta)[ GitHub Sponsors](https://github.com/olssonm)[ RSS](/packages/olssonm-swish-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (18)Versions (36)Used By (0)

[![Swish logo / olssonm/swish-php](/media/swish.png)](/media/swish.png)

Swish PHP
=========

[](#swish-php)

[![Supported PHP-versions](https://camo.githubusercontent.com/74fcb27cdc1f089671db0dcfc131e8d639dae03f70d4373a5fe7fd37c9e22369/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6f6c73736f6e6d2f73776973682d7068703f7374796c653d666c61742d737175617265)](https://packagist.org/packages/olssonm/swish-php)[![Latest Version on Packagist](https://camo.githubusercontent.com/b83540f3b9f2ec2aebc5bb69e657bfc6d7b06192874ec6a7f65b6579a70fae70/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6c73736f6e6d2f73776973682d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/olssonm/swish-php)[![Build Status](https://camo.githubusercontent.com/dafd05292f193c1b3a35dfdc33822f0acce6ab8da9c51a3a6fddbafb9973bf75/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6f6c73736f6e6d2f73776973682d7068702f746573742e79616d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/olssonm/swish-php/actions?query=workflow%3A%22Run+tests%22)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

The leading Swish API-wrapper for PHP: minimal setup, rigorous tests, and Laravel providers built in.

Prerequisites
-------------

[](#prerequisites)

This package supports PHP ^8.2. Tested against Laravel 11 &amp; 12. PHP needs to be compiled with the cURL and SSL extensions (in an absolute majority of cases, they should be available by default).

*Using an older version of PHP or Laravel? Check out v1 and v2 of this package. Note however that Payouts are available from v3.*

Installation
------------

[](#installation)

```
composer require olssonm/swish-php

```

Setup
-----

[](#setup)

You will need to have access to your Swish-certificates to use this package in production. You can however use their testing/Merchant Swish Simulator environment without being a Swish-customer during development.

Read more about testing in their MSS-environment in their [official documentation](https://developer.swish.nu/documentation/environments#merchant-swish-simulator). A quick rundown on using/creating Swish-certificates [is published here](https://marcusolsson.me/artiklar/hur-man-skapar-certifikat-for-swish) (in Swedish).

When creating the client, you will have to set which environment you are working with (otherwise it defaults to the production environment, `https://cpc.getswish.net/swish-cpcapi/api/`), you may use any of the following options:

```
Client::TEST_ENDPOINT // https://mss.cpc.getswish.net/swish-cpcapi/api/
Client::PRODUCTION_ENDPOINT // https://cpc.getswish.net/swish-cpcapi/api/
Client::SANDBOX_ENDPOINT // https://staging.getswish.pub.tds.tieto.com/swish-cpcapi/api/
```

```
use Olssonm\Swish\Certificate;
use Olssonm\Swish\Client;

$certificate = new Certificate(
    '/path/to/client.pem',
    'client-passphrase',
    '/path/to/root.pem', // Can also be omitted for "true" to verify peer
    '/path/to/signing.key', // Path to signing certificate, only used for payouts
    'signing-passphrase' // Only used for payouts
);
$client = new Client($certificate, $endpoint = Client::TEST_ENDPOINT);
```

Important

The paths to the certificates should be absolute. You can use `realpath -s YOUR_CERT.pem` for this.

### Laravel

[](#laravel)

With the Laravel service provider and facades you can work with the package more eloquently. Just require the package and publish the configuration:

```
php artisan vendor:publish --provider="Olssonm\Swish\Providers\SwishServiceProvider"

```

In `/config/swish.php`, you can then set your details accordingly:

```
return [
    'certificates' => [
        'client' => env('SWISH_CLIENT_CERTIFICATE_PATH'),
        'password' => env('SWISH_CLIENT_CERTIFICATE_PASSWORD'),
        'root' => env('SWISH_ROOT_CERTIFICATE_PATH', true),
        'signing' => env('SWISH_SIGNING_CERTIFICATE_PATH', null),
        'signing_password' => env('SWISH_CLIENT_SIGNING_PASSWORD', null),
    ],
    'endpoint' => env('SWISH_URL', \Olssonm\Swish\Client::PRODUCTION_ENDPOINT),
];
```

This may also be a good place to keep your payee-alias, callback-url and such, which you can then access with `config('swish.payee_alias')` etc.

For convenience and security, you can use relative paths to reference your certificates. Laravel will automatically resolve these paths from the `storage/app/private` directory.

```
SWISH_CLIENT_CERTIFICATE_PATH=swish/client.pem # storage/app/private/swish/client.pem
```

If you prefer to keep your certificates in a specific location outside the default storage directory, you can provide an absolute path. However, be aware that when using an absolute path, the package will not utilize the filesystem to resolve the relative path.

```
SWISH_CLIENT_CERTIFICATE_PATH=/absolute/path/swish/client.pem
```

Usage
-----

[](#usage)

A typical case for creating a Swish-payment.

```
use Olssonm\Swish\Certificate;
use Olssonm\Swish\Client;
use Olssonm\Swish\Payment;

$certificate = new Certificate(
    '/path/to/client.pem',
    'client-passphrase'
);
$client = new Client($certificate);

// Create a new payment-object
$payment = new Payment([
    'callbackUrl' => 'https://callback.url',
    'payeePaymentReference' => 'XVY77',
    'payeeAlias' => '123xxxxx',
    'payerAlias' => '321xxxxx',
    'amount' => '100',
    'currency' => 'SEK',
    'message' => 'A purchase of my product',
]);

// Perform the request
$response = $client->create($payment);

// $response->id = '11A86BE70EA346E4B1C39C874173F088'
// $response->location = 'https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests/11A86BE70EA346E4B1C39C874173F088'
// $response->paymentRequestToken = 'a-unique-token'
```

With Laravel you can also use the facade and save a few lines of code (in this example `Olssonm\Swish\Facades\Swish` has been aliased to `Swish`)

```
use Swish;
use Olssonm\Swish\Payment;

$response = Swish::create(new Payment([
    'callbackUrl' => 'https://callback-url.com',
    'payeePaymentReference' => 'XVY77',
    'payeeAlias' => '123xxxxx',
    'payerAlias' => '321xxxxx',
    'amount' => '100',
    'currency' => 'SEK',
    'message' => 'My product',
]));
```

*The easiest way to see more complete examples is to look at the tests; in [MMS.php](/tests/MMS.php) all major examples are covered.*

### Payments and Refunds

[](#payments-and-refunds)

Tip

Read more about [payments](https://developer.swish.nu/api/payment-request/v2) and [refunds](https://developer.swish.nu/api/refunds/v2) in the official documentation

Always when using the client, use the Payment and Refund-classes even if only the ID is needed for the action, i.e:

```
$payment = $client->get(new Payment(['id' => '5D59DA1B1632424E874DDB219AD54597']));
```

### QR-codes

[](#qr-codes)

QR codes is another way that users can pay – using them they do not need to supply their phone number. This also makes it possible to pay using POS terminals and such, which may lack any input-methods such as a keyboard or touch screen.

***Note** This package creates QR-codes according to the [Mcom to Qcom](https://developer.swish.nu/api/qr-codes/v1#mcom-to-qcom)-flow. While Swish provides endpoints to create "prefilled QR-codes" and more, they lack the ability to automatically verify payments which makes them unsuitable for web applications and therefore is outside the scope of this package.*

Creating QR-codes requires two requests to the Swish API; first to create the payment and retrieve the payment request token, and then a separate request to get a QR code. Create the payment as usual, but *exclude the payerAlias* (users phone number); else the QR-endpoint will fail.

```
use Olssonm\Swish\Payment;
use Olssonm\Swish\QR;

// Create payment
$payment = new Payment([
    'callbackUrl' => 'https://callback.url',
    'payeePaymentReference' => 'XVY77',
    'payeeAlias' => '123xxxxx',
    'amount' => '100',
    'currency' => 'SEK',
    'message' => 'A purchase of my product',
]);

$paymentResponse = $client->create($payment);

// Create QR code
$qr = new QR([
    'token' => $paymentResponse->paymentRequestToken
]);

$qrResponse = $client->create($qr);
```

With this response you could for instance save the image:

```
file_put_contents(__DIR__ . '/swish/qr.png', $qrResponse->data);
```

Or output it as base64 with the included helper:

```
