helmutschneider / swish-php Goto Github PK
View Code? Open in Web Editor NEWPHP-wrapper for the Swish merchant api
License: MIT License
PHP-wrapper for the Swish merchant api
License: MIT License
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
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.
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!
We should publish this package to Packagist so you can easily require this via composer, e.g.: composer require helmutschneider/swish-php
.
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?
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);
}
Hi.
I am very interesting for your posting.
Now I am doing Swish payment integration.
But now this error is appeared.
"SSL peer was unable to negotiate an acceptable set of security parameters."
I can't find the reason.
Here is my code for Swish.
If you can help me, I am very happy.
$config = array(
"callbackUrl" => "https://www.borstjanaren.se/borst_shop/shopPaymentType",
"payeeAlias" => "1233144318", //Swish number of the merchant
"currency" => "SEK", //currency code
"CAINFO" => '/var/www/vhosts/borstjanaren.se/httpdocs/swish/cert2782018/root.pem', //Path to root CA
"SSLCERT" => '/var/www/vhosts/borstjanaren.se/httpdocs/swish/cert2782018/swishclientcert.pem', //Path to client certificate
"SSLKEY" => '/var/www/vhosts/borstjanaren.se/httpdocs/swish/cert2782018/swishclientkey.key', //Path to private key*/
"SSLCERTTYPE" => 'PEM'
);
try {
if ($payerAlias != "") {//echo "1==>";
$data = array("payeePaymentReference" => $paymentReference, //'234567892'
"callbackUrl" => $config["callbackUrl"],
"payerAlias" => $payerAlias, //46739866319
"payeeAlias" => $config["payeeAlias"], //'1233144318'
"amount" => $amount, //'1'
"currency" => $config["currency"], //'SEK'
"message" => $message, //'Tack för din betalning!'
"sessiontest" => "session123");
} else {//echo "2==>";
$data = array("payeePaymentReference" => $paymentReference,
"callbackUrl" => $config["callbackUrl"],
"payeeAlias" => $config["payeeAlias"],
"amount" => $amount,
"currency" => $config["currency"],
"message" => $message);
}
$data_string = json_encode($data);
//$ch = curl_init('https://mss.swicpc.bankgirot.se/swish-cpcapi/api/v1/paymentrequests/');
//$ch = curl_init('https://swicpc.bankgirot.se/swish-cpcapi/api/v1/paymentrequests/');
$ch = curl_init('https://cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
////curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Uncomment this if you didn't add the root CA, curl will then ignore the SSL verification error.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
curl_setopt($ch, CURLOPT_CAINFO, $config["CAINFO"]);
curl_setopt($ch, CURLOPT_SSLCERT, $config["SSLCERT"]);
curl_setopt($ch, CURLOPT_SSLKEY, $config["SSLKEY"]);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $config["SSLCERTPASSWD"]);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $config["SSLKEYPASSWD"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//////
curl_setopt($ch, CURLOPT_VERBOSE, 0);////
curl_setopt($ch, CURLOPT_SSLVERSION, 4);////
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);////
$result = curl_exec($ch);
if (FALSE === $result)
throw new Exception(curl_error($ch), curl_errno($ch));
$headers = explode("\r\n", $result);
$headerArr = array();
foreach ($headers as $headerline) {
$lineArr = explode(": ", $headerline, 2);
$val = null;
if (sizeof($lineArr) > 1) {
$val = $lineArr[1];
}
$headerArr[$lineArr[0]] = $val;
}
if (array_key_exists("Location", $headerArr)) {
$locationURL = $headerArr["Location"];
$transactionId = explode("/", $locationURL)[sizeOf(explode("/", $locationURL)) - 1];
} else {
//Error: no location header present in response
header('Content-Type: application/json');
//return $headers[sizeof($headers) - 1];
//return $this->renderText('{"errorCode":"' . $headers[sizeof($headers) - 1] . '"}');
return $this->renderText($headers[sizeof($headers) - 1]);
}
// Store transactionId in the current order
header('Content-Type: application/json');
//return '{"transactionId":"' . $transactionId . '","transactionURL":"' . $locationURL . '"}';
return $this->renderText('{"transactionId":"' . $transactionId . '","transactionURL":"' . $locationURL . '"}');
} catch (Exception $e) {
//$errVar = trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
//return '{"errorMessage":"' . $e->getMessage() . '"}';
return $this->renderText('{"errorCode":"' . $e->getMessage() . '"}');
}
HELP ME!
I think you can help me.
I will pay for your effort.
Thank you.
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
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```
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!
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.
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 !!!
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
Hello, how far away are we from supporting Php 8.1 and Guzzle version more than 7?
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.