<s> from billing import Gateway , GatewayNotConfigured <EOL> from billing . utils . credit_card import InvalidCard , Visa , MasterCard , CreditCard <EOL> from billing . signals import transaction_was_successful , transaction_was_unsuccessful <EOL> from django . conf import settings <EOL> from wepay import WePay <EOL> from wepay . exceptions import WePayError <EOL> class WePayGateway ( Gateway ) : <EOL> display_name = "<STR_LIT>" <EOL> homepage_url = "<STR_LIT>" <EOL> default_currency = "<STR_LIT>" <EOL> supported_countries = [ "<STR_LIT>" ] <EOL> supported_cardtypes = [ Visa , MasterCard ] <EOL> def __init__ ( self ) : <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> super ( WePayGateway , self ) . __init__ ( ) <EOL> production = not self . test_mode <EOL> self . we_pay = WePay ( production ) <EOL> self . we_pay_settings = merchant_settings [ "<STR_LIT>" ] <EOL> def purchase ( self , money , credit_card , options = None ) : <EOL> options = options or { } <EOL> params = { } <EOL> params . update ( { <EOL> '<STR_LIT>' : options . pop ( "<STR_LIT>" , self . we_pay_settings . get ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> '<STR_LIT>' : options . pop ( "<STR_LIT:description>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : money , <EOL> } ) <EOL> if credit_card and not isinstance ( credit_card , CreditCard ) : <EOL> params [ "<STR_LIT>" ] = credit_card <EOL> params [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> params . update ( options ) <EOL> try : <EOL> response = self . we_pay . call ( '<STR_LIT>' , params , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <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> def authorize ( self , money , credit_card , options = None ) : <EOL> options = options or { } <EOL> resp = self . store ( credit_card , options ) <EOL> if resp [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = resp [ '<STR_LIT>' ] ) <EOL> return resp <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> try : <EOL> resp = self . we_pay . call ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : self . we_pay_settings [ "<STR_LIT>" ] , <EOL> '<STR_LIT>' : self . we_pay_settings [ "<STR_LIT>" ] , <EOL> '<STR_LIT>' : resp [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> } , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <EOL> params = { <EOL> "<STR_LIT>" : False <EOL> } <EOL> params . update ( options ) <EOL> response = self . purchase ( money , resp [ "<STR_LIT>" ] , params ) <EOL> if response [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = response [ "<STR_LIT>" ] ) <EOL> return response <EOL> transaction_was_successful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = response [ "<STR_LIT>" ] ) <EOL> return response <EOL> def capture ( self , money , authorization , options = None ) : <EOL> options = options or { } <EOL> params = { <EOL> '<STR_LIT>' : authorization , <EOL> } <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> try : <EOL> response = self . we_pay . call ( '<STR_LIT>' , params , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <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> def void ( self , identification , options = None ) : <EOL> options = options or { } <EOL> params = { <EOL> '<STR_LIT>' : identification , <EOL> '<STR_LIT>' : options . pop ( "<STR_LIT:description>" , "<STR_LIT>" ) <EOL> } <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> try : <EOL> response = self . we_pay . call ( '<STR_LIT>' , params , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <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> def credit ( self , money , identification , options = None ) : <EOL> options = options or { } <EOL> params = { <EOL> '<STR_LIT>' : identification , <EOL> '<STR_LIT>' : options . pop ( "<STR_LIT:description>" , "<STR_LIT>" ) , <EOL> } <EOL> if money : <EOL> params . update ( { '<STR_LIT>' : money } ) <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> try : <EOL> response = self . we_pay . call ( '<STR_LIT>' , params , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <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> def recurring ( self , money , credit_card , options = None ) : <EOL> options = options or { } <EOL> params = { <EOL> '<STR_LIT>' : self . we_pay_settings . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : options . pop ( "<STR_LIT:description>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : money , <EOL> } <EOL> params . update ( options ) <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> try : <EOL> response = self . we_pay . call ( "<STR_LIT>" , params , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <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> def store ( self , credit_card , options = None ) : <EOL> options = options or { } <EOL> if not self . validate_card ( credit_card ) : <EOL> raise InvalidCard ( "<STR_LIT>" ) <EOL> token = options . pop ( "<STR_LIT>" , self . we_pay_settings [ "<STR_LIT>" ] ) <EOL> try : <EOL> response = self . we_pay . call ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : self . we_pay_settings [ "<STR_LIT>" ] , <EOL> '<STR_LIT>' : credit_card . name , <EOL> '<STR_LIT:email>' : options . pop ( "<STR_LIT>" ) [ "<STR_LIT:email>" ] , <EOL> '<STR_LIT>' : credit_card . number , <EOL> '<STR_LIT>' : credit_card . verification_value , <EOL> '<STR_LIT>' : credit_card . month , <EOL> '<STR_LIT>' : credit_card . year , <EOL> '<STR_LIT:address>' : options . pop ( "<STR_LIT>" ) <EOL> } , token = token ) <EOL> except WePayError as error : <EOL> transaction_was_unsuccessful . send ( sender = self , <EOL> type = "<STR_LIT:store>" , <EOL> response = error ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : error } <EOL> transaction_was_successful . send ( sender = self , <EOL> type = "<STR_LIT:store>" , <EOL> response = response ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : response } </s>
