<s> """<STR_LIT>""" <EOL> import sys <EOL> import time <EOL> import re <EOL> import types <EOL> import logging <EOL> from urllib2 import Request <EOL> from urllib2 import urlopen <EOL> from urllib2 import urlparse <EOL> from classes import Address <EOL> from classes import Amount <EOL> from classes import CreditCard <EOL> from classes import parse_parameters <EOL> from classes import Profile <EOL> from classes import Response <EOL> from classes import Tracking <EOL> """<STR_LIT>""" <EOL> class CurrentTimeIdGenerator ( object ) : <EOL> def id ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( time . time ( ) * <NUM_LIT:1000> ) <EOL> class PayflowProClient ( object ) : <EOL> """<STR_LIT>""" <EOL> URL_BASE_TEST = '<STR_LIT>' <EOL> URL_BASE_LIVE = '<STR_LIT>' <EOL> REDIRECT_TEST = '<STR_LIT>' <EOL> REDIRECT_LIVE = '<STR_LIT>' <EOL> HOSTPORT = <NUM_LIT> <EOL> API_VERSION = '<STR_LIT:4>' <EOL> CLIENT_IDENTIFIER = '<STR_LIT>' <EOL> MAX_RETRY_COUNT = <NUM_LIT:5> <EOL> def __init__ ( self , partner , vendor , username , password , timeout_secs = <NUM_LIT> , <EOL> idgenerator = CurrentTimeIdGenerator ( ) , url_base = URL_BASE_TEST ) : <EOL> self . partner = partner <EOL> self . vendor = vendor <EOL> self . username = username <EOL> self . password = password <EOL> self . timeout = timeout_secs <EOL> self . url_base = url_base <EOL> self . idgenerator = idgenerator <EOL> self . log = logging . getLogger ( '<STR_LIT>' ) <EOL> if self . url_base == self . URL_BASE_TEST : <EOL> self . redirect = self . REDIRECT_TEST <EOL> else : <EOL> self . redirect = self . REDIRECT_LIVE <EOL> def _build_parmlist ( self , parameters ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> for key , value in parameters . items ( ) : <EOL> if not value is None : <EOL> if isinstance ( value , unicode ) : <EOL> key = '<STR_LIT>' % ( key . upper ( ) , len ( value . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> else : <EOL> key = '<STR_LIT>' % ( key . upper ( ) , len ( str ( value ) ) ) <EOL> args . append ( '<STR_LIT>' % ( key , value ) ) <EOL> args . sort ( ) <EOL> parmlist = '<STR_LIT:&>' . join ( args ) <EOL> return parmlist <EOL> def _parse_parmlist ( self , parmlist ) : <EOL> """<STR_LIT>""" <EOL> parmlist = "<STR_LIT:&>" + parmlist <EOL> name_re = re . compile ( r'<STR_LIT>' ) <EOL> results = { } <EOL> offset = <NUM_LIT:0> <EOL> match = name_re . search ( parmlist , offset ) <EOL> while match : <EOL> name , len_suffix = match . groups ( ) <EOL> offset = match . end ( ) <EOL> if len_suffix : <EOL> val_len = int ( len_suffix [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> else : <EOL> next_match = name_re . search ( parmlist , offset ) <EOL> if next_match : <EOL> val_len = next_match . start ( ) - match . end ( ) <EOL> else : <EOL> val_len = len ( parmlist ) - match . end ( ) <EOL> value = parmlist [ match . end ( ) : match . end ( ) + val_len ] <EOL> results [ name . lower ( ) ] = value <EOL> match = name_re . search ( parmlist , offset ) <EOL> return results <EOL> def _do_request ( self , request_id , parameters = { } ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if request_id is None : <EOL> request_id = self . idgenerator . id ( ) <EOL> req_params = dict ( parameters ) <EOL> req_params . update ( dict ( <EOL> partner = self . partner , <EOL> vendor = self . vendor , <EOL> user = self . username , <EOL> pwd = self . password , <EOL> ) ) <EOL> parmlist = self . _build_parmlist ( req_params ) <EOL> headers = { <EOL> '<STR_LIT>' : urlparse . urlsplit ( self . url_base ) [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : str ( request_id ) , <EOL> '<STR_LIT>' : str ( self . timeout ) , <EOL> '<STR_LIT>' : str ( self . timeout ) , <EOL> '<STR_LIT>' : self . CLIENT_IDENTIFIER , <EOL> '<STR_LIT>' : self . API_VERSION , <EOL> '<STR_LIT>' : sys . platform , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> } <EOL> self . log . debug ( u'<STR_LIT>' % headers ) <EOL> try_count = <NUM_LIT:0> <EOL> results = None <EOL> while ( results is None and try_count < self . MAX_RETRY_COUNT ) : <EOL> try : <EOL> try_count += <NUM_LIT:1> <EOL> request = Request ( <EOL> url = self . url_base , <EOL> data = parmlist . encode ( '<STR_LIT:utf-8>' ) , <EOL> headers = headers ) <EOL> response = urlopen ( request ) <EOL> result_parmlist = response . read ( ) <EOL> response . close ( ) <EOL> self . log . debug ( <EOL> u'<STR_LIT>' % result_parmlist . decode ( '<STR_LIT:utf-8>' ) <EOL> ) <EOL> results = self . _parse_parmlist ( result_parmlist ) <EOL> except Exception , e : <EOL> if try_count < self . MAX_RETRY_COUNT : <EOL> self . log . warn ( <EOL> u'<STR_LIT>' % ( <EOL> try_count , self . MAX_RETRY_COUNT ) , e <EOL> ) <EOL> else : <EOL> self . log . exception ( u'<STR_LIT>' , e ) <EOL> raise e <EOL> self . log . debug ( u'<STR_LIT>' % results ) <EOL> result_objects , unconsumed_data = parse_parameters ( results ) <EOL> self . log . debug ( u'<STR_LIT>' % result_objects ) <EOL> self . log . debug ( u'<STR_LIT>' % unconsumed_data ) <EOL> return ( result_objects , unconsumed_data ) <EOL> def sale ( self , credit_card , amount , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:S>" ) <EOL> for item in [ credit_card , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def authorization ( self , credit_card , amount , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:A>" ) <EOL> for item in [ credit_card , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def capture ( self , auth_pnref , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:D>" , origid = auth_pnref ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def voice_authorization ( self , voice_auth_code , credit_card , amount , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:F>" , authcode = voice_auth_code ) <EOL> for item in [ credit_card , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def credit_referenced ( self , original_pnref , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:C>" , origid = original_pnref ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def credit_unreferenced ( self , credit_card , amount , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:C>" ) <EOL> for item in [ credit_card , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def void ( self , original_pnref , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT>" , origid = original_pnref ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def inquiry ( self , original_pnref = None , customer_ref = None , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:I>" , origid = original_pnref , custref = customer_ref ) <EOL> if original_pnref is None and customer_ref is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not original_pnref is None and not customer_ref is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def reference_transaction ( self , transaction_type , original_pnref , amount , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = transaction_type , origid = original_pnref ) <EOL> for item in [ amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def set_checkout ( self , setpaypal , amount , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:S>" , action = "<STR_LIT:S>" ) <EOL> for item in [ setpaypal , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( None , params ) <EOL> def get_checkout ( self , getpaypal , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:S>" , action = "<STR_LIT>" ) <EOL> for item in [ getpaypal ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( None , params ) <EOL> def do_checkout ( self , dopaypal , amount , extras = [ ] ) : <EOL> params = dict ( trxtype = "<STR_LIT:S>" , action = "<STR_LIT:D>" ) <EOL> for item in [ dopaypal , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( None , params ) <EOL> def profile_add ( self , profile , credit_card , amount , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:A>' ) <EOL> for item in [ profile , credit_card , amount ] + extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def profile_add_from_transaction ( self , original_pnref , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:A>' , origid = original_pnref ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def profile_modify ( self , profile_id , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:M>' , origprofileid = profile_id ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def profile_reactivate ( self , profile_id , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:R>' , origprofileid = profile_id ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def profile_cancel ( self , profile_id , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:C>' , origprofileid = profile_id ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def profile_inquiry ( self , profile_id , payment_history_only = False , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:I>' , origprofileid = profile_id ) <EOL> if payment_history_only : <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:Y>' <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def profile_pay ( self , profile_id , payment_number , request_id = None , extras = [ ] ) : <EOL> params = dict ( trxtype = '<STR_LIT:R>' , action = '<STR_LIT:P>' , <EOL> origprofileid = profile_id , paymentnum = payment_number ) <EOL> for item in extras : <EOL> params . update ( item . data ) <EOL> return self . _do_request ( request_id , params ) <EOL> def find_class_in_list ( klass , lst ) : <EOL> """<STR_LIT>""" <EOL> filtered = filter ( lambda x : x . __class__ == klass , lst ) <EOL> if filtered : <EOL> return filtered [ <NUM_LIT:0> ] <EOL> return None <EOL> def find_classes_in_list ( klasses , lst ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( klasses , list ) : <EOL> klasses = [ klasses ] <EOL> return tuple ( map ( lambda klass : find_class_in_list ( klass , lst ) , klasses ) ) </s>
