refundableSale is a PMSale object with properties refundableTip, refundableTax and refundableSubtotal. The requestRefundFor method takes a total refund amount as argument along with the refundable sale.The requirement is you should first refund from the tips, then from the tax and then from the subtotal, if needed.The flow works like this: Refund the requested amount from tipsIf there's more amount to refund, refund from tax nextIf there's even more amount to refund, refund from subtotalAs you can see, my implementation of this has a lot of duplicate lines that I want to get rid of.func requestRefundFor(amount refundAmount: NSDecimalNumber, sale refundableSale: PMSale) {    var refundRequestedAmount = refundAmount    var tipAmountToRefund = NSDecimalNumber.zero()    var taxAmountToRefund = NSDecimalNumber.zero()    var subtotalAmountToRefund = NSDecimalNumber.zero()    if refundRequestedAmount.isGreaterThanOrEqualTo(refundableSale.refundableTip) {        refundRequestedAmount = refundRequestedAmount.decimalNumberBySubtracting(refundableSale.refundableTip)        tipAmountToRefund = refundableSale.refundableTip        if refundRequestedAmount.isGreaterThanOrEqualTo(refundableSale.refundableTax) {            refundRequestedAmount = refundRequestedAmount.decimalNumberBySubtracting(refundableSale.refundableTax)            taxAmountToRefund = refundableSale.refundableTax            if refundRequestedAmount.isGreaterThanOrEqualTo(refundableSale.refundableSubtotal) {                refundRequestedAmount = refundRequestedAmount.decimalNumberBySubtracting(refundableSale.refundableSubtotal)                subtotalAmountToRefund = refundableSale.refundableSubtotal            } else {                subtotalAmountToRefund = refundRequestedAmount            }        } else {            taxAmountToRefund = refundRequestedAmount        }    } else {        tipAmountToRefund = refundRequestedAmount    }    requestRefund(sale: refundableSale, subtotalRefundAmount: subtotalAmountToRefund, tip: tipAmountToRefund, tax: taxAmountToRefund)}
Calculating the tip and tax portions for a refund amount
swift