from .money_add import subtract_money from .money_amount import Money from .money_compare import min_money 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, )