def _create_wallet_to_wallet_transaction(payload, customer):

    _, destination = get_source_and_destination_of_transaction(payload.copy())
    payload.update({
        'source_country': customer.country.iso,
        'destination_country': destination.country.iso
    })
    operation_amount = currency_change(customer.country.currency.iso,
                                       destination.country.currency.iso,
                                       Decimal(payload.get('amount')))

    transaction = Transaction()
    transaction.transaction_type = TransactionType.WALLET_TO_WALLET.value
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.agent = AgentRepository.fetch_by_username('AGENT_WALLET')
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = customer
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = customer.country
    transaction.destination_country = destination.country
    transaction.paid_amount_in_destination_currency = operation_amount

    transaction.save()

    debit_customer_account(customer, get_customer_balance(customer),
                           payload.get('paid_amount'))
    credit_customer_account(destination, get_customer_balance(destination),
                            operation_amount)
    create_relation_between(customer, destination)
    return transaction
def _create_cash_to_wallet_transaction(payload, agent):

    source, destination = get_source_and_destination_of_transaction(
        payload.copy())
    payload.update({
        'source_country': agent.entity.country.iso,
        'destination_country': destination.country.iso
    })

    operation_amount = currency_change(agent.entity.country.currency.iso,
                                       destination.country.currency.iso,
                                       Decimal(payload.get('amount')))

    transaction = Transaction()
    transaction.transaction_type = TransactionType.CASH_TO_WALLET.value
    transaction.agent = agent
    transaction.number = random_code(10)
    transaction.code = random_code(9)
    transaction.amount = payload.get('amount')
    transaction.paid_amount = payload.get('paid_amount')
    transaction.source_content_object = source
    transaction.destination_content_object = destination
    transaction.grille = get_grille_tarifaire(payload)
    transaction.source_country = agent.entity.country
    transaction.destination_country = destination.country
    transaction.status = TransactionStatus.SUCCESS.value
    transaction.paid_amount_in_destination_currency = operation_amount
    transaction.save()

    credit_customer_account(destination, get_customer_balance(destination),
                            operation_amount)
    return transaction
def get_wallet_balance(tastypie, payload, request):
    try:
        data = payload.copy()
        data.update({'type': 'WALLET_BALANCE'})
        _validate_transaction_payload(data)
        customer = CustomerRepository.fetch_customer_by_phone_number(
            payload.get('phone_number'))
        balance = get_customer_balance(customer)
        payload.update({
            'response_code': '000',
            'balance': balance,
            'currency': customer.country.currency.iso
        })
        return tastypie.create_response(request, payload)
    except ValidationError as err:
        return tastypie.create_response(request, {
            'response_text': str(err),
            'response_code': '100'
        }, HttpUnauthorized)
    except CoreException as err:
        return tastypie.create_response(request, err.errors, HttpForbidden)
Ejemplo n.º 4
0
def _debit_customer(customer, amount, fee=0):
    last_balance = get_customer_balance(customer)
    debit_customer_account(customer, last_balance, amount)