def skim_credit(amount, ba): """Given a payout amount, return a lower amount, the fee, and taxes. The returned amount can be negative, look out for that. """ typecheck(amount, Decimal) country = get_bank_account_country(ba) if country in SEPA: fee = FEE_PAYOUT else: fee = FEE_PAYOUT_OUTSIDE_SEPA return skim_amount(amount, fee)
def prepare_direct_debit(db, route, amount): """Prepare to debit a bank account. """ typecheck(amount, Decimal) assert route.network == 'mango-ba' participant = route.participant debit_amount, fee, vat = upcharge_direct_debit(amount) amount = debit_amount - fee if not participant.mangopay_wallet_id: create_wallet(db, participant) status = 'pre' if route.mandate else 'pre-mandate' return record_exchange(db, route, amount, fee, vat, participant, status)
def skim_credit(amount, ba): """Given a payout amount, return a lower amount, the fee, and taxes. The returned amount can be negative, look out for that. """ typecheck(amount, Decimal) if ba.Type == 'IBAN': country = ba.Details.IBAN[:2].upper() elif ba.Type in ('US', 'GB', 'CA'): country = ba.Type else: assert ba.Type == 'OTHER', ba.Type country = ba.Details.Country.upper() if country in SEPA_ZONE: fee = FEE_PAYOUT else: fee = FEE_PAYOUT_OUTSIDE_SEPA return skim_amount(amount, fee)
def charge(db, participant, amount, return_url): """Charge the participant's credit card. Amount should be the nominal amount. We'll compute fees below this function and add it to amount to end up with charge_amount. """ typecheck(amount, Decimal) route = ExchangeRoute.from_network(participant, 'mango-cc') assert route charge_amount, fee, vat = upcharge_card(amount) amount = charge_amount - fee e_id = record_exchange(db, route, amount, fee, vat, participant, 'pre') payin = PayIn() payin.AuthorId = participant.mangopay_user_id if not participant.mangopay_wallet_id: create_wallet(db, participant) payin.CreditedWalletId = participant.mangopay_wallet_id payin.DebitedFunds = Money(int(charge_amount * 100), 'EUR') payin.ExecutionDetails = PayInExecutionDetailsDirect( CardId=route.address, SecureModeReturnURL=return_url, ) payin.Fees = Money(int(fee * 100), 'EUR') payin.PaymentDetails = PayInPaymentDetailsCard( CardType='CB_VISA_MASTERCARD') payin.Tag = str(e_id) try: test_hook() payin = mangoapi.payIns.Create(payin) except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, 'failed', error, participant) if payin.ExecutionDetails.SecureModeRedirectURL: raise Redirect(payin.ExecutionDetails.SecureModeRedirectURL) return record_exchange_result(db, e_id, payin.Status.lower(), repr_error(payin), participant)
def charge(db, route, amount, return_url): """Charge the given credit card (`route`). Amount should be the nominal amount. We'll compute fees below this function and add it to amount to end up with charge_amount. """ typecheck(amount, Decimal) assert route assert route.network == 'mango-cc' participant = route.participant charge_amount, fee, vat = upcharge_card(amount) amount = charge_amount - fee if not participant.mangopay_wallet_id: create_wallet(db, participant) e_id = record_exchange(db, route, amount, fee, vat, participant, 'pre').id payin = DirectPayIn() payin.AuthorId = participant.mangopay_user_id payin.CreditedWalletId = participant.mangopay_wallet_id payin.DebitedFunds = Money(int(charge_amount * 100), 'EUR') payin.CardId = route.address payin.SecureModeReturnURL = return_url payin.Fees = Money(int(fee * 100), 'EUR') payin.Tag = str(e_id) try: test_hook() payin.save() except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, '', 'failed', error, participant) if payin.SecureModeRedirectURL: raise Redirect(payin.SecureModeRedirectURL) return record_exchange_result(db, e_id, payin.Id, payin.Status.lower(), repr_error(payin), participant)
def upcharge(amount, fees, min_amount): """Given an amount, return a higher amount and the difference. """ typecheck(amount, Decimal) if amount < min_amount: amount = min_amount # a = c - vf * c - ff => c = (a + ff) / (1 - vf) # a = amount ; c = charge amount ; ff = fixed fee ; vf = variable fee charge_amount = (amount + fees.fix) / (1 - fees.var) fee = charge_amount - amount # + VAT vat = fee * FEE_VAT charge_amount += vat fee += vat # Round charge_amount = charge_amount.quantize(D_CENT, rounding=ROUND_UP) fee = fee.quantize(D_CENT, rounding=ROUND_UP) vat = vat.quantize(D_CENT, rounding=ROUND_UP) return charge_amount, fee, vat