['annual_fee_usd' => 0, 'platform_fee_percent' => 12.0], self::TIER_PRO => ['annual_fee_usd' => 149, 'platform_fee_percent' => 3.5], self::TIER_STUDIO => ['annual_fee_usd' => 299, 'platform_fee_percent' => 1.0], ]; $fee_key = 'seller_tier_' . $tier . '_platform_fee_percent'; $annual_key = 'seller_tier_' . $tier . '_annual_fee_usd'; $platform_fee_percent = isset($settings[$fee_key]) ? (float) $settings[$fee_key] : (float) ($defaults[$tier]['platform_fee_percent'] ?? 0); $platform_fee_percent = max(0.0, min(100.0, $platform_fee_percent)); $annual_fee_usd = isset($settings[$annual_key]) ? (int) $settings[$annual_key] : (int) ($defaults[$tier]['annual_fee_usd'] ?? 0); $annual_fee_usd = max(0, $annual_fee_usd); return [ 'annual_fee_usd' => $annual_fee_usd, 'platform_fee_percent' => $platform_fee_percent, // v1: payment processing fees are seller-paid across all tiers. 'processing_fees_included' => false, ]; } public static function normalize_tier(string $tier): string { $tier = strtolower(trim($tier)); $allowed = [self::TIER_STARTER => true, self::TIER_PRO => true, self::TIER_STUDIO => true]; return isset($allowed[$tier]) ? $tier : self::TIER_STARTER; } public static function get_seller_tier(int $seller_user_id): string { if ($seller_user_id <= 0) { return self::TIER_STARTER; } $tier = (string) get_user_meta($seller_user_id, self::USER_META_TIER, true); $tier = self::normalize_tier($tier); self::maybe_auto_downgrade_if_expired($seller_user_id, $tier); $tier = (string) get_user_meta($seller_user_id, self::USER_META_TIER, true); return self::normalize_tier($tier); } public static function set_seller_tier(int $seller_user_id, string $tier, string $expires_at_ymd = ''): void { if ($seller_user_id <= 0) { return; } $tier = self::normalize_tier($tier); update_user_meta($seller_user_id, self::USER_META_TIER, $tier); $expires_at_ymd = trim($expires_at_ymd); if ($expires_at_ymd === '') { delete_user_meta($seller_user_id, self::USER_META_TIER_EXPIRES_AT); return; } if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $expires_at_ymd)) { delete_user_meta($seller_user_id, self::USER_META_TIER_EXPIRES_AT); return; } update_user_meta($seller_user_id, self::USER_META_TIER_EXPIRES_AT, $expires_at_ymd); } public static function platform_fee_percent_for_seller(int $seller_user_id): float { $tier = self::get_seller_tier($seller_user_id); $cfg = self::tier_config($tier); return max(0.0, min(100.0, (float) ($cfg['platform_fee_percent'] ?? 0))); } public static function processing_fees_included_for_seller(int $seller_user_id): bool { $tier = self::get_seller_tier($seller_user_id); $cfg = self::tier_config($tier); return !empty($cfg['processing_fees_included']); } // Backwards-compatible alias for older call sites. public static function stripe_fees_included_for_seller(int $seller_user_id): bool { return self::processing_fees_included_for_seller($seller_user_id); } public static function maybe_auto_downgrade_if_expired(int $seller_user_id, ?string $known_tier = null): void { if ($seller_user_id <= 0) { return; } $tier = $known_tier !== null ? self::normalize_tier($known_tier) : self::normalize_tier((string) get_user_meta($seller_user_id, self::USER_META_TIER, true)); if ($tier !== self::TIER_PRO && $tier !== self::TIER_STUDIO) { return; } $expires = (string) get_user_meta($seller_user_id, self::USER_META_TIER_EXPIRES_AT, true); $expires = trim($expires); if ($expires === '' || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $expires)) { return; } $ts = strtotime($expires . ' 00:00:00 UTC'); if (!$ts) { return; } if (time() < $ts) { return; } // Downgrade when expired (renewal failed / not renewed). update_user_meta($seller_user_id, self::USER_META_TIER, self::TIER_STARTER); delete_user_meta($seller_user_id, self::USER_META_TIER_EXPIRES_AT); } /** * Simple processing fee estimate (USD): 2.9% + 30c. */ public static function estimate_processing_fee_minor(int $amount_minor, string $currency): int { $currency = strtoupper(trim($currency)); if ($amount_minor <= 0) { return 0; } if ($currency !== 'USD') { return 0; } $percent = 2.9; $fixed_minor = 30; $fee = (int) round(($amount_minor * ($percent / 100.0)) + $fixed_minor); return max(0, $fee); } }