Add model methods for accurate net calculations (€0.50 + 1.5% fees), eligibility, refund handling Update promoter/payouts controller for index (pending events), create (eligibility checks) Integrate admin processing via Stripe::Transfer, webhook for status sync Enhance views: index pending cards, events/show preview/form Add comprehensive tests (models, controllers, service, integration); run migrations
40 lines
1.0 KiB
Ruby
40 lines
1.0 KiB
Ruby
class PayoutService
|
|
def initialize(payout)
|
|
@payout = payout
|
|
end
|
|
|
|
def process!
|
|
return unless @payout.can_process?
|
|
|
|
@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
|
|
|
|
private
|
|
|
|
def update_earnings_status
|
|
@payout.event.earnings.where(status: 0).update_all(status: 1) # pending to paid
|
|
end
|
|
end
|