paypal/paypalhttp
Deprecated PayPalHttp SDK: a lightweight generic PHP HTTP client for PayPal-style REST APIs. Provides Environment base URL handling, request/response objects, injectors for pre-flight logic (logging, auth), and exception-based error handling.
HttpClient abstraction fits Laravel’s service-oriented architecture, allowing integration as a standalone HTTP client or as part of a larger payment service layer. Its decoupled design (e.g., Environment, HttpRequest) enables seamless extension or replacement without refactoring core business logic.PayPalRequestInjector to add auth headers or transform responses.Psr\Http\Client\ClientInterface), enabling dependency injection and mocking for tests. Example:
$this->app->bind(HttpClient::class, function () {
return new HttpClient(new Environment(config('paypal.environment')));
});
sandbox vs. live) via Laravel’s config system, reducing hardcoded values.ext-curl, which is standard in Laravel but may need explicit enabling in Docker or serverless environments. Test early in CI/CD pipelines.IOException may not integrate seamlessly with Laravel’s exception hierarchy. Plan to wrap exceptions in Laravel-specific classes (e.g., PayPalException extending HttpException).phpunit/phpunit@^9.5 for compatibility.Http facade to mock PayPal responses.public function test_paypal_client_returns_transaction()
{
$mockResponse = new HttpResponse(200, [], json_encode(['id' => '123']));
$this->app->instance(HttpClient::class, $this->createMock(HttpClient::class)
->method('execute')
->willReturn($mockResponse));
$service = new PayPalService(app(HttpClient::class));
$result = $service->getTransaction('123');
$this->assertEquals('123', $result->id);
}
HttpClient. Profile critical paths (e.g., checkout flows).HttpClient? If not, justify the technical debt.HttpClient.429 Too Many Requests) be translated to Laravel’s exception system? Example: Extend IOException to implement ShouldReport for Laravel’s error pages.Monolog) or APM tools (e.g., New Relic)? Example:
$client->addInjector(new class implements Injector {
public function inject(HttpRequest $request) {
\Log::debug('PayPal Request', [
'url' => $request->path,
'method' => $request->verb,
'headers' => $request->headers,
]);
}
});
PayPal-Request-Id) or response validations? Document these in a PayPalHttpService wrapper.HttpClient or Guzzle? Example migration path:
- $client = new HttpClient(new Environment('https://api.paypal.com'));
+ $client = Http::withOptions(['base_uri' => 'https://api.paypal.com']);
HttpClient to an interface (e.g., PayPalHttpClientInterface) for testability and loose coupling.sandbox, live) in config/paypal.php:
'environments' => [
'sandbox' => 'https://api.sandbox.paypal.com',
'live' => 'https://api.paypal.com',
],
PayPal facade to simplify usage in controllers:
use Illuminate\Support\Facades\Facade;
class PayPal extends Facade {
protected static function getFacadeAccessor() { return 'paypal.http'; }
}
Usage:
$response = PayPal::execute(new HttpRequest('/v1/payments/payment', 'GET'));
HttpClient: Prefer for most use cases (built-in JSON handling, retries, middleware). Example:
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . config('paypal.token'),
])->post('https://api.paypal.com/v1/payments/payment', $data);
guzzlehttp/guzzle package.Phase 1: Assessment (1–2 weeks)
GET /v1/transactions): Replace with Laravel’s HttpClient.paypal/paypalhttp (e.g., custom header formatting).git grep or IDE searches for HttpClient, HttpRequest, or paypalhttp.Phase 2: Wrapper Implementation (2–3 weeks)
PayPalHttpService wrapper to abstract the deprecated package:
class PayPalHttpService {
public function __construct(private HttpClient $client) {}
public function execute(HttpRequest $request): HttpResponse {
try {
$response = $this->client->execute($request);
return new PayPalResponse($response);
} catch (IOException $e) {
throw new PayPalException(
"PayPal API Error: {$e->getMessage()}",
$e->getCode(),
$e->getResponse()
);
}
}
}
AppServiceProvider:
$this->app->singleton(PayPalHttpService::class, function ($app) {
return new PayPalHttpService(new HttpClient(
new Environment(config('paypal.environment'))
));
});
Phase 3: Incremental Replacement (Ongoing)
- $client = new HttpClient(new Environment(config('paypal.environment')));
- $request = new HttpRequest('/v1/reportings/transactions', 'GET');
- $response = $client->execute($request);
+ $response = Http::get('
How can I help you explore Laravel packages today?