def test_authenticated_listing_with_bank_account(self, email=None): email = email or email_generator.next() self._create_user(email) payload = self._listing_payload() bank_account = balanced.BankAccount(name='Bob Saget', account_number=321174851, routing_number=321174851).save() payload['bank_account_href'] = bank_account.href user = User.query.filter(User.email == email).one() resp = self.client.post('/list', data=payload) self.assertEqual(resp.status_code, 302) self.assertIn('/list/1/complete', resp.data) # check in balanced user = User.query.filter(User.email == email).one() account = user.balanced_customer self.assertEqual(account.email, email) self.assertTrue( [ba for ba in account.bank_accounts if bank_account.id in ba.href])
def test_order_restrictions(self): merchant = balanced.Customer().save() order = merchant.create_order(description='foo order') card = balanced.Card(**INTERNATIONAL_CARD).save() # debit the card and associate with the order. card.debit(amount=100, order=order) another_bank_account = balanced.BankAccount( account_number="1234567890", routing_number="321174851", name="Jack Q Merchant", ).save() # not associated with the order with self.assertRaises(balanced.exc.BalancedError): another_bank_account.credit(amount=50, order=order)
def test_debits_failures(self): buyer_card = self.mp.create_card(**cards.CARD) buyer = self.mp.create_buyer(self._email_address(), buyer_card.uri) ba = balanced.BankAccount( **bank_accounts.BANK_ACCOUNT).save() merchant = self.mp.create_merchant( self._email_address(), merchant=merchants.BUSINESS_MERCHANT, bank_account_uri=ba.uri, ) merchant_card = self.mp.create_card(**cards.CARD) merchant.add_card(merchant_card.uri) merchant_card.is_valid = False merchant_card.save() cases = [ ] self._test_transaction_failures(cases)
def test_authenticated_listing_with_bank_account(self, email_address=None): email_address = email_address or email_generator.next() self._create_user(email_address) payload = self._listing_payload() bank_account = balanced.BankAccount(name='Bob Saget', account_number=321174851, bank_code=321174851).save() payload['bank_account_uri'] = bank_account.uri user = User.query.filter(User.email_address == email_address).one() resp = self.client.post('/list', data=payload) self.assertEqual(resp.status_code, 302) self.assertIn('/list/1/complete', resp.data) # check in balanced user = User.query.filter(User.email_address == email_address).one() account = user.balanced_account self.assertTrue('merchant' in account.roles) self.assertEqual(account.email_address, email_address) self.assertTrue( [ba for ba in account.bank_accounts if bank_account.id in ba.uri])
def test_31_reverse(self): self._create_marketplace() buyer = self._find_account('buyer') card = balanced.Marketplace.my_marketplace.create_card(**CARD) buyer.add_card(card.uri) buyer.debit(100000) # create bank account where transactions will switched to payed merchant = balanced.Customer().save() ba = balanced.BankAccount( routing_number="021000021", account_number="9900000002", name="lolz ftw", ).save() merchant.add_bank_account(ba) merchant.save() credit = merchant.credit(amount=5000) reverse = credit.reverse() self.assertEqual(reverse.amount, 5000) self.assertIn('reversal', reverse.uri) self.assertIn(credit.id, reverse.credit.uri)
def test_maximum_credit_amount(self): bank_account = balanced.BankAccount( **bank_accounts.BANK_ACCOUNT).save() merchant_account = self.mp.create_merchant( self._email_address(), merchant=merchants.BUSINESS_MERCHANT, bank_account_uri=bank_account.uri, ) balanced.bust_cache() self.mp = balanced.Marketplace.my_marketplace if self.mp.in_escrow: merchant_account.credit(self.mp.in_escrow) with self.assertRaises(balanced.exc.HTTPError) as ex_ctx: merchant_account.credit(100) ex = ex_ctx.exception self.assertIn('has insufficient funds to cover a transfer of', str(ex)) card = self.mp.create_card(**cards.CARD) buyer = self.mp.create_buyer(self._email_address(), card.uri) buyer.debit(200) merchant_account.credit(100)
def test_view_credits_once_bank_account_has_been_invalidated(self): bank_account = balanced.BankAccount( **bank_accounts.BANK_ACCOUNT).save() merchant = self.mp.create_merchant( self._email_address(), merchant=merchants.BUSINESS_MERCHANT, bank_account_uri=bank_account.uri, ) card = self.mp.create_card( **cards.CARD) buyer = self.mp.create_buyer(self._email_address(), card.uri) buyer.debit(100 + 200 + 300) credit1 = merchant.credit(100) credit2 = merchant.credit(200) credit3 = merchant.credit(300) merchant.bank_accounts[0].invalid = True merchant.bank_accounts[0].save() self.assertItemsEqual( [credit1.id, credit2.id, credit3.id], [c.id for c in merchant.credits] )
def test_associate_bank_account_valid(self): bank_account = balanced.BankAccount( name='Alice G. Krebs' , routing_number='321174851' , account_number='9900000001' , account_type='checking' ).save() billing.associate( self.db , u"bank account" , 'david' , self.david_href , unicode(bank_account.href) ) #args, _ = find.call_args customer = balanced.Customer.fetch(self.david_href) bank_accounts = customer.bank_accounts.all() assert len(bank_accounts) == 1 assert bank_accounts[0].href == unicode(bank_account.href) david = Participant.from_username('david') assert david.last_ach_result == ''
def test_anonymous_listing_with_bank_account(self): email = email_generator.next() payload = self._guest_listing_payload(email) bank_account = balanced.BankAccount(name='Myata Marketplace', account_number=321174851, routing_number=321174851).save() payload['bank_account_href'] = bank_account.href resp = self.client.post('/list', data=payload) self.assertEqual(resp.status_code, 302) self.assertIn('/list/1/complete', resp.data) # check locally user = User.query.filter(User.email == email).one() # NOTE: guest passwords currently disabled self.assertIsNone(user.password_hash) # self.assertTrue(user.check_password('ab')) # check in balanced account = user.balanced_customer self.assertEqual(account.email, email) self.assertTrue( [ba for ba in account.bank_accounts if bank_account.id in ba.href])
def main(): init() # create a bank account bank_account = balanced.BankAccount( account_number='1234567890', routing_number='321174851', name='Jack Q Merchant', ).save() customer = balanced.Customer().save() bank_account.associate_to_customer(customer) print 'you can\'t debit until you authenticate' try: bank_account.debit(100) except balanced.exc.HTTPError as ex: print 'Debit failed, %s' % ex.message # verify verification = bank_account.verify() print 'PROTIP: for TEST bank accounts the valid amount is always 1 and 1' try: verification.confirm(amount_1=1, amount_2=2) except balanced.exc.BankAccountVerificationFailure as ex: print 'Authentication error , %s' % ex.message # reload verification = balanced.BankAccount.fetch( bank_account.href).bank_account_verification if verification.confirm(1, 1).verification_status != 'succeeded': raise Exception('unpossible') debit = bank_account.debit(100) print 'debited the bank account %s for %d cents' % (debit.source.href, debit.amount) print 'and there you have it'
def test_settle_reverse_account_credit(self): merchant = balanced.Customer().save() order = merchant.create_order() card = balanced.Card(**INTERNATIONAL_CARD).save() order.debit_from(source=card, amount=1234) payable_account = merchant.payable_account account_credit = payable_account.credit( amount=1234, order=order.href, appears_on_statement_as='Payout') payable_account = merchant.payable_account self.assertEqual(payable_account.balance, 1234) bank_account = balanced.BankAccount( account_number='1234567890', routing_number='321174851', name='Someone', ).save() bank_account.associate_to_customer(merchant) payable_account.settle( funding_instrument=bank_account.href, appears_on_statement_as="Settlement Oct", description="Settlement for payouts from October") payable_account = merchant.payable_account self.assertEqual(payable_account.balance, 0) order_two = merchant.create_order() order_two.debit_from(source=card, amount=1234) payable_account.credit(amount=1234, order=order_two.href) payable_account = merchant.payable_account self.assertEqual(payable_account.balance, 1234) account_credit.reverse(amount=1234) payable_account = merchant.payable_account self.assertEqual(payable_account.balance, 0)
def test_add_funding_destination_to_nonmerchant(self): mp = balanced.Marketplace.query.one() card_payload = dict(self.us_card_payload) card = balanced.Card(**card_payload).save() card_uri = card.uri buyer = mp.create_buyer( email_address='*****@*****.**', card_uri=card_uri, meta={'foo': 'bar'}, ) # Debit money from this buyer to ensure the marketplace has enough to # credit her later buyer.debit(2 * 700) bank_account_payload = dict(self.bank_account_payload) bank_account = balanced.BankAccount(**bank_account_payload).save() bank_account_uri = bank_account.uri buyer.add_bank_account(bank_account_uri=bank_account_uri) # Implicit credit = buyer.credit(700) self.assertEqual(credit.destination.id, bank_account.id) # Explicit credit = buyer.credit(700, destination_uri=bank_account_uri) self.assertEqual(credit.destination.id, bank_account.id)
def test_debit_uses_newly_added_funding_src(self): bank_account = balanced.BankAccount( **bank_accounts.BANK_ACCOUNT).save() merchant_account = self.mp.create_merchant( self._email_address(), merchant=merchants.BUSINESS_MERCHANT, bank_account_uri=bank_account.uri, ) # debit bank account debit = merchant_account.debit(amount=100) self.assertEqual(debit.source.id, bank_account.id) # debit card card = balanced.Card(**cards.CARD).save() merchant_account.add_card(card.uri) debit = merchant_account.debit(amount=100) self.assertEqual(debit.source.id, card.id) # debit bank account card.is_valid = False card.save() debit = merchant_account.debit(amount=100) self.assertEqual(debit.source.id, bank_account.id)
cls.david_href = cls.make_balanced_customer() cls.janet_href = cls.make_balanced_customer() cls.card = balanced.Card( number='4111111111111111', expiration_month=10, expiration_year=2020, address={ 'line1': "123 Main Street", 'state': 'Confusion', 'postal_code': '90210', }, # gratipay stores some of the address data in the meta fields, # continue using them to support backwards compatibility meta={ 'address_2': 'Box 2', 'city_town': '', 'region': 'Confusion', }).save() cls.card.associate_to_customer(cls.janet_href) cls.card_href = unicode(cls.card.href) cls.homer_href = cls.make_balanced_customer() cls.bank_account = balanced.BankAccount( name='Homer Jay', account_number='112233a', routing_number='121042882', ).save() cls.bank_account.associate_to_customer(cls.homer_href) cls.bank_account_href = unicode(cls.bank_account.href)
def bank_accounts_delete(ctx): ba = balanced.BankAccount(**BANK_ACCOUNT).save() ba.delete() return ctx.last_req, ctx.last_resp
api_key = balanced.APIKey().save() balanced.configure(api_key.secret) marketplace = balanced.Marketplace().save() # https://docs.balancedpayments.com/1.1/overview/resources/#test-credit-card-numbers declined_card = balanced.Card( number='4444444444444448', expiration_month='12', expiration_year='2015', ).save() bank_account = balanced.BankAccount( account_number='1234567890', routing_number='321174851', name='Jack Q Merchant', ).save() # see https://github.com/balanced/balanced-api/blob/master/fixtures/_models/error.json for all possible error codes try: declined_card.debit(amount=100) except balanced.exc.BalancedError as ex: assert ex.category_code == 'card-declined' try: bank_account.credit(amount=1000) except balanced.exc.HTTPError as ex: assert ex.category_code == 'insufficient-funds' try:
import balanced balanced.configure("46c08048cd8811e2acae026ba7c1aba6") bank_account = balanced.BankAccount( routing_number='121000358', type='checking', account_number='9900000001', name='Johann Bernoulli', ).save()
print "cool. now let me refund the full amount" refund = debit.refund() # the full amount! print "notice how Balanced refunds you your fees? refund fees: {}".format( refund.fee) if refund.fee + debit.fee: raise Exception("Woah, fees are incorrect") print( "ok, we have a merchant that's signing up, let's create an account for " "them first, lets create their bank account.") bank_account = balanced.BankAccount( account_number="1234567890", bank_code="12", name="Jack Q Merchant", ).save() merchant = marketplace.create_merchant( "*****@*****.**", { 'type': "person", 'name': "Billy Jones", 'street_address': "801 High St.", 'postal_code': "94301", 'country': "USA", 'dob': "1842-01", 'phone_number': "+16505551234", }, bank_account.uri,
print "hmm, how much money do i have in escrow? should equal the debit amount" marketplace = balanced.Marketplace.my_marketplace if marketplace.in_escrow != 1500: raise Exception("1500 is not in escrow! this is wrong") print "i have {0} in escrow!".format(marketplace.in_escrow) print "cool. now let me refund the full amount" refund = debit.refund() # the full amount! print ("ok, we have a merchant that's signing up, let's create an account for " "them first, lets create their bank account.") bank_account = balanced.BankAccount( account_number="1234567890", routing_number="321174851", name="Jack Q Merchant", ).save() merchant = balanced.Customer( email_address="*****@*****.**", name="Billy Jones", address={ 'street_address': "801 High St.", 'postal_code': "94301", 'country': "USA", }, dob="1842-01", phone_number="+16505551234", destination=bank_account, ).save()
def test_credit_bank_account(self): card = balanced.Card(**INTERNATIONAL_CARD).save() card.debit(50) bank_account = balanced.BankAccount(**BANK_ACCOUNT_W_TYPE).save() cr = bank_account.credit(50) self.assertEqual(cr.amount, 50)
def test_delete_bank_account(self): customer = balanced.Customer().save() bank_account = balanced.BankAccount(**BANK_ACCOUNT_W_TYPE).save() bank_account.associate_to_customer(customer) bank_account.unstore()
import balanced balanced.configure('ak-test-1p1Tsac7gHeMQowL2seB7ieliuAJAufyq') bank_account = balanced.BankAccount( routing_number='121000358' type='checking' account_number='9900000001' name='Johann Bernoulli' ).save()
import balanced bank_account = balanced.BankAccount( **{ 'type': 'checking', 'account_number': '9900000001', 'routing_number': '321174851', 'name': 'Johann Bernoulli', } ).save() verification = bank_account.verify() request = { 'uri': verification.uri, 'bank_account_uri': bank_account.uri, }
def test_delete_bank_account(self): bank_account = balanced.BankAccount( **bank_accounts.BANK_ACCOUNT).save() bank_account.delete()