def test_transaction_error_property(self): """ Test ValidationError class 'transaction_error' property""" transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account( account_code='transactionmock' ) ) # Mock 'save transaction' request to throw declined # transaction validation error with self.mock_request('transaction/declined-transaction.xml'): try: transaction.save() except ValidationError as e: error = e transaction_error = error.transaction_error self.assertEqual(transaction_error.error_code, 'insufficient_funds') self.assertEqual(transaction_error.error_category, 'soft') self.assertEqual(transaction_error.customer_message, "The transaction was declined due to insufficient funds in your account. Please use a different card or contact your bank.") self.assertEqual(transaction_error.merchant_message, "The card has insufficient funds to cover the cost of the transaction.") self.assertEqual(transaction_error.gateway_error_code, "123")
def test_transaction_with_balance(self): transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account(), ) with self.mock_request('transaction-balance/transaction-no-account.xml'): try: transaction.save() except ValidationError, error: pass else:
def test_transaction_with_balance(self): transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account(), ) with self.mock_request( 'transaction-balance/transaction-no-account.xml'): try: transaction.save() except ValidationError, error: pass else:
def test_transaction_error_code_property(self): """ Test ValidationError class 'transaction_error_code' property""" transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account(account_code='transactionmock')) # Mock 'save transaction' request to throw declined # transaction validation error with self.mock_request('transaction/declined-transaction.xml'): try: transaction.save() except ValidationError as e: error = e self.assertEqual(error.transaction_error_code, 'insufficient_funds')
def test_transaction_error_code_property(self): """ Test ValidationError class 'transaction_error_code' property""" transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account( account_code='transactionmock' ) ) # Mock 'save transaction' request to throw declined # transaction validation error with self.mock_request('transaction/declined-transaction.xml'): try: transaction.save() except ValidationError as e: error = e self.assertEqual(error.transaction_error_code, 'insufficient_funds')
def failed_transaction(self): transaction = Transaction(amount_in_cents=1000, currency="USD", account=Account()) with self.mock_request("transaction/declined-stransaction.xml"): try: transaction.save() except ValidationError as _error: error = _error else: self.fail("Posting a transaction without an account code did not raise a ValidationError") self.assertEqual(transaction.error_code, "insufficient_funds") self.assertEqual(transaction.error_category, "soft") self.assertEqual( transaction.customer_message, "The transaction was declined due to insufficient funds in your account. Please use a different card or contact your bank.", ) self.assertEqual( transaction.merchant_message, "The card has insufficient funds to cover the cost of the transaction." ) self.assertEqual(transaction.gateway_error_code, "123")
def booking_create_handler(): print "In booking create" user = UserController().get_item(session['email']) if 'email' in session else None if user: form = request.form space_id = form['space_id'] room_id = form['room_id'] date = form['date'] start = form['start'] end = form['end'] print date, start, end controller = BookingController() account = Account.get(user['email']) account.billing_info = BillingInfo(token_id = form['recurly-token']) account.save() transaction = Transaction( amount_in_cents=int(form['amount'])*100, currency='INR', account=account ) transaction.save() success = False if transaction.status == 'success': third_party_user_controller = ThirdPartyUserController() tp_user = third_party_user_controller.get_item(user['email'], 'cobot') from_time = datetime.datetime.strptime(date+' '+start, '%Y-%m-%d %H:%M') to_time = datetime.datetime.strptime(date+' '+end, '%Y-%m-%d %H:%M') print from_time, to_time type='room' api = CobotAPI() result = api.create_booking(tp_user['id'], from_time, to_time, type+'_'+space_id+'_'+room_id+'_'+str(from_time)+'_'+str(to_time)) print result #create_booking(type='room', space_id=space_id, room_id=room_id, date=date, start_time=start, end_time=end) success = True bookings = api.list_bookings(tp_user['id'], from_time, to_time) print bookings return json.dumps({'success':success})
def test_unexpected_errors_thrown(self): """ Test UnexpectedClientError class """ transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account( account_code='transactionmock' ) ) # Mock 'save transaction' request to throw unexpected client error with self.mock_request('transaction/error-teapot.xml'): try: transaction.save() except UnexpectedStatusError as e: error = e self.assertIsInstance(error, UnexpectedClientError) # Mock 'save transaction' request to throw another unexpected client error with self.mock_request('transaction/error-client.xml'): try: transaction.save() except UnexpectedStatusError as e: error = e self.assertIsInstance(error, UnexpectedClientError) # Mock 'save transaction' request to throw unexpected server error with self.mock_request('transaction/error-server.xml'): try: transaction.save() except UnexpectedStatusError as e: error = e self.assertIsInstance(error, UnexpectedServerError)
def test_transaction(self): logging.basicConfig(level=logging.DEBUG) # make sure it's init'ed logger = logging.getLogger('recurly.http.request') logger.setLevel(logging.DEBUG) account_code = 'transaction%s' % self.test_id log_content = StringIO() log_handler = logging.StreamHandler(log_content) logger.addHandler(log_handler) transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account( account_code=account_code, billing_info=BillingInfo( first_name='Verena', last_name='Example', number='4111-1111-1111-1111', year='2014', address1='123 Main St', city='San Francisco', state='CA', zip='94105', country='US', month='7', verification_value='7777', ), ) ) with self.mock_request('transaction/created.xml'): transaction.save() logger.removeHandler(log_handler) try: transaction.get_refund_transaction() except ValueError: pass else: self.fail("Transaction with no refund transaction did not raise a ValueError from get_refund_transaction()") with self.mock_request('transaction/account-exists.xml'): account = Account.get(account_code) try: log_content = log_content.getvalue() self.assertTrue('<transaction' in log_content) self.assertTrue('<billing_info' in log_content) # See if we redacted our sensitive fields properly. self.assertTrue('4111' not in log_content) self.assertTrue('7777' not in log_content) with self.mock_request('transaction/refunded.xml'): refunded_transaction = transaction.refund() transaction_2 = Transaction( amount_in_cents=1000, currency='USD', account=Account(account_code=account_code), ) with self.mock_request('transaction/created-again.xml'): transaction_2.save() self.assertNotEqual(transaction_2.uuid, transaction.uuid) self.assertTrue(transaction_2.refundable) with self.mock_request('transaction/partial-refunded.xml'): refunded_transaction = transaction_2.refund(amount_in_cents=700) self.assertTrue(refunded_transaction is transaction_2) self.assertTrue(hasattr(transaction_2, 'get_refund_transaction')) with self.mock_request('transaction/partial-refunded-transaction.xml'): refund_transaction = transaction_2.get_refund_transaction() self.assertTrue(isinstance(refund_transaction, Transaction)) self.assertTrue(not refund_transaction.refundable) self.assertNotEqual(refund_transaction.uuid, transaction_2.uuid) finally: with self.mock_request('transaction/account-deleted.xml'): account.delete()
pass else: self.fail("Posting a transaction without an account code did not raise a ValidationError") # Make sure there really were errors. self.assertTrue(len(error.errors) > 0) account_code = 'transbalance%s' % self.test_id account = Account(account_code=account_code) with self.mock_request('transaction-balance/account-created.xml'): account.save() try: # Try to charge without billing info, should break. transaction = Transaction( amount_in_cents=1000, currency='USD', account=account, ) with self.mock_request('transaction-balance/transaction-no-billing-fails.xml'): try: transaction.save() except ValidationError, error: pass else: self.fail("Posting a transaction without billing info did not raise a ValidationError") # Make sure there really were errors. self.assertTrue(len(error.errors) > 0) binfo = BillingInfo( first_name='Verena', last_name='Example',
def test_transaction_with_balance(self): transaction = Transaction( amount_in_cents=1000, currency='USD', account=Account(), ) error = None with self.mock_request('transaction-balance/transaction-no-account.xml'): try: transaction.save() except ValidationError as _error: error = _error else: self.fail("Posting a transaction without an account code did not raise a ValidationError") # Make sure there really were errors. self.assertTrue(len(error.errors) > 0) account_code = 'transbalance%s' % self.test_id account = Account(account_code=account_code) with self.mock_request('transaction-balance/account-created.xml'): account.save() try: # Try to charge without billing info, should break. transaction = Transaction( amount_in_cents=1000, currency='USD', account=account, ) error = None with self.mock_request('transaction-balance/transaction-no-billing-fails.xml'): try: transaction.save() except ValidationError as _error: error = _error else: self.fail("Posting a transaction without billing info did not raise a ValidationError") # Make sure there really were errors. self.assertTrue(len(error.errors) > 0) binfo = BillingInfo( first_name='Verena', last_name='Example', address1='123 Main St', city=six.u('San Jos\xe9'), state='CA', zip='94105', country='US', type='credit_card', number='4111 1111 1111 1111', verification_value='7777', year='2015', month='12', ) with self.mock_request('transaction-balance/set-billing-info.xml'): account.update_billing_info(binfo) # Try to charge now, should be okay. transaction = Transaction( amount_in_cents=1000, currency='USD', account=account, ) with self.mock_request('transaction-balance/transacted.xml'): transaction.save() # Give the account a credit. credit = Adjustment(unit_amount_in_cents=-2000, currency='USD', description='transaction test credit') with self.mock_request('transaction-balance/credited.xml'): # TODO: maybe this should be adjust()? account.charge(credit) # Try to charge less than the account balance, which should fail (not a CC transaction). transaction = Transaction( amount_in_cents=500, currency='USD', account=account, ) with self.mock_request('transaction-balance/transacted-2.xml'): transaction.save() # The transaction doesn't actually save. self.assertTrue(transaction._url is None) # Try to charge more than the account balance, which should work. transaction = Transaction( amount_in_cents=3000, currency='USD', account=account, ) with self.mock_request('transaction-balance/transacted-3.xml'): transaction.save() # This transaction should be recorded. self.assertTrue(transaction._url is not None) finally: with self.mock_request('transaction-balance/account-deleted.xml'): account.delete()
def test_transaction(self): logging.basicConfig(level=logging.DEBUG) # make sure it's init'ed logger = logging.getLogger('recurly.http.request') logger.setLevel(logging.DEBUG) account_code = 'transaction%s' % self.test_id log_content = StringIO() log_handler = logging.StreamHandler(log_content) logger.addHandler(log_handler) transaction = Transaction(amount_in_cents=1000, currency='USD', account=Account( account_code=account_code, billing_info=BillingInfo( first_name='Verena', last_name='Example', number='4111-1111-1111-1111', year='2014', address1='123 Main St', city='San Francisco', state='CA', zip='94105', country='US', month='7', verification_value='7777', ), )) with self.mock_request('transaction/created.xml'): transaction.save() logger.removeHandler(log_handler) try: transaction.get_refund_transaction() except ValueError: pass else: self.fail( "Transaction with no refund transaction did not raise a ValueError from get_refund_transaction()" ) with self.mock_request('transaction/account-exists.xml'): account = Account.get(account_code) try: log_content = log_content.getvalue() self.assertTrue('<transaction' in log_content) self.assertTrue('<billing_info' in log_content) # See if we redacted our sensitive fields properly. self.assertTrue('4111' not in log_content) self.assertTrue('7777' not in log_content) with self.mock_request('transaction/refunded.xml'): refunded_transaction = transaction.refund() transaction_2 = Transaction( amount_in_cents=1000, currency='USD', account=Account(account_code=account_code), ) with self.mock_request('transaction/created-again.xml'): transaction_2.save() self.assertNotEqual(transaction_2.uuid, transaction.uuid) self.assertTrue(transaction_2.refundable) with self.mock_request('transaction/partial-refunded.xml'): refunded_transaction = transaction_2.refund( amount_in_cents=700) self.assertTrue(refunded_transaction is transaction_2) self.assertTrue(hasattr(transaction_2, 'get_refund_transaction')) with self.mock_request( 'transaction/partial-refunded-transaction.xml'): refund_transaction = transaction_2.get_refund_transaction() self.assertTrue(isinstance(refund_transaction, Transaction)) self.assertTrue(not refund_transaction.refundable) self.assertNotEqual(refund_transaction.uuid, transaction_2.uuid) finally: with self.mock_request('transaction/account-deleted.xml'): account.delete()
self.fail( "Posting a transaction without an account code did not raise a ValidationError" ) # Make sure there really were errors. self.assertTrue(len(error.errors) > 0) account_code = 'transbalance%s' % self.test_id account = Account(account_code=account_code) with self.mock_request('transaction-balance/account-created.xml'): account.save() try: # Try to charge without billing info, should break. transaction = Transaction( amount_in_cents=1000, currency='USD', account=account, ) with self.mock_request( 'transaction-balance/transaction-no-billing-fails.xml'): try: transaction.save() except ValidationError, error: pass else: self.fail( "Posting a transaction without billing info did not raise a ValidationError" ) # Make sure there really were errors. self.assertTrue(len(error.errors) > 0)
def test_transaction(self): logging.basicConfig(level=logging.DEBUG) # make sure it's init'ed logger = logging.getLogger("recurly.http.request") logger.setLevel(logging.DEBUG) account_code = "transaction%s" % self.test_id log_content = StringIO() log_handler = logging.StreamHandler(log_content) logger.addHandler(log_handler) transaction = Transaction( amount_in_cents=1000, currency="USD", account=Account( account_code=account_code, billing_info=BillingInfo( first_name="Verena", last_name="Example", number="4111-1111-1111-1111", year="2014", address1="123 Main St", city="San Francisco", state="CA", zip="94105", country="US", month="7", verification_value="7777", ), ), ) with self.mock_request("transaction/created.xml"): transaction.save() logger.removeHandler(log_handler) try: transaction.get_refund_transaction() except ValueError: pass else: self.fail("Transaction with no refund transaction did not raise a ValueError from get_refund_transaction()") with self.mock_request("transaction/account-exists.xml"): account = Account.get(account_code) try: log_content = log_content.getvalue() self.assertTrue("<transaction" in log_content) self.assertTrue("<billing_info" in log_content) # See if we redacted our sensitive fields properly. self.assertTrue("4111" not in log_content) self.assertTrue("7777" not in log_content) with self.mock_request("transaction/refunded.xml"): refunded_transaction = transaction.refund() transaction_2 = Transaction( amount_in_cents=1000, currency="USD", account=Account(account_code=account_code) ) with self.mock_request("transaction/created-again.xml"): transaction_2.save() self.assertNotEqual(transaction_2.uuid, transaction.uuid) self.assertTrue(transaction_2.refundable) with self.mock_request("transaction/partial-refunded.xml"): refunded_transaction = transaction_2.refund(amount_in_cents=700) self.assertTrue(refunded_transaction is transaction_2) self.assertTrue(hasattr(transaction_2, "get_refund_transaction")) with self.mock_request("transaction/partial-refunded-transaction.xml"): refund_transaction = transaction_2.get_refund_transaction() self.assertTrue(isinstance(refund_transaction, Transaction)) self.assertTrue(not refund_transaction.refundable) self.assertNotEqual(refund_transaction.uuid, transaction_2.uuid) finally: with self.mock_request("transaction/account-deleted.xml"): account.delete()