*/ private function tiktokFeeKeys(): array { return [ 'tiktok_shop_platform_commission', 'tiktok_shop_logistics_service_fee', 'tiktok_shop_dynamic_commission', 'tiktok_shop_order_processing_fee', 'tiktok_shop_affiliate', 'tiktok_shop_pre_order_service_fee', ]; } /** * @return list */ private function shopeeFeeKeys(): array { return [ 'shopee_admin_fee', 'shopee_program_fee', 'shopee_shipping_savings', 'shopee_premium', 'shopee_service_fee', 'shopee_order_processing_fee', 'shopee_ams_commission_fee', 'shopee_pre_order', 'shopee_live_extra', ]; } public function marketplaceData(): array { $settings = app(MarketplaceSettings::class); return [ 'tiktok_shop_platform_commission' => $this->presentFeeRule($settings->tiktok_shop_platform_commission), 'tiktok_shop_logistics_service_fee' => $this->presentFeeRule($settings->tiktok_shop_logistics_service_fee), 'tiktok_shop_dynamic_commission' => $this->presentFeeRule($settings->tiktok_shop_dynamic_commission), 'tiktok_shop_order_processing_fee' => $this->presentFeeRule($settings->tiktok_shop_order_processing_fee), 'tiktok_shop_affiliate' => $this->presentFeeRule($settings->tiktok_shop_affiliate), 'tiktok_shop_pre_order_service_fee' => $this->presentFeeRule($settings->tiktok_shop_pre_order_service_fee), 'shopee_admin_fee' => $this->presentFeeRule($settings->shopee_admin_fee), 'shopee_program_fee' => $this->presentFeeRule($settings->shopee_program_fee), 'shopee_shipping_savings' => $this->presentFeeRule($settings->shopee_shipping_savings), 'shopee_premium' => $this->presentFeeRule($settings->shopee_premium), 'shopee_service_fee' => $this->presentFeeRule($settings->shopee_service_fee), 'shopee_order_processing_fee' => $this->presentFeeRule($settings->shopee_order_processing_fee), 'shopee_ams_commission_fee' => $this->presentFeeRule($settings->shopee_ams_commission_fee), 'shopee_pre_order' => $this->presentFeeRule($settings->shopee_pre_order), 'shopee_live_extra' => $this->presentFeeRule($settings->shopee_live_extra), ]; } public function updateMarketplace(array $validated): void { $settings = app(MarketplaceSettings::class); foreach ($this->tiktokFeeKeys() as $key) { $settings->{$key} = $this->normalizeFeeRule($validated[$key]); } foreach ($this->shopeeFeeKeys() as $key) { $settings->{$key} = $this->normalizeFeeRule($validated[$key]); } $settings->save(); } /** * @param list $lineItems * @return array|null */ public function buildOrderSnapshot(OrderChannel $channel, int $totalAmount, array $lineItems): ?array { $feeSnapshot = $this->feeRulesForChannel($channel); if ($feeSnapshot === null) { return null; } $calculation = app(MarketplaceFeeCalculator::class)->calculate( $feeSnapshot['fees'], $totalAmount, $lineItems, ); return [ 'platform' => $feeSnapshot['platform'], 'fees' => $feeSnapshot['fees'], 'base_amount' => $totalAmount, 'results' => $calculation['results'], 'total_fee_amount' => $calculation['total_fee_amount'], 'net_amount' => $calculation['net_amount'], ]; } /** * @return array{platform: string, fees: array}|null */ private function feeRulesForChannel(OrderChannel $channel): ?array { $settings = app(MarketplaceSettings::class); return match ($channel) { OrderChannel::TIKTOK => [ 'platform' => 'tiktok', 'fees' => [ 'platform_commission' => $this->presentFeeRule($settings->tiktok_shop_platform_commission), 'logistics_service_fee' => $this->presentFeeRule($settings->tiktok_shop_logistics_service_fee), 'dynamic_commission' => $this->presentFeeRule($settings->tiktok_shop_dynamic_commission), 'order_processing_fee' => $this->presentFeeRule($settings->tiktok_shop_order_processing_fee), 'affiliate' => $this->presentFeeRule($settings->tiktok_shop_affiliate), 'pre_order_service_fee' => $this->presentFeeRule($settings->tiktok_shop_pre_order_service_fee), ], ], OrderChannel::SHOPEE => [ 'platform' => 'shopee', 'fees' => [ 'admin_fee' => $this->presentFeeRule($settings->shopee_admin_fee), 'program_fee' => $this->presentFeeRule($settings->shopee_program_fee), 'shipping_savings' => $this->presentFeeRule($settings->shopee_shipping_savings), 'premium' => $this->presentFeeRule($settings->shopee_premium), 'service_fee' => $this->presentFeeRule($settings->shopee_service_fee), 'order_processing_fee' => $this->presentFeeRule($settings->shopee_order_processing_fee), 'ams_commission_fee' => $this->presentFeeRule($settings->shopee_ams_commission_fee), 'pre_order' => $this->presentFeeRule($settings->shopee_pre_order), 'live_extra' => $this->presentFeeRule($settings->shopee_live_extra), ], ], OrderChannel::STORE => null, }; } /** * @param array{scope: string, value_type: string, value: float|int|string} $data * @return array{scope: string, value_type: string, value: float} */ private function presentFeeRule(array $data): array { return MarketplaceFeeRule::fromArray($data)->toArray(); } /** * @param array{scope: string, value_type: string, value: float|int|string} $data * @return array{scope: string, value_type: string, value: float} */ private function normalizeFeeRule(array $data): array { return MarketplaceFeeRule::fromArray($data)->toArray(); } }