-
Notifications
You must be signed in to change notification settings - Fork 348
Add revenue split to affiliate codes v2 #4672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good direction, would just address the comments and, overall, to strengthen the approach:
- 1<->1<->1 relationship between
charges,users_subscriptions_affiliations_payouts,payouts_values(one charge/payout value per row in usap, maybeUNIQUE (payout_value_id)?) - Handle refunded charges:
- Separate step which goes through refund charges which have a parent charge which have a usap row which have a payout_value that's not yet available and delete the usap row/payout_value
- In your current query,
JOINthe charge with its refund charges and handle the refund amount (don't forget refund charges have a many-to-1 relationship with their parent charge, since multiple refunds can be issued for 1 charge)
| continue; | ||
| } | ||
|
|
||
| let affiliate_cut = net * revenue_split; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
charges.net includes tax amount, you'll probably want to take taxes off the cut (so (charges.net - charges.tax_amount) * revenue_split)
| let now = Utc::now(); | ||
| let start: DateTime<Utc> = DateTime::from_naive_utc_and_offset( | ||
| (now - Duration::days(1)) | ||
| .date_naive() | ||
| .and_hms_nano_opt(0, 0, 0, 0) | ||
| .unwrap_or_default(), | ||
| Utc, | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The start time (created value of the payouts_values row) should be based on the charge's last attempt time rather than the current time
| payout_info.amount += affiliate_cut; | ||
| payout_info | ||
| .charge_subscription_ids | ||
| .push((row.charge_id, row.subscription_id)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make more sense to have a single payouts_values row for each unique charge? Then it's easier to handle refunds (which aren't handled here—what happens if the charge gets processed here, is added to payouts_values but is later refunded before it is available to withdraw for the affiliate?)
| INNER JOIN users_subscriptions_affiliations usa | ||
| ON c.subscription_id = usa.subscription_id | ||
| AND c.subscription_id IS NOT NULL | ||
| AND usa.deactivated_at IS NULL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be checking if c.last_attempt < usa.deactivated_at
| pub struct DBUsersSubscriptionsAffiliations { | ||
| pub subscription_id: DBUserSubscriptionId, | ||
| pub affiliate_code: DBAffiliateCodeId, | ||
| pub deactivated_at: Option<DateTime<Utc>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be setting deactivated_at when a subscription is cancelled in the subscription cancelled route handler
Supercedes #4549