def tearDownClass(cls): # Braintree Cleanup existing_holds = braintree.Transaction.search( braintree.TransactionSearch.status == 'authorized' ) for hold in existing_holds.items: cancel_card_hold(hold) super(BillingHarness, cls).tearDownClass()
def test_capture_card_hold_partial_amount(self): hold, error = create_card_hold(self.db, self.obama, D('20.00')) assert error == '' # sanity check capture_card_hold(self.db, self.obama, D('15.00'), hold) obama = Participant.from_id(self.obama.id) assert self.obama.balance == obama.balance == D('15.00') assert self.obama.get_credit_card_error() == '' # Clean up cancel_card_hold(hold)
def test_capture_card_hold_too_high_amount(self): hold, error = create_card_hold(self.db, self.janet, D("20.00")) assert error == "" # sanity check with self.assertRaises(balanced.exc.HTTPError): capture_card_hold(self.db, self.janet, D("20.01"), hold) janet = Participant.from_id(self.janet.id) assert self.janet.balance == janet.balance == 0 # Clean up cancel_card_hold(hold)
def test_create_card_hold_success(self): hold, error = create_card_hold(self.db, self.janet, D("1.00")) janet = Participant.from_id(self.janet.id) assert isinstance(hold, balanced.CardHold) assert hold.failure_reason is None assert hold.amount == 1000 assert hold.meta["state"] == "new" assert error == "" assert self.janet.balance == janet.balance == 0 # Clean up cancel_card_hold(hold)
def test_create_card_hold_success(self): hold, error = create_card_hold(self.db, self.janet, D('1.00')) janet = Participant.from_id(self.janet.id) assert isinstance(hold, balanced.CardHold) assert hold.failure_reason is None assert hold.amount == 1000 assert hold.meta['state'] == 'new' assert error == '' assert self.janet.balance == janet.balance == 0 # Clean up cancel_card_hold(hold)
def test_capture_card_hold_too_high_amount(self): hold, error = create_card_hold(self.db, self.janet, D('20.00')) assert error == '' # sanity check with self.assertRaises(balanced.exc.HTTPError): capture_card_hold(self.db, self.janet, D('20.01'), hold) janet = Participant.from_id(self.janet.id) assert self.janet.balance == janet.balance == 0 # Clean up cancel_card_hold(hold)
def test_capture_card_hold_too_high_amount(self): hold, error = create_card_hold(self.db, self.obama, D('20.00')) assert error == '' # sanity check with self.assertRaises(Exception): # How do I check the exception's msg here? capture_card_hold(self.db, self.obama, D('20.01'), hold) obama = Participant.from_id(self.obama.id) assert self.obama.balance == obama.balance == 0 # Clean up cancel_card_hold(hold)
def test_capture_card_hold_full_amount(self): hold, error = create_card_hold(self.db, self.obama, D('20.00')) assert error == '' # sanity check assert hold.status == 'authorized' capture_card_hold(self.db, self.obama, D('20.00'), hold) hold = braintree.Transaction.find(hold.id) obama = Participant.from_id(self.obama.id) assert self.obama.balance == obama.balance == D('20.00') assert self.obama.get_credit_card_error() == '' assert hold.status == 'submitted_for_settlement' # Clean up cancel_card_hold(hold)
def fetch_card_holds(participant_ids): log('Fetching card holds.') holds = {} existing_holds = braintree.Transaction.search( braintree.TransactionSearch.status == 'authorized') for hold in existing_holds.items: log_amount = hold.amount p_id = int(hold.custom_fields['participant_id']) if p_id in participant_ids: log('Reusing a ${:.2f} hold for {}.'.format(log_amount, p_id)) holds[p_id] = hold else: cancel_card_hold(hold) return holds
def tearDownClass(cls): has_exchange_id = balanced.Transaction.f.meta.contains('exchange_id') credits = balanced.Credit.query.filter(has_exchange_id) debits = balanced.Debit.query.filter(has_exchange_id) for t in itertools.chain(credits, debits): t.meta.pop('exchange_id') t.save() # Braintree Cleanup existing_holds = braintree.Transaction.search( braintree.TransactionSearch.status == 'authorized' ) for hold in existing_holds.items: cancel_card_hold(hold) super(BillingHarness, cls).tearDownClass()
def fetch_card_holds(participant_ids): log('Fetching card holds.') holds = {} existing_holds = braintree.Transaction.search( braintree.TransactionSearch.status == 'authorized' ) for hold in existing_holds.items: log_amount = hold.amount p_id = int(hold.custom_fields['participant_id']) if p_id in participant_ids: log('Reusing a ${:.2f} hold for {}.'.format(log_amount, p_id)) holds[p_id] = hold else: cancel_card_hold(hold) return holds
def f(p): amount = p.giving_today if p.old_balance < 0: amount -= p.old_balance if p.id in holds: charge_amount = upcharge(amount)[0] if holds[p.id].amount >= charge_amount * 100: return else: # The amount is too low, cancel the hold and make a new one cancel_card_hold(holds.pop(p.id)) hold, error = create_card_hold(self.db, p, amount) if error: return 1 else: holds[p.id] = hold
def f(p): amount = p.giving_today if p.old_balance < 0: amount -= p.old_balance if p.id in holds: charge_amount = upcharge(amount)[0] if holds[p.id].amount >= charge_amount: return else: # The amount is too low, cancel the hold and make a new one cancel_card_hold(holds.pop(p.id)) hold, error = create_card_hold(self.db, p, amount) if error: return 1 else: holds[p.id] = hold
def test_create_card_hold_multiple_cards(self): bob = self.make_participant('bob', is_suspicious=False) customer_id = bob.get_braintree_account().id for i in range(2): result = braintree.PaymentMethod.create({ "customer_id": customer_id, "payment_method_nonce": Nonces.Transactable }) assert result.is_success ExchangeRoute.insert(bob, 'braintree-cc', result.payment_method.token) hold, error = create_card_hold(self.db, bob, D('100.00')) assert error == '' # Clean up cancel_card_hold(hold)
def fetch_card_holds(participant_ids): holds = {} for hold in CardHold.query.filter(CardHold.f.meta.state == 'new'): state = 'new' if hold.failure_reason: state = 'failed' elif hold.voided_at: state = 'cancelled' elif getattr(hold, 'debit_href', None): state = 'captured' if state != 'new': hold.meta['state'] = state hold.save() continue p_id = int(hold.meta['participant_id']) if p_id in participant_ids: holds[p_id] = hold else: cancel_card_hold(hold) return holds
def f(p): amount = p.giving_today - p.old_balance if p.id in holds: if amount >= MINIMUM_CHARGE: charge_amount = upcharge(amount)[0] if holds[p.id].amount >= charge_amount: return else: # The amount is too low, cancel the hold and make a new one cancel_card_hold(holds.pop(p.id)) else: # not up to minimum charge level. cancel the hold cancel_card_hold(holds.pop(p.id)) return if amount >= MINIMUM_CHARGE: hold, error = create_card_hold(self.db, p, amount) if error: return 1 else: holds[p.id] = hold
def fetch_card_holds(participant_ids): log('Fetching card holds.') holds = {} for hold in CardHold.query.filter(CardHold.f.meta.state == 'new'): log_amount = hold.amount / 100.0 p_id = int(hold.meta['participant_id']) state = 'new' if hold.status == 'failed' or hold.failure_reason: state = 'failed' elif hold.voided_at: state = 'cancelled' elif getattr(hold, 'debit_href', None): state = 'captured' if state != 'new': hold.meta['state'] = state hold.save() log('Set state to {} on a ${:.2f} hold for {}.'.format(state, log_amount, p_id)) continue if p_id in participant_ids: log('Reusing a ${:.2f} hold for {}.'.format(log_amount, p_id)) holds[p_id] = hold else: cancel_card_hold(hold) return holds
def tearDown(self): if self.hold: cancel_card_hold(self.hold) super(TestsWithBillingHarness, self).tearDown()