def _call(self, method, **kwargs): """ Wrapper method for executing all API commands over HTTP. This method is further used to implement wrapper methods listed here: https://www.x.com/docs/DOC-1374 ``method`` must be a supported NVP method listed at the above address. ``kwargs`` the actual call parameters """ post_params = self._get_call_params(method, **kwargs) payload = post_params['data'] api_endpoint = post_params['url'] # This shows all of the key/val pairs we're sending to PayPal. if logger.isEnabledFor(logging.DEBUG): logger.debug('PayPal NVP Query Key/Vals:\n%s' % pformat(payload)) http_response = requests.post(**post_params) response = PayPalResponse(http_response.text, self.config) logger.debug('PayPal NVP API Endpoint: %s' % api_endpoint) if not response.success: raise PayPalAPIResponseError(response) return response
def _call(self, method, **kwargs): """ Wrapper method for executing all API commands over HTTP. This method is further used to implement wrapper methods listed here: https://www.x.com/docs/DOC-1374 ``method`` must be a supported NVP method listed at the above address. ``kwargs`` will be a hash of """ # This dict holds the key/value pairs to pass to the PayPal API. url_values = { 'METHOD': method, 'VERSION': self.config.API_VERSION, } if self.config.API_AUTHENTICATION_MODE == "3TOKEN": url_values['USER'] = self.config.API_USERNAME url_values['PWD'] = self.config.API_PASSWORD url_values['SIGNATURE'] = self.config.API_SIGNATURE elif self.config.API_AUTHENTICATION_MODE == "UNIPAY": url_values['SUBJECT'] = self.config.UNIPAY_SUBJECT # All values passed to PayPal API must be uppercase. for key, value in kwargs.items(): url_values[key.upper()] = value # This shows all of the key/val pairs we're sending to PayPal. if logger.isEnabledFor(logging.DEBUG): logger.debug('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) req = requests.post( self.config.API_ENDPOINT, data=url_values, timeout=self.config.HTTP_TIMEOUT, verify=self.config.API_CA_CERTS, ) # Call paypal API response = PayPalResponse(req.text, self.config) logger.debug('PayPal NVP API Endpoint: %s' % self.config.API_ENDPOINT) if not response.success: logger.error('A PayPal API error was encountered.') url_values_no_credentials = dict((p, 'X' * len(v) if p in \ self.__credentials else v) for (p, v) in url_values.items()) logger.error('PayPal NVP Query Key/Vals (credentials removed):' \ '\n%s' % pformat(url_values_no_credentials)) logger.error('PayPal NVP Query Response') logger.error(response) raise PayPalAPIResponseError(response) return response
def _call(self, method, **kwargs): """ Wrapper method for executing all API commands over HTTP. This method is further used to implement wrapper methods listed here: https://www.x.com/docs/DOC-1374 ``method`` must be a supported NVP method listed at the above address. ``kwargs`` will be a hash of """ socket.setdefaulttimeout(self.config.HTTP_TIMEOUT) url_values = {'METHOD': method, 'VERSION': self.config.API_VERSION} headers = {} if (self.config.API_AUTHENTICATION_MODE == "3TOKEN"): # headers['X-PAYPAL-SECURITY-USERID'] = API_USERNAME # headers['X-PAYPAL-SECURITY-PASSWORD'] = API_PASSWORD # headers['X-PAYPAL-SECURITY-SIGNATURE'] = API_SIGNATURE url_values['USER'] = self.config.API_USERNAME url_values['PWD'] = self.config.API_PASSWORD url_values['SIGNATURE'] = self.config.API_SIGNATURE elif (self.config.API_AUTHENTICATION_MODE == "UNIPAY"): # headers['X-PAYPAL-SECURITY-SUBJECT'] = SUBJECT url_values['SUBJECT'] = self.config.SUBJECT # headers['X-PAYPAL-REQUEST-DATA-FORMAT'] = 'NV' # headers['X-PAYPAL-RESPONSE-DATA-FORMAT'] = 'NV' # print(headers) for key, value in kwargs.iteritems(): url_values[key.upper()] = value # When in DEBUG level 2 or greater, print out the NVP pairs. if self.config.DEBUG_LEVEL >= 2: k = url_values.keys() k.sort() for i in k: print " %-20s : %s" % (i, url_values[i]) url = self._encode_utf8(**url_values) data = urllib.urlencode(url) req = urllib2.Request(self.config.API_ENDPOINT, data, headers) response = PayPalResponse(urllib2.urlopen(req).read(), self.config) if self.config.DEBUG_LEVEL >= 1: print " %-20s : %s" % ("ENDPOINT", self.config.API_ENDPOINT) if not response.success: if self.config.DEBUG_LEVEL >= 1: print response raise PayPalAPIResponseError(response) return response
def _call(self, method, **kwargs): """ Wrapper method for executing all API commands over HTTP. This method is further used to implement wrapper methods listed here: https://www.x.com/docs/DOC-1374 ``method`` must be a supported NVP method listed at the above address. ``kwargs`` will be a hash of """ # Beware, this is a global setting. socket.setdefaulttimeout(self.config.HTTP_TIMEOUT) # This dict holds the key/value pairs to pass to the PayPal API. url_values = { 'METHOD': method, 'VERSION': self.config.API_VERSION, } if (self.config.API_AUTHENTICATION_MODE == "3TOKEN"): url_values['USER'] = self.config.API_USERNAME url_values['PWD'] = self.config.API_PASSWORD url_values['SIGNATURE'] = self.config.API_SIGNATURE elif (self.config.API_AUTHENTICATION_MODE == "UNIPAY"): url_values['SUBJECT'] = self.config.SUBJECT # All values passed to PayPal API must be uppercase. for key, value in kwargs.iteritems(): url_values[key.upper()] = value # This shows all of the key/val pairs we're sending to PayPal. if logger.isEnabledFor(logging.DEBUG): logger.debug('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) url = self._encode_utf8(**url_values) data = urllib.urlencode(url).encode('utf-8') req = urllib2.Request(self.config.API_ENDPOINT, data) response = PayPalResponse( urllib2.urlopen(req).read().decode('utf-8'), self.config) logger.debug('PayPal NVP API Endpoint: %s' % self.config.API_ENDPOINT) if not response.success: logger.error('A PayPal API error was encountered.') logger.error('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) logger.error('PayPal NVP Query Response') logger.error(response) raise PayPalAPIResponseError(response) return response
def __init__(self, raw, config): self.raw = raw self.config = config l_regex = re.compile("L_([a-zA-Z]+)([0-9]{0,2})") # name-value pair list syntax documented at # https://developer.paypal.com/docs/classic/api/NVPAPIOverview/#id084E30EC030 # api returns max 100 items, so only two digits required self.list_items_dict = {} for key in self.raw.keys(): match = l_regex.match(key) if match: index = match.group(2) d_key = match.group(1) if isinstance(self.raw[key], list) and len(self.raw[key]) == 1: d_val = self.raw[key][0] else: d_val = self.raw[key] # Skip error codes if d_key in self.skippable_error_codes: continue if index in self.list_items_dict: # Dict for index exists, update self.list_items_dict[index][d_key] = d_val else: # Create new dict self.list_items_dict[index] = {d_key: d_val} # Log ResponseErrors from warning keys if self.raw['ACK'][0].upper() == self.config.ACK_SUCCESS_WITH_WARNING: self.errors = [PayPalAPIResponseError(self)] logger.error(self.errors)
def _call(self, method, **kwargs): """ Wrapper method for executing all API commands over HTTP. This method is further used to implement wrapper methods listed here: https://www.x.com/docs/DOC-1374 ``method`` must be a supported NVP method listed at the above address. ``kwargs`` will be a hash of """ # This dict holds the key/value pairs to pass to the PayPal API. url_values = { 'METHOD': method, 'VERSION': self.config.API_VERSION, } if self.config.API_AUTHENTICATION_MODE == "3TOKEN": url_values['USER'] = self.config.API_USERNAME url_values['PWD'] = self.config.API_PASSWORD url_values['SIGNATURE'] = self.config.API_SIGNATURE elif self.config.API_AUTHENTICATION_MODE == "3TOKEN_SUBJECT": url_values['USER'] = self.config.API_USERNAME url_values['PWD'] = self.config.API_PASSWORD url_values['SIGNATURE'] = self.config.API_SIGNATURE url_values['SUBJECT'] = self.config.SUBJECT elif self.config.API_AUTHENTICATION_MODE == "UNIPAY": url_values['SUBJECT'] = self.config.UNIPAY_SUBJECT # All values passed to PayPal API must be uppercase. for key, value in kwargs.items(): url_values[key.upper()] = value headers = {} if self.config.API_AUTHENTICATION_MODE == 'ACCESS_TOKEN': timestamp, signature = getAuthHeader(self.config.API_USERNAME, self.config.API_PASSWORD, self.config.ACCESS_TOKEN, self.config.TOKEN_SECRET, 'POST', self.config.API_ENDPOINT) headers['X-PAYPAL-AUTHORIZATION'] = str('token=' + self.config.ACCESS_TOKEN + ',signature=' + signature + ',timestamp=' + timestamp) headers['X-PAYPAL-APPLICATION-ID'] = self.config.APPLICATION_ID headers['X-PAYPAL-REQUEST-DATA-FORMAT'] = 'NV' headers['X-PAYPAL-RESPONSE-DATA-FORMAT'] = 'NV' # This shows all of the key/val pairs we're sending to PayPal. if logger.isEnabledFor(logging.DEBUG): logger.debug('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) req = requests.post( self.config.API_ENDPOINT, data=url_values, timeout=self.config.HTTP_TIMEOUT, verify=self.config.API_CA_CERTS, headers=headers, ) # Call paypal API response = PayPalResponse(req.text, self.config) logger.debug('PayPal NVP API Endpoint: %s' % self.config.API_ENDPOINT) if not response.success: #logger.error('A PayPal API error was encountered.') #logger.error('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) #logger.error('PayPal NVP Query Response') #logger.error(response) raise PayPalAPIResponseError(response) return response
def _call(self, method, **kwargs): """ Wrapper method for executing all API commands over HTTP. This method is further used to implement wrapper methods listed here: https://www.x.com/docs/DOC-1374 ``method`` must be a supported NVP method listed at the above address. ``kwargs`` will be a hash of """ # Beware, this is a global setting. socket.setdefaulttimeout(self.config.HTTP_TIMEOUT) headers = { 'X-PAYPAL-SECURITY-USERID': self.config.API_USERID, 'X-PAYPAL-SECURITY-PASSWORD': self.config.API_PASSWORD, 'X-PAYPAL-SECURITY-SIGNATURE': self.config.API_SIGNATURE, 'X-PAYPAL-REQUEST-DATA-FORMAT': 'JSON', 'X-PAYPAL-RESPONSE-DATA-FORMAT': 'JSON', 'X-PAYPAL-APPLICATION-ID': self.config.APPLICATION_ID, } data = { 'currencyCode': 'USD', 'returnUrl': self.config.return_url, 'cancelUrl': self.config.cancel_url, 'requestEnvelope': { 'errorLanguage': 'en_US' }, } if shipping: data['actionType'] = 'CREATE' else: data['actionType'] = 'PAY' if secondary_receiver == None: # simple payment data['receiverList'] = { 'receiver': [{ 'email': settings.PAYPAL_EMAIL, 'amount': '%f' % amount }] } else: # chained commission = amount * settings.PAYPAL_COMMISSION data['receiverList'] = { 'receiver': [ { 'email': settings.PAYPAL_EMAIL, 'amount': '%0.2f' % amount, 'primary': 'true' }, { 'email': secondary_receiver, 'amount': '%0.2f' % (amount - commission), 'primary': 'false' }, ] } if ipn_url != None: data['ipnNotificationUrl'] = ipn_url self.raw_request = json.dumps(data) # This dict holds the key/value pairs to pass to the PayPal API. url_values = { 'METHOD': method, 'VERSION': self.config.API_VERSION, } if (self.config.API_AUTHENTICATION_MODE == "3TOKEN"): url_values['USER'] = self.config.API_USERNAME url_values['PWD'] = self.config.API_PASSWORD url_values['SIGNATURE'] = self.config.API_SIGNATURE elif (self.config.API_AUTHENTICATION_MODE == "UNIPAY"): url_values['SUBJECT'] = self.config.SUBJECT # All values passed to PayPal API must be uppercase. for key, value in kwargs.iteritems(): url_values[key.upper()] = value # This shows all of the key/val pairs we're sending to PayPal. if logger.isEnabledFor(logging.DEBUG): logger.debug('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) url = self._encode_utf8(**url_values) data = urllib.urlencode(url).encode('utf-8') req = urllib2.Request(self.config.API_ENDPOINT, data) # If certificate provided build an opener that will validate PayPal server cert if self.config.API_CA_CERTS: handler = CertValidatingHTTPSHandler( ca_certs=self.config.API_CA_CERTS) opener = urllib2.build_opener(handler) if logger.isEnabledFor(logging.DEBUG): logger.debug( 'Validating PayPal server with certificate:\n%s\n' % self.config.API_CA_CERTS) else: opener = urllib2.build_opener() if logger.isEnabledFor(logging.DEBUG): logger.debug('Skipping PayPal server certificate validation') # Call paypal API response = PayPalResponse( opener.open(req).read().decode('utf-8'), self.config) logger.debug('PayPal NVP API Endpoint: %s' % self.config.API_ENDPOINT) if not response.success: logger.error('A PayPal API error was encountered.') logger.error('PayPal NVP Query Key/Vals:\n%s' % pformat(url_values)) logger.error('PayPal NVP Query Response') logger.error(response) raise PayPalAPIResponseError(response) return response