Skip to content

Conversation

@aecsocket
Copy link
Contributor

No description provided.

@aecsocket aecsocket added the backend Involves work from the backend team label Oct 17, 2025
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This implementation isn't idempotent and will lead to double payouts/off-by-one errors. It should use a secondary table to track which charges were paid out and prevent double payouts.
  2. This doesn't handle refunded charges.
  3. This doesn't handle cancelled subscriptions, but that doesn't matter since it doesn't seem the affiliate code will be assigned to the subscription's third and forward charges anyways (is this what we want?)
  4. The affiliate code won't be assigned to proration charges (is this what we want?)

I would approach this by:

  • Add users_subscriptions_affiliations with subscription_id, affiliate_id and deactivated_at TIMESTAMPTZ (or add these columns to the users_subscriptions table)

  • A users_subscriptions_affiliations_payouts table, with

    • id BIGSERIAL PK
    • charge_id FK
    • subscription_id FK
    • affiliate_id FK
    • payout_value_id FK
    • UNIQUE (charge_id)
  • When a user cancels their subscription, deactivate the affiliation

  • Have a task that periodically synchronizes charges associated with a subscription with an affiliate code with the table mentioned above + payouts_values

    • Insert new charges with correct created/available timestamps
    • Remove/mark cancelled charges that were refunded

    optionally use FOR NO KEY UPDATE SKIP LOCKED when processing charges. this will be idempotent, concurrent-safe and can be run on any schedule

This leaves less room for error than transferring over the affiliate_code across every charge + offers better flexibility w.r.t future features & analytics

The task for processing affiliates revenue really needs to be more resilient.

@aecsocket aecsocket marked this pull request as draft October 24, 2025 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Involves work from the backend team

Development

Successfully merging this pull request may close these issues.

2 participants