def payin_bank_wire(db, participant, debit_amount):
    """Prepare to receive a bank wire payin.

    The amount should be how much the user intends to send, not how much will
    arrive in the wallet.
    """

    route = ExchangeRoute.upsert_bankwire_route(participant)

    if not isinstance(debit_amount, Money):
        debit_amount = Money(debit_amount, 'EUR')
    amount, fee, vat = skim_bank_wire(debit_amount)

    wallet = participant.get_current_wallet(amount.currency, create=True)
    e_id = record_exchange(db, route, amount, fee, vat, participant, 'pre').id
    payin = BankWirePayIn()
    payin.AuthorId = participant.mangopay_user_id
    payin.CreditedWalletId = wallet.remote_id
    payin.DeclaredDebitedFunds = debit_amount.int()
    payin.DeclaredFees = fee.int()
    payin.Tag = str(e_id)
    try:
        test_hook()
        payin.save()
    except Exception as e:
        error = repr_exception(e)
        return None, record_exchange_result(db, e_id, '', 'failed', error, participant)

    e = record_exchange_result(
        db, e_id, payin.Id, payin.Status.lower(), repr_error(payin), participant
    )
    return payin, e
def payin_bank_wire(db, participant, debit_amount):
    """Prepare to receive a bank wire payin.

    The amount should be how much the user intends to send, not how much will
    arrive in the wallet.
    """

    route = ExchangeRoute.upsert_bankwire_route(participant)

    if not isinstance(debit_amount, Money):
        debit_amount = Money(debit_amount, 'EUR')
    amount, fee, vat = skim_bank_wire(debit_amount)

    wallet = participant.get_current_wallet(amount.currency, create=True)
    e_id = record_exchange(db, route, amount, fee, vat, participant, 'pre').id
    payin = BankWirePayIn()
    payin.AuthorId = participant.mangopay_user_id
    payin.CreditedWalletId = wallet.remote_id
    payin.DeclaredDebitedFunds = debit_amount.int()
    payin.DeclaredFees = fee.int()
    payin.Tag = str(e_id)
    try:
        test_hook()
        payin.save()
    except Exception as e:
        error = repr_exception(e)
        return None, record_exchange_result(db, e_id, '', 'failed', error, participant)

    e = record_exchange_result(
        db, e_id, payin.Id, payin.Status.lower(), repr_error(payin), participant
    )
    return payin, e
def record_unexpected_payin(db, payin):
    """Record an unexpected bank wire payin.
    """
    assert payin.PaymentType == 'BANK_WIRE'
    debited_amount = payin.DebitedFunds / Decimal(100)
    paid_fee = payin.Fees / Decimal(100)
    vat = skim_bank_wire(debited_amount)[2]
    wallet_id = payin.CreditedWalletId
    participant = Participant.from_mangopay_user_id(payin.AuthorId)
    current_wallet = participant.get_current_wallet(debited_amount.currency)
    assert current_wallet.remote_id == wallet_id
    route = ExchangeRoute.upsert_bankwire_route(participant)
    amount = debited_amount - paid_fee
    return db.one("""
        INSERT INTO exchanges
               (amount, fee, vat, participant, status, route, note, remote_id, wallet_id)
        VALUES (%s, %s, %s, %s, 'created', %s, NULL, %s, %s)
     RETURNING id
    """, (amount, paid_fee, vat, participant.id, route.id, payin.Id, wallet_id))
def record_unexpected_payin(db, payin):
    """Record an unexpected bank wire payin.
    """
    assert payin.PaymentType == 'BANK_WIRE'
    debited_amount = payin.DebitedFunds / Decimal(100)
    paid_fee = payin.Fees / Decimal(100)
    vat = skim_bank_wire(debited_amount)[2]
    wallet_id = payin.CreditedWalletId
    participant = Participant.from_mangopay_user_id(payin.AuthorId)
    current_wallet = participant.get_current_wallet(debited_amount.currency)
    assert current_wallet.remote_id == wallet_id
    route = ExchangeRoute.upsert_bankwire_route(participant)
    amount = debited_amount - paid_fee
    return db.one("""
        INSERT INTO exchanges
               (amount, fee, vat, participant, status, route, note, remote_id, wallet_id)
        VALUES (%s, %s, %s, %s, 'created', %s, NULL, %s, %s)
     RETURNING id
    """, (amount, paid_fee, vat, participant.id, route.id, payin.Id, wallet_id))