83 lines
2.5 KiB
Ruby
83 lines
2.5 KiB
Ruby
class PayoutService
|
|
def initialize(payout)
|
|
@payout = payout
|
|
end
|
|
|
|
# Check if user is in France or doesn't have a Stripe account (manual processing)
|
|
def process_with_stripe_or_manual
|
|
if should_process_manually?
|
|
process_manually!
|
|
else
|
|
process_with_stripe!
|
|
end
|
|
end
|
|
|
|
# Generate payout summary for manual transfer
|
|
def generate_transfer_summary
|
|
return nil unless @payout.approved? || @payout.processing?
|
|
|
|
{
|
|
payout_id: @payout.id,
|
|
recipient: @payout.user.name,
|
|
account_holder: @payout.user.account_holder_name,
|
|
bank_name: @payout.user.bank_name,
|
|
iban: @payout.user.iban,
|
|
amount_euros: @payout.net_amount_euros,
|
|
description: "Payout for event: #{@payout.event.name}",
|
|
event_name: @payout.event.name,
|
|
event_date: @payout.event.date,
|
|
total_orders: @payout.total_orders_count,
|
|
refunded_orders: @payout.refunded_orders_count
|
|
}
|
|
end
|
|
|
|
# Validate banking information before processing
|
|
def validate_banking_info
|
|
errors = []
|
|
user = @payout.user
|
|
|
|
errors << "Missing IBAN" unless user.iban.present?
|
|
errors << "Missing bank name" unless user.bank_name.present?
|
|
errors << "Missing account holder name" unless user.account_holder_name.present?
|
|
errors << "Invalid IBAN format" if user.iban.present? && !valid_iban?(user.iban)
|
|
|
|
errors
|
|
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_completed!(User.admin.first || User.first, "Manual processing completed")
|
|
|
|
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)
|
|
end
|
|
|
|
def valid_iban?(iban)
|
|
# Basic IBAN validation (simplified)
|
|
iban.match?(/\A[A-Z]{2}[0-9]{2}[A-Z0-9]{4}[0-9]{7}([A-Z0-9]?){0,16}\z/)
|
|
end
|
|
|
|
def update_earnings_status
|
|
@payout.event.earnings.where(status: 0).update_all(status: 1) # pending to paid
|
|
end
|
|
end |