def customers_update(ctx): customer = balanced.Customer() customer.save() customer.name = 'Richie McCaw' customer.email_address = '*****@*****.**' customer.save() return ctx.last_req, ctx.last_resp
def main(): init() # create a bank account bank_account = balanced.BankAccount( account_number='1234567890', bank_code='321174851', name='Jack Q Merchant', ).save() customer = balanced.Customer().save() customer.add_bank_account(bank_account) bank_account = customer.bank_accounts[0] 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(1, 2) except balanced.exc.BankAccountVerificationFailure as ex: print 'Authentication error , %s' % ex.message if verification.confirm(1, 1).state != 'verified': raise Exception('unpossible') debit = bank_account.debit(100) print 'debited the bank account %s for %d cents' % (debit.source.uri, debit.amount) print 'and there you have it'
def setUp(self): Harness.setUp(self) self.alice = self.make_participant('alice', elsewhere='github') self.balanced_customer_href = unicode(balanced.Customer().save().href) self.card_href = unicode( balanced.Card( number='4111111111111111', expiration_month=10, expiration_year=2020, address={ 'line1': "123 Main Street", 'state': 'Confusion', 'postal_code': '90210', }, # gittip 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().href ) # XXX Why don't we actually associate this with the customer? See XXX in # test_billing_payday.TestPaydayChargeOnBalanced. self.bank_account_href = unicode( balanced.BankAccount( name='Homer Jay', account_number='112233a', routing_number='121042882', ).save().href)
def test_29_customers(self): mp = self._create_marketplace() customer = balanced.Customer().save() self.assertIsNone(customer.source) card = mp.create_card(**CARD) customer.add_card(card.uri) card = mp.create_card(**CARD) customer.add_card(card) customer.add_card(CARD) self.assertIsNotNone(customer.source) self.assertEqual(customer.source.id, customer.active_card.id) self.assertIsNone(customer.destination) bank_account = mp.create_bank_account(**BANK_ACCOUNT) customer.add_bank_account(bank_account.uri) bank_account = mp.create_bank_account(**BANK_ACCOUNT) customer.add_bank_account(bank_account) customer.add_bank_account(BANK_ACCOUNT) self.assertIsNotNone(customer.destination) self.assertEqual(customer.destination.id, customer.active_bank_account.id) debit = customer.debit(100) self.assertEqual(customer.active_card.id, debit.source.id) credit = customer.credit(100) self.assertEqual(customer.active_bank_account.id, credit.destination.id)
def test_settle_account_negative_balance(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') 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) account_credit.reverse(amount=1234) payable_account = merchant.payable_account self.assertEqual(payable_account.balance, -1234) 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)
def test_order(self): merchant = balanced.Customer().save() bank_account = balanced.BankAccount(**BANK_ACCOUNT).save() bank_account.associate_to_customer(merchant) order = merchant.create_order(description='foo order') card = balanced.Card(**INTERNATIONAL_CARD).save() # debit to increment escrow card.debit(amount=1000) # debit the card and associate with the order. card.debit(amount=100, order=order) order = balanced.Order.fetch(order.href) # the order captured the amount of the debit self.assertEqual(order.amount_escrowed, 100) # pay out half credit = bank_account.credit(amount=50, order=order) self.assertEqual(credit.order.href, order.href) order = balanced.Order.fetch(order.href) # half the money remains self.assertEqual(order.amount_escrowed, 50) # not enough money in the order to pay out with self.assertRaises(balanced.exc.BalancedError): bank_account.credit(amount=150, order=order)
def test_anonymous_listing_with_existing_buyer_account(self): email = email_generator.next() card = balanced.Card(number='4111111111111111', expiration_month=12, expiration_year=2020, cvv=123).save() ogaccount = balanced.Customer( email=email, source=card.href, ).save() payload = self._guest_listing_payload(email) 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.assertEqual(ogaccount.href, account.href)
def test_get_none_for_none(self): card = balanced.Card(**CARD).save() customer = balanced.Customer().save() self.assertIsNone(card.customer) card.associate_to_customer(customer) card = balanced.Card.get(card.href) self.assertIsNotNone(card.customer) self.assertTrue(isinstance(card.customer, balanced.Customer))
def test_general_resources(self): card = balanced.Card(**CARD).save() customer = balanced.Customer().save() card.associate_to_customer(customer) debit = card.debit(amount=1000) self.assertIsNotNone(debit) self.assertIsNotNone(debit.source) self.assertTrue(isinstance(debit.source, balanced.Card))
def _create_balanced_merchant(self, merchant_data): marketplace = balanced.Marketplace.my_marketplace try: account = balanced.Customer.query.filter(email=self.email).one() except wac.NoResultFound as ex: account = balanced.Customer(email=self.email, name=self.name, merchant=merchant_data).save() return account
def test_fetch_resource(self): customer = balanced.Customer().save() customer2 = balanced.Customer.fetch(customer.href) for prop in ('id', 'href', 'name', 'created_at'): self.assertEqual( getattr(customer, prop), getattr(customer2, prop), )
def test_create_a_person_customer(self): customer = balanced.Customer(**PERSON).save() for key, value in PERSON.iteritems(): if key == 'dob': continue if isinstance(value, dict): self.assertDictEqual(getattr(customer, key), value) else: self.assertEqual(getattr(customer, key), value)
def test_customer(self): customer = balanced.Customer().save() for uri in self._iter_customer_uris( marketplace=self.marketplace, customer=customer, ): result_customer = balanced.Customer.fetch(uri) self.assertEqual(result_customer.id, customer.id) self.assert_not_rev0(result_customer)
def test_create_a_business_customer(self): customer = balanced.Customer(**BUSINESS).save() for key, value in BUSINESS.iteritems(): if key == 'dob': continue if isinstance(value, dict): self.assertDictEqual(getattr(customer, key), value) else: self.assertEqual(getattr(customer, key), value)
def test_anonymous_purchase_with_existing_customer_account(self, *_): email = email_generator.next() # 1. create an account on balanced payload = merchant_fixtures.balanced_customer_payload(email) customer = balanced.Customer( email=email, merchant=payload['customer'], ) customer.save() # 2. anonymous purchase using this account should work. self.test_anonymous_purchase()
def test_anonymous_purchase_with_existing_buyer_account(self, *_): email = email_generator.next() # 1. create an account on balanced card_href = self._card_payload() balanced.Customer( email=email, source=card_href, ) # 2. anonymous purchase using this account should work. self.test_anonymous_purchase()
def customers_show(ctx): customer = balanced.Customer( address={ 'line1': '965 Mission St', 'line2': '#425', 'city': 'San Francisco', 'state': 'CA', 'postal_code': '94103', 'country_code': 'USA', }).save() customer = balanced.Customer.find(customer.uri) return ctx.last_req, ctx.last_resp
def customers_index(ctx): balanced.Customer( address={ 'line1': '965 Mission St', 'line2': '#425', 'city': 'San Francisco', 'state': 'CA', 'postal_code': '94103', 'country_code': 'USA', }).save() balanced.Customer.query.all() return ctx.last_req, ctx.last_resp
def test_debit(self): customer = balanced.Customer().save() card = balanced.Card(**CARD).save() card.associate_to_customer(customer) for uri in self._iter_card_uris( marketplace=self.marketplace, customer=customer, card=card, ): debit = balanced.Debit(amount=100, source=uri).save() self.assertEqual(debit.source.href, card.href) self.assertEqual(debit.amount, 100)
def test_charge_on_balanced_failure(self): customer_with_bad_card = unicode(balanced.Customer().save().href) card = balanced.Card(number='4444444444444448', expiration_year=2020, expiration_month=12).save() card.associate_to_customer(customer_with_bad_card) actual = self.payday.charge_on_balanced('whatever username', customer_with_bad_card, D('10.00')) assert actual == (D('10.61'), D('0.61'), '402 Client Error: PAYMENT REQUIRED')
def main(): init() request_bin = RequestBinClient() print 'let\'s create a callback' balanced.Callback(url=request_bin.callback_url, ).save() print "let's create a customer" balanced.Customer(name='Bob McTavish').save() print 'let\'s create a card and associate it with a new account' card = balanced.Card( expiration_month='12', csc='123', number='5105105105105100', expiration_year='2020', ).save() print 'generate a debit (which implicitly creates and captures a hold)' card.debit(100) print 'event creation is an async operation, let\'s wait until we have ' \ 'some events!' while not balanced.Event.query.count(): print 'Zzzz' time.sleep(0) print 'Woop, we got some events, let us see what there is to look at' for event in balanced.Event.query: print 'this was a {0} event, it occurred at {1}, the callback has a ' \ 'status of {2}'.format( event.type, event.occurred_at, event.callback_statuses ) print 'you can inspect each event to see the logs' event = balanced.Event.query.first() for callback in event.callbacks: print 'inspecting callback to {0} for event {1}'.format( callback.url, event.type, ) for log in callback.logs: print 'this attempt to the callback has a status "{0}"'.format( log.status) print 'ok, let\'s check with requestb.in to see if our callbacks fired' print 'we received {0} callbacks, you can view them at {1}'.format( len(request_bin.get_requests()), request_bin.view_url, )
def test_associate_card(self): customer = balanced.Customer().save() cards = set() for uri in self._iter_customer_uris( marketplace=self.marketplace, customer=customer, ): card = balanced.Card(**CARD).save() card.customer = uri card.save() cards.add(card.href) customer_cards = set(card.href for card in customer.cards) self.assertEqual(cards, customer_cards)
def test_charge_on_balanced_small_amount(self): # XXX Why can't we do this in BalancedHarness.setUp? Understand VCR! balanced_customer_href = unicode(balanced.Customer().save().href) balanced.Card.fetch(self.card_href) \ .associate_to_customer(balanced_customer_href) actual = self.payday.charge_on_balanced( 'whatever username', balanced_customer_href, D('0.06') # $0.06 USD ) assert actual == (D('10.00'), D('0.59'), '')
def test_order_helper_methods(self): merchant = balanced.Customer().save() order = merchant.create_order() card = balanced.Card(**INTERNATIONAL_CARD).save() order.debit_from(source=card, amount=1234) bank_account = balanced.BankAccount( account_number='1234567890', routing_number='321174851', name='Someone', ).save() bank_account.associate_to_customer(merchant) order.credit_to(destination=bank_account, amount=1234)
def test_create_a_customer(self): meta = {'test#': 'test_d'} card = balanced.Card(**CARD).save() buyer = balanced.Customer( source=card, meta=meta, **PERSON ).save() self.assertEqual(buyer.name, PERSON['name']) self.assertIsNotNone(buyer.created_at) self.assertIsNotNone(buyer.href) self.assertEqual(buyer.cards.count(), 1) self.assertEqual(buyer.cards.first().id, card.id)
def add_card(self, card_href): """ Adds a card to an account within Balanced. """ try: account = balanced.Customer.query.filter(email=self.email).one() except wac.NoResultFound: account = balanced.Customer( email=self.email, card_href=card_href, name=self.name).save() else: card = balanced.Card.fetch(card_href) card.associate_to_customer(account.href) return account
def test_accounts_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 self.assertEqual(payable_account.balance, 0) account_credit = payable_account.credit( amount=1234, order=order.href, appears_on_statement_as='Payout') payable_account = merchant.payable_account self.assertEqual(account_credit.status, 'succeeded') self.assertEqual(payable_account.balance, 1234) self.assertEqual(account_credit.appears_on_statement_as, 'Payout')
def test_credit(self): # make sufficient amount for credit later card = balanced.Card(**CARD).save() card.debit(amount=1000000) customer = balanced.Customer().save() bank_account = balanced.BankAccount(**BANK_ACCOUNT).save() bank_account.associate_to_customer(customer) for uri in self._iter_bank_account_uris( marketplace=self.marketplace, customer=customer, bank_account=bank_account, ): credit = balanced.Credit(amount=100, destination=uri).save() self.assertEqual(credit.destination.href, bank_account.href) self.assertEqual(credit.amount, 100)
def test_associate_bank_account(self): customer = balanced.Customer().save() bank_accounts = set() for uri in self._iter_customer_uris( marketplace=self.marketplace, customer=customer, ): bank_account = balanced.BankAccount(**BANK_ACCOUNT).save() bank_account.customer = uri bank_account.save() bank_accounts.add(bank_account.href) customer_bank_accounts = set( bank_account.href for bank_account in customer.bank_accounts ) self.assertEqual(bank_accounts, customer_bank_accounts)
def test_wbtba_withdraws_balance_to_bank_account(self): customer = balanced.Customer().save() bank_account = balanced.BankAccount( name='Alice G. Krebs' , routing_number='321174851' , account_number='9900000001' , account_type='checking' ).save() bank_account.associate_to_customer(customer.href) alice = self.make_participant( 'alice' , balance=D('10.00') , is_suspicious=False , balanced_customer_href=customer.href ) alice.cancel('bank')