andrepayone/payone-sdk-http-message
Install the Package Require the package in your Laravel project:
composer require andrepayone/payone-sdk-http-message
Ensure your composer.json includes PHP 8.1+ and dependencies like psr/http-message.
First Use Case: Integrate with PAYONE SDK Use the package to create PSR-7 compliant HTTP messages for the PAYONE SDK. Example:
use Andrepayone\PayoneSdk\HttpMessage\Factory\PayoneHttpFactory;
use Payone\Sdk\Client;
// Initialize the factory
$httpFactory = new PayoneHttpFactory();
// Create a PAYONE client with PSR-7 support
$payoneClient = new Client($httpFactory, [
'username' => 'your_username',
'password' => 'your_password',
'endpoint' => 'https://api.payone.com',
]);
// Example: Create an authorization request
$request = $payoneClient->createAuthorizationRequest([
'amount' => 100.00,
'currency' => 'EUR',
'order_id' => 'order_123',
]);
// Send the request (returns a PSR-7 Response)
$response = $payoneClient->sendRequest($request);
Where to Look First
PayoneHttpFactory (for creating PSR-7 messages).PSR-7 Request Creation Use the factory to create requests for PAYONE API calls:
$factory = new PayoneHttpFactory();
$request = $factory->createRequest('POST', '/payment');
$request->getBody()->write(json_encode(['amount' => 50.00]));
Laravel Integration Bridge PSR-7 messages with Laravel’s HTTP layer:
use Illuminate\Http\Request as LaravelRequest;
// Convert Laravel Request to PSR-7
$psr7Request = LaravelRequest::capture()->toPsrRequest();
// Process with PAYONE SDK
$response = $payoneClient->sendRequest($psr7Request);
// Convert PSR-7 Response to Laravel Response
return new Illuminate\Http\Response(
$response->getBody(),
$response->getStatusCode(),
$response->getHeaders()
);
Middleware for PAYONE Requests Add middleware to handle PAYONE-specific logic (e.g., logging, auth):
use Closure;
use Psr\Http\Message\RequestInterface;
class PayoneRequestMiddleware
{
public function handle(RequestInterface $request, Closure $next): ResponseInterface
{
// Pre-process request (e.g., add headers)
$request = $request->withHeader('X-Payone-API', 'v1');
// Log the request
\Log::info('PAYONE Request:', $request->getBody());
return $next($request);
}
}
Webhook Handling Process PAYONE webhook callbacks in Laravel:
public function handleWebhook(Request $request)
{
$psr7Request = $request->toPsrRequest();
$webhookData = json_decode($psr7Request->getBody(), true);
// Validate and process webhook
if ($this->validateWebhook($webhookData)) {
$this->processWebhook($webhookData);
}
return response()->json(['status' => 'processed']);
}
Use Laravel’s HTTP Client
Leverage Laravel’s Http facade to send PAYONE requests with PSR-7 support:
use Illuminate\Support\Facades\Http;
$response = Http::withOptions(['decode' => false])
->post('https://api.payone.com/payment', [
'amount' => 100.00,
])
->toPsrResponse(); // Convert to PSR-7 Response
Testing with Mocks Mock PSR-7 messages in tests:
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
$mockRequest = $this->createMock(RequestInterface::class);
$mockRequest->method('getBody')->willReturn(new \GuzzleHttp\Psr7\Stream(fopen('php://memory', 'r+')));
$this->payoneService->processPayment($mockRequest);
Error Handling Catch PAYONE SDK exceptions and convert them to Laravel responses:
try {
$response = $payoneClient->sendRequest($request);
} catch (\Payone\Sdk\Exception\PayoneException $e) {
return response()->json([
'error' => $e->getMessage(),
'code' => $e->getCode(),
], 400);
}
PSR-7 Message Cloning
PSR-7 messages are immutable. Always use with* methods to modify them:
// Wrong: Directly modifying properties
$request->headers['X-Custom'] = 'value'; // Throws error
// Correct: Use withHeader()
$request = $request->withHeader('X-Custom', 'value');
Stream Handling Body streams must be rewound after reading:
$body = $response->getBody();
$data = $body->getContents();
$body->rewind(); // Critical for re-reading
Laravel Request Conversion
Laravel’s toPsrRequest() may not preserve all headers. Manually add missing ones:
$psr7Request = $request->toPsrRequest()
->withHeader('Content-Type', 'application/json');
PHP 8.1+ Requirements The package requires PHP 8.1+. Older Laravel versions (e.g., 8.x) may need:
Stringable interface).PAYONE SDK Version Lock
Ensure compatibility between this package and the PAYONE SDK. Check the SDK’s composer.json for required versions.
Log PSR-7 Messages Use a helper to log request/response bodies:
function logPsrMessage($message) {
\Log::debug('PSR-7 Message', [
'method' => $message->getMethod(),
'uri' => $message->getUri(),
'headers' => $message->getHeaders(),
'body' => $message->getBody()->getContents(),
]);
}
Validate Headers
PAYONE may require specific headers (e.g., Accept, Content-Type). Validate these before sending:
$request = $request->withHeader('Accept', 'application/json')
->withHeader('Content-Type', 'application/json');
Test with Real PAYONE API Use PAYONE’s sandbox environment to test requests/responses:
$payoneClient = new Client($httpFactory, [
'endpoint' => 'https://sandbox.payone.com', // Sandbox URL
]);
Custom Factories
Extend PayoneHttpFactory to add Laravel-specific logic:
class LaravelPayoneFactory extends PayoneHttpFactory
{
public function createRequestFromLaravel(Request $laravelRequest): RequestInterface
{
$psr7Request = parent::createRequest(
$laravelRequest->method(),
$laravelRequest->getUri()
);
return $psr7Request->withHeader('X-Laravel', 'true');
}
}
Middleware for PSR-7 Create middleware to wrap PAYONE requests/responses:
class PayoneLoggingMiddleware
{
public function __invoke(RequestInterface $request, Closure $next): ResponseInterface
{
\Log::info('PAYONE Request', $request->getUri());
$response = $next($request);
\Log::info('PAYONE Response', $response->getStatusCode());
return $response;
}
}
Laravel Service Provider Bind the factory and PAYONE client in Laravel’s service container:
public function register()
{
How can I help you explore Laravel packages today?