<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . utils . translation import ugettext as _ <EOL> import stripe <EOL> from . widgets import StripeCheckoutWidget , StripeWidget <EOL> from . . import RedirectNeeded <EOL> from . . forms import PaymentForm as BasePaymentForm , CreditCardPaymentFormWithName <EOL> from . . models import FRAUD_CHOICES <EOL> class StripeFormMixin ( object ) : <EOL> charge = None <EOL> def _handle_potentially_fraudulent_charge ( self , charge , commit = True ) : <EOL> fraud_details = charge [ '<STR_LIT>' ] <EOL> if fraud_details . get ( '<STR_LIT>' , None ) == '<STR_LIT>' : <EOL> reject_fraud_choice = FRAUD_CHOICES [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> self . payment . change_fraud_status ( <EOL> reject_fraud_choice , commit = commit ) <EOL> else : <EOL> accept_fraud_choice = FRAUD_CHOICES [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> self . payment . change_fraud_status ( <EOL> accept_fraud_choice , commit = commit ) <EOL> def clean ( self ) : <EOL> data = self . cleaned_data <EOL> if not self . errors : <EOL> if not self . payment . transaction_id : <EOL> stripe . api_key = self . provider . secret_key <EOL> try : <EOL> self . charge = stripe . Charge . create ( <EOL> capture = False , <EOL> amount = int ( self . payment . total * <NUM_LIT:100> ) , <EOL> currency = self . payment . currency , <EOL> card = data [ '<STR_LIT>' ] , <EOL> description = '<STR_LIT>' % ( <EOL> self . payment . billing_last_name , <EOL> self . payment . billing_first_name ) ) <EOL> except stripe . CardError as e : <EOL> charge_id = e . json_body [ '<STR_LIT:error>' ] [ '<STR_LIT>' ] <EOL> self . charge = stripe . Charge . retrieve ( charge_id ) <EOL> self . _handle_potentially_fraudulent_charge ( <EOL> self . charge , commit = False ) <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( [ str ( e ) ] ) <EOL> self . payment . change_status ( '<STR_LIT:error>' , str ( e ) ) <EOL> else : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> self . _errors [ '<STR_LIT>' ] = self . error_class ( [ msg ] ) <EOL> return data <EOL> def save ( self ) : <EOL> self . payment . transaction_id = self . charge . id <EOL> self . payment . attrs . charge = stripe . util . json . dumps ( self . charge ) <EOL> self . payment . change_status ( '<STR_LIT>' ) <EOL> if self . provider . _capture : <EOL> self . payment . capture ( ) <EOL> self . _handle_potentially_fraudulent_charge ( self . charge ) <EOL> class ModalPaymentForm ( StripeFormMixin , BasePaymentForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( StripeFormMixin , self ) . __init__ ( hidden_inputs = False , * args , ** kwargs ) <EOL> widget = StripeCheckoutWidget ( provider = self . provider , payment = self . payment ) <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( widget = widget ) <EOL> if self . is_bound and not self . data . get ( '<STR_LIT>' ) : <EOL> self . payment . change_status ( '<STR_LIT>' ) <EOL> raise RedirectNeeded ( self . payment . get_failure_url ( ) ) <EOL> class PaymentForm ( StripeFormMixin , CreditCardPaymentFormWithName ) : <EOL> stripeToken = forms . CharField ( widget = StripeWidget ( ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( PaymentForm , self ) . __init__ ( * args , ** kwargs ) <EOL> stripe_attrs = self . fields [ '<STR_LIT>' ] . widget . attrs <EOL> stripe_attrs [ '<STR_LIT>' ] = self . provider . public_key <EOL> stripe_attrs [ '<STR_LIT>' ] = self . payment . billing_address_1 <EOL> stripe_attrs [ '<STR_LIT>' ] = self . payment . billing_address_2 <EOL> stripe_attrs [ '<STR_LIT>' ] = self . payment . billing_city <EOL> stripe_attrs [ '<STR_LIT>' ] = self . payment . billing_country_area <EOL> stripe_attrs [ '<STR_LIT>' ] = self . payment . billing_postcode <EOL> stripe_attrs [ '<STR_LIT>' ] = self . payment . billing_country_code </s>
