8
8
import os
9
9
from pprint import pformat
10
10
11
- from paypal .exceptions import PayPalConfigError , PayPalError
11
+ from paypal .exceptions import PayPalConfigError
12
12
13
13
logger = logging .getLogger ('paypal.settings' )
14
14
@@ -21,8 +21,8 @@ class PayPalConfig(object):
21
21
"""
22
22
# Used to validate correct values for certain config directives.
23
23
_valid_ = {
24
- 'API_ENVIRONMENT' : ['sandbox' ,'production' ],
25
- 'API_AUTHENTICATION_MODE' : ['3TOKEN' ,'CERTIFICATE' ],
24
+ 'API_ENVIRONMENT' : ['sandbox' , 'production' ],
25
+ 'API_AUTHENTICATION_MODE' : ['3TOKEN' , 'CERTIFICATE' ],
26
26
}
27
27
28
28
# Various API servers.
@@ -56,9 +56,11 @@ class PayPalConfig(object):
56
56
API_ENDPOINT = None
57
57
PAYPAL_URL_BASE = None
58
58
59
- # API Endpoint CA certificate chain
60
- # (filename with path e.g. '/etc/ssl/certs/Verisign_Class_3_Public_Primary_Certification_Authority.pem')
61
- API_CA_CERTS = None
59
+ # API Endpoint CA certificate chain. If this is True, do a simple SSL
60
+ # certificate check on the endpoint. If it's a full path, verify against
61
+ # a private cert.
62
+ # e.g. '/etc/ssl/certs/Verisign_Class_3_Public_Primary_Certification_Authority.pem'
63
+ API_CA_CERTS = True
62
64
63
65
# UNIPAY credentials
64
66
UNIPAY_SUBJECT = None
@@ -67,7 +69,7 @@ class PayPalConfig(object):
67
69
ACK_SUCCESS_WITH_WARNING = "SUCCESSWITHWARNING"
68
70
69
71
# In seconds. Depending on your setup, this may need to be higher.
70
- HTTP_TIMEOUT = 15
72
+ HTTP_TIMEOUT = 15.0
71
73
72
74
def __init__ (self , ** kwargs ):
73
75
"""
@@ -79,30 +81,38 @@ def __init__(self, **kwargs):
79
81
are applied for certain directives in the absence of
80
82
user-provided values.
81
83
"""
82
- if 'API_ENVIRONMENT' not in kwargs :
83
- kwargs ['API_ENVIRONMENT' ]= self .API_ENVIRONMENT
84
- # Make sure the environment is one of the acceptable values.
85
- if kwargs ['API_ENVIRONMENT' ] not in self ._valid_ ['API_ENVIRONMENT' ]:
86
- raise PayPalConfigError ('Invalid API_ENVIRONMENT' )
87
- self .API_ENVIRONMENT = kwargs ['API_ENVIRONMENT' ]
88
-
89
- if 'API_AUTHENTICATION_MODE' not in kwargs :
90
- kwargs ['API_AUTHENTICATION_MODE' ]= self .API_AUTHENTICATION_MODE
91
- # Make sure the auth mode is one of the known/implemented methods.
92
- if kwargs ['API_AUTHENTICATION_MODE' ] not in self ._valid_ ['API_AUTHENTICATION_MODE' ]:
93
- raise PayPalConfigError ("Not a supported auth mode. Use one of: %s" % \
94
- ", " .join (self ._valid_ ['API_AUTHENTICATION_MODE' ]))
84
+ if kwargs .get ('API_ENVIRONMENT' ):
85
+ api_environment = kwargs ['API_ENVIRONMENT' ].upper ()
86
+ # Make sure the environment is one of the acceptable values.
87
+ if api_environment not in self ._valid_ ['API_ENVIRONMENT' ]:
88
+ raise PayPalConfigError ('Invalid API_ENVIRONMENT' )
89
+ else :
90
+ self .API_ENVIRONMENT = api_environment
91
+
92
+ if kwargs .get ('API_AUTHENTICATION_MODE' ):
93
+ auth_mode = kwargs ['API_AUTHENTICATION_MODE' ].upper ()
94
+ # Make sure the auth mode is one of the known/implemented methods.
95
+ if auth_mode not in self ._valid_ ['API_AUTHENTICATION_MODE' ]:
96
+ choices = ", " .join (self ._valid_ ['API_AUTHENTICATION_MODE' ])
97
+ raise PayPalConfigError (
98
+ "Not a supported auth mode. Use one of: %s" % choices
99
+ )
100
+ else :
101
+ self .API_AUTHENTICATION_MODE = auth_mode
95
102
96
103
# Set the API endpoints, which is a cheesy way of saying API servers.
97
104
self .API_ENDPOINT = self ._API_ENDPOINTS [self .API_AUTHENTICATION_MODE ][self .API_ENVIRONMENT ]
98
105
self .PAYPAL_URL_BASE = self ._PAYPAL_URL_BASE [self .API_ENVIRONMENT ]
99
106
100
- # Set the CA_CERTS location
101
- if 'API_CA_CERTS' not in kwargs :
102
- kwargs ['API_CA_CERTS' ]= self .API_CA_CERTS
103
- if kwargs ['API_CA_CERTS' ] and not os .path .exists (kwargs ['API_CA_CERTS' ]):
104
- raise PayPalConfigError ('Invalid API_CA_CERTS' )
105
- self .API_CA_CERTS = kwargs ['API_CA_CERTS' ]
107
+ # Set the CA_CERTS location. This can either be a None, a bool, or a
108
+ # string path.
109
+ if kwargs .get ('API_CA_CERTS' ):
110
+ self .API_CA_CERTS = kwargs ['API_CA_CERTS' ]
111
+
112
+ if isinstance (self .API_CA_CERTS , basestring ) and \
113
+ not os .path .exists (self .API_CA_CERTS ):
114
+ # A CA Cert path was specified, but it's invalid.
115
+ raise PayPalConfigError ('Invalid API_CA_CERTS' )
106
116
107
117
# set the 3TOKEN required fields
108
118
if self .API_AUTHENTICATION_MODE == '3TOKEN' :
@@ -115,5 +125,6 @@ def __init__(self, **kwargs):
115
125
if arg in kwargs :
116
126
setattr (self , arg , kwargs [arg ])
117
127
118
- logger .debug ('PayPalConfig object instantiated with kwargs: %s' %
119
- pformat (kwargs ))
128
+ logger .debug (
129
+ 'PayPalConfig object instantiated with kwargs: %s' % pformat (kwargs )
130
+ )
0 commit comments