GithubHelp home page GithubHelp logo

swish-php's Introduction

Swish-PHP

Swish-PHP is a small wrapper for the swish merchant api. See https://www.getswish.se/handel/ for more information.

Dependencies

  • php 7.3 or newer with curl & openssl
  • composer

Upgrade notes

3.0.0

  • The function createPaymentRequest now returns an object with the two properties id and paymentRequestToken. In earlier versions the id was returned directly.

Installation via git

git clone https://github.com/helmutschneider/swish-php.git
composer install

Installation via composer

composer require helmutschneider/swish-php:^3.0

Obtaining test certificates

As of 2022-05-29, a test certificate bundle (mss_test_1.9.2.zip) can be found by visiting: https://developer.swish.nu/documentation/environments#certificates

Begin by obtaining the SSL certificates required by Swish. The Swish server itself uses a self-signed root certificated so a CA-bundle to verify its origin is needed. You will also need a client certificate and corresponding private key so the Swish server can identify you.

As of 2022-05-29 the test certificates are called Swish_Merchant_TestCertificate_1234679304.key, Swish Merchant Test Certificate 1231181189.pem and Swish_TLS_RootCA.pem. You must concatenate Swish_Merchant_TestCertificate_1234679304.key and Swish Merchant Test Certificate 1231181189.pem together, otherwise they will not work with cURL. This bundle is your client certificate.

Usage

The client closely mirrors the swish api:

class Client
{

    /**
     * @param PaymentRequest $request
     * @return CreatePaymentRequestResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws ValidationException
     */
    public function createPaymentRequest(PaymentRequest $request): CreatePaymentRequestResponse;
    
    /**
     * @param string $id Payment request id
     * @return PaymentRequest
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getPaymentRequest(string $id): PaymentRequest;
    
    /**
     * @param Refund $refund
     * @return string refund id
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws ValidationException
     */
    public function createRefund(Refund $refund): string;
    
    /**
     * @param string $id Refund id
     * @return Refund
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getRefund(string $id): Refund;
}

When you have the SSL certificates ready you may instantiate the client:

use HelmutSchneider\Swish\Client;
use HelmutSchneider\Swish\PaymentRequest;

// Swish CA root cert
$rootCert = 'path/to/swish-root.crt'; // forwarded to guzzle's "verify" option

// .pem-bundle containing your client cert and it's corresponding private key. forwarded to guzzle's "cert" option
// you may use an empty string for "password" if you are using the test certificates.
$clientCert = ['path/to/client-cert.pem', 'password'];

// Create a client for the production environment. To connect to the test environment
// instead, you must pass the constant Client::SWISH_TEST_URL as the third parameter.
$client = Client::make($rootCert, $clientCert);

$pr = new PaymentRequest([
    'callbackUrl' => 'https://localhost/swish',
    'payeePaymentReference' => '12345',
    'payerAlias' => '4671234768',
    'payeeAlias' => '1231181189',
    'amount' => '100',
])

$res = $client->createPaymentRequest($pr);

var_dump($res->id);

//
//  string(32) "0D3AD8F1AE484A57B82A87FAB8C602EB"
//

Notes for OSX

The bundled PHP in OSX 10.12 and earlier is not compatible with the above approach of forwarding SSL certificates. You must obtain a PHP-version that is compiled with cURL linked against OpenSSL or similar.

Run the tests

To run the tests you need certificates for the Swish test server. Place the root certificate in tests/_data/root.pem and the client certificate in tests/_data/client.pem.

vendor/bin/phpunit

swish-php's People

Contributors

dhedberg avatar helmutschneider avatar livijn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swish-php's Issues

Getting cURL error 7

Hello,

I am getting this errow when trying to use the payment request:

Fatal error: Uncaught exception 'GuzzleHttp\Exception\ConnectException' with message 'cURL error 7: (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in /home/www/youroffice.in/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186 Stack trace: #0 /home/www/youroffice.in/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(150): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 /home/www/youroffice.in/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(103): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2 /home/www/youroffice.in/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 /home/www/youroffice.in/swish-php/vendor/guzzlehttp/guzzle/src/Prep in /home/www/youroffice.in/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 186

So please suggest what is going wrong with my code/server.

Thanks

Php 8.1 and Guzzle 8

Hello, how far away are we from supporting Php 8.1 and Guzzle version more than 7?

Working against production endpoint but failing on test

I am trying to test this but get problems when trying to do it against test endpoint.
If I use it against test endpoint i get:

cURL error 60: SSL certificate problem: self signed certificate in certificate chain (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

if i set verify to false(rootcert) i get other errors.

When using production URL it works fine, it just gets expensive testing.
Do you have any hints on what i can be missing?


$rootCert = storage_path('certs/root.pem');
		$clientCert = null;//[storage_path('certs/swish.pem'),null];

		$client = Client::make($rootCert, $clientCert, Client::SWISH_TEST_URL);
		try {
            $response = $client->createPaymentRequest(
                [
                    'callbackUrl'           => 'https://atb.dalnix.systems/swishdebug',
                    'payeePaymentReference' => $order->id,
                    'payerAlias'            => $phoneNo,
                    'payeeAlias'            => env('SWISH_PAYEE_NR', null),
                    'amount'                => 1,//$order->price,
                    'currency'              => 'SEK',
                    'message'               => 'Beställning'

                ]
            );
        }
        catch(\GuzzleHttp\Exception\ClientException $e){
		    dd($e, $phoneNo);
        }

Getting cURL error 35

Really appreciate this repo, their API documentation is somewhat over my head.

However cant really get it to work locally. My implementation:

$pr = new PaymentRequest([
      'callbackUrl' => 'https://localhost/swish',
      'payeePaymentReference' => $order_id,
      'payerAlias' => $customer_phone,
      'payeeAlias' => '1231181189',
      'amount' => 1, //$total
      'currency' => 'SEK',
      'message' => 'Köp'
    ]);

    $rootCert = ABSPATH .'certs/root.pem';
    $clientCert = [ABSPATH . 'certs/swish.pem', ''];

    $client = Client::make($rootCert, $clientCert,  Client::SWISH_TEST_URL);
  
    $id = $client->createPaymentRequest($pr);

My error:

[11-Jan-2019 10:08:14 UTC] PHP Fatal error:  Uncaught GuzzleHttp\Exception\ConnectException: cURL error 35: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in /Users/home/www/rfsu/wp-content/themes/rfsu/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:185
Stack trace:
#0 /Users/home/www/rfsu/wp-content/themes/rfsu/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(149): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array)
#1 /Users/home/www/rfsu/wp-content/themes/rfsu/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(102): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#2 /Users/home/www/rfsu/wp-content/themes/rfsu/api/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(Gu in /Users/home/www/rfsu/wp-content/themes/rfsu/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 185```

CURL Error when trying to create a payment request with test certificates

When i try to send a create payment request using this code

 $rootCert = '../storage/app/certs/Swish_TLS_RootCA.pem';
$clientCert = ['../storage/app/certs/client-cert.pem', ''];

$client = Client::make($rootCert, $clientCert);

$pr = new PaymentRequest([
    'callbackUrl' => env('APP_URL') . '/api/swishcallback',
    'payeePaymentReference' => '12345',
    'payerAlias' => $this->swishNumber,
    'payeeAlias' => '1231181189',
    'amount' => '100',
]);

$res = $client->createPaymentRequest($pr);

var_dump($res->id);

i get

cURL error 56: OpenSSL SSL_read: error:0A000119:SSL routines::decryption failed or bad record mac, errno 0 (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests

the client-cert.pem is a file where i've created which is a concatenation of Swish_Merchant_TestCertificate_1234679304.key and Swish Merchant Test Certificate 1231181189.pem where the .key file is added first.

Production certificate

Hi Helmut !

Your code works fine on test and returns a token. I have also created a production Key and CSR according to:
openssl req -nodes -newkey rsa:4096 -keyout MYKEY.key -out MYCSR.csr
With MYCSR.csr I created a certificate on SWISH cert page.
I then conatinated MYKEY.key with the created cerificate hierarchy (Key + 3 cert)
into a file client.pem but I do not have anything for my root.pem, and Swedbank does not require a root.pem. So can I just fill in the root.pen with an empty file or have I missed something ?

I get lost after I created the CERT (3 certs output in one strong) on SWISH cert page and don't know if I choosed the right Key to concat and how I shall deal with the file root.pem

possible to pay to my customers/consumers ?

In Swish API , is this possible to pay to users/customers/consumers who ever visit my site, login and ready to sell their products/items.

Please let me know if this possible and how i could achieve this???

Thanks in advance !!!

cURL error 60: SSL certificate problem: self signed certificate in certificate chain

I downloaded the test-keys from https://www.getswish.se/content/uploads/2015/06/Guide_Testverktyg_160118.zip

I extracted them from the p12 using extract.sh ..
Result was ca.crt and cl.pem.

My code looks like this:

<?php 
require_once(__DIR__.'/vendor/swish-php/vendor/autoload.php');


use HelmutSchneider\Swish\Client;
use HelmutSchneider\Swish\Util;

// Swish CA root cert
$rootCert = __DIR__.'/../swish/cert_test/ca.crt'; // forwarded to guzzle's "verify" option

// .pem-bundle containing your client cert and it's corresponding private key. forwarded to guzzle's "cert" option
$clientCert = [__DIR__.'/../swish/cert_test/cl.pem', 'swish'];

$client = Client::make($rootCert, $clientCert);

$response = $client->createPaymentRequest([
                                              'callbackUrl' => 'https://localhost/swish',
                                              'payeePaymentReference' => '12345',
                                              'payerAlias' => '4671234768',
                                              'payeeAlias' => '1231181189',
                                              'amount' => '100',
                                              'currency' => 'SEK',
                                          ]);

$data = Util::decodeResponse($response);
var_dump($data);

This throws the following exception:

PHP Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: self signed certificate in certificate chain (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in /home/robert/work/tomtebrevet/tomtebrevet.se/vendor/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:187
Stack trace:

0 /home/robert/work/tomtebrevet/tomtebrevet.se/vendor/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(150): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array)

1 /home/robert/work/tomtebrevet/tomtebrevet.se/vendor/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(103): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))

2 /home/robert/work/tomtebrevet/tomtebrevet.se/vendor/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHan in /home/robert/work/tomtebrevet/tomtebrevet.se/vendor/swish-php/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 187

Empty return on callback

When I'm performing the payment request my browser prints "NULL" while I get the payment request in my Swish app.

However when the payment is done, Swish returns an empty _POST-array in the callback. I've logged it to database and this is what it returns: https://gyazo.com/ed84a84205992626295d514028791fad

What could be the issue? Thanks in advance!

File types

I seem to only get CSR, KEY and PEM/P7B-files according to Swish manual.
How do I use these files with your application? Is there any tutorial out how I can convert them to the right file formats?

Thanks in advance!

empty/null response

My code:

<?php
require_once(__DIR__.'/vendor/swish-php/vendor/autoload.php');


use HelmutSchneider\Swish\Client;
use HelmutSchneider\Swish\Util;

// Swish CA root cert
$rootCert = __DIR__.'/../swish/cert_test/ca.crt'; // forwarded to guzzle's "verify" option

// .pem-bundle containing your client cert and it's corresponding private key. forwarded to guzzle's "cert" option
$clientCert = [__DIR__.'/../swish/cert_test/cl.pem', 'swish'];

$client = Client::make($rootCert, $clientCert, Client::SWISH_TEST_URL);

$response = $client->createPaymentRequest([
											  'callbackUrl' => 'https://localhost/swish',
											  'payeePaymentReference' => '12345',
											  'payerAlias' => '4671234768',
											  'payeeAlias' => '1231181189',
											  'amount' => '100',
											  'currency' => 'SEK',
										  ]);

$data = Util::decodeResponse($response, true);
var_dump($response->getBody()); // empty stream 
var_dump($response); // show "Created" and statusCode 201 .. so seems successful?!
var_dump($data); // NULL

Response in command line:

/home/robert/work/tomtebrevet/tomtebrevet.se/tswish.php:26:
class GuzzleHttp\Psr7\Response#28 (6) {
  private $reasonPhrase =>
  string(7) "Created"
  private $statusCode =>
  int(201)
  private $headers =>
  array(4) {
    'Server' =>
    array(1) {
      [0] =>
      string(17) "Apache-Coyote/1.1"
    }
    'Location' =>
    array(1) {
      [0] =>
      string(100) "https://mss.swicpc.bankgirot.se/swish-cpcapi/api/v1/paymentrequests/D890F10E3AB047C39C3B2DB31605BC4F"
    }
    'Content-Length' =>
    array(1) {
      [0] =>
      string(1) "0"
    }
    'Date' =>
    array(1) {
      [0] =>
      string(29) "Wed, 26 Jul 2017 09:57:28 GMT"
    }
  }
  private $headerNames =>
  array(4) {
    'server' =>
    string(6) "Server"
    'location' =>
    string(8) "Location"
    'content-length' =>
    string(14) "Content-Length"
    'date' =>
    string(4) "Date"
  }
  private $protocol =>
  string(3) "1.1"
  private $stream =>
  class GuzzleHttp\Psr7\Stream#26 (7) {
    private $stream =>
    resource(44) of type (stream)
    private $size =>
    NULL
    private $seekable =>
    bool(true)
    private $readable =>
    bool(true)
    private $writable =>
    bool(true)
    private $uri =>
    string(10) "php://temp"
    private $customMetadata =>
    array(0) {
    }
  }
}
/home/robert/work/tomtebrevet/tomtebrevet.se/tswish.php:27:
NULL

Any idea why I don't get a full response?

Publish to Packagist

We should publish this package to Packagist so you can easily require this via composer, e.g.: composer require helmutschneider/swish-php.

Concatenate certificates

You write in the instructions "You must concatenate Swish Merchant Test Certificate 1231181189.key and Swish Merchant Test Certificate 1231181189.pem"

I was about to do that with cat but what should extension should the new file have? crt?

need workaround for host without openssl

SSL Version = NSS/3.27.1

Any workaround / idea on how to make it work with this? I could make a pull request with an auto-detection / setting for this as a way to give back if you can give some hint on how to make it work...

right now its giving "CURL error 35: SSL connect error"

Any help much appriciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.