108 lines
3.4 KiB
PHP
108 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Paddle;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
class PaddleTransactionService
|
|
{
|
|
public function __construct(private readonly PaddleClient $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([
|
|
'customer_id' => $customerId,
|
|
'order_by' => '-created_at',
|
|
], $query), static fn ($value) => $value !== null && $value !== '');
|
|
|
|
$response = $this->client->get('/transactions', $payload);
|
|
|
|
$transactions = Arr::get($response, 'data', []);
|
|
$meta = Arr::get($response, 'meta.pagination', []);
|
|
|
|
if (! is_array($transactions)) {
|
|
$transactions = [];
|
|
}
|
|
|
|
return [
|
|
'data' => array_map([$this, 'mapTransaction'], $transactions),
|
|
'meta' => $this->mapPagination($meta),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Issue a refund for a Paddle transaction.
|
|
*
|
|
* @param array{reason?: string|null} $options
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function refund(string $transactionId, array $options = []): array
|
|
{
|
|
$payload = array_filter([
|
|
'reason' => $options['reason'] ?? null,
|
|
], static fn ($value) => $value !== null && $value !== '');
|
|
|
|
return $this->client->post("/transactions/{$transactionId}/refunds", $payload);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $transaction
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function mapTransaction(array $transaction): array
|
|
{
|
|
$totals = Arr::get($transaction, 'totals', []);
|
|
|
|
return [
|
|
'id' => $transaction['id'] ?? null,
|
|
'status' => $transaction['status'] ?? null,
|
|
'amount' => $this->resolveAmount($transaction, $totals),
|
|
'currency' => $transaction['currency_code'] ?? Arr::get($transaction, 'currency') ?? 'EUR',
|
|
'origin' => $transaction['origin'] ?? null,
|
|
'checkout_id' => $transaction['checkout_id'] ?? Arr::get($transaction, 'details.checkout_id'),
|
|
'created_at' => $transaction['created_at'] ?? null,
|
|
'updated_at' => $transaction['updated_at'] ?? null,
|
|
'receipt_url' => Arr::get($transaction, 'invoice_url') ?? Arr::get($transaction, 'receipt_url'),
|
|
'tax' => Arr::get($totals, 'tax_total') ?? null,
|
|
'grand_total' => Arr::get($totals, 'grand_total') ?? null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $transaction
|
|
* @param array<string, mixed>|null $totals
|
|
*/
|
|
protected function resolveAmount(array $transaction, $totals): ?float
|
|
{
|
|
$amount = Arr::get($totals ?? [], 'subtotal') ?? Arr::get($totals ?? [], 'grand_total');
|
|
|
|
if ($amount !== null) {
|
|
return (float) $amount;
|
|
}
|
|
|
|
$raw = $transaction['amount'] ?? null;
|
|
|
|
if ($raw === null) {
|
|
return null;
|
|
}
|
|
|
|
return (float) $raw;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $pagination
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function mapPagination(array $pagination): array
|
|
{
|
|
return [
|
|
'next' => $pagination['next'] ?? null,
|
|
'previous' => $pagination['previous'] ?? null,
|
|
'has_more' => (bool) ($pagination['has_more'] ?? false),
|
|
];
|
|
}
|
|
}
|