<s> import cgi <EOL> from datetime import datetime <EOL> import braintree <EOL> from braintree . util . crypto import Crypto <EOL> from braintree . error_result import ErrorResult <EOL> from braintree . exceptions . forged_query_string_error import ForgedQueryStringError <EOL> from braintree . util . http import Http <EOL> from braintree . signature_service import SignatureService <EOL> from braintree . successful_result import SuccessfulResult <EOL> from braintree . transparent_redirect import TransparentRedirect <EOL> class TransparentRedirectGateway ( object ) : <EOL> def __init__ ( self , gateway ) : <EOL> self . gateway = gateway <EOL> self . config = gateway . config <EOL> def confirm ( self , query_string ) : <EOL> """<STR_LIT>""" <EOL> parsed_query_string = self . _parse_and_validate_query_string ( query_string ) <EOL> confirmation_gateway = { <EOL> TransparentRedirect . Kind . CreateCustomer : "<STR_LIT>" , <EOL> TransparentRedirect . Kind . UpdateCustomer : "<STR_LIT>" , <EOL> TransparentRedirect . Kind . CreatePaymentMethod : "<STR_LIT>" , <EOL> TransparentRedirect . Kind . UpdatePaymentMethod : "<STR_LIT>" , <EOL> TransparentRedirect . Kind . CreateTransaction : "<STR_LIT>" <EOL> } [ parsed_query_string [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ] <EOL> return getattr ( self . gateway , confirmation_gateway ) . _post ( "<STR_LIT>" + parsed_query_string [ "<STR_LIT:id>" ] [ <NUM_LIT:0> ] + "<STR_LIT>" ) <EOL> def tr_data ( self , data , redirect_url ) : <EOL> data = self . __flatten_dictionary ( data ) <EOL> date_string = datetime . utcnow ( ) . strftime ( "<STR_LIT>" ) <EOL> data [ "<STR_LIT:time>" ] = date_string <EOL> data [ "<STR_LIT>" ] = redirect_url <EOL> data [ "<STR_LIT>" ] = self . config . public_key <EOL> data [ "<STR_LIT>" ] = self . config . api_version ( ) <EOL> return SignatureService ( self . config . private_key ) . sign ( data ) <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . config . base_url ( ) + self . config . base_merchant_path ( ) + "<STR_LIT>" <EOL> def _parse_and_validate_query_string ( self , query_string ) : <EOL> query_params = cgi . parse_qs ( query_string ) <EOL> http_status = int ( query_params [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> message = query_params . get ( "<STR_LIT>" ) <EOL> if message is not None : <EOL> message = message [ <NUM_LIT:0> ] <EOL> if Http . is_error_status ( http_status ) : <EOL> Http . raise_exception_from_status ( http_status , message ) <EOL> if not self . _is_valid_tr_query_string ( query_string ) : <EOL> raise ForgedQueryStringError <EOL> return query_params <EOL> def _is_valid_tr_query_string ( self , query_string ) : <EOL> content , hash = query_string . split ( "<STR_LIT>" ) <EOL> return hash == Crypto . sha1_hmac_hash ( self . config . private_key , content ) <EOL> def __flatten_dictionary ( self , params , parent = None ) : <EOL> data = { } <EOL> for key , val in params . items ( ) : <EOL> full_key = parent + "<STR_LIT:[>" + key + "<STR_LIT:]>" if parent else key <EOL> if isinstance ( val , dict ) : <EOL> data . update ( self . __flatten_dictionary ( val , full_key ) ) <EOL> else : <EOL> data [ full_key ] = val <EOL> return data </s>
