. * * @package Simple PHP Integration * @link https://www.bspayone.com/ * @copyright (C) BS PAYONE GmbH 2016, 2018 * @author Florian Bender * @author Timo Kuchel * @author Hannes Reinberger */ namespace App\Services; //require 'vendor/autoload.php'; use Exception; use GuzzleHttp\Client; use Psr\Http\Message\ResponseInterface; /** * Class Payone */ class Payone { /** * The URL of the Payone API */ const PAYONE_SERVER_API_URL = 'https://api.pay1.de/post-gateway/'; const PAYONE_CLIENT_API_URL = 'https://secure.pay1.de/client-api//'; /** * performing the HTTP POST request to the PAYONE platform * * @param array $request * @param string $responsetype * @throws Exception * @return array|\Psr\Http\Message\StreamInterface Returns an array of response * parameters in "classic" mode, a Stream for any other mode. */ 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 Client(['headers' => ['accept' => 'application/json']]); } else { // if $responsetype is set to anything else than "json", use the standard request $client = new 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'){ // if the content type is text/plain, parse response into array $return = self::parseResponse($response); } else { // if the content type is anything else, just return the response body $return = $response->getBody(); } } 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"; echo "
"; */ return $return; } /** * gets response string an puts it into an array * * @param \Psr\Http\Message\ResponseInterface $response * @throws Exception * @return array */ public static function parseResponse(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; } }