geschenkgutscheine implementiert ("Paket verschenken"). Neuer Upload-Provider: Sparkbooth.

This commit is contained in:
Codex Agent
2025-12-07 16:54:58 +01:00
parent 3f3c0f1d35
commit 046e2fe3ec
50 changed files with 2422 additions and 130 deletions

View File

@@ -51,7 +51,7 @@ class PaddleCatalogService
*/
public function createPrice(Package $package, string $productId, array $overrides = []): array
{
$payload = $this->buildPricePayload($package, $productId, $overrides);
$payload = $this->buildPricePayload($package, $productId, $overrides, includeProduct: true);
return $this->extractEntity($this->client->post('/prices', $payload));
}
@@ -61,7 +61,12 @@ class PaddleCatalogService
*/
public function updatePrice(string $priceId, Package $package, array $overrides = []): array
{
$payload = $this->buildPricePayload($package, $overrides['product_id'] ?? $package->paddle_product_id, $overrides);
$payload = $this->buildPricePayload(
$package,
$overrides['product_id'] ?? $package->paddle_product_id,
$overrides,
includeProduct: false
);
return $this->extractEntity($this->client->patch("/prices/{$priceId}", $payload));
}
@@ -85,19 +90,24 @@ class PaddleCatalogService
/**
* @return array<string, mixed>
*/
public function buildPricePayload(Package $package, string $productId, array $overrides = []): array
public function buildPricePayload(Package $package, string $productId, array $overrides = [], bool $includeProduct = true): array
{
$unitPrice = $overrides['unit_price'] ?? [
'amount' => (string) $this->priceToMinorUnits($package->price),
'currency_code' => Str::upper((string) ($package->currency ?? 'EUR')),
];
$payload = array_merge([
'product_id' => $productId,
$base = [
'description' => $this->resolvePriceDescription($package, $overrides),
'unit_price' => $unitPrice,
'custom_data' => $this->buildCustomData($package, $overrides['custom_data'] ?? []),
], Arr::except($overrides, ['unit_price', 'description', 'custom_data']));
];
if ($includeProduct) {
$base['product_id'] = $productId;
}
$payload = array_merge($base, Arr::except($overrides, ['unit_price', 'description', 'custom_data', 'product_id']));
return $this->cleanPayload($payload);
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Services\Paddle;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class PaddleGiftVoucherCatalogService
{
public function __construct(private readonly PaddleClient $client) {}
/**
* @param array{key:string,label:string,amount:float,currency?:string,paddle_product_id?:string|null,paddle_price_id?:string|null} $tier
* @return array{product_id:string,price_id:string}
*/
public function ensureTier(array $tier): array
{
$product = $tier['paddle_product_id'] ?? null;
$price = $tier['paddle_price_id'] ?? null;
if (! $product) {
$product = $this->createProduct($tier)['id'];
}
if (! $price) {
$price = $this->createPrice($tier, $product)['id'];
}
return [
'product_id' => $product,
'price_id' => $price,
];
}
/**
* @param array{key:string,label:string,amount:float,currency?:string} $tier
* @return array<string, mixed>
*/
public function createProduct(array $tier): array
{
$payload = [
'name' => $tier['label'],
'description' => sprintf('Geschenkgutschein im Wert von %.2f %s für Fotospiel Pakete.', $tier['amount'], $this->currency($tier)),
'type' => 'standard',
'tax_category' => 'standard',
'custom_data' => [
'kind' => 'gift_voucher',
'tier_key' => $tier['key'],
'amount' => $tier['amount'],
'currency' => $this->currency($tier),
],
];
$response = $this->client->post('/products', $payload);
return Arr::get($response, 'data', $response);
}
/**
* @param array{key:string,label:string,amount:float,currency?:string} $tier
* @return array<string, mixed>
*/
public function createPrice(array $tier, string $productId): array
{
$payload = [
'product_id' => $productId,
'description' => sprintf('Geschenkgutschein %.2f %s', $tier['amount'], $this->currency($tier)),
'unit_price' => [
'amount' => (string) $this->toMinorUnits($tier['amount']),
'currency_code' => $this->currency($tier),
],
'custom_data' => [
'kind' => 'gift_voucher',
'tier_key' => $tier['key'],
],
];
$response = $this->client->post('/prices', $payload);
return Arr::get($response, 'data', $response);
}
protected function currency(array $tier): string
{
return Str::upper($tier['currency'] ?? 'EUR');
}
protected function toMinorUnits(float $amount): int
{
return (int) round($amount * 100);
}
}