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
+35 -3
View File
@@ -2,8 +2,10 @@
-- as for any package (stripe_checkouts.gift_id); when the payment is
-- confirmed the gift gets its code, which is e-mailed to the recipient, or
-- the buyer hands it over (the gift card can be downloaded as a PDF).
-- A gift code is not a promo code: it cannot be withdrawn (it was paid for),
-- and it adds the whole package.
-- A gift code is not a promo code: it adds the whole package, and it is only
-- withdrawn when the buyer gets the money back (the buyer may withdraw from
-- the purchase within 14 days, while it is not redeemed). The refund is
-- made by an admin: Stripe pays back, the invoice gets a storno.
CREATE TABLE gifts (
id uuid PRIMARY KEY,
buyer_id uuid REFERENCES users (id) ON DELETE SET NULL,
@@ -27,13 +29,43 @@ CREATE TABLE gifts (
-- E-mails to the recipient so far (sending again is limited).
sendings integer NOT NULL DEFAULT 0,
last_sent_at timestamptz,
-- Refunded: the code is withdrawn (set before Stripe is asked).
refunded_at timestamptz,
refunded_by uuid REFERENCES users (id) ON DELETE SET NULL,
-- Stripe's refund (re_…), or why Stripe refused it (an admin tries again).
refund_id text,
refund_error text,
CHECK (paid_at IS NULL OR code IS NOT NULL),
CHECK (redeemed_at IS NULL OR paid_at IS NOT NULL)
CHECK (redeemed_at IS NULL OR paid_at IS NOT NULL),
CHECK (redeemed_at IS NULL OR refunded_at IS NULL)
);
CREATE INDEX gifts_buyer_idx ON gifts (buyer_id, created_at DESC);
ALTER TABLE stripe_checkouts ADD COLUMN gift_id uuid UNIQUE REFERENCES gifts (id) ON DELETE SET NULL;
-- The storno of a refunded payment's invoice: the worker cancels the issued
-- invoice in Billingo (the cancellation document is the storno invoice),
-- and Billingo e-mails it to the buyer.
CREATE TABLE invoice_cancellations (
id uuid PRIMARY KEY,
invoice_id uuid NOT NULL UNIQUE REFERENCES invoices (id) ON DELETE RESTRICT,
reason text NOT NULL,
-- pending → done; failed after the last retry or a lost answer (an admin looks at it).
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'done', 'failed')),
attempts integer NOT NULL DEFAULT 0,
next_attempt_at timestamptz NOT NULL DEFAULT now(),
last_error text,
-- Set before Billingo is asked: after a lost answer the storno is not
-- asked for again (an admin checks it in Billingo).
requested_at timestamptz,
-- The storno invoice, from Billingo.
billingo_document_id bigint UNIQUE,
invoice_number text,
document jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
done_at timestamptz
);
-- A redeemed gift is recorded as an order with source `gift`.
ALTER TABLE package_orders DROP CONSTRAINT package_orders_source_check;
ALTER TABLE package_orders ADD CONSTRAINT package_orders_source_check
+4 -3
View File
@@ -375,7 +375,7 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
}
),
quote: Some(
"A fizetés előtt kérted a szolgáltatás azonnali megkezdését, és tudomásul vetted, hogy ezzel elveszíted a 14 napos elállási jogodat. Az ajándékkódot ennek megfelelően azonnal kiállítottuk."
"A vásárlástól számított 14 napon belül, amíg az ajándékkódot nem váltották be, elállhatsz a vásárlástól: írj az info@sorsfonalak.hu címre, és visszatérítjük az árát. A fizetés előtt kérted, hogy a kód beváltásakor a szolgáltatás azonnal megkezdődjön: a beváltással az elállási jog megszűnik."
.into(),
),
points: None,
@@ -425,7 +425,7 @@ fn purchase_confirmation(lang: Lang, payload: &Value, url: String) -> Result<Con
}
),
quote: Some(
"Before paying you asked us to start the service at once and acknowledged that you thereby lose your 14-day right of withdrawal. The gift code was issued at once accordingly."
"Within 14 days of the purchase, while the gift code is not redeemed, you may withdraw from the purchase: write to info@sorsfonalak.hu and we refund it. Before paying you asked for the service to start as soon as the code is redeemed: redeeming it ends the right of withdrawal."
.into(),
),
points: None,
@@ -1043,7 +1043,8 @@ mod tests {
assert!(
hu.text.contains("ajándékba vetted Bea részére.") && hu.text.contains("elküldtük ide: bea@example.com")
);
assert!(hu.text.contains("AJÁNDÉKKÓD: ABCD-EFGH-JKMN") && hu.text.contains("14 napos elállási jogodat"));
assert!(hu.text.contains("AJÁNDÉKKÓD: ABCD-EFGH-JKMN") && hu.text.contains("14 napon belül"));
assert!(hu.text.contains("a beváltással az elállási jog megszűnik"));
assert!(!hu.text.contains("jóváírtuk"));
payload["gift"]["recipient_email"] = Value::Null;
let hu = render("purchase_confirmation", "hu", &payload).unwrap();