Use auto payments with paypal (#472)

* Use auto payments with paypal

* Remove sandbox key
This commit is contained in:
Geometrically
2022-11-07 15:38:25 -07:00
committed by GitHub
parent 35891c74cd
commit 2c1bcaafc1
18 changed files with 1424 additions and 825 deletions

View File

@@ -1,7 +1,7 @@
use super::ids::*;
use crate::database::models::User;
use crate::models::teams::Permissions;
use crate::models::users::Badges;
use crate::models::users::{Badges, RecipientType, RecipientWallet};
use rust_decimal::Decimal;
pub struct TeamBuilder {
@@ -158,7 +158,9 @@ impl TeamMember {
SELECT tm.id id, tm.role member_role, tm.permissions permissions, tm.accepted accepted, tm.payouts_split payouts_split,
u.id user_id, u.github_id github_id, u.name user_name, u.email email,
u.avatar_url avatar_url, u.username username, u.bio bio,
u.created created, u.role user_role, u.badges badges, u.paypal_email paypal_email
u.created created, u.role user_role, u.badges badges, u.balance balance,
u.payout_wallet payout_wallet, u.payout_wallet_type payout_wallet_type,
u.payout_address payout_address
FROM team_members tm
INNER JOIN users u ON u.id = tm.user_id
WHERE tm.team_id = $1
@@ -186,7 +188,10 @@ impl TeamMember {
created: m.created,
role: m.user_role,
badges: Badges::from_bits(m.badges as u64).unwrap_or_default(),
paypal_email: m.paypal_email
balance: m.balance,
payout_wallet: m.payout_wallet.map(|x| RecipientWallet::from_string(&*x)),
payout_wallet_type: m.payout_wallet_type.map(|x| RecipientType::from_string(&*x)),
payout_address: m.payout_address
},
payouts_split: m.payouts_split
})))
@@ -221,7 +226,9 @@ impl TeamMember {
SELECT tm.id id, tm.team_id team_id, tm.role member_role, tm.permissions permissions, tm.accepted accepted, tm.payouts_split payouts_split,
u.id user_id, u.github_id github_id, u.name user_name, u.email email,
u.avatar_url avatar_url, u.username username, u.bio bio,
u.created created, u.role user_role, u.badges badges, u.paypal_email paypal_email
u.created created, u.role user_role, u.badges badges, u.balance balance,
u.payout_wallet payout_wallet, u.payout_wallet_type payout_wallet_type,
u.payout_address payout_address
FROM team_members tm
INNER JOIN users u ON u.id = tm.user_id
WHERE tm.team_id = ANY($1)
@@ -250,7 +257,10 @@ impl TeamMember {
created: m.created,
role: m.user_role,
badges: Badges::from_bits(m.badges as u64).unwrap_or_default(),
paypal_email: m.paypal_email
balance: m.balance,
payout_wallet: m.payout_wallet.map(|x| RecipientWallet::from_string(&*x)),
payout_wallet_type: m.payout_wallet_type.map(|x| RecipientType::from_string(&*x)),
payout_address: m.payout_address
},
payouts_split: m.payouts_split
})))

View File

@@ -1,6 +1,7 @@
use super::ids::{ProjectId, UserId};
use crate::models::users::Badges;
use crate::models::users::{Badges, RecipientType, RecipientWallet};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
pub struct User {
pub id: UserId,
@@ -13,7 +14,10 @@ pub struct User {
pub created: DateTime<Utc>,
pub role: String,
pub badges: Badges,
pub paypal_email: Option<String>,
pub balance: Decimal,
pub payout_wallet: Option<RecipientWallet>,
pub payout_wallet_type: Option<RecipientType>,
pub payout_address: Option<String>,
}
impl User {
@@ -57,7 +61,9 @@ impl User {
"
SELECT u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges, u.paypal_email
u.created, u.role, u.badges,
u.balance, u.payout_wallet, u.payout_wallet_type,
u.payout_address
FROM users u
WHERE u.id = $1
",
@@ -79,7 +85,14 @@ impl User {
role: row.role,
badges: Badges::from_bits(row.badges as u64)
.unwrap_or_default(),
paypal_email: row.paypal_email,
balance: row.balance,
payout_wallet: row
.payout_wallet
.map(|x| RecipientWallet::from_string(&*x)),
payout_wallet_type: row
.payout_wallet_type
.map(|x| RecipientType::from_string(&*x)),
payout_address: row.payout_address,
}))
} else {
Ok(None)
@@ -97,7 +110,9 @@ impl User {
"
SELECT u.id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges, u.paypal_email
u.created, u.role, u.badges,
u.balance, u.payout_wallet, u.payout_wallet_type,
u.payout_address
FROM users u
WHERE u.github_id = $1
",
@@ -119,7 +134,14 @@ impl User {
role: row.role,
badges: Badges::from_bits(row.badges as u64)
.unwrap_or_default(),
paypal_email: row.paypal_email,
balance: row.balance,
payout_wallet: row
.payout_wallet
.map(|x| RecipientWallet::from_string(&*x)),
payout_wallet_type: row
.payout_wallet_type
.map(|x| RecipientType::from_string(&*x)),
payout_address: row.payout_address,
}))
} else {
Ok(None)
@@ -137,7 +159,9 @@ impl User {
"
SELECT u.id, u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges, u.paypal_email
u.created, u.role, u.badges,
u.balance, u.payout_wallet, u.payout_wallet_type,
u.payout_address
FROM users u
WHERE LOWER(u.username) = LOWER($1)
",
@@ -159,7 +183,14 @@ impl User {
role: row.role,
badges: Badges::from_bits(row.badges as u64)
.unwrap_or_default(),
paypal_email: row.paypal_email,
balance: row.balance,
payout_wallet: row
.payout_wallet
.map(|x| RecipientWallet::from_string(&*x)),
payout_wallet_type: row
.payout_wallet_type
.map(|x| RecipientType::from_string(&*x)),
payout_address: row.payout_address,
}))
} else {
Ok(None)
@@ -181,7 +212,9 @@ impl User {
"
SELECT u.id, u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
u.created, u.role, u.badges, u.paypal_email
u.created, u.role, u.badges,
u.balance, u.payout_wallet, u.payout_wallet_type,
u.payout_address
FROM users u
WHERE u.id = ANY($1)
",
@@ -200,7 +233,14 @@ impl User {
created: u.created,
role: u.role,
badges: Badges::from_bits(u.badges as u64).unwrap_or_default(),
paypal_email: u.paypal_email,
balance: u.balance,
payout_wallet: u
.payout_wallet
.map(|x| RecipientWallet::from_string(&*x)),
payout_wallet_type: u
.payout_wallet_type
.map(|x| RecipientType::from_string(&*x)),
payout_address: u.payout_address,
}))
})
.try_collect::<Vec<User>>()
@@ -367,6 +407,16 @@ impl User {
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM historical_payouts
WHERE user_id = $1
",
id as UserId,
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM users

View File

@@ -1,5 +1,6 @@
use crate::file_hosting::S3Host;
use crate::queue::download::DownloadQueue;
use crate::queue::payouts::PayoutsQueue;
use crate::ratelimit::errors::ARError;
use crate::ratelimit::memory::{MemoryStore, MemoryStoreActor};
use crate::ratelimit::middleware::RateLimiter;
@@ -11,6 +12,7 @@ use log::{error, info, warn};
use search::indexing::index_projects;
use search::indexing::IndexingSettings;
use std::sync::Arc;
use tokio::sync::Mutex;
mod database;
mod file_hosting;
@@ -157,6 +159,8 @@ async fn main() -> std::io::Result<()> {
}
});
let payouts_queue = Arc::new(Mutex::new(PayoutsQueue::new()));
let ip_salt = Pepper {
pepper: models::ids::Base62Id(models::ids::random_base62(11))
.to_string(),
@@ -215,6 +219,7 @@ async fn main() -> std::io::Result<()> {
.app_data(web::Data::new(file_host.clone()))
.app_data(web::Data::new(search_config.clone()))
.app_data(web::Data::new(download_queue.clone()))
.app_data(web::Data::new(payouts_queue.clone()))
.app_data(web::Data::new(ip_salt.clone()))
.configure(routes::v1_config)
.configure(routes::v2_config)
@@ -305,5 +310,9 @@ fn check_env_vars() -> bool {
failed |= check_var::<String>("STRIPE_TOKEN");
failed |= check_var::<String>("STRIPE_WEBHOOK_SECRET");
failed |= check_var::<String>("PAYPAL_API_URL");
failed |= check_var::<String>("PAYPAL_CLIENT_ID");
failed |= check_var::<String>("PAYPAL_CLIENT_SECRET");
failed
}

View File

@@ -1,5 +1,6 @@
use super::ids::Base62Id;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -45,7 +46,76 @@ pub struct User {
pub created: DateTime<Utc>,
pub role: Role,
pub badges: Badges,
pub paypal_email: Option<String>,
pub payout_data: Option<UserPayoutData>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct UserPayoutData {
pub balance: Decimal,
pub payout_wallet: Option<RecipientWallet>,
pub payout_wallet_type: Option<RecipientType>,
pub payout_address: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RecipientType {
Email,
Phone,
UserHandle,
}
impl std::fmt::Display for RecipientType {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str(self.as_str())
}
}
impl RecipientType {
pub fn from_string(string: &str) -> RecipientType {
match string {
"user_handle" => RecipientType::UserHandle,
"phone" => RecipientType::Phone,
_ => RecipientType::Email,
}
}
pub fn as_str(&self) -> &'static str {
match self {
RecipientType::Email => "email",
RecipientType::Phone => "phone",
RecipientType::UserHandle => "user_handle",
}
}
}
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub enum RecipientWallet {
Venmo,
PayPal,
}
impl std::fmt::Display for RecipientWallet {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str(self.as_str())
}
}
impl RecipientWallet {
pub fn from_string(string: &str) -> RecipientWallet {
match string {
"venmo" => RecipientWallet::Venmo,
_ => RecipientWallet::PayPal,
}
}
pub fn as_str(&self) -> &'static str {
match self {
RecipientWallet::PayPal => "paypal",
RecipientWallet::Venmo => "venmo",
}
}
}
use crate::database::models::user_item::User as DBUser;
@@ -62,7 +132,7 @@ impl From<DBUser> for User {
created: data.created,
role: Role::from_string(&*data.role),
badges: data.badges,
paypal_email: None,
payout_data: None,
}
}
}

View File

@@ -1 +1,2 @@
pub mod download;
pub mod payouts;

138
src/queue/payouts.rs Normal file
View File

@@ -0,0 +1,138 @@
use crate::models::users::{RecipientType, RecipientWallet};
use crate::routes::ApiError;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
pub struct PayoutsQueue {
credential: PaypalCredential,
credential_expires: DateTime<Utc>,
}
#[derive(Deserialize, Default)]
struct PaypalCredential {
access_token: String,
token_type: String,
expires_in: i64,
}
#[derive(Serialize)]
pub struct PayoutItem {
pub amount: PayoutAmount,
pub receiver: String,
pub note: String,
pub recipient_type: RecipientType,
pub recipient_wallet: RecipientWallet,
pub sender_item_id: String,
}
#[derive(Serialize)]
pub struct PayoutAmount {
pub currency: String,
pub value: String,
}
// Batches payouts and handles token refresh
impl PayoutsQueue {
pub fn new() -> Self {
PayoutsQueue {
credential: Default::default(),
credential_expires: Utc::now() - Duration::days(30),
}
}
pub async fn refresh_token(&mut self) -> Result<(), ApiError> {
let client = reqwest::Client::new();
let combined_key = format!(
"{}:{}",
dotenvy::var("PAYPAL_CLIENT_ID")?,
dotenvy::var("PAYPAL_CLIENT_SECRET")?
);
let formatted_key = format!("Basic {}", base64::encode(combined_key));
let mut form = HashMap::new();
form.insert("grant_type", "client_credentials");
let credential: PaypalCredential = client
.post(&format!("{}oauth2/token", dotenvy::var("PAYPAL_API_URL")?))
.header("Accept", "application/json")
.header("Accept-Language", "en_US")
.header("Authorization", formatted_key)
.form(&form)
.send()
.await
.map_err(|_| {
ApiError::Payments(
"Error while authenticating with PayPal".to_string(),
)
})?
.json()
.await
.map_err(|_| {
ApiError::Payments(
"Error while authenticating with PayPal (deser error)"
.to_string(),
)
})?;
self.credential_expires =
Utc::now() + Duration::seconds(credential.expires_in);
self.credential = credential;
Ok(())
}
pub async fn send_payout(
&mut self,
payout: PayoutItem,
) -> Result<(), ApiError> {
if self.credential_expires < Utc::now() {
self.refresh_token().await.map_err(|_| {
ApiError::Payments(
"Error while authenticating with PayPal".to_string(),
)
})?;
}
let client = reqwest::Client::new();
let res = client.post(&format!("{}payments/payouts", dotenvy::var("PAYPAL_API_URL")?))
.header("Authorization", format!("{} {}", self.credential.token_type, self.credential.access_token))
.json(&json! ({
"sender_batch_header": {
"sender_batch_id": format!("{}-payouts", Utc::now().to_rfc3339()),
"email_subject": "You have received a payment from Modrinth!",
"email_message": "Thank you for creating projects on Modrinth. Please claim this payment within 30 days.",
},
"items": vec![payout]
}))
.send().await.map_err(|_| ApiError::Payments("Error while sending payout to PayPal".to_string()))?;
if !res.status().is_success() {
#[derive(Deserialize)]
struct PayPalError {
pub body: PayPalErrorBody,
}
#[derive(Deserialize)]
struct PayPalErrorBody {
pub message: String,
}
let body: PayPalError = res.json().await.map_err(|_| {
ApiError::Payments(
"Error while registering payment in PayPal!".to_string(),
)
})?;
return Err(ApiError::Payments(format!(
"Error while registering payment in PayPal: {}",
body.body.message
)));
}
Ok(())
}
}

View File

@@ -1,10 +1,8 @@
use crate::models::ids::ProjectId;
use crate::routes::ApiError;
use crate::util::auth::check_is_admin_from_headers;
use crate::util::guards::admin_key_guard;
use crate::util::payout_calc::get_claimable_time;
use crate::DownloadQueue;
use actix_web::{get, patch, post, web, HttpRequest, HttpResponse};
use actix_web::{patch, post, web, HttpResponse};
use chrono::{DateTime, SecondsFormat, Utc};
use rust_decimal::Decimal;
use serde::Deserialize;
@@ -136,16 +134,6 @@ pub async fn process_payout(
)
})?;
sqlx::query!(
"
DELETE FROM payouts_values
WHERE created = $1
",
start
)
.execute(&mut *transaction)
.await?;
struct Project {
project_type: String,
// user_id, payouts_split
@@ -266,22 +254,27 @@ pub async fn process_payout(
let project = projects_map.get_mut(&project_id);
if let Some(project) = project {
for dependency in dependencies {
let project_multiplier: Decimal =
Decimal::from(dependency.1) / Decimal::from(dep_sum);
if dep_sum > 0 {
for dependency in dependencies {
let project_multiplier: Decimal =
Decimal::from(dependency.1)
/ Decimal::from(dep_sum);
if let Some(members) = team_members.get(&dependency.0) {
let members_sum: Decimal =
members.iter().map(|x| x.1).sum();
if let Some(members) = team_members.get(&dependency.0) {
let members_sum: Decimal =
members.iter().map(|x| x.1).sum();
for member in members {
let member_multiplier: Decimal =
member.1 / members_sum;
project.split_team_members.push((
member.0,
member_multiplier * project_multiplier,
project_id,
));
if members_sum > Decimal::from(0) {
for member in members {
let member_multiplier: Decimal =
member.1 / members_sum;
project.split_team_members.push((
member.0,
member_multiplier * project_multiplier,
project_id,
));
}
}
}
}
}
@@ -303,50 +296,80 @@ pub async fn process_payout(
let sum_tm_splits: Decimal =
project.split_team_members.iter().map(|x| x.1).sum();
for (user_id, split) in project.team_members {
let payout: Decimal = data.amount
* project_multiplier
* (split / sum_splits)
* (if !project.split_team_members.is_empty() {
&split_given
} else {
&default_split_given
});
if sum_splits > Decimal::from(0) {
for (user_id, split) in project.team_members {
let payout: Decimal = data.amount
* project_multiplier
* (split / sum_splits)
* (if !project.split_team_members.is_empty() {
&split_given
} else {
&default_split_given
});
if payout > Decimal::from(0) {
sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
VALUES ($1, $2, $3, $4)
",
user_id,
id,
payout,
start
)
if payout > Decimal::from(0) {
sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
VALUES ($1, $2, $3, $4)
",
user_id,
id,
payout,
start
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
UPDATE users
SET balance = balance + $1
WHERE id = $2
",
payout,
user_id
)
.execute(&mut *transaction)
.await?;
}
}
}
for (user_id, split, project_id) in project.split_team_members {
let payout: Decimal = data.amount
* project_multiplier
* (split / sum_tm_splits)
* split_retention;
if sum_tm_splits > Decimal::from(0) {
for (user_id, split, project_id) in project.split_team_members {
let payout: Decimal = data.amount
* project_multiplier
* (split / sum_tm_splits)
* split_retention;
sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
VALUES ($1, $2, $3, $4)
",
user_id,
project_id,
payout,
start
)
.execute(&mut *transaction)
.await?;
if payout > Decimal::from(0) {
sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
VALUES ($1, $2, $3, $4)
",
user_id,
project_id,
payout,
start
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
UPDATE users
SET balance = balance + $1
WHERE id = $2
",
payout,
user_id
)
.execute(&mut *transaction)
.await?;
}
}
}
}
}
@@ -355,35 +378,3 @@ pub async fn process_payout(
Ok(HttpResponse::NoContent().body(""))
}
#[get("/_get-payout-data")]
pub async fn get_payout_data(
req: HttpRequest,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, ApiError> {
check_is_admin_from_headers(req.headers(), &**pool).await?;
use futures::stream::TryStreamExt;
let mut payouts: HashMap<String, Decimal> = sqlx::query!(
"
SELECT u.paypal_email, SUM(pv.amount) amount
FROM payouts_values pv
INNER JOIN users u ON pv.user_id = u.id AND u.paypal_email IS NOT NULL
WHERE pv.created <= $1 AND pv.claimed = FALSE
GROUP BY u.paypal_email
",
get_claimable_time(Utc::now(), false)
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|r| (r.paypal_email.unwrap_or_default(), r.amount.unwrap_or_default())))
})
.try_collect::<HashMap<String, Decimal>>()
.await?;
let mut minimum_payout = Decimal::from(5);
payouts.retain(|_k, v| v > &mut minimum_payout);
Ok(HttpResponse::Ok().json(payouts))
}

View File

@@ -22,6 +22,7 @@ use actix_web::http::StatusCode;
use actix_web::web::{scope, Data, Query, ServiceConfig};
use actix_web::{get, HttpResponse};
use chrono::Utc;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPool;
use thiserror::Error;
@@ -273,7 +274,10 @@ pub async fn auth_callback(
created: Utc::now(),
role: Role::Developer.to_string(),
badges: Badges::default(),
paypal_email: None,
balance: Decimal::from(0),
payout_wallet: None,
payout_wallet_type: None,
payout_address: None,
}
.insert(&mut transaction)
.await?;

View File

@@ -124,7 +124,8 @@ pub fn users_config(cfg: &mut web::ServiceConfig) {
.service(users::user_icon_edit)
.service(users::user_notifications)
.service(users::user_follows)
.service(users::user_payouts),
.service(users::user_payouts)
.service(users::user_payouts_request),
);
}
@@ -172,8 +173,7 @@ pub fn admin_config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("admin")
.service(admin::count_download)
.service(admin::process_payout)
.service(admin::get_payout_data),
.service(admin::process_payout),
);
}

View File

@@ -1,14 +1,16 @@
use crate::database::models::User;
use crate::file_hosting::FileHost;
use crate::models::notifications::Notification;
use crate::models::projects::{Project, ProjectId, ProjectStatus};
use crate::models::users::{Badges, Role, UserId};
use crate::models::projects::{Project, ProjectStatus};
use crate::models::users::{
Badges, RecipientType, RecipientWallet, Role, UserId,
};
use crate::queue::payouts::{PayoutAmount, PayoutItem, PayoutsQueue};
use crate::routes::ApiError;
use crate::util::auth::{check_is_admin_from_headers, get_user_from_headers};
use crate::util::payout_calc::get_claimable_time;
use crate::util::auth::get_user_from_headers;
use crate::util::routes::read_from_payload;
use crate::util::validate::validation_errors_to_string;
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
use actix_web::{delete, get, patch, post, web, HttpRequest, HttpResponse};
use chrono::{DateTime, Utc};
use lazy_static::lazy_static;
use regex::Regex;
@@ -17,6 +19,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::PgPool;
use std::sync::Arc;
use tokio::sync::Mutex;
use validator::Validate;
#[get("user")]
@@ -157,8 +160,16 @@ pub struct EditUser {
pub bio: Option<Option<String>>,
pub role: Option<Role>,
pub badges: Option<Badges>,
#[validate(email, length(max = 128))]
pub paypal_email: Option<Option<String>>,
#[validate]
pub payout_data: Option<EditPayoutData>,
}
#[derive(Serialize, Deserialize, Validate)]
pub struct EditPayoutData {
pub payout_wallet: RecipientWallet,
pub payout_wallet_type: RecipientType,
#[validate(length(max = 128))]
pub payout_address: String,
}
#[patch("{id}")]
@@ -303,18 +314,43 @@ pub async fn user_edit(
.await?;
}
if let Some(paypal_email) = &new_user.paypal_email {
if let Some(payout_data) = &new_user.payout_data {
if payout_data.payout_wallet_type == RecipientType::UserHandle
&& payout_data.payout_wallet == RecipientWallet::PayPal
{
return Err(ApiError::InvalidInput(
"You cannot use a paypal wallet with a user handle!"
.to_string(),
));
}
if !match payout_data.payout_wallet_type {
RecipientType::Email => {
validator::validate_email(&payout_data.payout_address)
}
RecipientType::Phone => {
validator::validate_phone(&payout_data.payout_address)
}
RecipientType::UserHandle => true,
} {
return Err(ApiError::InvalidInput(
"Invalid wallet specified!".to_string(),
));
}
sqlx::query!(
"
UPDATE users
SET paypal_email = $1
WHERE (id = $2)
SET payout_wallet = $1, payout_wallet_type = $2, payout_address = $3
WHERE (id = $4)
",
paypal_email.as_deref(),
payout_data.payout_wallet.as_str(),
payout_data.payout_wallet_type.as_str(),
payout_data.payout_address,
id as crate::database::models::ids::UserId,
)
.execute(&mut *transaction)
.await?;
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
@@ -349,11 +385,8 @@ pub async fn user_icon_edit(
let cdn_url = dotenvy::var("CDN_URL")?;
let user = get_user_from_headers(req.headers(), &**pool).await?;
let id_option =
crate::database::models::User::get_id_from_username_or_id(
&*info.into_inner().0,
&**pool,
)
.await?;
User::get_id_from_username_or_id(&*info.into_inner().0, &**pool)
.await?;
if let Some(id) = id_option {
if user.id != id.into() && !user.role.is_mod() {
@@ -442,11 +475,9 @@ pub async fn user_delete(
removal_type: web::Query<RemovalType>,
) -> Result<HttpResponse, ApiError> {
let user = get_user_from_headers(req.headers(), &**pool).await?;
let id_option = crate::database::models::User::get_id_from_username_or_id(
&*info.into_inner().0,
&**pool,
)
.await?;
let id_option =
User::get_id_from_username_or_id(&*info.into_inner().0, &**pool)
.await?;
if let Some(id) = id_option {
if !user.role.is_admin() && user.id != id.into() {
@@ -458,10 +489,9 @@ pub async fn user_delete(
let mut transaction = pool.begin().await?;
let result = if &*removal_type.removal_type == "full" {
crate::database::models::User::remove_full(id, &mut transaction)
.await?
User::remove_full(id, &mut transaction).await?
} else {
crate::database::models::User::remove(id, &mut transaction).await?
User::remove(id, &mut transaction).await?
};
transaction.commit().await?;
@@ -563,11 +593,9 @@ pub async fn user_notifications(
#[derive(Serialize)]
pub struct Payout {
pub claimed: bool,
pub claimable: bool,
pub created: DateTime<Utc>,
pub project: Option<ProjectId>,
pub amount: Decimal,
pub status: String,
}
#[get("{id}/payouts")]
@@ -589,39 +617,51 @@ pub async fn user_payouts(
));
}
use futures::TryStreamExt;
let payouts: Vec<Payout> = sqlx::query!(
"
SELECT pv.mod_id, pv.created, pv.claimed, pv.amount
FROM payouts_values pv
WHERE pv.user_id = $1
ORDER BY pv.created DESC
",
id as crate::database::models::UserId
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|row| {
let claimable_time: DateTime<Utc> =
get_claimable_time(row.created, true);
Payout {
claimed: row.claimed,
claimable: Utc::now() > claimable_time,
let (all_time, last_month, payouts) = futures::future::try_join3(
sqlx::query!(
"
SELECT SUM(pv.amount) amount
FROM payouts_values pv
WHERE pv.user_id = $1
",
id as crate::database::models::UserId
)
.fetch_one(&**pool),
sqlx::query!(
"
SELECT SUM(pv.amount) amount
FROM payouts_values pv
WHERE pv.user_id = $1 AND created > NOW() - '1 month'::interval
",
id as crate::database::models::UserId
)
.fetch_one(&**pool),
sqlx::query!(
"
SELECT hp.created, hp.amount, hp.status
FROM historical_payouts hp
WHERE hp.user_id = $1
ORDER BY hp.created DESC
",
id as crate::database::models::UserId
)
.fetch_many(&**pool)
.try_filter_map(|e| async {
Ok(e.right().map(|row| Payout {
created: row.created,
project: row.mod_id.map(|x| ProjectId(x as u64)),
amount: row.amount,
}
}))
})
.try_collect::<Vec<Payout>>()
status: row.status,
}))
})
.try_collect::<Vec<Payout>>(),
)
.await?;
use futures::TryStreamExt;
Ok(HttpResponse::Ok().json(json!({
"all_time": payouts.iter().map(|x| x.amount).sum::<Decimal>(),
"current_period": payouts.iter().filter(|x| !x.claimed && !x.claimable).map(|x| x.amount).sum::<Decimal>(),
"withdrawable": payouts.iter().filter(|x| x.claimable && !x.claimed).map(|x| x.amount).sum::<Decimal>(),
"all_time": all_time.amount,
"last_month": last_month.amount,
"payouts": payouts,
})))
} else {
@@ -629,32 +669,97 @@ pub async fn user_payouts(
}
}
#[get("{id}/payouts")]
pub async fn finish_user_payout(
#[derive(Deserialize)]
pub struct PayoutData {
amount: Decimal,
}
#[post("{id}/payouts")]
pub async fn user_payouts_request(
req: HttpRequest,
info: web::Path<(String,)>,
pool: web::Data<PgPool>,
data: web::Json<PayoutData>,
payouts_queue: web::Data<Arc<Mutex<PayoutsQueue>>>,
) -> Result<HttpResponse, ApiError> {
check_is_admin_from_headers(req.headers(), &**pool).await?;
let user = get_user_from_headers(req.headers(), &**pool).await?;
let id_option =
User::get_id_from_username_or_id(&*info.into_inner().0, &**pool)
.await?;
if let Some(id) = id_option {
sqlx::query!(
"
UPDATE payouts_values
SET claimed = TRUE
WHERE (claimed = FALSE AND user_id = $1 AND created <= $2)
",
id as crate::database::models::ids::UserId,
get_claimable_time(Utc::now(), false)
)
.execute(&**pool)
.await?;
if !user.role.is_admin() && user.id != id.into() {
return Err(ApiError::CustomAuthentication(
"You do not have permission to request payouts of this user!"
.to_string(),
));
}
Ok(HttpResponse::NoContent().body(""))
if let Some(payouts_data) = user.payout_data {
if let Some(payout_address) = payouts_data.payout_address {
if let Some(payout_wallet_type) =
payouts_data.payout_wallet_type
{
if let Some(payout_wallet) = payouts_data.payout_wallet {
return if data.amount > payouts_data.balance {
let mut transaction = pool.begin().await?;
sqlx::query!(
"
INSERT INTO historical_payouts (user_id, amount, status)
VALUES ($1, $2, $3)
",
id as crate::database::models::ids::UserId,
data.amount,
"success"
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
UPDATE users
SET balance = balance - $1
WHERE id = $2
",
data.amount,
id as crate::database::models::ids::UserId
)
.execute(&mut *transaction)
.await?;
let mut payouts_queue = payouts_queue.lock().await;
payouts_queue
.send_payout(PayoutItem {
amount: PayoutAmount {
currency: "USD".to_string(),
value: data.amount.to_string(),
},
receiver: payout_address,
note: "Payment from Modrinth creator monetization program".to_string(),
recipient_type: payout_wallet_type,
recipient_wallet: payout_wallet,
sender_item_id: format!("{}-{}", UserId::from(id), Utc::now().timestamp()),
})
.await?;
transaction.commit().await?;
Ok(HttpResponse::NoContent().body(""))
} else {
Err(ApiError::InvalidInput(
"You do not have enough funds to make this payout!"
.to_string(),
))
};
}
}
}
}
Err(ApiError::InvalidInput(
"You are not enrolled in the payouts program yet!".to_string(),
))
} else {
Ok(HttpResponse::NotFound().body(""))
}

View File

@@ -1,7 +1,7 @@
use crate::database;
use crate::database::models;
use crate::database::models::project_item::QueryProject;
use crate::models::users::{Role, User, UserId};
use crate::models::users::{Role, User, UserId, UserPayoutData};
use crate::routes::ApiError;
use actix_web::http::header::HeaderMap;
use actix_web::web;
@@ -73,7 +73,12 @@ where
created: result.created,
role: Role::from_string(&result.role),
badges: result.badges,
paypal_email: result.paypal_email,
payout_data: Some(UserPayoutData {
balance: result.balance,
payout_wallet: result.payout_wallet,
payout_wallet_type: result.payout_wallet_type,
payout_address: result.payout_address,
}),
}),
None => Err(AuthenticationError::InvalidCredentials),
}

View File

@@ -2,7 +2,6 @@ pub mod auth;
pub mod env;
pub mod ext;
pub mod guards;
pub mod payout_calc;
pub mod routes;
pub mod validate;
pub mod webhook;

View File

@@ -1,22 +0,0 @@
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, Utc};
pub fn get_claimable_time(
current: DateTime<Utc>,
future: bool,
) -> DateTime<Utc> {
let adder = if current.month() == 1 && !future {
(-1, 12)
} else if current.month() == 12 && future {
(1, 1)
} else {
(0, current.month())
};
DateTime::from_utc(
NaiveDateTime::new(
NaiveDate::from_ymd(current.year() + adder.0, adder.1, 16),
NaiveTime::default(),
),
Utc,
)
}