<s> import time <EOL> import logging <EOL> from paython . gateways . core import Gateway <EOL> from paython . exceptions import * <EOL> logger = logging . getLogger ( __name__ ) <EOL> try : <EOL> import samurai . config as config <EOL> from samurai . payment_method import PaymentMethod <EOL> from samurai . processor import Processor <EOL> from samurai . transaction import Transaction <EOL> except ImportError : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> class Samurai ( Gateway ) : <EOL> """<STR_LIT>""" <EOL> VERSION = '<STR_LIT>' <EOL> REQUEST_FIELDS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:address>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT:state>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> RESPONSE_KEYS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , merchant_key = None , password = None , processor = None , debug = False ) : <EOL> """<STR_LIT>""" <EOL> config . merchant_key = merchant_key <EOL> config . merchant_password = password <EOL> config . processor_token = processor <EOL> super ( Samurai , self ) . __init__ ( set_method = self . set , translations = self . REQUEST_FIELDS , debug = debug ) <EOL> if debug : <EOL> self . debug = True <EOL> debug_string = "<STR_LIT>" <EOL> logger . debug ( debug_string . center ( <NUM_LIT> , '<STR_LIT:=>' ) ) <EOL> def set ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def translate ( self , info ) : <EOL> """<STR_LIT>""" <EOL> new_dict = dict ( ) <EOL> for k , v in info . items ( ) : <EOL> try : <EOL> new_dict [ self . REQUEST_FIELDS [ k ] ] = v <EOL> except KeyError : <EOL> pass <EOL> return new_dict <EOL> def charge_setup ( self , card , billing_info ) : <EOL> """<STR_LIT>""" <EOL> billing_info = self . translate ( billing_info ) <EOL> card . _exp_yr_style = True <EOL> super ( Samurai , self ) . use_credit_card ( card ) <EOL> pm = PaymentMethod . create ( <EOL> card . number , <EOL> card . verification_value , <EOL> card . exp_month , card . exp_year , ** billing_info ) <EOL> debug_string = "<STR_LIT>" <EOL> logger . debug ( debug_string . center ( <NUM_LIT> , '<STR_LIT:=>' ) ) <EOL> logger . debug ( dir ( pm ) ) <EOL> if pm . errors : <EOL> raise DataValidationError ( '<STR_LIT>' % pm . errors [ pm . error_messages [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] ) <EOL> return pm . payment_method_token <EOL> def auth ( self , amount , credit_card = None , billing_info = None , shipping_info = None ) : <EOL> card_token = self . charge_setup ( credit_card , billing_info ) <EOL> start = time . time ( ) <EOL> response = Processor . authorize ( card_token , amount ) <EOL> end = time . time ( ) <EOL> response_time = '<STR_LIT>' % ( end - start ) <EOL> return self . parse ( response , response_time ) <EOL> def settle ( self , amount , trans_id ) : <EOL> txn = Transaction . find ( trans_id ) <EOL> if txn . errors : <EOL> raise GatewayError ( '<STR_LIT>' % txn . errors [ txn . error_messages [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> start = time . time ( ) <EOL> response = txn . capture ( amount ) <EOL> end = time . time ( ) <EOL> response_time = '<STR_LIT>' % ( end - start ) <EOL> return self . parse ( response , response_time ) <EOL> def capture ( self , amount , credit_card = None , billing_info = None , shipping_info = None ) : <EOL> card_token = self . charge_setup ( credit_card , billing_info ) <EOL> start = time . time ( ) <EOL> response = Processor . purchase ( card_token , amount ) <EOL> end = time . time ( ) <EOL> response_time = '<STR_LIT>' % ( end - start ) <EOL> return self . parse ( response , response_time ) <EOL> def void ( self , trans_id ) : <EOL> txn = Transaction . find ( trans_id ) <EOL> if txn . errors : <EOL> raise GatewayError ( '<STR_LIT>' % txn . errors [ txn . error_messages [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> start = time . time ( ) <EOL> response = txn . void ( ) <EOL> end = time . time ( ) <EOL> response_time = '<STR_LIT>' % ( end - start ) <EOL> return self . parse ( response , response_time ) <EOL> def credit ( self , amount , trans_id ) : <EOL> txn = Transaction . find ( trans_id ) <EOL> if txn . errors : <EOL> raise GatewayError ( '<STR_LIT>' % txn . errors [ txn . error_messages [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> start = time . time ( ) <EOL> response = txn . reverse ( amount ) <EOL> end = time . time ( ) <EOL> response_time = '<STR_LIT>' % ( end - start ) <EOL> return self . parse ( response , response_time ) <EOL> def parse ( self , response , response_time ) : <EOL> """<STR_LIT>""" <EOL> resp = response . __dict__ <EOL> rd = super ( Samurai , self ) . standardize ( resp , self . RESPONSE_KEYS , response_time , resp [ '<STR_LIT:success>' ] ) <EOL> if response . errors : <EOL> rd [ '<STR_LIT>' ] = resp [ '<STR_LIT>' ] [ resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> rd [ '<STR_LIT>' ] = resp [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> rd [ '<STR_LIT>' ] = resp [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> return rd </s>
