ycdfdsf
This commit is contained in:
@@ -1801,7 +1801,9 @@ final class Router
|
||||
$paymentType = $this->enumValue($input['payment_type'] ?? 'fiat', ['fiat', 'crypto'], 'payment_type');
|
||||
$baseSpeed = self::BASE_OFFER_SPEEDS[$paymentType] ?? self::BASE_OFFER_SPEEDS['fiat'];
|
||||
$bonusPercent = $this->optionalDecimal($input['bonus_percent'] ?? null);
|
||||
$basePriceCurrency = $this->requiredCurrency($input['base_price_currency'] ?? ($input['reference_price_currency'] ?? $input['price_currency'] ?? null), 'base_price_currency');
|
||||
$basePriceCurrency = $paymentType === 'crypto'
|
||||
? 'USD'
|
||||
: $this->requiredCurrency($input['base_price_currency'] ?? ($input['reference_price_currency'] ?? $input['price_currency'] ?? null), 'base_price_currency');
|
||||
$payload = [
|
||||
'label' => $this->requiredString($input['label'] ?? null, 'label', 120),
|
||||
'runtime_months' => $this->optionalPositiveInt($input['runtime_months'] ?? null),
|
||||
@@ -1822,7 +1824,7 @@ final class Router
|
||||
throw new ApiException('Bonus-Prozent darf nicht negativ sein.', 422);
|
||||
}
|
||||
|
||||
$this->assertCurrencyType($payload['base_price_currency'], $paymentType === 'crypto', 'base_price_currency');
|
||||
$this->assertCurrencyType($payload['base_price_currency'], false, 'base_price_currency');
|
||||
|
||||
return $this->repository()->saveMinerOffer($projectKey, $payload);
|
||||
}
|
||||
@@ -1837,6 +1839,7 @@ final class Router
|
||||
$settings = $this->settings($projectKey);
|
||||
$cryptoCurrency = $this->requiredCurrency($settings['crypto_currency'] ?? 'DOGE', 'crypto_currency');
|
||||
$isAutoRenew = array_key_exists('auto_renew', $input) ? !empty($input['auto_renew']) : !empty($offer['auto_renew']);
|
||||
$paymentType = $this->enumValue($offer['payment_type'] ?? 'fiat', ['fiat', 'crypto'], 'payment_type');
|
||||
$selectedSpeedValue = $this->optionalDecimal($input['mining_speed_value'] ?? null);
|
||||
$selectedSpeedUnit = $this->optionalSpeedUnit($input['mining_speed_unit'] ?? null);
|
||||
if (($selectedSpeedValue === null) xor ($selectedSpeedUnit === null)) {
|
||||
@@ -1845,17 +1848,21 @@ final class Router
|
||||
|
||||
$resolvedSpeedValue = $selectedSpeedValue ?? $this->optionalDecimal($offer['mining_speed_value'] ?? null);
|
||||
$resolvedSpeedUnit = $selectedSpeedUnit ?? $this->optionalSpeedUnit($offer['mining_speed_unit'] ?? null);
|
||||
$resolvedBonusPercent = $this->offerBonusPercent($offer);
|
||||
$selectedBonusPercent = $this->optionalDecimal($input['bonus_percent'] ?? null);
|
||||
if ($selectedBonusPercent !== null && $selectedBonusPercent < 0) {
|
||||
throw new ApiException('Bonus-Prozent darf nicht negativ sein.', 422);
|
||||
}
|
||||
$resolvedBonusPercent = $selectedBonusPercent ?? $this->offerBonusPercent($offer);
|
||||
$resolvedBonusValue = $this->bonusSpeedFromPercent($resolvedSpeedValue, $resolvedSpeedUnit, $resolvedBonusPercent);
|
||||
$resolvedBonusUnit = $resolvedBonusValue !== null ? $resolvedSpeedUnit : null;
|
||||
|
||||
$purchaseCurrency = $this->optionalCurrency($input['currency'] ?? null) ?? (string) ($offer['effective_price_currency'] ?? $offer['price_currency'] ?? $offer['base_price_currency'] ?? '');
|
||||
if (!$isAutoRenew) {
|
||||
if ($paymentType === 'crypto' || !$isAutoRenew) {
|
||||
$purchaseCurrency = $cryptoCurrency;
|
||||
}
|
||||
$purchaseCost = $this->optionalDecimal($input['total_cost_amount'] ?? null);
|
||||
if ($purchaseCost === null) {
|
||||
$purchaseCost = $this->resolveOfferPurchaseCost(array_merge($offer, [
|
||||
$purchaseCost = $this->resolveOfferPurchaseCost($projectKey, array_merge($offer, [
|
||||
'mining_speed_value' => $resolvedSpeedValue,
|
||||
'mining_speed_unit' => $resolvedSpeedUnit,
|
||||
'bonus_speed_value' => $resolvedBonusValue,
|
||||
@@ -1865,8 +1872,8 @@ final class Router
|
||||
'price_currency' => $purchaseCurrency !== '' ? $purchaseCurrency : ($offer['price_currency'] ?? ''),
|
||||
]));
|
||||
}
|
||||
$referencePriceAmount = $this->optionalDecimal($input['reference_price_amount'] ?? ($offer['reference_price_amount'] ?? $offer['usd_reference_amount'] ?? null));
|
||||
$referencePriceCurrency = $this->optionalCurrency($input['reference_price_currency'] ?? ($offer['reference_price_currency'] ?? (is_numeric($offer['usd_reference_amount'] ?? null) ? 'USD' : null)));
|
||||
$referencePriceAmount = $this->optionalDecimal($input['reference_price_amount'] ?? ($offer['base_price_amount'] ?? $offer['reference_price_amount'] ?? $offer['usd_reference_amount'] ?? null));
|
||||
$referencePriceCurrency = $this->optionalCurrency($input['reference_price_currency'] ?? ($offer['base_price_currency'] ?? $offer['reference_price_currency'] ?? (is_numeric($offer['usd_reference_amount'] ?? null) ? 'USD' : null)));
|
||||
$purchasedAt = array_key_exists('purchased_at', $input)
|
||||
? $this->requiredDateTime($input['purchased_at'], 'purchased_at', $this->projectTimezone($projectKey))
|
||||
: $this->currentTimestamp();
|
||||
@@ -2457,7 +2464,7 @@ final class Router
|
||||
return abs(time() - $parsed) <= (int) round(max(0.25, $maxAgeHours) * 3600);
|
||||
}
|
||||
|
||||
private function resolveOfferPurchaseCost(array $offer): float
|
||||
private function resolveOfferPurchaseCost(string $projectKey, array $offer): float
|
||||
{
|
||||
$purchaseCurrency = (string) ($offer['price_currency'] ?? '');
|
||||
$baseAmount = is_numeric($offer['base_price_amount'] ?? null)
|
||||
@@ -2468,6 +2475,11 @@ final class Router
|
||||
$baseCurrency = (string) ($offer['base_price_currency'] ?? $offer['reference_price_currency'] ?? (is_numeric($offer['usd_reference_amount'] ?? null) ? 'USD' : ''));
|
||||
|
||||
if ($purchaseCurrency !== '' && $baseAmount !== null && $baseAmount > 0 && $baseCurrency !== '') {
|
||||
$latestConverted = $this->convertWithLatestMeasurement($projectKey, $baseAmount, $baseCurrency, $purchaseCurrency);
|
||||
if ($latestConverted !== null && $latestConverted > 0) {
|
||||
return $latestConverted;
|
||||
}
|
||||
|
||||
$converted = $this->fx()->convert($baseAmount, $baseCurrency, $purchaseCurrency);
|
||||
if (is_numeric($converted) && (float) $converted > 0) {
|
||||
return (float) $converted;
|
||||
@@ -2477,6 +2489,56 @@ final class Router
|
||||
return (float) ($baseAmount ?? $offer['price_amount'] ?? 0);
|
||||
}
|
||||
|
||||
private function convertWithLatestMeasurement(string $projectKey, float $amount, string $fromCurrency, string $toCurrency): ?float
|
||||
{
|
||||
$from = strtoupper(trim($fromCurrency));
|
||||
$to = strtoupper(trim($toCurrency));
|
||||
if ($from === '' || $to === '' || $amount <= 0) {
|
||||
return null;
|
||||
}
|
||||
if ($from === $to) {
|
||||
return $amount;
|
||||
}
|
||||
|
||||
$latestRows = $this->repository()->listRecentMeasurements($projectKey, 1);
|
||||
$latest = $latestRows[0] ?? null;
|
||||
if (!is_array($latest)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$coinCurrency = strtoupper(trim((string) ($latest['coin_currency'] ?? $this->settings($projectKey)['crypto_currency'] ?? '')));
|
||||
$priceCurrency = strtoupper(trim((string) ($latest['price_currency'] ?? '')));
|
||||
$pricePerCoin = is_numeric($latest['price_per_coin'] ?? null) ? (float) $latest['price_per_coin'] : null;
|
||||
if ($coinCurrency === '' || $priceCurrency === '' || $pricePerCoin === null || $pricePerCoin <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($from === $priceCurrency && $to === $coinCurrency) {
|
||||
return $amount / $pricePerCoin;
|
||||
}
|
||||
|
||||
if ($from === $coinCurrency && $to === $priceCurrency) {
|
||||
return $amount * $pricePerCoin;
|
||||
}
|
||||
|
||||
if ($to === $coinCurrency) {
|
||||
$convertedFiat = $this->fx()->convert($amount, $from, $priceCurrency);
|
||||
if (is_numeric($convertedFiat) && (float) $convertedFiat > 0) {
|
||||
return (float) $convertedFiat / $pricePerCoin;
|
||||
}
|
||||
}
|
||||
|
||||
if ($from === $coinCurrency) {
|
||||
$convertedFiat = $amount * $pricePerCoin;
|
||||
$convertedTarget = $this->fx()->convert($convertedFiat, $priceCurrency, $to);
|
||||
if (is_numeric($convertedTarget)) {
|
||||
return (float) $convertedTarget;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function bonusSpeedFromPercent(?float $speedValue, ?string $speedUnit, ?float $bonusPercent): ?float
|
||||
{
|
||||
if ($speedValue === null || $speedUnit === null || $bonusPercent === null) {
|
||||
|
||||
Reference in New Issue
Block a user