Пример #1
0
def update_multi_tasks(multi_task_key, distribute=False):
    multi_task_key = clean_instance(multi_task_key, MultiTaskPaymentKey)

    if multi_task_key.distribute_only:
        connected_tasks = multi_task_key.distribute_tasks
        connected_tasks.filter(paid=True).update(
            btc_price=multi_task_key.btc_price,
            withhold_tunga_fee_distribute=multi_task_key.withhold_tunga_fee,
            btc_paid=multi_task_key.paid,
            btc_paid_at=multi_task_key.paid_at
        )
    else:
        connected_tasks = multi_task_key.tasks
        connected_tasks.filter(paid=False).update(
            payment_method=multi_task_key.payment_method,
            btc_price=multi_task_key.btc_price,
            withhold_tunga_fee=multi_task_key.withhold_tunga_fee,
            paid=multi_task_key.paid,
            paid_at=multi_task_key.paid_at,
            processing=multi_task_key.processing,
            processing_at=multi_task_key.processing_at
        )

    # Generate invoices for all connected tasks
    for task in connected_tasks.all():
        if multi_task_key.distribute_only:
            if task.paid and multi_task_key.paid:
                # Coinbase waits for 6 confirmations, so not safe to distribute yet
                # distribute_task_payment.delay(task.id)
                pass
            return

        # Save Invoice
        if not task.btc_address or not bitcoin_utils.is_valid_btc_address(task.btc_address):
            address = coinbase_utils.get_new_address(coinbase_utils.get_api_client())
            task.btc_address = address
            task.save()

        TaskInvoice.objects.create(
            task=task,
            user=multi_task_key.user,
            title=task.title,
            fee=task.pay,
            client=task.owner or task.user,
            # developer=developer,
            payment_method=multi_task_key.payment_method,
            btc_price=multi_task_key.btc_price,
            btc_address=task.btc_address,
            withhold_tunga_fee=multi_task_key.withhold_tunga_fee
        )

        if distribute and multi_task_key.paid:
            distribute_task_payment_payoneer.delay(task.id)
Пример #2
0
    def btc_address(self):
        if not self.profile:
            return None

        if self.profile.payment_method == PAYMENT_METHOD_BTC_ADDRESS:
            if bitcoin_utils.is_valid_btc_address(self.profile.btc_address):
                return self.profile.btc_address
        elif self.profile.payment_method == PAYMENT_METHOD_BTC_WALLET:
            wallet = self.profile.btc_wallet
            if wallet.provider == BTC_WALLET_PROVIDER_COINBASE:
                client = coinbase_utils.get_oauth_client(wallet.token, wallet.token_secret, self)
                return coinbase_utils.get_new_address(client)
        return None
Пример #3
0
    def btc_address(self):
        if not self.profile:
            return None

        if self.profile.payment_method == PAYMENT_METHOD_BTC_ADDRESS:
            if bitcoin_utils.is_valid_btc_address(self.profile.btc_address):
                return self.profile.btc_address
        elif self.profile.payment_method == PAYMENT_METHOD_BTC_WALLET:
            wallet = self.profile.btc_wallet
            if wallet.provider == BTC_WALLET_PROVIDER_COINBASE:
                client = coinbase_utils.get_oauth_client(
                    wallet.token, wallet.token_secret, self)
                return coinbase_utils.get_new_address(client)
        return None
Пример #4
0
    def invoice(self, request, pk=None):
        """
        Task Invoice Endpoint
        ---
        request_serializer: TaskPaymentSerializer
        response_serializer: TaskInvoiceSerializer
        omit_parameters:
            - query
        """
        task = get_object_or_404(self.get_queryset(), pk=pk)
        self.check_object_permissions(request, task)

        invoice = task.invoice

        if request.method == 'POST':
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)

            fee = serializer.validated_data['fee']
            payment_method = serializer.validated_data['payment_method']

            if fee < task.pay:
                raise ValidationError({
                    'fee': 'You cannot reduce the fee for the task, Please contact [email protected] for assistance'
                })

            task.bid = fee
            task.payment_method = payment_method

            btc_price = coinbase_utils.get_btc_price(task.currency)
            task.btc_price = btc_price

            if not task.btc_address or not bitcoin_utils.is_valid_btc_address(task.btc_address):
                address = coinbase_utils.get_new_address(coinbase_utils.get_api_client())
                task.btc_address = address

            task.full_clean()
            task.save()

            developer = None
            try:
                assignee = task.participation_set.filter(accepted=True).order_by('-assignee').earliest('created_at')
                developer = assignee.user
            except:
                pass

            if not developer:
                raise ValidationError({
                    'fee': 'Please assign a developer to the task or contact [email protected] for assistance'
                })

            # Save Invoice
            invoice = TaskInvoice.objects.create(
                task=task,
                title=task.title,
                fee=task.pay,
                client=task.user,
                developer=developer,
                payment_method=task.payment_method,
                btc_price=btc_price,
                btc_address=task.btc_address
            )

            if task.payment_method == TASK_PAYMENT_METHOD_BANK:
                # Send notification for requested invoice
                send_task_invoice_request_email.delay(task.id)

        response_serializer = TaskInvoiceSerializer(invoice, context={'request': request})
        return Response(response_serializer.data)