From 2d030fbf59e3318529841ff95230f8bf87fcf802 Mon Sep 17 00:00:00 2001 From: "Jonathan C. Otsuka" Date: Thu, 15 Sep 2016 14:21:25 -0500 Subject: [PATCH 1/5] Add ACTIVE/INACTIVE to HpsPayPlanPaymentMethodStatus and HpsPayPlanScheduleStatus vs subclassing. --- securesubmit/infrastructure/enums.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/securesubmit/infrastructure/enums.py b/securesubmit/infrastructure/enums.py index 8c20510..060f212 100644 --- a/securesubmit/infrastructure/enums.py +++ b/securesubmit/infrastructure/enums.py @@ -123,11 +123,13 @@ class HpsPayPlanCustomerStatus(Enum): INACTIVE = 'Inactive' -class HpsPayPlanPaymentMethodStatus(HpsPayPlanCustomerStatus): +class HpsPayPlanPaymentMethodStatus(Enum): INVALID = 'Invalid' REVOKED = 'Revoked' EXPIRED = 'Expired' LOST_STOLEN = 'Lost/Stolen' + ACTIVE = 'Active' + INACTIVE = 'Inactive' class HpsPayPlanPaymentMethodType(Enum): @@ -151,8 +153,10 @@ class HpsPayPlanScheduleFrequency(Enum): ANNUALLY = 'Annually' -class HpsPayPlanScheduleStatus(HpsPayPlanCustomerStatus): +class HpsPayPlanScheduleStatus(Enum): FAILED = 'FAILED' + ACTIVE = 'Active' + INACTIVE = 'Inactive' class HpsTrackDataMethod(Enum): @@ -170,4 +174,4 @@ class HpsAdditionalAmountType(Enum): class HpsTokenMappingType(Enum): UNIQUE = 'UNIQUE' - CONSTANT = 'CONSTANT' \ No newline at end of file + CONSTANT = 'CONSTANT' From f46ec70e1935046d2665a740ea2360476752ce2e Mon Sep 17 00:00:00 2001 From: "Jonathan C. Otsuka" Date: Wed, 25 Jan 2017 13:55:13 -0600 Subject: [PATCH 2/5] Add timeout to all http requests/urllib3. --- securesubmit/services/gateway.py | 53 ++++++++++++++++++++++---------- securesubmit/services/token.py | 10 ++++-- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/securesubmit/services/gateway.py b/securesubmit/services/gateway.py index d927330..379b07b 100644 --- a/securesubmit/services/gateway.py +++ b/securesubmit/services/gateway.py @@ -34,11 +34,13 @@ class HpsSoapGatewayService(object): _base_config = None _url = None _logging = False + _timeout = None - def __init__(self, config=None, enable_logging=False): + def __init__(self, config=None, enable_logging=False, timeout=None): self._base_config = HpsConfiguration() self._config = config self._logging = enable_logging + self._timeout = timeout self._url = self._base_config.soap_service_uri if self._config is not None: @@ -145,7 +147,13 @@ def do_transaction(self, transaction, client_transaction_id=None): request_headers = {'Content-type': 'text/xml; charset=UTF-8', 'Content-length': str(len(xml))} - request = http.request('POST', self._url, headers=request_headers, body=xml) + + request = None + if self._timeout is None: + request = http.request('POST', self._url, headers=request_headers, body=xml) + else: + request = http.request('POST', self._url, headers=request_headers, body=xml, timeout=self._timeout) + raw_response = request.data if self._logging: print 'Response: ' + raw_response @@ -477,10 +485,12 @@ class HpsRestGatewayService(object): _offset = None _search_fields = None _logging = False + _timeout = None - def __init__(self, config=None, enable_logging=False): + def __init__(self, config=None, enable_logging=False, timeout=None): self._config = config self._logging = enable_logging + self._timeout = timeout config.validate() self._url = config.service_uri() @@ -511,11 +521,20 @@ def do_request(self, verb, endpoint, data=None, additional_headers=None): if self._logging: print 'Request: ' + encoded_data - response = http.request(verb, url, headers=headers, body=encoded_data) + response = None + if self._timeout is None: + response = http.request(verb, url, headers=headers, body=encoded_data) + else: + response = http.request(verb, url, headers=headers, body=encoded_data, timeout=self._timeout) else: if self._logging: print 'Request: ' + url - response = http.request(verb, url, headers=headers) + + response = None + if self._timeout is None: + response = http.request(verb, url, headers=headers) + else: + response = http.request(verb, url, headers=headers, timeout=self._timeout) if self._logging: print 'Response: ' + response.data @@ -539,8 +558,8 @@ def hydrate_response(object_type, response): class HpsCreditService(HpsSoapGatewayService): _filter_by = None - def __init__(self, config=None, enable_logging=False): - HpsSoapGatewayService.__init__(self, config, enable_logging) + def __init__(self, config=None, enable_logging=False, timeout=None): + HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) def get(self, transaction_id): if transaction_id is None or transaction_id <= 0: @@ -1019,8 +1038,8 @@ def _process_charge_gateway_response(self, response, expected_type, *args): class HpsBatchService(HpsSoapGatewayService): - def __init__(self, config=None, enable_logging=False): - HpsSoapGatewayService.__init__(self, config, enable_logging) + def __init__(self, config=None, enable_logging=False, timeout=None): + HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) def close_batch(self): transaction = Et.Element('BatchClose') @@ -1050,8 +1069,8 @@ def close_batch(self): class HpsCheckService(HpsSoapGatewayService): - def __init__(self, config, enable_logging=False): - HpsSoapGatewayService.__init__(self, config, enable_logging) + def __init__(self, config, enable_logging=False, timeout=None): + HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) def override(self, check, amount, client_transaction_id=None): return self._build_transaction('OVERRIDE', check, amount, client_transaction_id) @@ -1152,8 +1171,8 @@ def _submit_transaction(self, transaction, client_transaction_id=None): class HpsGiftCardService(HpsSoapGatewayService): - def __init__(self, config=None, enable_logging=False): - HpsSoapGatewayService.__init__(self, config, enable_logging) + def __init__(self, config=None, enable_logging=False, timeout=None): + HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) def activate(self, amount, currency, gift_card): HpsInputValidation.check_amount(amount) @@ -1322,8 +1341,8 @@ def _submit_transaction(self, transaction): class HpsPayPlanService(HpsRestGatewayService): - def __init__(self, config=None, enable_logging=False): - HpsRestGatewayService.__init__(self, config, enable_logging) + def __init__(self, config=None, enable_logging=False, timeout=None): + HpsRestGatewayService.__init__(self, config, enable_logging, timeout) """ Customers """ @@ -1453,8 +1472,8 @@ def delete_schedule(self, schedule, force_delete=False): class HpsActivationService(HpsRestGatewayService): - def __init__(self, config=None, enable_logging=False): - HpsRestGatewayService.__init__(self, config, enable_logging) + def __init__(self, config=None, enable_logging=False, timeout=None): + HpsRestGatewayService.__init__(self, config, enable_logging, timeout) def device_activation_key(self, merchant_id, activation_code): args = { diff --git a/securesubmit/services/token.py b/securesubmit/services/token.py index 96042f8..7e25051 100644 --- a/securesubmit/services/token.py +++ b/securesubmit/services/token.py @@ -22,9 +22,11 @@ class HpsTokenService(object): _public_api_key = None _url = None + _timeout = None - def __init__(self, public_api_key): + def __init__(self, public_api_key, timeout=None): self._public_api_key = public_api_key + self._timeout = timeout if public_api_key is None or public_api_key == "": raise HpsArgumentException("publicAPIKey is None or Empty.") @@ -57,7 +59,11 @@ def _request_token(self, input_token): # headers=headers, # auth=(self._public_api_key, None)) - response = http.request('post', self._url, headers=headers, body=data) + response = None + if self._timeout is None: + response = http.request('post', self._url, headers=headers, body=data) + else: + response = http.request('post', self._url, headers=headers, body=data, timeout=self._timeout) token = HpsToken() if len(response.data) > 0: From 85ef1073d17c57cff43fbadd18fc814dd15d4927 Mon Sep 17 00:00:00 2001 From: "Jonathan C. Otsuka" Date: Wed, 1 Feb 2017 19:33:48 -0600 Subject: [PATCH 3/5] Remove duplicate enums added during merge of upstream. --- securesubmit/infrastructure/enums.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/securesubmit/infrastructure/enums.py b/securesubmit/infrastructure/enums.py index 834b154..d5f001e 100644 --- a/securesubmit/infrastructure/enums.py +++ b/securesubmit/infrastructure/enums.py @@ -162,8 +162,6 @@ class HpsPayPlanPaymentMethodStatus(Enum): REVOKED = 'Revoked' EXPIRED = 'Expired' LOST_STOLEN = 'Lost/Stolen' - ACTIVE = 'Active' - INACTIVE = 'Inactive' def __str__(self): return str(self.value) @@ -203,8 +201,6 @@ class HpsPayPlanScheduleStatus(Enum): ACTIVE = 'Active' INACTIVE = 'Inactive' FAILED = 'FAILED' - ACTIVE = 'Active' - INACTIVE = 'Inactive' def __str__(self): return str(self.value) From e1003b75c5648e061f79ebb81e4f6579b874734a Mon Sep 17 00:00:00 2001 From: "Jonathan C. Otsuka" Date: Wed, 1 Feb 2017 19:38:10 -0600 Subject: [PATCH 4/5] Use HpsServicesConfig timeout attribute instead of adding timeout directly to GatewayService objects. --- securesubmit/services/__init__.py | 1 + securesubmit/services/gateway.py | 50 +++++++++++-------------------- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/securesubmit/services/__init__.py b/securesubmit/services/__init__.py index 62ad79b..7f28780 100644 --- a/securesubmit/services/__init__.py +++ b/securesubmit/services/__init__.py @@ -97,6 +97,7 @@ class HpsServicesConfig(_HpsConfigInterface): password = None developer_id = None site_trace = None + timeout = None def __init__(self): self.UAT_URL = 'https://api-uat.heartlandportico.com/paymentserver.v1/PosGatewayService.asmx?wsdl' diff --git a/securesubmit/services/gateway.py b/securesubmit/services/gateway.py index 379b07b..d6ab1db 100644 --- a/securesubmit/services/gateway.py +++ b/securesubmit/services/gateway.py @@ -34,13 +34,11 @@ class HpsSoapGatewayService(object): _base_config = None _url = None _logging = False - _timeout = None - def __init__(self, config=None, enable_logging=False, timeout=None): + def __init__(self, config=None, enable_logging=False): self._base_config = HpsConfiguration() self._config = config self._logging = enable_logging - self._timeout = timeout self._url = self._base_config.soap_service_uri if self._config is not None: @@ -148,11 +146,7 @@ def do_transaction(self, transaction, client_transaction_id=None): request_headers = {'Content-type': 'text/xml; charset=UTF-8', 'Content-length': str(len(xml))} - request = None - if self._timeout is None: - request = http.request('POST', self._url, headers=request_headers, body=xml) - else: - request = http.request('POST', self._url, headers=request_headers, body=xml, timeout=self._timeout) + request = http.request('POST', self._url, headers=request_headers, body=xml, timeout=self._config.timeout) raw_response = request.data if self._logging: @@ -485,12 +479,10 @@ class HpsRestGatewayService(object): _offset = None _search_fields = None _logging = False - _timeout = None - def __init__(self, config=None, enable_logging=False, timeout=None): + def __init__(self, config=None, enable_logging=False): self._config = config self._logging = enable_logging - self._timeout = timeout config.validate() self._url = config.service_uri() @@ -521,20 +513,12 @@ def do_request(self, verb, endpoint, data=None, additional_headers=None): if self._logging: print 'Request: ' + encoded_data - response = None - if self._timeout is None: - response = http.request(verb, url, headers=headers, body=encoded_data) - else: - response = http.request(verb, url, headers=headers, body=encoded_data, timeout=self._timeout) + response = http.request(verb, url, headers=headers, body=encoded_data, timeout=self._config.timeout) else: if self._logging: print 'Request: ' + url - response = None - if self._timeout is None: - response = http.request(verb, url, headers=headers) - else: - response = http.request(verb, url, headers=headers, timeout=self._timeout) + response = http.request(verb, url, headers=headers, timeout=self._config.timeout) if self._logging: print 'Response: ' + response.data @@ -558,8 +542,8 @@ def hydrate_response(object_type, response): class HpsCreditService(HpsSoapGatewayService): _filter_by = None - def __init__(self, config=None, enable_logging=False, timeout=None): - HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) + def __init__(self, config=None, enable_logging=False): + HpsSoapGatewayService.__init__(self, config, enable_logging) def get(self, transaction_id): if transaction_id is None or transaction_id <= 0: @@ -1038,8 +1022,8 @@ def _process_charge_gateway_response(self, response, expected_type, *args): class HpsBatchService(HpsSoapGatewayService): - def __init__(self, config=None, enable_logging=False, timeout=None): - HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) + def __init__(self, config=None, enable_logging=False): + HpsSoapGatewayService.__init__(self, config, enable_logging) def close_batch(self): transaction = Et.Element('BatchClose') @@ -1069,8 +1053,8 @@ def close_batch(self): class HpsCheckService(HpsSoapGatewayService): - def __init__(self, config, enable_logging=False, timeout=None): - HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) + def __init__(self, config, enable_logging=False): + HpsSoapGatewayService.__init__(self, config, enable_logging) def override(self, check, amount, client_transaction_id=None): return self._build_transaction('OVERRIDE', check, amount, client_transaction_id) @@ -1171,8 +1155,8 @@ def _submit_transaction(self, transaction, client_transaction_id=None): class HpsGiftCardService(HpsSoapGatewayService): - def __init__(self, config=None, enable_logging=False, timeout=None): - HpsSoapGatewayService.__init__(self, config, enable_logging, timeout) + def __init__(self, config=None, enable_logging=False): + HpsSoapGatewayService.__init__(self, config, enable_logging) def activate(self, amount, currency, gift_card): HpsInputValidation.check_amount(amount) @@ -1341,8 +1325,8 @@ def _submit_transaction(self, transaction): class HpsPayPlanService(HpsRestGatewayService): - def __init__(self, config=None, enable_logging=False, timeout=None): - HpsRestGatewayService.__init__(self, config, enable_logging, timeout) + def __init__(self, config=None, enable_logging=False): + HpsRestGatewayService.__init__(self, config, enable_logging) """ Customers """ @@ -1472,8 +1456,8 @@ def delete_schedule(self, schedule, force_delete=False): class HpsActivationService(HpsRestGatewayService): - def __init__(self, config=None, enable_logging=False, timeout=None): - HpsRestGatewayService.__init__(self, config, enable_logging, timeout) + def __init__(self, config=None, enable_logging=False): + HpsRestGatewayService.__init__(self, config, enable_logging) def device_activation_key(self, merchant_id, activation_code): args = { From fecb35cd910ffb9ad9c18f0d99d839d55eb53d06 Mon Sep 17 00:00:00 2001 From: "Jonathan C. Otsuka" Date: Tue, 7 Feb 2017 17:07:15 -0600 Subject: [PATCH 5/5] Fix formatting to conform to PEP8 standards. --- securesubmit/infrastructure/enums.py | 1 + 1 file changed, 1 insertion(+) diff --git a/securesubmit/infrastructure/enums.py b/securesubmit/infrastructure/enums.py index d5f001e..f060d9b 100644 --- a/securesubmit/infrastructure/enums.py +++ b/securesubmit/infrastructure/enums.py @@ -155,6 +155,7 @@ class HpsPayPlanCustomerStatus(Enum): def __str__(self): return str(self.value) + class HpsPayPlanPaymentMethodStatus(Enum): ACTIVE = 'Active' INACTIVE = 'Inactive'