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 login(tastypie, data, request):

    username = data['username']
    password = data['password']

    try:
        user = User.objects.get(username=username)
        if user and user.is_active:
            result = user.check_password(password)
            if result:
                agent = AgentRepository.fetch_by_user(user)
                bundle = tastypie.build_bundle(obj=agent, request=request)
                bundle = tastypie.full_dehydrate(bundle)

                entity = EntityRepository.fetch_by_agent(agent)
                bundle.data.update({'entity': entity})
                bundle.data.update({'response_code': '000'})
                bundle.data.update(create_jwt_token_for(agent, 'agent_api_secret_key'))
                bundle.data.update({'first_name': user.first_name})
                bundle.data.update({'last_name': user.last_name})

                # TODO : Send sms asyncrhonously

                return tastypie.create_response(request, bundle)
            else:
                return tastypie.create_response(request, {'response_text': 'wrong password', 'response_code': '100'}, HttpForbidden)
        else:
            return tastypie.create_response(request, {'response_text': 'agent is not active', 'response_code': '100'}, HttpUnauthorized)
    except User.DoesNotExist:
        return tastypie.create_response(request, {'response_text': 'unknwonw user', 'response_code': '100'}, HttpForbidden)
Beispiel #3
0
def get_agent_transactions_historique(tastypie, request):
    try:
        agent_code = decode_jwt_token(get_request_token(request), 'agent_api_secret_key')
        agent = AgentRepository.fetch_by_code(agent_code.get('code'))
        result = TransactionRepository.retreive_transactions_stat_by_type_giving_entity_agent(agent)

        return tastypie.create_response(request, result)
    except CoreException as err:
        return tastypie.create_response(request, err.errors, HttpForbidden)
def otp_renew(tastypie, request):
    try:
        agent_code = decode_jwt_token(get_request_token(request), 'agent_api_secret_key')
        agent = AgentRepository.fetch_by_code(agent_code.get('code'))
        otp = generate_totp()
        sms_task.run(agent.phone_number, otp)

        return tastypie.create_response(request, {'response_text': 'OK', 'response_code': '000'})
    except Exception:
        return tastypie.create_response(request, {'response_text': 'Unable to renew OTP', 'response_code': '100'}, HttpForbidden)
Beispiel #5
0
def get_entity_financial_situation(tastypie, request):
    try:
        agent_code = decode_jwt_token(get_request_token(request), 'agent_api_secret_key')
        agent = AgentRepository.fetch_by_code(agent_code.get('code'))
        balance = get_entity_balance_by_agent(agent)
        commission = get_entity_commission(agent.entity)

        result = {'balance': balance, 'commission': commission, 'response_code': '000'}

        return tastypie.create_response(request, result)
    except CoreException as err:
        return tastypie.create_response(request, err.errors, HttpForbidden)
def create_batch_transaction(entity, commission):

    batch = Transaction()
    batch.transaction_type = TransactionType.BATCH.value
    batch.number = random_code(10)
    batch.code = random_code(9)
    batch.amount = commission
    batch.paid_amount = commission
    batch.destination_content_object = entity
    batch.agent = AgentRepository.get_batch_agent()
    batch.source_content_object = entity
    batch.status = TransactionStatus.SUCCESS.value
    batch.paid_amount_in_destination_currency = commission
    batch.save()
    return batch
Beispiel #7
0
def _get_agent_info(payload):
    return AgentRepository.fetch_by_code(payload.get('agent').get('code'))
Beispiel #8
0
def _add_agent_informations(transaction, payload):
    transaction_type = payload.get('type')
    if transaction_type in AGENT_TRANSACTIONS and not transaction_type == TransactionType.RECOUVREMENT.value:
        agent_informations = AgentRepository.to_json(payload.get('agent').get('code'))
        payload.update({'agent': agent_informations})
    return payload