Download the PHP package olssonm/swish-php without Composer
On this page you can find all versions of the php package olssonm/swish-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download olssonm/swish-php
More information about olssonm/swish-php
Files in olssonm/swish-php
Package swish-php
Short Description Swish API-wrapper. Compatible with Laravel
License MIT
Homepage https://github.com/olssonm/swish-php
Informations about the package swish-php
Swish PHP
A simple and easy to use wrapper for the Swish-API in PHP. Also includes providers and facades for quick setup with Laravel.
Prerequisites
This package supports PHP ^8.1. Tested against Laravel 10 & 11. 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.
Installation
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 Similator-environment without being a Swish-customer during development.
Read more about testing in their MSS-environment in their official documentation. A quick rundown on using/creating Swish-certificates is published here (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:
[!IMPORTANT]
The paths to the certificates should be absolute. You can userealpath -s YOUR_CERT.pem
for this.
Laravel
With the Laravel service provider and facades you can work with the package more eloquently. Just require the package and publish the configuration:
In /config/swish.php
, you can then set your details accordingly:
This may also be a good place to keep you payee-alias, callback-url and such, which you can then access with config('swish.payee_alias)
etc.
Usage
A typical case for creating a Swish-payment.
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
)
Payments and Refunds
[!TIP] Read more about payments and refunds 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:
Payouts
[!TIP] Read more about payouts in the official documentation
Payouts need to be hashed using SHA512 and signed with a signing certificate before being sent to Swish – don't worry though, this package will handle most of this automatically. Just make sure that the path to your signing certificate is set:
Additionally your certificate's (note: your signing certificate, not your client certificate) serial needs to be supplied. You can either use the Certificate
-class to handle on the fly:
Or assign it yourself (see gist for extracting serial):
[!IMPORTANT]
Note that Payouts usespayoutInstructionUUID
instead of anID
, you should set this yourself to keep track of it. If it's missing, it will be set automatically upon creation.
Regarding IDs/UUIDs
This package uses the v2 of the Swish API where a UUID is set by the merchant. This package handles all these aspects automatically as needed, you may however choose to manually set the ID/instructionUUID (either in Swish's own format, or a default v4-format):
When generating the UUIDs on the fly, the package uses Ramsey/Uuid to generate RFC4122 (v4) UUIDs. Swish accepts V1, 3, 4 and 5 UUIDs if you chose to set your own UUIDs.
If an invalid UUID is used, a Olssonm\Swish\Exceptions\InvalidUuidException
will be thrown.
[!NOTE] No matter if you set your own UUID och let the package handle the generation, the UUID will always be formatted for Swish automatically (dashes removed and in uppercase).
Available methods
This package handles the most common Swish-related tasks; retrieve, make and cancel payments. Retrieve and create payouts, aswell as refunds can be created and retrieved. All of them are performed via Olssonm\Swish\Client
;
Exception-handling
When encountering a validation-error an Olssonm\Swish\Exceptions\ValidationException
will be thrown. The Object will contain both the request, response as well as the getErrorCode()
and
getErrorMessage()
-helpers.
For 4xx
-error a \Olssonm\Swish\Exceptions\ClientException
will be thrown, and for 5xx
-errors \Olssonm\Swish\Exceptions\ServerException
. Both of these implements Guzzles BadResponseException
which makes the request- and response-objects available if needed.
Callbacks
Swish recommends to not use the payments
-endpoint to get the status of a payment or refund (even if they themselves use it in some of their examples...), but instead use callbacks.
This package includes a simple helper to retrieve a Payment
, Refund
or Payout
object from a callback that will contain all data from Swish:
The helper automatically retrieve the current HTTP-request (via file_get_contents('php://input')
). You may however inject your own data if needed (or if you for example has a Laravel request-object ready):
In a real world scenario you probably want to use separate callback-urls for your refunds, payouts and payments to prevent unnecessary parsing as the example above.
[!CAUTION] Please note that the callback from Swish is not encrypted or encoded in any way , instead you should make sure that the callback is coming from a valid IP-range.
License
The MIT License (MIT). Please see the LICENSE for more information.
© 2022-2024 Marcus Olsson.
All versions of swish-php with dependencies
guzzlehttp/guzzle Version ^6.3.1 || ^7.0
nesbot/carbon Version ^2 || ^3
ramsey/uuid Version ^4.2