Migrate billing from Paddle to Lemon Squeezy
This commit is contained in:
@@ -17,14 +17,19 @@ class EventAddonWebhookService
|
||||
|
||||
public function handle(array $payload): bool
|
||||
{
|
||||
$eventType = $payload['event_type'] ?? null;
|
||||
$eventType = $payload['meta']['event_name'] ?? null;
|
||||
$data = $payload['data'] ?? [];
|
||||
|
||||
if ($eventType !== 'transaction.completed') {
|
||||
if (! in_array($eventType, ['order_created', 'order_updated'], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$metadata = $this->extractMetadata($data);
|
||||
$status = strtolower((string) data_get($data, 'attributes.status', ''));
|
||||
if ($status !== 'paid') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$metadata = $this->extractMetadata($payload);
|
||||
$intentId = $metadata['addon_intent'] ?? null;
|
||||
$addonKey = $metadata['addon_key'] ?? null;
|
||||
|
||||
@@ -32,8 +37,8 @@ class EventAddonWebhookService
|
||||
return false;
|
||||
}
|
||||
|
||||
$transactionId = $data['id'] ?? $data['transaction_id'] ?? null;
|
||||
$checkoutId = $data['checkout_id'] ?? null;
|
||||
$transactionId = $data['id'] ?? null;
|
||||
$checkoutId = data_get($data, 'attributes.checkout_id') ?? null;
|
||||
|
||||
$addon = EventPackageAddon::query()
|
||||
->where('addon_key', $addonKey)
|
||||
@@ -66,10 +71,12 @@ class EventAddonWebhookService
|
||||
'transaction_id' => $transactionId,
|
||||
'checkout_id' => $addon->checkout_id ?: $checkoutId,
|
||||
'status' => 'completed',
|
||||
'amount' => Arr::get($data, 'totals.grand_total') ?? Arr::get($data, 'amount'),
|
||||
'currency' => Arr::get($data, 'currency_code') ?? Arr::get($data, 'currency'),
|
||||
'amount' => $this->resolveAmount($data),
|
||||
'currency' => Arr::get($data, 'attributes.currency') ?? Arr::get($data, 'currency'),
|
||||
'metadata' => array_merge($addon->metadata ?? [], ['webhook_payload' => $data]),
|
||||
'receipt_payload' => Arr::get($data, 'receipt_url') ? ['receipt_url' => Arr::get($data, 'receipt_url')] : null,
|
||||
'receipt_payload' => Arr::get($data, 'attributes.urls.receipt')
|
||||
? ['receipt_url' => Arr::get($data, 'attributes.urls.receipt')]
|
||||
: null,
|
||||
'purchased_at' => now(),
|
||||
])->save();
|
||||
|
||||
@@ -118,17 +125,36 @@ class EventAddonWebhookService
|
||||
{
|
||||
$metadata = [];
|
||||
|
||||
if (isset($data['metadata']) && is_array($data['metadata'])) {
|
||||
$metadata = $data['metadata'];
|
||||
if (isset($data['meta']['custom_data']) && is_array($data['meta']['custom_data'])) {
|
||||
$metadata = $data['meta']['custom_data'];
|
||||
}
|
||||
|
||||
if (isset($data['custom_data']) && is_array($data['custom_data'])) {
|
||||
$metadata = array_merge($metadata, $data['custom_data']);
|
||||
if (isset($data['metadata']) && is_array($data['metadata'])) {
|
||||
$metadata = array_merge($metadata, $data['metadata']);
|
||||
}
|
||||
|
||||
if (isset($data['attributes']['custom_data']) && is_array($data['attributes']['custom_data'])) {
|
||||
$metadata = array_merge($metadata, $data['attributes']['custom_data']);
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
private function resolveAmount(array $data): ?float
|
||||
{
|
||||
$total = Arr::get($data, 'attributes.total');
|
||||
|
||||
if ($total === null || $total === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_numeric($total)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return round(((float) $total) / 100, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user