forked from SiLioLabs/PayFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrial.rs
More file actions
34 lines (26 loc) · 1.13 KB
/
Copy pathtrial.rs
File metadata and controls
34 lines (26 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use crate::storage;
use soroban_sdk::{Address, Env};
/// Returns the timestamp when the trial period ends, or None if no active trial.
/// A trial is active if the last_charged timestamp is set in the future.
pub fn get_trial_end(env: Env, user: Address) -> Option<u64> {
let sub = storage::get_subscription(&env, &user)?;
let now = env.ledger().timestamp();
if sub.last_charged > now {
Some(sub.last_charged)
} else {
None
}
}
pub fn extend_trial(env: &Env, user: &Address, additional_seconds: u64) {
if additional_seconds == 0 {
env.panic_with_error(crate::errors::ContractError::IntervalMustBePositive);
}
let mut sub = storage::get_subscription(env, user)
.unwrap_or_else(|| env.panic_with_error(crate::errors::ContractError::NoSubscriptionFound));
if !sub.active {
env.panic_with_error(crate::errors::ContractError::SubscriptionInactive);
}
sub.last_charged = sub.last_charged.checked_add(additional_seconds).unwrap();
storage::set_subscription(env, user, &sub);
crate::events::publish_trial_extended(env, user, additional_seconds, sub.last_charged);
}