Use auto payments with paypal (#472)
* Use auto payments with paypal * Remove sandbox key
This commit is contained in:
parent
35891c74cd
commit
2c1bcaafc1
6
.env
6
.env
@ -48,4 +48,8 @@ ARIADNE_ADMIN_KEY=feedbeef
|
||||
ARIADNE_URL=https://staging-ariadne.modrinth.com/v1/
|
||||
|
||||
STRIPE_TOKEN=none
|
||||
STRIPE_WEBHOOK_SECRET=none
|
||||
STRIPE_WEBHOOK_SECRET=none
|
||||
|
||||
PAYPAL_API_URL=https://api-m.sandbox.paypal.com/v1/
|
||||
PAYPAL_CLIENT_ID=none
|
||||
PAYPAL_CLIENT_SECRET=none
|
||||
|
||||
85
Cargo.lock
generated
85
Cargo.lock
generated
@ -425,6 +425,15 @@ version = "1.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf"
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "1.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@ -1420,6 +1429,15 @@ dependencies = [
|
||||
"nom 5.1.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
@ -1475,7 +1493,7 @@ dependencies = [
|
||||
"futures-timer",
|
||||
"hex",
|
||||
"hmac 0.11.0",
|
||||
"itertools",
|
||||
"itertools 0.10.5",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"meilisearch-sdk",
|
||||
@ -1564,6 +1582,12 @@ dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
|
||||
|
||||
[[package]]
|
||||
name = "local-channel"
|
||||
version = "0.1.3"
|
||||
@ -1601,6 +1625,15 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lru-cache"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
|
||||
dependencies = [
|
||||
"linked-hash-map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.9"
|
||||
@ -1795,6 +1828,12 @@ version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
|
||||
|
||||
[[package]]
|
||||
name = "oncemutex"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44d11de466f4a3006fe8a5e7ec84e93b79c70cb992ae0aa0eb631ad2df8abfe2"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.3.0"
|
||||
@ -1920,6 +1959,26 @@ version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
|
||||
|
||||
[[package]]
|
||||
name = "phonenumber"
|
||||
version = "0.3.1+8.12.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1261a014e5f5e048bf2c6f1a72fa5e4c223009dc5f296a385b95fe19b464608f"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"either",
|
||||
"fnv",
|
||||
"itertools 0.9.0",
|
||||
"lazy_static",
|
||||
"nom 5.1.2",
|
||||
"quick-xml",
|
||||
"regex",
|
||||
"regex-cache",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project"
|
||||
version = "1.0.12"
|
||||
@ -2011,6 +2070,15 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quick-xml"
|
||||
version = "0.18.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3cc440ee4802a86e357165021e3e255a9143724da31db1e2ea540214c96a0f82"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.21"
|
||||
@ -2081,6 +2149,18 @@ dependencies = [
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-cache"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f7b62d69743b8b94f353b6b7c3deb4c5582828328bcb8d5fedf214373808793"
|
||||
dependencies = [
|
||||
"lru-cache",
|
||||
"oncemutex",
|
||||
"regex",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.27"
|
||||
@ -2516,7 +2596,7 @@ version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f87e292b4291f154971a43c3774364e2cbcaec599d3f5bf6fa9d122885dbc38a"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"itertools 0.10.5",
|
||||
"nom 7.1.1",
|
||||
"unicode_categories",
|
||||
]
|
||||
@ -2969,6 +3049,7 @@ checksum = "32ad5bf234c7d3ad1042e5252b7eddb2c4669ee23f32c7dd0e9b7705f07ef591"
|
||||
dependencies = [
|
||||
"idna 0.2.3",
|
||||
"lazy_static",
|
||||
"phonenumber",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
|
||||
@ -56,7 +56,7 @@ zip = { git = "https://github.com/zip-rs/zip", rev = "bb230ef56adc13436d1fcdfaa4
|
||||
|
||||
itertools = "0.10.5"
|
||||
|
||||
validator = { version = "0.16.0", features = ["derive"] }
|
||||
validator = { version = "0.16.0", features = ["derive", "phone"] }
|
||||
regex = "1.6.0"
|
||||
censor = "0.2.0"
|
||||
|
||||
|
||||
24
migrations/20221107171016_payouts-overhaul.sql
Normal file
24
migrations/20221107171016_payouts-overhaul.sql
Normal file
@ -0,0 +1,24 @@
|
||||
ALTER TABLE users DROP COLUMN paypal_email;
|
||||
ALTER TABLE payouts_values DROP COLUMN claimed;
|
||||
|
||||
ALTER TABLE users ADD COLUMN payout_wallet varchar(128) NULL;
|
||||
ALTER TABLE users ADD COLUMN payout_wallet_type varchar(128) NULL;
|
||||
ALTER TABLE users ADD COLUMN payout_address varchar(128) NULL;
|
||||
ALTER TABLE users ADD COLUMN balance numeric(96, 48) NOT NULL DEFAULT 0;
|
||||
|
||||
UPDATE users
|
||||
SET balance = COALESCE((SELECT SUM(T2.amount) FROM payouts_values T2 WHERE T2.user_id = users.id), 0, 0)
|
||||
WHERE id > 1;
|
||||
|
||||
CREATE TABLE historical_payouts (
|
||||
id bigserial PRIMARY KEY,
|
||||
user_id bigint REFERENCES users NOT NULL,
|
||||
amount numeric(96, 48) NOT NULL,
|
||||
created timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
status varchar(128) NOT NULL
|
||||
);
|
||||
|
||||
DELETE FROM payouts_values WHERE amount = 0;
|
||||
|
||||
CREATE INDEX historical_payouts_user_id
|
||||
ON historical_payouts (user_id);
|
||||
1342
sqlx-data.json
1342
sqlx-data.json
File diff suppressed because it is too large
Load Diff
@ -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
|
||||
})))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1 +1,2 @@
|
||||
pub mod download;
|
||||
pub mod payouts;
|
||||
|
||||
138
src/queue/payouts.rs
Normal file
138
src/queue/payouts.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
@ -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(""))
|
||||
}
|
||||
|
||||
@ -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),
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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,
|
||||
)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user