def coinbase_notification(request): client = coinbase_utils.get_api_client() # Verify that the request came from coinbase cb_signature = request.META.get(coinbase_utils.HEADER_COINBASE_SIGNATURE, None) if not client.verify_callback(request.body, cb_signature): return Response('Unauthorized Request', status=status.HTTP_401_UNAUTHORIZED) payload = request.data if payload.get(coinbase_utils.PAYLOAD_TYPE, None) == coinbase_utils.PAYLOAD_TYPE_NEW_PAYMENT: id = payload[coinbase_utils.PAYLOAD_ID] address = payload[coinbase_utils.PAYLOAD_DATA][coinbase_utils.PAYLOAD_ADDRESS] paid_at = parse(payload[coinbase_utils.PAYLOAD_DATA][coinbase_utils.PAYLOAD_CREATED_AT], ignoretz=True) amount = payload[coinbase_utils.PAYLOAD_ADDITIONAL_DATA][coinbase_utils.PAYLOAD_AMOUNT][coinbase_utils.PAYLOAD_AMOUNT] try: task = Task.objects.get(btc_address=address) except: task = None if task: TaskPayment.objects.get_or_create( task=task, ref=id, btc_address=task.btc_address, defaults={ 'btc_received': amount, 'btc_price': task.btc_price, 'received_at':paid_at } ) task.paid = True task.paid_at = paid_at task.save() distribute_task_payment.delay(task.id) return Response('Received')
def send_payment_share(destination, amount, idem, description=None): client = coinbase_utils.get_api_client() account = client.get_primary_account() transaction = account.send_money( to=destination, amount=bitcoin_utils.get_valid_btc_amount(amount), currency=CURRENCY_BTC, idem=idem, description=description) return transaction
def send_payment_share(destination, amount, idem, description=None): client = coinbase_utils.get_api_client() account = client.get_primary_account() transaction = account.send_money( to=destination, amount=bitcoin_utils.get_valid_btc_amount(amount), currency=CURRENCY_BTC, idem=idem, description=description ) return transaction
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)
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)