allow vendord
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 16s
Build, Push and Deploy / deploy-staging (push) Successful in 32s
Build, Push and Deploy / deploy-production (push) Has been skipped
Build, Push and Deploy / cleanup (push) Successful in 1s

This commit is contained in:
2026-03-30 14:54:57 +07:00
parent 66aed7c4e8
commit b5e3a778ce
21316 changed files with 2777892 additions and 13 deletions

View File

@@ -0,0 +1,230 @@
<?php
namespace Mollie\Api\HttpAdapter;
use Composer\CaBundle\CaBundle;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Exceptions\CurlConnectTimeoutException;
use Mollie\Api\MollieApiClient;
final class CurlMollieHttpAdapter implements MollieHttpAdapterInterface
{
/**
* Default response timeout (in seconds).
*/
public const DEFAULT_TIMEOUT = 10;
/**
* Default connect timeout (in seconds).
*/
public const DEFAULT_CONNECT_TIMEOUT = 2;
/**
* The maximum number of retries
*/
public const MAX_RETRIES = 5;
/**
* The amount of milliseconds the delay is being increased with on each retry.
*/
public const DELAY_INCREASE_MS = 1000;
/**
* @param string $httpMethod
* @param string $url
* @param array $headers
* @param string $httpBody
* @return \stdClass|void|null
* @throws \Mollie\Api\Exceptions\ApiException
* @throws \Mollie\Api\Exceptions\CurlConnectTimeoutException
*/
public function send($httpMethod, $url, $headers, $httpBody)
{
for ($i = 0; $i <= self::MAX_RETRIES; $i++) {
usleep($i * self::DELAY_INCREASE_MS);
try {
return $this->attemptRequest($httpMethod, $url, $headers, $httpBody);
} catch (CurlConnectTimeoutException $e) {
// Nothing
}
}
throw new CurlConnectTimeoutException(
"Unable to connect to Mollie. Maximum number of retries (". self::MAX_RETRIES .") reached."
);
}
/**
* @param string $httpMethod
* @param string $url
* @param array $headers
* @param string $httpBody
* @return \stdClass|void|null
* @throws \Mollie\Api\Exceptions\ApiException
*/
protected function attemptRequest($httpMethod, $url, $headers, $httpBody)
{
$curl = curl_init($url);
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->parseHeaders($headers));
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::DEFAULT_CONNECT_TIMEOUT);
curl_setopt($curl, CURLOPT_TIMEOUT, self::DEFAULT_TIMEOUT);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_CAINFO, CaBundle::getBundledCaBundlePath());
switch ($httpMethod) {
case MollieApiClient::HTTP_POST:
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $httpBody);
break;
case MollieApiClient::HTTP_GET:
break;
case MollieApiClient::HTTP_PATCH:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $httpBody);
break;
case MollieApiClient::HTTP_DELETE:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($curl, CURLOPT_POSTFIELDS, $httpBody);
break;
default:
throw new \InvalidArgumentException("Invalid http method: ". $httpMethod);
}
$startTime = microtime(true);
$response = curl_exec($curl);
$endTime = microtime(true);
if ($response === false) {
$executionTime = $endTime - $startTime;
$curlErrorNumber = curl_errno($curl);
$curlErrorMessage = "Curl error: " . curl_error($curl);
if ($this->isConnectTimeoutError($curlErrorNumber, $executionTime)) {
throw new CurlConnectTimeoutException("Unable to connect to Mollie. " . $curlErrorMessage);
}
throw new ApiException($curlErrorMessage);
}
$statusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
return $this->parseResponseBody($response, $statusCode, $httpBody);
}
/**
* The version number for the underlying http client, if available.
* @example Guzzle/6.3
*
* @return string|null
*/
public function versionString()
{
return 'Curl/*';
}
/**
* Whether this http adapter provides a debugging mode. If debugging mode is enabled, the
* request will be included in the ApiException.
*
* @return false
*/
public function supportsDebugging()
{
return false;
}
/**
* @param int $curlErrorNumber
* @param string|float $executionTime
* @return bool
*/
protected function isConnectTimeoutError($curlErrorNumber, $executionTime)
{
$connectErrors = [
\CURLE_COULDNT_RESOLVE_HOST => true,
\CURLE_COULDNT_CONNECT => true,
\CURLE_SSL_CONNECT_ERROR => true,
\CURLE_GOT_NOTHING => true,
];
if (isset($connectErrors[$curlErrorNumber])) {
return true;
};
if ($curlErrorNumber === \CURLE_OPERATION_TIMEOUTED) {
if ($executionTime > self::DEFAULT_TIMEOUT) {
return false;
}
return true;
}
return false;
}
/**
* @param string $response
* @param int $statusCode
* @param string $httpBody
* @return \stdClass|null
* @throws \Mollie\Api\Exceptions\ApiException
*/
protected function parseResponseBody($response, $statusCode, $httpBody)
{
if (empty($response) && $statusCode >= 200 && $statusCode < 300) {
return null;
}
$body = @json_decode($response);
// GUARDS
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ApiException("Unable to decode Mollie response: '{$response}'.");
}
if (isset($body->error)) {
throw new ApiException($body->error->message);
}
if ($statusCode >= 400) {
$message = "Error executing API call ({$body->status}: {$body->title}): {$body->detail}";
$field = null;
if (! empty($body->field)) {
$field = $body->field;
}
if (isset($body->_links, $body->_links->documentation)) {
$message .= ". Documentation: {$body->_links->documentation->href}";
}
if ($httpBody) {
$message .= ". Request body: {$httpBody}";
}
throw new ApiException($message, $statusCode, $field);
}
return $body;
}
protected function parseHeaders($headers)
{
$result = [];
foreach ($headers as $key => $value) {
$result[] = $key .': ' . $value;
}
return $result;
}
}

View File

@@ -0,0 +1,190 @@
<?php
namespace Mollie\Api\HttpAdapter;
use Composer\CaBundle\CaBundle;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions as GuzzleRequestOptions;
use Mollie\Api\Exceptions\ApiException;
use Psr\Http\Message\ResponseInterface;
final class Guzzle6And7MollieHttpAdapter implements MollieHttpAdapterInterface
{
/**
* Default response timeout (in seconds).
*/
public const DEFAULT_TIMEOUT = 10;
/**
* Default connect timeout (in seconds).
*/
public const DEFAULT_CONNECT_TIMEOUT = 2;
/**
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Whether debugging is enabled. If debugging mode is enabled, the request will
* be included in the ApiException. By default, debugging is disabled to prevent
* sensitive request data from leaking into exception logs.
*
* @var bool
*/
protected $debugging = false;
public function __construct(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
/**
* Instantiate a default adapter with sane configuration for Guzzle 6 or 7.
*
* @return static
*/
public static function createDefault()
{
$retryMiddlewareFactory = new Guzzle6And7RetryMiddlewareFactory;
$handlerStack = HandlerStack::create();
$handlerStack->push($retryMiddlewareFactory->retry());
$client = new Client([
GuzzleRequestOptions::VERIFY => CaBundle::getBundledCaBundlePath(),
GuzzleRequestOptions::TIMEOUT => self::DEFAULT_TIMEOUT,
GuzzleRequestOptions::CONNECT_TIMEOUT => self::DEFAULT_CONNECT_TIMEOUT,
'handler' => $handlerStack,
]);
return new Guzzle6And7MollieHttpAdapter($client);
}
/**
* Send a request to the specified Mollie api url.
*
* @param string $httpMethod
* @param string $url
* @param array $headers
* @param string $httpBody
* @return \stdClass|null
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function send($httpMethod, $url, $headers, $httpBody)
{
$request = new Request($httpMethod, $url, $headers, $httpBody);
try {
$response = $this->httpClient->send($request, ['http_errors' => false]);
} catch (GuzzleException $e) {
// Prevent sensitive request data from ending up in exception logs unintended
if (! $this->debugging) {
$request = null;
}
// Not all Guzzle Exceptions implement hasResponse() / getResponse()
if (method_exists($e, 'hasResponse') && method_exists($e, 'getResponse')) {
if ($e->hasResponse()) {
throw ApiException::createFromResponse($e->getResponse(), $request);
}
}
throw new ApiException($e->getMessage(), $e->getCode(), null, $request, null);
}
return $this->parseResponseBody($response);
}
/**
* Whether this http adapter provides a debugging mode. If debugging mode is enabled, the
* request will be included in the ApiException.
*
* @return true
*/
public function supportsDebugging()
{
return true;
}
/**
* Whether debugging is enabled. If debugging mode is enabled, the request will
* be included in the ApiException. By default, debugging is disabled to prevent
* sensitive request data from leaking into exception logs.
*
* @return bool
*/
public function debugging()
{
return $this->debugging;
}
/**
* Enable debugging. If debugging mode is enabled, the request will
* be included in the ApiException. By default, debugging is disabled to prevent
* sensitive request data from leaking into exception logs.
*/
public function enableDebugging()
{
$this->debugging = true;
}
/**
* Disable debugging. If debugging mode is enabled, the request will
* be included in the ApiException. By default, debugging is disabled to prevent
* sensitive request data from leaking into exception logs.
*/
public function disableDebugging()
{
$this->debugging = false;
}
/**
* Parse the PSR-7 Response body
*
* @param ResponseInterface $response
* @return \stdClass|null
* @throws ApiException
*/
private function parseResponseBody(ResponseInterface $response)
{
$body = (string) $response->getBody();
if (empty($body) && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
return null;
}
$object = @json_decode($body);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ApiException("Unable to decode Mollie response: '{$body}'.");
}
if ($response->getStatusCode() >= 400) {
throw ApiException::createFromResponse($response, null);
}
return $object;
}
/**
* The version number for the underlying http client, if available. This is used to report the UserAgent to Mollie,
* for convenient support.
* @example Guzzle/6.3
*
* @return string|null
*/
public function versionString()
{
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { // Guzzle 7
return "Guzzle/" . ClientInterface::MAJOR_VERSION;
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { // Before Guzzle 7
return "Guzzle/" . ClientInterface::VERSION;
}
return null;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Mollie\Api\HttpAdapter;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
class Guzzle6And7RetryMiddlewareFactory
{
/**
* The maximum number of retries
*/
public const MAX_RETRIES = 5;
/**
* The amount of milliseconds the delay is being increased with on each retry.
*/
public const DELAY_INCREASE_MS = 1000;
/**
* @param bool $delay default to true, can be false to speed up tests
*
* @return callable
*/
public function retry($delay = true)
{
return Middleware::retry(
$this->newRetryDecider(),
$delay ? $this->getRetryDelay() : $this->getZeroRetryDelay()
);
}
/**
* Returns a method that takes the number of retries and returns the number of milliseconds
* to wait
*
* @return callable
*/
private function getRetryDelay()
{
return function ($numberOfRetries) {
return static::DELAY_INCREASE_MS * $numberOfRetries;
};
}
/**
* Returns a method that returns zero milliseconds to wait
*
* @return callable
*/
private function getZeroRetryDelay()
{
return function ($numberOfRetries) {
return 0;
};
}
/**
* @return callable
*/
private function newRetryDecider()
{
return function (
$retries,
Request $request,
?Response $response = null,
?TransferException $exception = null
) {
if ($retries >= static::MAX_RETRIES) {
return false;
}
if ($exception instanceof ConnectException) {
return true;
}
return false;
};
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Mollie\Api\HttpAdapter;
interface MollieHttpAdapterInterface
{
/**
* Send a request to the specified Mollie api url.
*
* @param string $httpMethod
* @param string $url
* @param string|array $headers
* @param string $httpBody
* @return \stdClass|null
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function send($httpMethod, $url, $headers, $httpBody);
/**
* The version number for the underlying http client, if available.
* @example Guzzle/6.3
*
* @return string|null
*/
public function versionString();
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Mollie\Api\HttpAdapter;
use Mollie\Api\Exceptions\UnrecognizedClientException;
class MollieHttpAdapterPicker implements MollieHttpAdapterPickerInterface
{
/**
* @param \GuzzleHttp\ClientInterface|\Mollie\Api\HttpAdapter\MollieHttpAdapterInterface|null|\stdClass $httpClient
*
* @return \Mollie\Api\HttpAdapter\MollieHttpAdapterInterface
* @throws \Mollie\Api\Exceptions\UnrecognizedClientException
*/
public function pickHttpAdapter($httpClient)
{
if (! $httpClient) {
if ($this->guzzleIsDetected()) {
$guzzleVersion = $this->guzzleMajorVersionNumber();
if ($guzzleVersion && in_array($guzzleVersion, [6, 7])) {
return Guzzle6And7MollieHttpAdapter::createDefault();
}
}
return new CurlMollieHttpAdapter;
}
if ($httpClient instanceof MollieHttpAdapterInterface) {
return $httpClient;
}
if ($httpClient instanceof \GuzzleHttp\ClientInterface) {
return new Guzzle6And7MollieHttpAdapter($httpClient);
}
throw new UnrecognizedClientException('The provided http client or adapter was not recognized.');
}
/**
* @return bool
*/
private function guzzleIsDetected()
{
return interface_exists('\\' . \GuzzleHttp\ClientInterface::class);
}
/**
* @return int|null
*/
private function guzzleMajorVersionNumber()
{
// Guzzle 7
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
return (int) \GuzzleHttp\ClientInterface::MAJOR_VERSION;
}
// Before Guzzle 7
if (defined('\GuzzleHttp\ClientInterface::VERSION')) {
return (int) \GuzzleHttp\ClientInterface::VERSION[0];
}
return null;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Mollie\Api\HttpAdapter;
interface MollieHttpAdapterPickerInterface
{
/**
* @param \GuzzleHttp\ClientInterface|\Mollie\Api\HttpAdapter\MollieHttpAdapterInterface $httpClient
*
* @return \Mollie\Api\HttpAdapter\MollieHttpAdapterInterface
*/
public function pickHttpAdapter($httpClient);
}