Exemplo n.º 1
0
def charge(db, route, amount, return_url, billing_address=None):
    """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.

    """
    assert isinstance(amount, (Decimal, Money)), type(amount)
    assert route
    assert route.network == 'mango-cc'

    participant = route.participant

    amount = Money(amount, 'EUR') if isinstance(amount, Decimal) else amount
    charge_amount, fee, vat = upcharge_card(amount)
    amount = charge_amount - fee

    wallet = participant.get_current_wallet(amount.currency, create=True)
    e_id = record_exchange(db, route, amount, fee, vat, participant, 'pre').id
    payin = DirectPayIn()
    payin.AuthorId = participant.mangopay_user_id
    if billing_address:
        payin.Billing = {'Address': billing_address}
    payin.CreditedWalletId = wallet.remote_id
    payin.DebitedFunds = charge_amount.int()
    payin.CardId = route.address
    payin.SecureMode = 'FORCE'
    payin.SecureModeReturnURL = return_url
    payin.Fees = fee.int()
    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)
Exemplo n.º 2
0
def charge(db, route, amount, return_url, billing_address=None):
    """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.

    """
    assert isinstance(amount, (Decimal, Money)), type(amount)
    assert route
    assert route.network == 'mango-cc'

    participant = route.participant

    amount = Money(amount, 'EUR') if isinstance(amount, Decimal) else amount
    charge_amount, fee, vat = upcharge_card(amount)
    amount = charge_amount - fee

    wallet = participant.get_current_wallet(amount.currency, create=True)
    e_id = record_exchange(db, route, amount, fee, vat, participant, 'pre').id
    payin = DirectPayIn()
    payin.AuthorId = participant.mangopay_user_id
    if billing_address:
        payin.Billing = {'Address': billing_address}
    payin.CreditedWalletId = wallet.remote_id
    payin.DebitedFunds = Money_to_cents(charge_amount)
    payin.CardId = route.address
    payin.SecureMode = 'FORCE'
    payin.SecureModeReturnURL = return_url
    payin.Fees = Money_to_cents(fee)
    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
    )
Exemplo n.º 3
0
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)