Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Payone Sdk Http Message Laravel Package

andrepayone/payone-sdk-http-message

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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.

  2. 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);
    
  3. Where to Look First

    • Factory Class: PayoneHttpFactory (for creating PSR-7 messages).
    • PAYONE SDK Docs: https://github.com/Cakasim/php-payone-sdk (to understand request/response structures).
    • PSR-7 Standards: Familiarize yourself with PSR-7 for request/response handling.

Implementation Patterns

Core Workflows

  1. 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]));
    
  2. 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()
    );
    
  3. 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);
        }
    }
    
  4. 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']);
    }
    

Integration Tips

  • 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);
    }
    

Gotchas and Tips

Pitfalls

  1. 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');
    
  2. Stream Handling Body streams must be rewound after reading:

    $body = $response->getBody();
    $data = $body->getContents();
    $body->rewind(); // Critical for re-reading
    
  3. Laravel Request Conversion Laravel’s toPsrRequest() may not preserve all headers. Manually add missing ones:

    $psr7Request = $request->toPsrRequest()
        ->withHeader('Content-Type', 'application/json');
    
  4. PHP 8.1+ Requirements The package requires PHP 8.1+. Older Laravel versions (e.g., 8.x) may need:

    • PHP 8.1 runtime.
    • Polyfills for missing features (e.g., Stringable interface).
  5. PAYONE SDK Version Lock Ensure compatibility between this package and the PAYONE SDK. Check the SDK’s composer.json for required versions.

Debugging Tips

  • 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
    ]);
    

Extension Points

  1. 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');
        }
    }
    
  2. 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;
        }
    }
    
  3. Laravel Service Provider Bind the factory and PAYONE client in Laravel’s service container:

    public function register()
    {
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky