feat(payouts): implement promoter earnings viewing, request flow, and admin Stripe processing with webhooks

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
This commit is contained in:
kbe
2025-09-17 02:07:52 +02:00
parent 47f4f50e5b
commit 3c1e17c2af
31 changed files with 1096 additions and 148 deletions

View File

@@ -1,4 +1,22 @@
class Earning < ApplicationRecord
def self.create_from_order(order)
return unless order.paid? || order.completed?
gross_cents = order.tickets.active.sum(:price_cents)
fee_cents = order.tickets.active.sum do |ticket|
50 + (ticket.price_cents * 0.015).to_i
end
amount_cents = gross_cents - fee_cents
create!(
event: order.event,
user: order.event.user,
order: order,
amount_cents: amount_cents,
fee_cents: fee_cents,
status: :pending
)
end
# === Relations ===
belongs_to :event
belongs_to :user
@@ -13,4 +31,24 @@ class Earning < ApplicationRecord
validates :net_amount_cents, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
validates :status, presence: true
validates :stripe_payout_id, allow_blank: true, uniqueness: true
# Recalculate earning based on active tickets in the order
def recalculate!
return unless order.present?
active_tickets = order.tickets.active
if active_tickets.empty?
update!(amount_cents: 0, fee_cents: 0)
else
gross_cents = active_tickets.sum(:price_cents)
fee_cents = active_tickets.sum do |ticket|
50 + (ticket.price_cents * 0.015).to_i
end
update!(amount_cents: gross_cents - fee_cents, fee_cents: fee_cents)
end
end
def recalculate_on_refund(order)
recalculate!
end
end