<s> import bitcoinrpc <EOL> from decimal import Decimal <EOL> from billing import Gateway , GatewayNotConfigured <EOL> from billing . signals import transaction_was_unsuccessful , transaction_was_successful <EOL> from billing . utils . credit_card import CreditCard <EOL> from django . conf import settings <EOL> from django . utils import simplejson as json <EOL> class BitcoinGateway ( Gateway ) : <EOL> display_name = "<STR_LIT>" <EOL> homepage_url = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> test_mode = getattr ( settings , "<STR_LIT>" , True ) <EOL> merchant_settings = getattr ( settings , "<STR_LIT>" ) <EOL> if not merchant_settings or not merchant_settings . get ( "<STR_LIT>" ) : <EOL> raise GatewayNotConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . display_name ) <EOL> bitcoin_settings = merchant_settings [ "<STR_LIT>" ] <EOL> self . rpcuser = bitcoin_settings [ "<STR_LIT>" ] <EOL> self . rpcpassword = bitcoin_settings [ "<STR_LIT>" ] <EOL> self . host = bitcoin_settings . get ( "<STR_LIT>" , "<STR_LIT:127.0.0.1>" ) <EOL> self . port = bitcoin_settings . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . account = bitcoin_settings [ "<STR_LIT>" ] <EOL> self . minconf = bitcoin_settings . get ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . connection = bitcoinrpc . connect_to_remote ( <EOL> self . rpcuser , <EOL> self . rpcpassword , <EOL> self . host , <EOL> self . port <EOL> ) <EOL> def get_new_address ( self ) : <EOL> return self . connection . getnewaddress ( self . account ) <EOL> def get_transactions ( self ) : <EOL> return self . connection . listtransactions ( self . account ) <EOL> def get_transactions_by_address ( self , address ) : <EOL> all_txns = self . get_transactions ( ) <EOL> return filter ( lambda txn : txn . address == address , all_txns ) <EOL> def get_txns_sum ( self , txns ) : <EOL> return sum ( txn . amount for txn in txns ) <EOL> def purchase ( self , money , address , options = None ) : <EOL> options = options or { } <EOL> money = Decimal ( str ( money ) ) <EOL> txns = self . get_transactions_by_address ( address ) <EOL> received = self . get_txns_sum ( txns ) <EOL> response = [ txn . __dict__ for txn in txns ] <EOL> if received == money : <EOL> transaction_was_successful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = response ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : response } <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = response ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : response } </s>
