<s> from rest_framework import serializers <EOL> from django . contrib . auth . models import User <EOL> from rest_framework import status <EOL> from crowdsourcing . models import Transaction , FinancialAccount , PayPalFlow , UserProfile <EOL> from crowdsourcing . serializers . dynamic import DynamicFieldsModelSerializer <EOL> from crowdsourcing . validators . utils import InequalityValidator , ConditionallyRequiredValidator <EOL> from crowdsourcing . utils import PayPalBackend , get_model_or_none <EOL> class FinancialAccountSerializer ( DynamicFieldsModelSerializer ) : <EOL> class Meta : <EOL> model = FinancialAccount <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TransactionSerializer ( DynamicFieldsModelSerializer ) : <EOL> class Meta : <EOL> model = Transaction <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> read_only_fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def create ( self , * args , ** kwargs ) : <EOL> transaction = Transaction . objects . create ( ** self . validated_data ) <EOL> transaction . recipient . balance += transaction . amount <EOL> transaction . recipient . save ( ) <EOL> if transaction . sender . type not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> transaction . sender . balance -= transaction . amount <EOL> transaction . sender . save ( ) <EOL> return transaction <EOL> class PayPalFlowSerializer ( DynamicFieldsModelSerializer ) : <EOL> class Meta : <EOL> model = PayPalFlow <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT:state>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> read_only_fields = ( '<STR_LIT:state>' , '<STR_LIT>' ) <EOL> def create ( self , * args , ** kwargs ) : <EOL> flow = PayPalFlow . objects . create ( <EOL> state = '<STR_LIT>' , <EOL> recipient = kwargs [ '<STR_LIT>' ] , <EOL> paypal_id = self . validated_data [ '<STR_LIT>' ] , <EOL> redirect_url = self . validated_data [ '<STR_LIT>' ] <EOL> ) <EOL> return flow <EOL> def execute ( self , * args , ** kwargs ) : <EOL> paypalbackend = PayPalBackend ( ) <EOL> payment = paypalbackend . paypalrestsdk . Payment . find ( self . validated_data [ '<STR_LIT>' ] ) <EOL> if payment [ '<STR_LIT:state>' ] == '<STR_LIT>' and not Transaction . objects . filter ( reference = payment [ "<STR_LIT:id>" ] ) : <EOL> return self . create_transaction ( payment , self . validated_data [ '<STR_LIT>' ] ) <EOL> if payment . execute ( { "<STR_LIT>" : self . validated_data [ '<STR_LIT>' ] } ) : <EOL> return self . create_transaction ( payment , self . validated_data [ '<STR_LIT>' ] ) <EOL> else : <EOL> if Transaction . objects . filter ( reference = payment [ "<STR_LIT:id>" ] ) : <EOL> return payment . error [ '<STR_LIT:message>' ] , status . HTTP_400_BAD_REQUEST <EOL> else : <EOL> return self . create_transaction ( payment , self . validated_data [ '<STR_LIT>' ] ) <EOL> def create_transaction ( self , payment , payer_id ) : <EOL> flow = PayPalFlow . objects . get ( paypal_id = payment [ '<STR_LIT:id>' ] ) <EOL> flow . state = '<STR_LIT>' <EOL> flow . payer_id = payer_id <EOL> flow . save ( ) <EOL> sender = FinancialAccount . objects . filter ( is_system = True , type = "<STR_LIT>" ) . first ( ) <EOL> transaction = { <EOL> "<STR_LIT>" : payment [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : payment [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : flow . recipient . id , <EOL> "<STR_LIT>" : payment [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : payment [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : sender . id <EOL> } <EOL> if not self . context [ '<STR_LIT>' ] . user . is_anonymous ( ) : <EOL> transaction [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> else : <EOL> transaction [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> serializer = TransactionSerializer ( data = transaction ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . create ( ) <EOL> return '<STR_LIT>' , status . HTTP_201_CREATED <EOL> return serializer . errors , status . HTTP_400_BAD_REQUEST <EOL> class CreditCardSerializer ( serializers . Serializer ) : <EOL> type = serializers . ChoiceField ( choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> number = serializers . CharField ( min_length = <NUM_LIT> , max_length = <NUM_LIT> ) <EOL> expire_month = serializers . IntegerField ( min_value = <NUM_LIT:1> , max_value = <NUM_LIT:12> ) <EOL> expire_year = serializers . IntegerField ( ) <EOL> cvv2 = serializers . RegexField ( regex = '<STR_LIT>' , required = True ) <EOL> first_name = serializers . CharField ( ) <EOL> last_name = serializers . CharField ( ) <EOL> class Meta : <EOL> fields = ( '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class PayPalPaymentSerializer ( serializers . Serializer ) : <EOL> amount = serializers . DecimalField ( max_digits = <NUM_LIT> , decimal_places = <NUM_LIT:4> ) <EOL> type = serializers . ChoiceField ( choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> username = serializers . SlugRelatedField ( queryset = User . objects . all ( ) , slug_field = '<STR_LIT:username>' , allow_null = True , <EOL> required = False ) <EOL> method = serializers . ChoiceField ( choices = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> credit_card = CreditCardSerializer ( many = False , required = False ) <EOL> class Meta : <EOL> validators = [ <EOL> InequalityValidator ( <EOL> field = '<STR_LIT>' , operator = '<STR_LIT>' , value = <NUM_LIT:0> <EOL> ) , <EOL> ConditionallyRequiredValidator ( field = '<STR_LIT>' , field2 = '<STR_LIT>' , value = '<STR_LIT>' ) <EOL> ] <EOL> def build_payment ( self , * args , ** kwargs ) : <EOL> host = '<STR_LIT>' <EOL> if self . context [ '<STR_LIT>' ] . is_secure ( ) : <EOL> host = '<STR_LIT>' <EOL> else : <EOL> host = '<STR_LIT>' <EOL> host += self . context [ '<STR_LIT>' ] . META [ '<STR_LIT>' ] <EOL> payment = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . validated_data [ '<STR_LIT>' ] <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : host + "<STR_LIT>" , <EOL> "<STR_LIT>" : host + "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : str ( self . validated_data [ '<STR_LIT>' ] ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> } ] } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : str ( self . validated_data [ '<STR_LIT>' ] ) , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> "<STR_LIT:description>" : "<STR_LIT>" } ] <EOL> } <EOL> if self . validated_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> payment [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ { <EOL> "<STR_LIT>" : self . validated_data [ '<STR_LIT>' ] <EOL> } ] <EOL> return payment <EOL> def create ( self , * args , ** kwargs ) : <EOL> recipient = None <EOL> recipient_profile = None <EOL> payment_data = self . build_payment ( ) <EOL> if self . validated_data [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> recipient_profile = self . context [ '<STR_LIT>' ] . user . userprofile <EOL> else : <EOL> recipient_profile = get_model_or_none ( UserProfile , user__username = self . validated_data [ '<STR_LIT:username>' ] ) <EOL> recipient = get_model_or_none ( FinancialAccount , owner = recipient_profile , type = '<STR_LIT>' ) <EOL> paypalbackend = PayPalBackend ( ) <EOL> payment = paypalbackend . paypalrestsdk . Payment ( payment_data ) <EOL> if payment . create ( ) : <EOL> redirect_url = next ( ( link for link in payment [ '<STR_LIT>' ] if link [ '<STR_LIT>' ] == '<STR_LIT>' ) , '<STR_LIT:#>' ) <EOL> if payment [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> redirect_url = { <EOL> '<STR_LIT>' : '<STR_LIT:#>' <EOL> } <EOL> flow_data = { <EOL> "<STR_LIT>" : redirect_url [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : payment . id <EOL> } <EOL> payment_flow = PayPalFlowSerializer ( data = flow_data , fields = ( '<STR_LIT>' , '<STR_LIT>' , ) , <EOL> context = { '<STR_LIT>' : self . context [ '<STR_LIT>' ] } ) <EOL> if payment_flow . is_valid ( ) : <EOL> payment_flow . create ( recipient = recipient ) <EOL> if payment [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> data = { <EOL> "<STR_LIT>" : payment [ '<STR_LIT:id>' ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> execute_serializer = PayPalFlowSerializer ( fields = ( '<STR_LIT>' , '<STR_LIT>' ) , data = data , <EOL> context = { "<STR_LIT>" : self . context [ '<STR_LIT>' ] } ) <EOL> if execute_serializer . is_valid ( ) : <EOL> message , https_status = execute_serializer . execute ( ) <EOL> return { "<STR_LIT:message>" : message } , https_status <EOL> else : <EOL> return execute_serializer . errors , status . HTTP_400_BAD_REQUEST <EOL> return payment_flow . data , status . HTTP_201_CREATED <EOL> else : <EOL> return payment_flow . errors , status . HTTP_400_BAD_REQUEST <EOL> else : <EOL> return payment . error , status . HTTP_400_BAD_REQUEST </s>
