In this article, I'd like to focus on working with 3rd party services. In this article, I'll write a Gumroad (e-commerce) SDK in three different ways: Using one service for the whole API.

Get one product by ID: namespace App\Services\Gumroad; class GumroadService { public function product(string $id): ProductData { $product = Http::get( $this->url("products/$id"), $this->query(), )->json('product'); return ProductData::fromArray($product); }}

As you can see, I still use the url and query helpers, but now they are in the base Request class: namespace App\Services\Gumroad\Requests; abstract class Request { public function __construct( protected readonly string $accessToken, protected readonly string $uri, ) {} protected function query(array $extra = []): array { return [ 'access_token' => $this->accessToken, ...$extra, ]; } protected function url(string $path): string { return "{$this->uri}/$path"; }}

To solve these problems we can keep the GumroadService class as an entry point to access these requests: namespace App\Services\Gumroad; class GumroadService { public function sales(?Carbon $after = null): Collection { return app(GetSalesRequest::class)->send($after); } public function products(): Collection { return app(GetProductsRequest::class)->send(); } public function product(string $id): ProductData { return app(GetProductRequest::class)->send($id); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners