<s> from __future__ import unicode_literals <EOL> import datetime <EOL> import logging <EOL> import pprint <EOL> import time <EOL> import requests <EOL> from django . conf import settings <EOL> from django . forms . models import fields_for_model <EOL> from django . http import QueryDict <EOL> from django . utils import timezone <EOL> from django . utils . functional import cached_property <EOL> from django . utils . http import urlencode <EOL> from paypal . pro . exceptions import PayPalFailure <EOL> from paypal . pro . models import PayPalNVP <EOL> from paypal . pro . signals import ( <EOL> payment_profile_created , payment_was_successful , recurring_cancel , recurring_reactivate , recurring_suspend <EOL> ) <EOL> USER = settings . PAYPAL_WPP_USER <EOL> PASSWORD = settings . PAYPAL_WPP_PASSWORD <EOL> SIGNATURE = settings . PAYPAL_WPP_SIGNATURE <EOL> VERSION = <NUM_LIT> <EOL> BASE_PARAMS = dict ( USER = USER , PWD = PASSWORD , SIGNATURE = SIGNATURE , VERSION = VERSION ) <EOL> ENDPOINT = "<STR_LIT>" <EOL> SANDBOX_ENDPOINT = "<STR_LIT>" <EOL> EXPRESS_ENDPOINT = "<STR_LIT>" <EOL> SANDBOX_EXPRESS_ENDPOINT = "<STR_LIT>" <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def paypal_time ( time_obj = None ) : <EOL> """<STR_LIT>""" <EOL> if time_obj is None : <EOL> time_obj = time . gmtime ( ) <EOL> return time . strftime ( PayPalNVP . TIMESTAMP_FORMAT , time_obj ) <EOL> def paypaltime2datetime ( s ) : <EOL> """<STR_LIT>""" <EOL> naive = datetime . datetime . strptime ( s , PayPalNVP . TIMESTAMP_FORMAT ) <EOL> if not settings . USE_TZ : <EOL> return naive <EOL> else : <EOL> return timezone . make_aware ( naive , timezone . UTC ( ) ) <EOL> class PayPalError ( TypeError ) : <EOL> """<STR_LIT>""" <EOL> def express_endpoint ( ) : <EOL> if getattr ( settings , '<STR_LIT>' , True ) : <EOL> return SANDBOX_EXPRESS_ENDPOINT <EOL> else : <EOL> return EXPRESS_ENDPOINT <EOL> def express_endpoint_for_token ( token , commit = False ) : <EOL> """<STR_LIT>""" <EOL> pp_params = dict ( token = token ) <EOL> if commit : <EOL> pp_params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return express_endpoint ( ) % urlencode ( pp_params ) <EOL> class PayPalWPP ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request = None , params = BASE_PARAMS ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> if getattr ( settings , '<STR_LIT>' , True ) : <EOL> self . endpoint = SANDBOX_ENDPOINT <EOL> else : <EOL> self . endpoint = ENDPOINT <EOL> self . signature_values = params <EOL> self . signature = urlencode ( self . signature_values ) + "<STR_LIT:&>" <EOL> @ cached_property <EOL> def NVP_FIELDS ( self ) : <EOL> return list ( fields_for_model ( PayPalNVP ) . keys ( ) ) <EOL> def doDirectPayment ( self , params ) : <EOL> """<STR_LIT>""" <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:state>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> payment_was_successful . send ( sender = nvp_obj , ** params ) <EOL> return nvp_obj <EOL> def setExpressCheckout ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in params : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> params = params . copy ( ) <EOL> params . update ( { '<STR_LIT>' : params [ '<STR_LIT>' ] } ) <EOL> del params [ '<STR_LIT>' ] <EOL> if self . _is_recurring ( params ) : <EOL> params = self . _recurring_setExpressCheckout_adapter ( params ) <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:1> } <EOL> required = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def doExpressCheckoutPayment ( self , params ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in params : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> params = params . copy ( ) <EOL> params . update ( { '<STR_LIT>' : params [ '<STR_LIT>' ] } ) <EOL> del params [ '<STR_LIT>' ] <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> payment_was_successful . send ( sender = nvp_obj , ** params ) <EOL> return nvp_obj <EOL> def createRecurringPaymentsProfile ( self , params , direct = False ) : <EOL> """<STR_LIT>""" <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if direct : <EOL> required + [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> required + [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> payment_profile_created . send ( sender = nvp_obj , ** params ) <EOL> return nvp_obj <EOL> def getExpressCheckoutDetails ( self , params ) : <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def setCustomerBillingAgreement ( self , params ) : <EOL> raise DeprecationWarning <EOL> def createBillingAgreement ( self , params ) : <EOL> """<STR_LIT>""" <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def getTransactionDetails ( self , params ) : <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def massPay ( self , params ) : <EOL> raise NotImplementedError <EOL> def getRecurringPaymentsProfileDetails ( self , params ) : <EOL> raise NotImplementedError <EOL> def updateRecurringPaymentsProfile ( self , params ) : <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def billOutstandingAmount ( self , params ) : <EOL> raise NotImplementedError <EOL> def manangeRecurringPaymentsProfileStatus ( self , params , fail_silently = False ) : <EOL> """<STR_LIT>""" <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" , "<STR_LIT:action>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> flag_info_test_string = '<STR_LIT>' <EOL> if not nvp_obj . flag or ( fail_silently and nvp_obj . flag_info == flag_info_test_string ) : <EOL> if params [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> recurring_cancel . send ( sender = nvp_obj ) <EOL> elif params [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> recurring_suspend . send ( sender = nvp_obj ) <EOL> elif params [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> recurring_reactivate . send ( sender = nvp_obj ) <EOL> else : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def refundTransaction ( self , params ) : <EOL> raise NotImplementedError <EOL> def doReferenceTransaction ( self , params ) : <EOL> """<STR_LIT>""" <EOL> defaults = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> required = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> nvp_obj = self . _fetch ( params , required , defaults ) <EOL> if nvp_obj . flag : <EOL> raise PayPalFailure ( nvp_obj . flag_info , nvp = nvp_obj ) <EOL> return nvp_obj <EOL> def _is_recurring ( self , params ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' in params <EOL> def _recurring_setExpressCheckout_adapter ( self , params ) : <EOL> """<STR_LIT>""" <EOL> params [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> params [ '<STR_LIT>' ] = params [ '<STR_LIT>' ] <EOL> REMOVE = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> for k in params . keys ( ) : <EOL> if k in REMOVE : <EOL> del params [ k ] <EOL> return params <EOL> def _fetch ( self , params , required , defaults ) : <EOL> """<STR_LIT>""" <EOL> defaults . update ( params ) <EOL> pp_params = self . _check_and_update_params ( required , defaults ) <EOL> pp_string = self . signature + urlencode ( pp_params ) <EOL> response = self . _request ( pp_string ) <EOL> response_params = self . _parse_response ( response ) <EOL> log . debug ( '<STR_LIT>' , pprint . pformat ( defaults ) ) <EOL> log . debug ( '<STR_LIT>' , pprint . pformat ( response_params ) ) <EOL> nvp_params = { } <EOL> tmpd = defaults . copy ( ) <EOL> tmpd . update ( response_params ) <EOL> for k , v in tmpd . items ( ) : <EOL> if k in self . NVP_FIELDS : <EOL> nvp_params [ str ( k ) ] = v <EOL> if '<STR_LIT>' in nvp_params : <EOL> nvp_params [ '<STR_LIT>' ] = paypaltime2datetime ( nvp_params [ '<STR_LIT>' ] ) <EOL> nvp_obj = PayPalNVP ( ** nvp_params ) <EOL> nvp_obj . init ( self . request , params , response_params ) <EOL> nvp_obj . save ( ) <EOL> return nvp_obj <EOL> def _request ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return requests . post ( self . endpoint , data = data . encode ( "<STR_LIT:ascii>" ) ) . content <EOL> def _check_and_update_params ( self , required , params ) : <EOL> """<STR_LIT>""" <EOL> for r in required : <EOL> if r not in params : <EOL> raise PayPalError ( "<STR_LIT>" % r ) <EOL> return ( dict ( ( k . upper ( ) , v ) for k , v in params . items ( ) ) ) <EOL> def _parse_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> q = QueryDict ( response , encoding = '<STR_LIT>' ) . dict ( ) <EOL> return { k . lower ( ) : v for k , v in q . items ( ) } </s>
