>, meta: array} */ 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 */ 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|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 */ 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 $order * @return array */ 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 $meta * @return array */ 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; } }