You've already forked AstralRinth
forked from didirus/AstralRinth
Use auto payments with paypal (#472)
* Use auto payments with paypal * Remove sandbox key
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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?;
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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(""))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user