def spoof_store_received_balance(store, amount): """For cryptoassets backend, put some imaginary value on the account.""" assets_app = get_cryptoassets() # Get hold of Bitcoin SQLAlchemy model descriptions btc = assets_app.coins.get("btc") # Get handle of BitcoinAccount class Account = btc.coin_description.Account @assetdb.managed_transaction def tx(session): account_id, balance = payment.get_store_account_info(store) assert balance == 0 # Now simulate incoming transaction wallet = payment.get_wallet(session) sending_account = wallet.create_account("Test account") receiving_account = session.query(Account).get(account_id) session.flush() # Fake balance on the account making the payment sending_account.balance = amount logger.info("Making test payments from:%d to account %d", sending_account.id, receiving_account.id) tx_obj = wallet.send_internal(sending_account, receiving_account, amount, "Test account funding") session.flush() logger.info("Credit transaction is: %s", tx_obj) tx()
def setUp(self): clear() owner = models.User.objects.create(username="******", email="*****@*****.**") tasks.update_exchange_rates() converter = models.get_rate_converter() self.fiat_price = Decimal(settings.TEST_CREDITING_PRICE) self.btc_price = converter.convert(settings.DEFAULT_PRICING_CURRENCY, settings.PAYMENT_CURRENCY, self.fiat_price) logger.debug("Test album price is %s USD, %s BTC", self.fiat_price, self.btc_price) self.test_store = test_store = models.Store.objects.create(name=u"Test Store åäö") test_store.currency = settings.DEFAULT_PRICING_CURRENCY test_store.store_url = "http://localhost:8000/store/test-store/" test_store.operators = [owner] test_store.btc_address = "19356KxTs9Bw5AAdxens5hoxDSp5bsUKse" test_store.save() test_song1 = models.Song.objects.create(name="Song A", store=test_store) test_song1.fiat_price = self.fiat_price test_song1.save() self.test_store = test_store self.test_song = test_song1 if settings.PAYMENT_SOURCE == "cryptoassets": assets_app = get_cryptoassets() assets_app.create_tables()
def managed_transaction(func): """Handle the decorated function inside ConflictResolver managed transaction. http://cryptoassetscore.readthedocs.org/en/latest/api/utils.html#module-cryptoassets.core.utils.conflictresolver """ app = get_cryptoassets() return app.conflict_resolver.managed_transaction(func)
def ready(self): # Register signal handlers from tatianastore import payment from tatianastore import signals # Set 1 confirmations required for finalized payments if settings.PAYMENT_CURRENCY == "btc": btc = get_cryptoassets().coins.get("btc") btc.coin_description.Transaction.confirmation_count = 1
def clear(): models.Store.objects.all().delete() models.Album.objects.all().delete() models.Song.objects.all().delete() models.User.objects.all().delete() assets_app = get_cryptoassets() assets_app.create_tables() assets_app.clear_tables() redis = get_cache("default") redis.clear()
def setUp(self): clear() tasks.update_exchange_rates() converter = models.get_rate_converter() self.fiat_price = Decimal(settings.TEST_CREDITING_PRICE) self.btc_price = pricing = converter.convert( settings.DEFAULT_PRICING_CURRENCY, settings.PAYMENT_CURRENCY, self.fiat_price) logger.debug("Test album price is %s USD, %s BTC", self.fiat_price, self.btc_price) owner = models.User.objects.create(username="******", email="*****@*****.**") test_store = models.Store.objects.create(name="Test Store") test_store.currency = settings.DEFAULT_PRICING_CURRENCY test_store.store_url = "http://localhost:8000/store/test-store/" test_store.operators = [owner] test_store.save() test_album = models.Album.objects.create(name="Test Album", store=test_store) test_album.fiat_price = self.fiat_price test_album.description = "My very first album åäö" test_album.save() test_song1 = models.Song.objects.create(name="Song A", album=test_album, store=test_store) test_song1.fiat_price = self.fiat_price test_song1.save() self.test_store = test_store self.test_album = test_album self.test_song = test_song1 assets_app = get_cryptoassets() assets_app.create_tables()
def force_check_old_address(tx): """Handle incoming transaction set even if the event handling process is dead. (which will not happen, but was required for blockchain.info) :return True if the transaction has succeeded """ if tx.btc_received_at: # Already paid return True btc = get_cryptoassets().coins.get(settings.PAYMENT_CURRENCY) Address = btc.coin_description.Address @assetdb.managed_transaction def get_value(session): address = session.query(Address).filter(Address.address == tx.btc_address).first() # noqa if not address: logger.warn("Could not find address %s", tx.btc_address) return 0, None balance = address.get_balance_by_confirmations(confirmations=0) logger.debug("Address %s, unconfirmed balance %s", address.address, balance) if address.transactions: txid = address.transactions[0].txid else: txid = None return balance, txid value, txid = get_value() if not value: return False if tx.check_balance(value, transaction_hash=txid): return True return False
def setUp(self): clear() owner = models.User.objects.create(username="******", email="*****@*****.**") tasks.update_exchange_rates() converter = models.get_rate_converter() self.fiat_price = Decimal(settings.TEST_CREDITING_PRICE) self.btc_price = converter.convert(settings.DEFAULT_PRICING_CURRENCY, settings.PAYMENT_CURRENCY, self.fiat_price) logger.debug("Test album price is %s USD, %s BTC", self.fiat_price, self.btc_price) self.test_store = test_store = models.Store.objects.create( name="Test Store åäö") test_store.currency = settings.DEFAULT_PRICING_CURRENCY test_store.store_url = "http://localhost:8000/store/test-store/" test_store.operators = [owner] test_store.btc_address = "19356KxTs9Bw5AAdxens5hoxDSp5bsUKse" test_store.save() test_song1 = models.Song.objects.create(name="Song A", store=test_store) test_song1.fiat_price = self.fiat_price test_song1.save() self.test_store = test_store self.test_song = test_song1 if settings.PAYMENT_SOURCE == "cryptoassets": assets_app = get_cryptoassets() assets_app.create_tables()
def setUp(self): clear() tasks.update_exchange_rates() converter = models.get_rate_converter() self.fiat_price = Decimal(settings.TEST_CREDITING_PRICE) self.btc_price = pricing = converter.convert(settings.DEFAULT_PRICING_CURRENCY, settings.PAYMENT_CURRENCY, self.fiat_price) logger.debug("Test album price is %s USD, %s BTC", self.fiat_price, self.btc_price) owner = models.User.objects.create(username="******", email="*****@*****.**") test_store = models.Store.objects.create(name="Test Store") test_store.currency = settings.DEFAULT_PRICING_CURRENCY test_store.store_url = "http://localhost:8000/store/test-store/" test_store.operators = [owner] test_store.save() test_album = models.Album.objects.create(name="Test Album", store=test_store) test_album.fiat_price = self.fiat_price test_album.description = u"My very first album åäö" test_album.save() test_song1 = models.Song.objects.create(name="Song A", album=test_album, store=test_store) test_song1.fiat_price = self.fiat_price test_song1.save() self.test_store = test_store self.test_album = test_album self.test_song = test_song1 assets_app = get_cryptoassets() assets_app.create_tables()
def get_wallet(session): """Return the master shared wallet used to receive payments. """ cryptoassets = get_cryptoassets() wallet_class = cryptoassets.coins.get(settings.PAYMENT_CURRENCY).wallet_model wallet = wallet_class.get_or_create_by_name("default", session) return wallet
def test_credit_artist(self): """Check that crediting artists work. We do testing by creating "third party" account in our default wallet. When the crediting happens, cryptoassets detects the receiving address is internal and makes an internal transaction to this address. """ from tatianastore import payment assets_app = get_cryptoassets() # Get hold of Bitcoin SQLAlchemy model descriptions btc = assets_app.coins.get("btc") # Get handle of BitcoinAccount class Account = btc.coin_description.Account store = self.test_store # First create address where the payments go in @assetdb.managed_transaction def tx(session): wallet = payment.get_wallet(session) external_account = wallet.create_account( "external account {}".format(time.time())) session.flush() address = wallet.create_receiving_address( external_account, "tatianastore credit test {}".format(time.time())) return address.address address_str = tx() logger.info("The crediting target address is %s", address_str) store.btc_address = address_str store.save() # Create a transaction session_id = "123" transaction = models.DownloadTransaction.objects.create() user_currency = settings.DEFAULT_PRICING_CURRENCY items = [self.test_song] transaction.prepare(items, description="Test download", session_id=session_id, ip="1.1.1.1", user_currency=user_currency) transaction.mark_payment_received() spoof_store_received_balance(store, self.btc_price) account_id, balance = payment.get_store_account_info(store) self.assertEqual(balance, self.btc_price) credited_count = creditor.credit_store(store) self.assertEqual(1, credited_count) @assetdb.managed_transaction def tx2(session): wallet = payment.get_wallet(session) # Check the "external" store owner account got the payment external_account = wallet.get_or_create_account_by_name( "external account") # We lose a bit in currency conversions forth and back self.assertAlmostEqual(self.btc_price, external_account.balance, 2) # This is account where the customer paid and it should be zero after crediting, as the btc has been credited away receiving_account = session.query(Account).get(account_id) logger.info("Checking account %s", receiving_account) self.assertAlmostEqual(receiving_account.balance, 0, 2) tx2()
def test_credit_artist(self): """Check that crediting artists work. We do testing by creating "third party" account in our default wallet. When the crediting happens, cryptoassets detects the receiving address is internal and makes an internal transaction to this address. """ from tatianastore import payment assets_app = get_cryptoassets() # Get hold of Bitcoin SQLAlchemy model descriptions btc = assets_app.coins.get("btc") # Get handle of BitcoinAccount class Account = btc.coin_description.Account store = self.test_store # First create address where the payments go in @assetdb.managed_transaction def tx(session): wallet = payment.get_wallet(session) external_account = wallet.create_account("external account {}".format(time.time())) session.flush() address = wallet.create_receiving_address(external_account, "tatianastore credit test {}".format(time.time())) return address.address address_str = tx() logger.info("The crediting target address is %s", address_str) store.btc_address = address_str store.save() # Create a transaction session_id = "123" transaction = models.DownloadTransaction.objects.create() user_currency = settings.DEFAULT_PRICING_CURRENCY items = [self.test_song] transaction.prepare(items, description="Test download", session_id=session_id, ip="1.1.1.1", user_currency=user_currency) transaction.mark_payment_received() spoof_store_received_balance(store, self.btc_price) account_id, balance = payment.get_store_account_info(store) self.assertEqual(balance, self.btc_price) credited_count = creditor.credit_store(store) self.assertEqual(1, credited_count) @assetdb.managed_transaction def tx2(session): wallet = payment.get_wallet(session) # Check the "external" store owner account got the payment external_account = wallet.get_or_create_account_by_name("external account") # We lose a bit in currency conversions forth and back self.assertAlmostEqual(self.btc_price, external_account.balance, 2) # This is account where the customer paid and it should be zero after crediting, as the btc has been credited away receiving_account = session.query(Account).get(account_id) logger.info("Checking account %s", receiving_account) self.assertAlmostEqual(receiving_account.balance, 0, 2) tx2()