| 1 |
|
| 2 |
|
| 3 |
use chrono::{DateTime, Utc}; |
| 4 |
use serde::Serialize; |
| 5 |
use sqlx::FromRow; |
| 6 |
|
| 7 |
use super::super::id_types::{ |
| 8 |
ClaimToken, DownloadToken, ItemId, LoginTokenId, ProjectId, PromoCodeId, TransactionId, UserId, |
| 9 |
}; |
| 10 |
use super::super::validated_types::{Cents, KeyCode}; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#[derive(Debug, Clone)] |
| 15 |
pub struct CompletedTransactionInfo { |
| 16 |
|
| 17 |
pub stripe_payment_intent_id: String, |
| 18 |
|
| 19 |
pub completed_at: DateTime<Utc>, |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Debug, Clone, FromRow, Serialize)] |
| 33 |
pub struct DbTransaction { |
| 34 |
|
| 35 |
pub id: TransactionId, |
| 36 |
|
| 37 |
pub buyer_id: Option<UserId>, |
| 38 |
|
| 39 |
pub seller_id: Option<UserId>, |
| 40 |
|
| 41 |
pub item_id: Option<ItemId>, |
| 42 |
|
| 43 |
pub amount_cents: Cents, |
| 44 |
|
| 45 |
pub platform_fee_cents: Cents, |
| 46 |
|
| 47 |
|
| 48 |
pub currency: String, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
pub presentment_amount_cents: Option<i64>, |
| 53 |
|
| 54 |
|
| 55 |
pub presentment_currency: Option<String>, |
| 56 |
|
| 57 |
pub status: super::super::TransactionStatus, |
| 58 |
|
| 59 |
pub stripe_payment_intent_id: Option<String>, |
| 60 |
|
| 61 |
pub stripe_checkout_session_id: Option<String>, |
| 62 |
|
| 63 |
pub created_at: DateTime<Utc>, |
| 64 |
|
| 65 |
pub completed_at: Option<DateTime<Utc>>, |
| 66 |
|
| 67 |
|
| 68 |
pub item_title: Option<String>, |
| 69 |
|
| 70 |
pub seller_username: Option<String>, |
| 71 |
|
| 72 |
pub share_contact: bool, |
| 73 |
|
| 74 |
pub project_id: Option<ProjectId>, |
| 75 |
|
| 76 |
pub parent_transaction_id: Option<TransactionId>, |
| 77 |
|
| 78 |
pub promo_code_id: Option<PromoCodeId>, |
| 79 |
|
| 80 |
pub guest_email: Option<String>, |
| 81 |
|
| 82 |
pub claim_token: Option<ClaimToken>, |
| 83 |
|
| 84 |
pub claimed_by: Option<UserId>, |
| 85 |
|
| 86 |
pub download_token: Option<DownloadToken>, |
| 87 |
} |
| 88 |
|
| 89 |
impl DbTransaction { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub fn currency(&self) -> crate::currency::SettlementCurrency { |
| 98 |
crate::currency::SettlementCurrency::from_db(&self.currency) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn completed_info(&self) -> Option<CompletedTransactionInfo> { |
| 106 |
Some(CompletedTransactionInfo { |
| 107 |
stripe_payment_intent_id: self.stripe_payment_intent_id.clone()?, |
| 108 |
completed_at: self.completed_at?, |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
#[derive(Debug, Clone, FromRow)] |
| 115 |
pub struct DbTransactionExportRow { |
| 116 |
pub created_at: DateTime<Utc>, |
| 117 |
pub item_id: Option<ItemId>, |
| 118 |
pub item_title: Option<String>, |
| 119 |
pub amount_cents: Cents, |
| 120 |
pub status: super::super::TransactionStatus, |
| 121 |
|
| 122 |
pub buyer_email: Option<String>, |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
#[derive(Debug, Clone, FromRow)] |
| 127 |
pub struct DbPurchaseRow { |
| 128 |
|
| 129 |
pub transaction_id: TransactionId, |
| 130 |
|
| 131 |
pub item_id: ItemId, |
| 132 |
|
| 133 |
pub title: String, |
| 134 |
|
| 135 |
pub creator: String, |
| 136 |
|
| 137 |
pub item_type: super::super::ItemType, |
| 138 |
|
| 139 |
pub purchased_at: DateTime<Utc>, |
| 140 |
|
| 141 |
pub is_free: bool, |
| 142 |
|
| 143 |
pub license_key_code: Option<KeyCode>, |
| 144 |
|
| 145 |
pub has_new_version: bool, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
#[derive(Debug, Clone, FromRow)] |
| 150 |
#[allow(dead_code)] |
| 151 |
pub struct DbLoginToken { |
| 152 |
|
| 153 |
pub id: LoginTokenId, |
| 154 |
|
| 155 |
pub user_id: UserId, |
| 156 |
|
| 157 |
pub token_hash: String, |
| 158 |
|
| 159 |
pub expires_at: DateTime<Utc>, |
| 160 |
|
| 161 |
pub used_at: Option<DateTime<Utc>>, |
| 162 |
|
| 163 |
pub created_at: DateTime<Utc>, |
| 164 |
} |
| 165 |
|
| 166 |
#[cfg(test)] |
| 167 |
mod tests { |
| 168 |
use super::*; |
| 169 |
|
| 170 |
fn make_transaction( |
| 171 |
status: super::super::super::TransactionStatus, |
| 172 |
pi_id: Option<&str>, |
| 173 |
completed: Option<DateTime<Utc>>, |
| 174 |
) -> DbTransaction { |
| 175 |
DbTransaction { |
| 176 |
id: TransactionId::nil(), |
| 177 |
buyer_id: Some(UserId::nil()), |
| 178 |
seller_id: None, |
| 179 |
item_id: None, |
| 180 |
amount_cents: Cents::ZERO, |
| 181 |
platform_fee_cents: Cents::ZERO, |
| 182 |
currency: "usd".to_string(), |
| 183 |
presentment_amount_cents: None, |
| 184 |
presentment_currency: None, |
| 185 |
status, |
| 186 |
stripe_payment_intent_id: pi_id.map(std::string::ToString::to_string), |
| 187 |
stripe_checkout_session_id: None, |
| 188 |
created_at: Utc::now(), |
| 189 |
completed_at: completed, |
| 190 |
item_title: None, |
| 191 |
seller_username: None, |
| 192 |
share_contact: false, |
| 193 |
project_id: None, |
| 194 |
parent_transaction_id: None, |
| 195 |
promo_code_id: None, |
| 196 |
guest_email: None, |
| 197 |
claim_token: None, |
| 198 |
claimed_by: None, |
| 199 |
download_token: None, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn completed_info_for_paid_transaction() { |
| 205 |
let now = Utc::now(); |
| 206 |
let tx = make_transaction( |
| 207 |
super::super::super::TransactionStatus::Completed, |
| 208 |
Some("pi_123"), |
| 209 |
Some(now), |
| 210 |
); |
| 211 |
let info = tx.completed_info().unwrap(); |
| 212 |
assert_eq!(info.stripe_payment_intent_id, "pi_123"); |
| 213 |
assert_eq!(info.completed_at, now); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn completed_info_none_for_pending() { |
| 218 |
let tx = make_transaction(super::super::super::TransactionStatus::Pending, None, None); |
| 219 |
assert!(tx.completed_info().is_none()); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn completed_info_none_for_free_claim() { |
| 224 |
|
| 225 |
let tx = make_transaction( |
| 226 |
super::super::super::TransactionStatus::Completed, |
| 227 |
None, |
| 228 |
Some(Utc::now()), |
| 229 |
); |
| 230 |
assert!(tx.completed_info().is_none()); |
| 231 |
} |
| 232 |
} |
| 233 |
|