<s> from __future__ import unicode_literals <EOL> from requests . exceptions import HTTPError <EOL> from . . forms import CreditCardPaymentFormWithName <EOL> from . . core import get_credit_card_issuer <EOL> class PaymentForm ( CreditCardPaymentFormWithName ) : <EOL> VALID_TYPES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def clean ( self ) : <EOL> cleaned_data = super ( PaymentForm , self ) . clean ( ) <EOL> if not self . errors : <EOL> if not self . payment . transaction_id : <EOL> number = cleaned_data . get ( '<STR_LIT>' ) <EOL> card_type , _card_issuer = get_credit_card_issuer ( number ) <EOL> request_data = { '<STR_LIT:type>' : card_type } <EOL> request_data . update ( cleaned_data ) <EOL> try : <EOL> data = self . provider . create_payment ( <EOL> self . payment , cleaned_data ) <EOL> except HTTPError as e : <EOL> response = e . response <EOL> if response . status_code == <NUM_LIT> : <EOL> error_data = e . response . json ( ) <EOL> errors = [ <EOL> error [ '<STR_LIT>' ] for error in error_data [ '<STR_LIT>' ] ] <EOL> else : <EOL> errors = [ '<STR_LIT>' ] <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( errors ) <EOL> self . payment . change_status ( '<STR_LIT:error>' ) <EOL> else : <EOL> self . payment . transaction_id = data [ '<STR_LIT:id>' ] <EOL> self . provider . set_response_links ( self . payment , data ) <EOL> if self . provider . _capture : <EOL> self . payment . captured_amount = self . payment . total <EOL> self . payment . change_status ( '<STR_LIT>' ) <EOL> else : <EOL> self . payment . change_status ( '<STR_LIT>' ) <EOL> return cleaned_data </s>
