示例#1
0
def payout(db, route, amount, ignore_high_fee=False):
    """Withdraw money to the specified bank account (`route`).
    """
    assert amount > 0
    assert route
    assert route.network == 'mango-ba'

    participant = route.participant
    if participant.is_suspended:
        raise AccountSuspended()

    payday = db.one("SELECT * FROM paydays WHERE ts_start > ts_end")
    if payday:
        raise PaydayIsRunning

    ba = BankAccount.get(route.address, user_id=participant.mangopay_user_id)

    # Do final calculations
    amount = Money(amount, 'EUR') if isinstance(amount, Decimal) else amount
    credit_amount, fee, vat = skim_credit(amount, ba)
    if credit_amount <= 0 and fee > 0:
        raise FeeExceedsAmount
    fee_percent = fee / amount
    if fee_percent > FEE_PAYOUT_WARN and not ignore_high_fee:
        raise TransactionFeeTooHigh(fee_percent, fee, amount)

    # Try to dance with MangoPay
    e_id = record_exchange(db, route, -credit_amount, fee, vat, participant,
                           'pre').id
    payout = BankWirePayOut()
    payout.AuthorId = participant.mangopay_user_id
    payout.DebitedFunds = amount.int()
    payout.DebitedWalletId = participant.get_current_wallet(
        amount.currency).remote_id
    payout.Fees = fee.int()
    payout.BankAccountId = route.address
    payout.BankWireRef = str(e_id)
    payout.Tag = str(e_id)
    try:
        test_hook()
        payout.save()
        return record_exchange_result(db, e_id,
                                      payout.Id, payout.Status.lower(),
                                      repr_error(payout), participant)
    except Exception as e:
        error = repr_exception(e)
        return record_exchange_result(db, e_id, '', 'failed', error,
                                      participant)
示例#2
0
def payout(db, route, amount, ignore_high_fee=False):
    """Withdraw money to the specified bank account (`route`).
    """
    assert amount > 0
    assert route
    assert route.network == 'mango-ba'

    participant = route.participant
    if participant.is_suspended:
        raise AccountSuspended()

    payday = db.one("SELECT * FROM paydays WHERE ts_start > ts_end")
    if payday:
        raise PaydayIsRunning

    ba = BankAccount.get(route.address, user_id=participant.mangopay_user_id)

    # Do final calculations
    amount = Money(amount, 'EUR') if isinstance(amount, Decimal) else amount
    credit_amount, fee, vat = skim_credit(amount, ba)
    if credit_amount <= 0 and fee > 0:
        raise FeeExceedsAmount
    fee_percent = fee / amount
    if fee_percent > FEE_PAYOUT_WARN and not ignore_high_fee:
        raise TransactionFeeTooHigh(fee_percent, fee, amount)

    # Try to dance with MangoPay
    e_id = record_exchange(db, route, -credit_amount, fee, vat, participant, 'pre').id
    payout = BankWirePayOut()
    payout.AuthorId = participant.mangopay_user_id
    payout.DebitedFunds = Money_to_cents(amount)
    payout.DebitedWalletId = participant.get_current_wallet(amount.currency).remote_id
    payout.Fees = Money_to_cents(fee)
    payout.BankAccountId = route.address
    payout.BankWireRef = str(e_id)
    payout.Tag = str(e_id)
    try:
        test_hook()
        payout.save()
        return record_exchange_result(
            db, e_id, payout.Id, payout.Status.lower(), repr_error(payout), participant
        )
    except Exception as e:
        error = repr_exception(e)
        return record_exchange_result(db, e_id, '', 'failed', error, participant)
示例#3
0
def payout(db, participant, amount, ignore_high_fee=False):
    assert amount > 0

    if participant.is_suspended:
        raise AccountSuspended()

    payday = db.one("SELECT * FROM paydays WHERE ts_start > ts_end")
    if payday:
        raise PaydayIsRunning

    route = ExchangeRoute.from_network(participant, 'mango-ba')
    assert route
    ba = BankAccount.get(route.address, user_id=participant.mangopay_user_id)

    # Do final calculations
    credit_amount, fee, vat = skim_credit(amount, ba)
    if credit_amount <= 0 and fee > 0:
        raise FeeExceedsAmount
    fee_percent = fee / amount
    if fee_percent > FEE_PAYOUT_WARN and not ignore_high_fee:
        raise TransactionFeeTooHigh(fee_percent, fee, amount)

    # Try to dance with MangoPay
    e_id = record_exchange(db, route, -credit_amount, fee, vat, participant, 'pre')
    payout = BankWirePayOut()
    payout.AuthorId = participant.mangopay_user_id
    payout.DebitedFunds = Money(int(amount * 100), 'EUR')
    payout.DebitedWalletId = participant.mangopay_wallet_id
    payout.Fees = Money(int(fee * 100), 'EUR')
    payout.BankAccountId = route.address
    payout.BankWireRef = str(e_id)
    payout.Tag = str(e_id)
    try:
        test_hook()
        payout.save()
        return record_exchange_result(db, e_id, payout.Status.lower(), repr_error(payout), participant)
    except Exception as e:
        error = repr_exception(e)
        return record_exchange_result(db, e_id, 'failed', error, participant)