This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPayone.php
115 lines (106 loc) · 4.14 KB
/
Payone.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* This class is a wrapper to be able to send arrays of Payone request
* to the Payone platform.
*
* Payone Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Payone Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Payone Connector. If not, see <http://www.gnu.org/licenses/>.
*
* @package Simple PHP Integration
* @link https://www.bspayone.com/
* @copyright (C) BS PAYONE GmbH 2016, 2018
* @author Florian Bender <florian.bender@bspayone.com>
* @author Timo Kuchel <timo.kuchel@bspayone.com>
* @author Hannes Reinberger <hannes.reinberger@bspayone.com>
*/
require 'vendor/autoload.php';
/**
* Class Payone
*/
class Payone
{
/**
* The URL of the Payone API
*/
const PAYONE_SERVER_API_URL = 'https://api.pay1.de/post-gateway/';
/**
* performing the HTTP POST request to the PAYONE platform
*
* @param array $request
* @param string $responsetype
* @return array|\Psr\Http\Message\StreamInterface Returns an array of response
* parameters in "classic" mode, a Stream for any other mode.
* @throws Exception
*/
public static function sendRequest($request, $responsetype = "")
{
if ($responsetype === "json") {
// appends the accept: application/json header to the request
// This is used to retrieve structured JSON in the response
$client = new \GuzzleHttp\Client(['headers' => ['accept' => 'application/json']]);
} else {
// if $responsetype is set to anything else than "json", use the standard request
$client = new \GuzzleHttp\Client();
}
echo "Requesting...";
$begin = microtime(true);
if ($response = $client->request('POST', self::PAYONE_SERVER_API_URL, ['form_params' => $request])) {
if (implode($response->getHeader('Content-Type')) == 'text/plain; charset=UTF-8' ||
implode($response->getHeader('Content-Type')) == 'text/plain; charset=ISO-8859-1') {
// if the content type is text/plain, parse response into array
$return = self::parseResponse($response);
} else {
// if the content type is anything else, decode response body and parse into array
// we can safely assume it's JSON because of the way the API currently works
$return = json_decode($response->getBody(), true);
}
} else {
throw new Exception('Something went wrong during the HTTP request.');
}
$end = microtime(true);
$duration = $end - $begin;
echo "done.\n";
echo "Request took " . $duration . " seconds.\n";
return $return;
}
/**
* gets response string an puts it into an array
*
* @param \Psr\Http\Message\ResponseInterface $response
* @return array
* @throws Exception
*/
public static function parseResponse(\Psr\Http\Message\ResponseInterface $response)
{
$responseArray = array();
$explode = explode("\n", $response->getBody());
foreach ($explode as $e) {
$keyValue = explode("=", $e);
if (trim($keyValue[0]) != "") {
if (count($keyValue) == 2) {
$responseArray[$keyValue[0]] = trim($keyValue[1]);
} else {
$key = $keyValue[0];
unset($keyValue[0]);
$value = implode("=", $keyValue);
$responseArray[$key] = $value;
}
}
}
if ($responseArray['status'] == "ERROR") {
$msg = "Payone returned an error:\n" . print_r($responseArray, true);
throw new Exception($msg);
}
return $responseArray;
}
}