switched to paddle inline checkout, removed paypal and most of stripe. added product sync between app and paddle.
This commit is contained in:
92
app/Services/Paddle/PaddleTransactionService.php
Normal file
92
app/Services/Paddle/PaddleTransactionService.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?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),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user