Created
March 24, 2024 11:14
-
-
Save karakhanyans/95703b69322479c1968f41aeb8069aba to your computer and use it in GitHub Desktop.
LemonSqueezyServiceProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
use Illuminate\Http\Client\PendingRequest; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Http; | |
class LemonSqueezyService | |
{ | |
public function __construct(public PendingRequest $client, public ?string $store = null) | |
{ | |
} | |
public function setKey($key): static | |
{ | |
$this->client = Http::baseUrl(config('services.lemonsqueezy.base_url')) | |
->timeout(3600) | |
->withToken($key); | |
return $this; | |
} | |
public function setStore($store): static | |
{ | |
$this->store = $store; | |
return $this; | |
} | |
public function stores() | |
{ | |
return $this->client->get('stores')->json(); | |
} | |
public function store($id) | |
{ | |
return $this->client->get('stores/'.$id)->json(); | |
} | |
public function products() | |
{ | |
return $this->client->get('products?filter[store_id]='.$this->store)->json(); | |
} | |
public function variant($variantId) | |
{ | |
return $this->client->get('variants/'.$variantId)->json(); | |
} | |
public function variants($productId) | |
{ | |
return $this->client->get('variants?filter[product_id]='.$productId)->json(); | |
} | |
public function checkout($variant) | |
{ | |
return $this->client->post('checkouts', [ | |
'data' => [ | |
'type' => 'checkouts', | |
'attributes' => [ | |
'product_options' => array_filter([ | |
'enabled_variants' => [(string) $variant], | |
]), | |
'expires_at' => null, | |
], | |
'relationships' => [ | |
'store' => [ | |
'data' => [ | |
'type' => 'stores', | |
'id' => $this->store, | |
], | |
], | |
'variant' => [ | |
'data' => [ | |
'type' => 'variants', | |
'id' => (string) $variant, | |
], | |
], | |
], | |
], | |
])->json(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Services\LemonSqueezyService; | |
use Illuminate\Support\Facades\Http; | |
use Illuminate\Support\ServiceProvider; | |
class LemonSqueezyServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->app->bind(LemonSqueezyService::class, function () { | |
$client = Http::baseUrl(config('services.lemonsqueezy.base_url')) | |
->timeout(3600) | |
->withToken(config('services.lemonsqueezy.key')); | |
return new \App\Services\LemonSqueezyService($client, config('services.lemonsqueezy.store')); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment