Gift withdrawal: admin refund (Stripe) and invoice storno (Billingo)
CI / test (push) Successful in 3m25s
CI / build (push) Skipped
CI / deploy (push) Skipped

- The buyer may withdraw from a gift purchase within 14 days while the
  code is not redeemed (ÁSZF 3., 5., 6.; terms version 2026-11-23); the
  consent on Stripe's page and the confirmation e-mail say so
- Admin → Ajándékok: the gifts with payment, invoice and storno; the
  refund button locks the gift and refuses a redeemed one, withdraws the
  code, refunds the PaymentIntent on Stripe (one refund per gift via the
  Idempotency-Key, retry when Stripe refuses) and queues the storno
- Worker: cancels the issued invoice in Billingo (POST /documents/{id}/
  cancel, e-mailed to the buyer), keeps the storno invoice's id and
  number; a lost answer is not asked again but left for an admin
- A withdrawn code cannot be redeemed, sent or downloaded; "My gifts"
  shows it as refunded

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-23 21:22:46 +00:00
co-authored by Claude Opus 5.5
parent c90232f40c
commit 0d62888831
18 changed files with 1014 additions and 46 deletions
+45 -1
View File
@@ -1,5 +1,6 @@
//! The little of Stripe's API we use: Checkout Sessions (a hosted payment
//! page for one package) and webhook signature checking. The package catalog
//! page for one package), refunds (a gift given back) and webhook signature
//! checking. The package catalog
//! stays in `packages.rs`; each session carries its name and price, so no
//! products have to be kept in the Stripe dashboard.
@@ -54,6 +55,15 @@ impl Session {
}
}
/// A refund (`re_…`).
#[derive(Debug, Deserialize)]
pub struct Refund {
pub id: String,
/// `pending`, `succeeded`, `failed`, …
#[serde(default)]
pub status: Option<String>,
}
pub struct Client {
http: reqwest::Client,
}
@@ -100,6 +110,40 @@ impl Client {
Self::session(res).await
}
/// Pays a PaymentIntent back in full. The same `key` (Stripe's
/// Idempotency-Key, kept for a day) never refunds twice.
pub async fn refund(
&self,
config: &StripeConfig,
payment_intent: &str,
key: &str,
metadata: &[(&str, &str)],
) -> Result<Refund, ApiError> {
let mut form = vec![
("payment_intent".to_string(), payment_intent.to_string()),
("reason".to_string(), "requested_by_customer".to_string()),
];
form.extend(metadata.iter().map(|(k, v)| (format!("metadata[{k}]"), (*v).to_string())));
let res = self
.http
.post(format!("{}/v1/refunds", config.api_url))
.bearer_auth(&config.secret_key)
.header("Idempotency-Key", key)
.form(&form)
.send()
.await
.map_err(|e| unavailable(e.to_string()))?;
let status = res.status();
if !status.is_success() {
// Stripe says why (e.g. already refunded): the admin sees it.
let body: Value = res.json().await.unwrap_or_default();
let message = body["error"]["message"].as_str().unwrap_or("ismeretlen hiba").to_string();
tracing::error!(%status, %message, "stripe refund refused");
return Err(ApiError::conflict("refund_failed", format!("A Stripe nem téríti vissza: {message}")));
}
res.json().await.map_err(|e| unavailable(format!("unexpected response: {e}")))
}
async fn session(res: reqwest::Response) -> Result<Session, ApiError> {
let status = res.status();
if !status.is_success() {