def create_transaction(**data): if not data["transaction"]: raise ValueError("Transaction ID is required") # If we already had the transaction recorded, we can just update the confirmations transaction = get_transaction(transaction_id=data["transaction"]) # Get a rpc client for the payment currency type client = CoindClient(data["currency"]) # Get the transaction data transaction_data = client.get_transaction(data["transaction"]) data.update({ "amount": transaction_data.amount, "fee": transaction_data.fee, "confirmations": transaction_data.confirmations, "category": transaction_data.category }) # Find the payment associated with the receiving address payment = payments.get_payment(merchant_address=transaction_data.address) # Either update the confirmations on the existing transaction, or create a new transaction if transaction: transaction.confirmations = transaction_data.confirmations else: if payment: data["payment_id"] = payment.id transaction_data, errors = TransactionSchema().load(data) transaction = Transaction(**transaction_data) if payment: payments.update_status(payment) return transaction
def process_withdrawal(payment): if not hasattr(payment, "id"): payment = get_payment(payment) if not payment: raise ValueError("Invalid payment id") # Check if a withdrawal already exists for this payment withdrawal = Withdrawal.by_payment(payment.id) if withdrawal: return withdrawal # Check the payment has been paid and confirmed is_paid = payment.amount_confirmed() == payment.amount if not is_paid: raise ValueError("Payment has not been confirmed") # Get a RPC client for the payment currency type client = CoindClient(payment.currency) # Send the payment to the merchant transaction_id = client.send(payment.merchant_address, payment.amount) # Create the withdrawal record withdrawal_data = dict(payment_id=payment.id, transaction_id=transaction_id) withdrawal = Withdrawal.from_dict(withdrawal_data) return withdrawal
def create_payment(**raw_data): # Get a rpc client for the payment currency type client = CoindClient(raw_data.get("currency")) # Parse the input payment data data, errors = PaymentSchema().load(raw_data) # Check if the merchant address is a valid crypto address valid_address = client.is_valid_address(data.get("merchant_address")) if not valid_address: raise ValueError("Invalid address") # Generate a new wallet address for this payment data["payment_address"] = client.create_address() payment = Payment(**data) return payment
def update_unconfirmed_transactions(currency): transactions = get_unconfirmed(currency) client = CoindClient(currency) updated_transactions = [] # Update the confirmations count of unconfirmed transactions for transaction in transactions: info = client.get_transaction(transaction.transaction_id) if info.confirmations != transaction.confirmations: transaction.confirmations = info.confirmations updated_transactions.append(transaction) # Update the status of any payments that had changes in their transactions updated_payments = list(set([t.payment_id for t in updated_transactions])) for payment_id in updated_payments: payments.update_status(payment_id) return updated_transactions