Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions abis/invoice.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"unpause",
"set_grace_window",
"get_grace_window",
"release_escrow"
"release_escrow",
"approve_refund"
],
"events": [
"invoice_created",
Expand All @@ -23,6 +24,7 @@
"invoice_refund_req",
"escrow_released",
"contract_paused",
"contract_unpaused"
"contract_unpaused",
"refund_approved"
]
}
5 changes: 5 additions & 0 deletions contracts/invoice/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub fn invoice_refund_requested(env: &Env, id: u64, invoice: &Invoice) {
);
}

pub fn refund_approved(env: &Env, id: u64, invoice: &Invoice) {
env.events()
.publish((Symbol::new(env, "refund_approved"), id), invoice.clone());
}

pub fn escrow_released(env: &Env, id: u64, invoice: &Invoice) {
env.events()
.publish((Symbol::new(env, "escrow_released"), id), invoice.clone());
Expand Down
2 changes: 2 additions & 0 deletions contracts/invoice/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub enum InvoiceStatus {
RefundRequested,
/// Escrow funds have been released to the merchant after payment confirmation.
Released,
/// Refund has been approved by admin; terminal status for disputed invoices.
Refunded,
}

// contracttype enum wrappers for optional complex types; Option<Address> and
Expand Down
24 changes: 24 additions & 0 deletions contracts/invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,30 @@ impl InvoiceContract {
Ok(())
}

/// Approve a refund request. Admin-only. Transitions RefundRequested → Refunded.
pub fn approve_refund(env: Env, admin: Address, id: u64) -> Result<(), InvoiceError> {
require_admin(&env, &admin)?;
require_not_paused(&env)?;

let mut invoice: Invoice = env
.storage()
.persistent()
.get(&DataKey::Invoice(id))
.ok_or(InvoiceError::NotFound)?;

if invoice.status != InvoiceStatus::RefundRequested {
return Err(InvoiceError::NotRefundRequested);
}

invoice.status = InvoiceStatus::Refunded;
env.storage()
.persistent()
.set(&DataKey::Invoice(id), &invoice);
append_history(&env, id, InvoiceStatus::RefundRequested, InvoiceStatus::Refunded);
events::refund_approved(&env, id, &invoice);
Ok(())
}

// --- #9: paginated merchant invoice index read ---

/// Return a page of invoice IDs for `merchant`.
Expand Down
67 changes: 67 additions & 0 deletions contracts/invoice/tests/invoice_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ fn test_abi_snapshot_matches_contract() {
"set_grace_window",
"get_grace_window",
"release_escrow",
"approve_refund",
]
.iter()
.copied()
Expand All @@ -628,6 +629,7 @@ fn test_abi_snapshot_matches_contract() {
"escrow_released",
"contract_paused",
"contract_unpaused",
"refund_approved",
]
.iter()
.copied()
Expand Down Expand Up @@ -966,3 +968,68 @@ fn test_request_refund_only_recorded_payer_can_call() {
assert_eq!(client.get_invoice(&id).status, InvoiceStatus::RefundRequested);
}

// --- approve_refund tests ---

#[test]
fn test_approve_refund_transitions_to_refunded() {
let (env, admin, client) = setup();
let merchant = Address::generate(&env);
let payer = Address::generate(&env);
let id = client.create_invoice(
&merchant,
&10_000_000,
&10_250_000,
&3600,
&MaybeBytes::None,
&MaybeBytes::None,
&0,
);
client.mark_paid(&admin, &id, &payer, &MaybeBytes::None);
client.request_refund(&payer, &id);
assert_eq!(client.get_invoice(&id).status, InvoiceStatus::RefundRequested);
client.approve_refund(&admin, &id);
assert_eq!(client.get_invoice(&id).status, InvoiceStatus::Refunded);
}

#[test]
fn test_approve_refund_requires_admin() {
let (env, admin, client) = setup();
let merchant = Address::generate(&env);
let payer = Address::generate(&env);
let rogue = Address::generate(&env);
let id = client.create_invoice(
&merchant,
&10_000_000,
&10_250_000,
&3600,
&MaybeBytes::None,
&MaybeBytes::None,
&0,
);
client.mark_paid(&admin, &id, &payer, &MaybeBytes::None);
client.request_refund(&payer, &id);
let err = client.try_approve_refund(&rogue, &id).unwrap_err().unwrap();
assert_eq!(err, InvoiceError::Unauthorized);
assert_eq!(client.get_invoice(&id).status, InvoiceStatus::RefundRequested);
}

#[test]
fn test_approve_refund_requires_refund_requested_status() {
let (env, admin, client) = setup();
let merchant = Address::generate(&env);
let payer = Address::generate(&env);
let id = client.create_invoice(
&merchant,
&10_000_000,
&10_250_000,
&3600,
&MaybeBytes::None,
&MaybeBytes::None,
&0,
);
client.mark_paid(&admin, &id, &payer, &MaybeBytes::None);
// Invoice is Paid, not RefundRequested → should fail.
let err = client.try_approve_refund(&admin, &id).unwrap_err().unwrap();
assert_eq!(err, InvoiceError::NotRefundRequested);
assert_eq!(client.get_invoice(&id).status, InvoiceStatus::Paid);
}
Loading