1use super::transfer::descriptor_for;
7use super::*;
8use crate::codec::decode_segment;
9use crate::codec::defs::HiIps1;
10
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14#[non_exhaustive]
15pub enum SepaOrderStatus {
16 Scheduled,
18 RejectedByFirstCollector,
20 InProcess,
22 Processed,
24 ReturnInitiated,
26 Failed,
28 Executed,
30 RejectedByPayerPsp,
32 RejectedByPayeePsp,
34 Other(String),
36}
37
38impl SepaOrderStatus {
39 fn from_code(code: &str) -> Self {
40 match code {
41 "1" => Self::Scheduled,
42 "2" => Self::RejectedByFirstCollector,
43 "3" => Self::InProcess,
44 "4" => Self::Processed,
45 "5" => Self::ReturnInitiated,
46 "6" => Self::Failed,
47 "7" => Self::Executed,
48 "8" => Self::RejectedByPayerPsp,
49 "9" => Self::RejectedByPayeePsp,
50 other => Self::Other(other.to_string()),
51 }
52 }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57pub struct InstantStatus {
58 pub status: Option<SepaOrderStatus>,
61 pub again_after: Option<Timestamp>,
64 pub source: StatusSource,
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "snake_case")]
71pub enum StatusSource {
72 StatusRequest,
74 Statement,
83}
84
85pub async fn instant_status(
99 dialog: &mut Dialog,
100 from: &Account,
101 receipt: &TransferReceipt,
102) -> Result<Step<InstantStatus>> {
103 dialog.ready()?;
104 let params = dialog.bank_params();
105 let order_id = match (
106 params.order_version(Order::InstantStatus),
107 &receipt.order_id,
108 ) {
109 (Err(e @ FinTSError::NoBpd { .. }), _) => return Err(e),
110 (Ok(_), Some(id)) => id.clone(),
111 _ => return from_account(dialog, from, receipt).await,
112 };
113 let descriptor = descriptor_for(params, Order::InstantStatus)?;
114 let min_wait = params
115 .bpd()
116 .and_then(|b| b.instant())
117 .and_then(|i| i.status)
118 .map(|s| s.min_wait);
119 dialog
120 .start(JobInput::InstantStatus {
121 account: from.clone(),
122 descriptors: vec![descriptor.announced],
123 order_id,
124 min_wait,
125 })
126 .await?
127 .into_step()
128}
129
130pub(super) fn read(response: &Response, min_wait: Option<u32>) -> Result<InstantStatus> {
132 let status = response
133 .find_segment("HIIPS")
134 .map(|raw| {
135 decode_segment::<HiIps1>(raw)
136 .map(|(_, v)| v)
137 .map_err(FinTSError::SegmentUnreadable)
138 })
139 .transpose()?
140 .and_then(|s| s.status)
141 .map(|c| SepaOrderStatus::from_code(c.as_str()));
142 if !response.has(Kind::InstantStatusQuery) {
143 return Ok(InstantStatus {
144 status,
145 again_after: None,
146 source: StatusSource::StatusRequest,
147 });
148 }
149 let wait = min_wait.map(|s| Duration::from_secs(u64::from(s)));
150 Ok(InstantStatus {
151 status: None,
152 again_after: not_before_after(Timestamp::now(), wait),
153 source: StatusSource::StatusRequest,
154 })
155}
156
157async fn from_account(
159 dialog: &mut Dialog,
160 from: &Account,
161 receipt: &TransferReceipt,
162) -> Result<Step<InstantStatus>> {
163 info!("[FinTS] No HKIPS for this payment: reading the statement instead");
164 let format = choose_format(dialog.bank_params(), from, Format::Auto)?;
165 let today = dialog.inner.now().date();
166 dialog
167 .start(JobInput::Transactions {
168 account: from.clone(),
169 range: Range::new(receipt.sent_on.min(today), today),
170 format,
171 touchdown: None,
172 booked: Vec::new(),
173 pending: Vec::new(),
174 pending_sent: false,
175 find_payment: Some(receipt.end_to_end_id.clone()),
176 })
177 .await?
178 .into_step()
179}
180
181pub(super) fn from_statement(statement: &Statement, id: &str) -> InstantStatus {
184 let ours =
185 |t: &Transaction| t.amount.is_sign_negative() && t.end_to_end_id.as_deref() == Some(id);
186 let pending = match &statement.pending {
187 Pending::Reported(p) => p.as_slice(),
188 _ => &[],
189 };
190 let status = if statement.booked.iter().any(ours) {
191 Some(SepaOrderStatus::Executed)
192 } else if pending.iter().any(ours) {
193 Some(SepaOrderStatus::InProcess)
194 } else {
195 None
196 };
197 InstantStatus {
198 status,
199 again_after: None,
200 source: StatusSource::Statement,
201 }
202}