Migrate billing from Paddle to Lemon Squeezy
This commit is contained in:
151
app/Services/LemonSqueezy/LemonSqueezyOrderService.php
Normal file
151
app/Services/LemonSqueezy/LemonSqueezyOrderService.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\LemonSqueezy;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class LemonSqueezyOrderService
|
||||
{
|
||||
public function __construct(private readonly LemonSqueezyClient $client) {}
|
||||
|
||||
/**
|
||||
* @return array{data: array<int, array<string, mixed>>, meta: array<string, mixed>}
|
||||
*/
|
||||
public function listForCustomer(string $customerId, array $query = []): array
|
||||
{
|
||||
$payload = array_filter(array_merge([
|
||||
'filter[customer_id]' => $customerId,
|
||||
'sort' => '-created_at',
|
||||
], $query), static fn ($value) => $value !== null && $value !== '');
|
||||
|
||||
$response = $this->client->get('/orders', $payload);
|
||||
|
||||
$orders = Arr::get($response, 'data', []);
|
||||
$meta = Arr::get($response, 'meta', []);
|
||||
|
||||
if (! is_array($orders)) {
|
||||
$orders = [];
|
||||
}
|
||||
|
||||
return [
|
||||
'data' => array_map([$this, 'mapOrder'], $orders),
|
||||
'meta' => $this->mapPagination($meta),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function retrieve(string $orderId): array
|
||||
{
|
||||
$response = $this->client->get("/orders/{$orderId}");
|
||||
$order = Arr::get($response, 'data');
|
||||
|
||||
return is_array($order) ? $order : (is_array($response) ? $response : []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function findByCheckoutId(string $checkoutId): ?array
|
||||
{
|
||||
$response = $this->client->get("/checkouts/{$checkoutId}");
|
||||
$checkout = Arr::get($response, 'data');
|
||||
|
||||
if (! is_array($checkout)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$orderId = Arr::get($checkout, 'attributes.order_id');
|
||||
if (! $orderId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->retrieve((string) $orderId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue a refund for a Lemon Squeezy order.
|
||||
*
|
||||
* @param array{reason?: string|null} $options
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function refund(string $orderId, array $options = []): array
|
||||
{
|
||||
$payload = [
|
||||
'data' => [
|
||||
'type' => 'refunds',
|
||||
'attributes' => array_filter([
|
||||
'order_id' => $orderId,
|
||||
'reason' => $options['reason'] ?? null,
|
||||
], static fn ($value) => $value !== null && $value !== ''),
|
||||
],
|
||||
];
|
||||
|
||||
return $this->client->post('/refunds', $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $order
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function mapOrder(array $order): array
|
||||
{
|
||||
$attributes = Arr::get($order, 'attributes', []);
|
||||
|
||||
return [
|
||||
'id' => $order['id'] ?? null,
|
||||
'order_number' => $attributes['order_number'] ?? null,
|
||||
'status' => $attributes['status'] ?? null,
|
||||
'amount' => $this->convertAmount($attributes['subtotal'] ?? null),
|
||||
'currency' => $attributes['currency'] ?? 'EUR',
|
||||
'origin' => 'lemonsqueezy',
|
||||
'checkout_id' => $attributes['checkout_id'] ?? null,
|
||||
'created_at' => $attributes['created_at'] ?? null,
|
||||
'updated_at' => $attributes['updated_at'] ?? null,
|
||||
'receipt_url' => Arr::get($attributes, 'urls.receipt'),
|
||||
'tax' => $this->convertAmount($attributes['tax'] ?? null),
|
||||
'grand_total' => $this->convertAmount($attributes['total'] ?? null),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $meta
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function mapPagination(array $meta): array
|
||||
{
|
||||
$page = Arr::get($meta, 'page', []);
|
||||
$current = (int) ($page['currentPage'] ?? $page['current_page'] ?? 1);
|
||||
$totalPages = (int) ($page['totalPages'] ?? $page['total_pages'] ?? 1);
|
||||
|
||||
return [
|
||||
'next' => $current < $totalPages ? (string) ($current + 1) : null,
|
||||
'previous' => $current > 1 ? (string) ($current - 1) : null,
|
||||
'has_more' => $current < $totalPages,
|
||||
];
|
||||
}
|
||||
|
||||
protected function convertAmount(mixed $value): ?float
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_array($value) && isset($value['amount'])) {
|
||||
$value = $value['amount'];
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
$value = preg_replace('/[^0-9.-]/', '', $value);
|
||||
}
|
||||
|
||||
if ($value === '' || $value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$amount = (float) $value;
|
||||
|
||||
return $amount / 100;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user