Paddle Coupon Sync prüft nun zuerst, ob der Discount schon existiert.

This commit is contained in:
Codex Agent
2026-01-08 13:36:58 +01:00
parent cff014ede5
commit 3e2b63f71f

View File

@@ -17,6 +17,11 @@ class PaddleDiscountService
*/ */
public function createDiscount(Coupon $coupon): array public function createDiscount(Coupon $coupon): array
{ {
$existing = $this->findExistingDiscount($coupon->code);
if ($existing !== null) {
return $existing;
}
$payload = $this->buildDiscountPayload($coupon); $payload = $this->buildDiscountPayload($coupon);
$response = $this->client->post('/discounts', $payload); $response = $this->client->post('/discounts', $payload);
@@ -82,6 +87,35 @@ class PaddleDiscountService
return Arr::get($response, 'data', $response); return Arr::get($response, 'data', $response);
} }
/**
* @return array<string, mixed>|null
*/
protected function findExistingDiscount(?string $code): ?array
{
$normalized = Str::upper(trim((string) $code));
if ($normalized === '') {
return null;
}
$response = $this->client->get('/discounts', [
'code' => $normalized,
'per_page' => 1,
]);
$items = Arr::get($response, 'data', []);
if (! is_array($items) || $items === []) {
return null;
}
$match = Collection::make($items)->first(static function ($item) use ($normalized) {
$codeValue = Str::upper((string) Arr::get($item, 'code', ''));
return $codeValue === $normalized ? $item : null;
});
return is_array($match) ? $match : null;
}
/** /**
* @return array<string, mixed> * @return array<string, mixed>
*/ */