def create_charges(until=datetime.datetime.now()): """Create a set of charges based on the transaction table.""" for customer_id, balance in read_balances(until): customer = Organization.objects.get(pk=customer_id) charges = Charge.objects.filter(customer=customer).exclude( state=Charge.DONE).aggregate(Sum('amount')) inflight_charges = charges['amount__sum'] # We will create charges only when we have no charges # already in flight for this customer. if not inflight_charges: inflight_charges = 0 # Such that subsequent logic works regardless amount = balance - inflight_charges if amount > 50: LOGGER.info('CHARGE %dc to %s', amount, customer.name) # Stripe will not processed charges less than 50 cents. try: charge = Charge.objects.charge_card(customer, amount=amount) except: raise else: LOGGER.info('SKIP %s (less than 50c)', customer.name) else: LOGGER.info('SKIP %s (one charge already in flight)', customer.name)
def setUp(self): if LedgerTests.firstTime: # Implementation Node: # It seems the fixture is only loaded after setUpClass was called. for customer_id, amount in read_balances(): # Make sure all organizations have a valid procesor_id # and associated credit card. #print "XXX %s balance of %dc" %(customer_id, amount) Organization.objects.associate_processor( customer_id, card={'number': '4242424242424242', 'exp_month': '12', 'exp_year': '2014'}) LedgerTests.firstTime = False