class PayoutService def initialize(payout) @payout = payout end def process! return unless @payout.can_process? # Check if user is in France or doesn't have a Stripe account (manual processing) if should_process_manually? process_manually! else process_with_stripe! end end private def should_process_manually? # For now, we'll assume manual processing for all users # In a real implementation, this could check the user's country !@payout.user.has_stripe_account? end def process_manually! @payout.update!(status: :processing) begin # For manual processing, we just mark it as completed # In a real implementation, this would trigger notifications to admin @payout.mark_as_manually_processed! Rails.logger.info "Manual payout processed for payout #{@payout.id} for event #{@payout.event.name}" rescue => e @payout.update!(status: :failed) Rails.logger.error "Manual payout failed for payout #{@payout.id}: #{e.message}" raise e end end def process_with_stripe! @payout.update!(status: :processing) begin net_amount = @payout.amount_cents - @payout.fee_cents transfer = Stripe::Transfer.create({ amount: (net_amount / 100.0).to_i, currency: "eur", destination: @payout.user.stripe_connected_account_id, description: "Payout for event #{@payout.event.name}", metadata: { payout_id: @payout.id, event_id: @payout.event_id } }, idempotency_key: SecureRandom.uuid) @payout.update!( status: :completed, stripe_payout_id: transfer.id ) update_earnings_status rescue Stripe::StripeError => e @payout.update!(status: :failed) Rails.logger.error "Stripe payout failed for payout #{@payout.id}: #{e.message}" raise e end end def update_earnings_status @payout.event.earnings.where(status: 0).update_all(status: 1) # pending to paid end end