Functional Weave
Code in Rust

retail.gift-card-balance@1.0.0

impl/python.py

901 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .money_add import subtract_money  ← from money.add ^1.0.0 · built alongside by fune
from .money_amount import Money  ← from money.amount ^1.0.0 · built alongside by fune
from .money_compare import min_money  ← from money.compare ^1.0.0 · built alongside by fune
from .retail_gift_card_balance_types import GiftCardRedemption


def redeem_gift_card(balance: Money, amount_due: Money) -> GiftCardRedemption:
    """Pay as much of ``amount_due`` as the card's balance allows."""
    if balance.minor < 0:
        raise ValueError("balance must not be negative, received %d" % balance.minor)
    if amount_due.minor < 0:
        raise ValueError(
            "amountDue must not be negative, received %d; a refund to a gift card is a top-up" % amount_due.minor
        )
    applied = min_money(balance, amount_due)
    still_due = subtract_money(amount_due, applied)
    return GiftCardRedemption(
        applied=applied,
        remaining_balance=subtract_money(balance, applied),
        still_due=still_due,
        fully_paid=still_due.minor == 0,
    )