def capture_card_hold(db, participant, amount, hold): """Capture the previously created hold on the participant's credit card. """ typecheck( hold, braintree.Transaction , amount, Decimal ) username = participant.username assert participant.id == int(hold.custom_fields['participant_id']) route = ExchangeRoute.from_address(participant, 'braintree-cc', hold.credit_card['token']) assert isinstance(route, ExchangeRoute) cents, amount_str, charge_amount, fee = _prep_hit(amount) amount = charge_amount - fee # account for possible rounding e_id = record_exchange(db, route, amount, fee, participant, 'pre') # TODO: Find a way to link transactions and corresponding exchanges # meta = dict(participant_id=participant.id, exchange_id=e_id) error = '' try: result = braintree.Transaction.submit_for_settlement(hold.id, str(cents/100.00)) assert result.is_success if result.transaction.status != 'submitted_for_settlement': error = result.transaction.status except Exception as e: error = repr_exception(e) if error == '': record_exchange_result(db, e_id, 'succeeded', None, participant) log("Captured " + amount_str + " on Braintree for " + username) else: record_exchange_result(db, e_id, 'failed', error, participant) raise Exception(error)
def capture_card_hold(db, participant, amount, hold): """Capture the previously created hold on the participant's credit card. """ typecheck( hold, balanced.CardHold , amount, Decimal ) username = participant.username assert participant.id == int(hold.meta['participant_id']) route = ExchangeRoute.from_address(participant, 'balanced-cc', hold.card_href) assert isinstance(route, ExchangeRoute) cents, amount_str, charge_amount, fee = _prep_hit(amount) amount = charge_amount - fee # account for possible rounding e_id = record_exchange(db, route, amount, fee, participant, 'pre') meta = dict(participant_id=participant.id, exchange_id=e_id) try: hold.capture(amount=cents, description=username, meta=meta) record_exchange_result(db, e_id, 'succeeded', None, participant) except Exception as e: error = repr_exception(e) record_exchange_result(db, e_id, 'failed', error, participant) raise hold.meta['state'] = 'captured' hold.save() log("Captured " + amount_str + " on Balanced for " + username)
def test_associate_and_delete_bank_account_valid(self): bank_account = balanced.BankAccount( name='Alice G. Krebs' , routing_number='321174851' , account_number='9900000001' , account_type='checking' ).save() customer = self.david.get_balanced_account() customer.merchant_status = 'underwritten' with mock.patch.object(Participant, 'get_balanced_account') as gba: gba.return_value = customer self.hit('david', 'associate', 'balanced-ba', bank_account.href) bank_accounts = customer.bank_accounts.all() assert len(bank_accounts) == 1 assert bank_accounts[0].href == bank_account.href assert self.david.get_bank_account_error() == '' assert self.david.has_payout_route self.hit('david', 'delete', 'balanced-ba', bank_account.href) david = Participant.from_username('david') route = ExchangeRoute.from_address(david, 'balanced-ba', bank_account.href) assert route.error == david.get_bank_account_error() == 'invalidated' assert david.balanced_customer_href # Check that update_error doesn't update an invalidated route route.update_error('some error') assert route.error == david.get_bank_account_error() == 'invalidated' assert not self.david.has_payout_route
def test_associate_and_delete_bank_account_valid(self): bank_account = balanced.BankAccount(name='Alice G. Krebs', routing_number='321174851', account_number='9900000001', account_type='checking').save() customer = self.david.get_balanced_account() customer.merchant_status = 'underwritten' with mock.patch.object(Participant, 'get_balanced_account') as gba: gba.return_value = customer self.hit('david', 'associate', 'balanced-ba', bank_account.href) bank_accounts = customer.bank_accounts.all() assert len(bank_accounts) == 1 assert bank_accounts[0].href == bank_account.href assert self.david.get_bank_account_error() == '' self.hit('david', 'delete', 'balanced-ba', bank_account.href) david = Participant.from_username('david') route = ExchangeRoute.from_address(david, 'balanced-ba', bank_account.href) assert route.error == david.get_bank_account_error() == 'invalidated' assert david.balanced_customer_href # Check that update_error doesn't update an invalidated route route.update_error('some error') assert route.error == david.get_bank_account_error() == 'invalidated'