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 test_by_payment(self): payment = self.mixer.blend('transient.models.payment.Payment') withdrawal = self.mixer.blend('transient.models.withdrawal.Withdrawal', payment_id=payment.id) self.session.add(payment) self.session.add(withdrawal) self.session.commit() payment_withdrawal = Withdrawal.by_payment(payment.id) self.assertEqual(payment_withdrawal.payment_id, payment.id)
def test_defaults(self): withdrawal = Withdrawal() self.assertTrue(bool(self.uuid_pattern.match(str(withdrawal.id))), "Should set the ID default on create")