示例#1
0
def check_transaction(output):
    with transaction.atomic():
        accounts = Accounts.objects.filter(address=output['address'])

        if not accounts.exists():
            return

        account = accounts.first()
        print('Processando conta do usuario {}'.format(account.user.username))

        if Statement.objects.filter(account=account,
                                    tx_id=output['id']).exists():
            return

        satoshis = Decimal(output['value'])

        if satoshis < 0:
            return

        amount = satoshis / Decimal('100000000')
        account.deposit += amount
        account.save()

        statement = Statement()
        statement.account = account
        statement.tx_id = output['id']
        statement.amount = amount
        statement.description = 'Deposit'
        statement.type = Statement.TYPES.deposit
        statement.save()

        print("Transfering {} to {} account".format(amount, account.pk))
示例#2
0
    def post(self, request, gateway, account_pk):
        # Importa dinamicamente o modulo do gateway configurado para a moeda
        gateway_module = importlib.import_module(
            'exchange_payments.gateways.{}'.format(gateway))
        gateway = gateway_module.Gateway()
        account = Accounts.objects.get(pk=account_pk)

        # Validates if transaction already exists in our system
        if Statement.objects.filter(tx_id=request.POST['txn_id']).exists():
            raise Exception('Transaction already exists!')

        # Checa se o deposito pode ser feito
        if gateway.can_deposit(account, request.POST):
            # Adiciona o saldo na conta do usuario, e cria a entrada no extrato
            with transaction.atomic():
                account.deposit += gateway.deposit_amount
                account.save()

                statement = Statement()
                statement.account = account
                statement.description = 'Deposit'
                statement.amount = gateway.deposit_amount
                statement.type = Statement.TYPES.deposit
                statement.tx_id = gateway.tx_id
                statement.save()

                return {'amount': statement.amount}

        return {'error': _("Deposit can't be done.")}
示例#3
0
    def handle(self, *args, **options):
        while True:
            txs = rpc_proxy._call('listtransactions')

            for tx in txs:
                with transaction.atomic():
                    # Valida se o tipo e o valor da transacao atendedem os requisitos para deposito
                    if tx['category'] != 'receive' or tx['amount'] <= 0:
                        continue

                    # Pega a conta TCOIN do usuario usando a carteira dele
                    currency = Currencies.objects.get(
                        code=settings.BITCOINZ_CURRENCY_CODE)
                    accounts = Accounts.objects.filter(currency=currency,
                                                       address=tx['address'])

                    # Se a conta para a carteira nao existir, vai para a proxima transacao
                    if not accounts.exists():
                        continue

                    account = accounts.first()

                    # Valida se a transacao ja foi processada
                    statements = Statement.objects.filter(account=account,
                                                          tx_id=tx['txid'])
                    if statements.exists():
                        continue

                    # Deposita o valor para o usuario
                    statement = Statement()
                    statement.account = account
                    statement.amount = Decimal(tx['amount'])
                    statement.description = 'Deposit'
                    statement.type = Statement.TYPES.deposit
                    statement.tx_id = tx['txid']
                    statement.save()

                    account.deposit += Decimal(tx['amount'])
                    account.save()

                    print('Pagando {} para a conta Bitcoin Z do usuario {}'.
                          format(tx['amount'], account.user.username))

            time.sleep(settings.BITCOINZ_CONFIRM_PAYMENTS_WAIT_SECONDS)