def _make_call(self, params): response = requests.post(self.url, data=params) fields = parse_response(response.content) if fields['response_code'] != '1': e = AuthorizeResponseError('%s full_response=%r' % (fields['response_reason_text'], fields)) e.full_response = fields raise e return fields
def _make_call(self, params): params = urllib.urlencode(params) url = '{0}?{1}'.format(self.url, params) try: response = urllib.urlopen(url).read() except IOError as e: raise AuthorizeConnectionError(e) fields = parse_response(response) if fields['response_code'] != '1': e = AuthorizeResponseError(fields['response_reason_text']) e.full_response = fields raise e return fields
def create_saved_profile(self, internal_id, payments=None, email=None): """ Creates a user profile to which you can attach saved payments. Requires an internal_id to uniquely identify this user. If a list of saved payments is provided, as generated by create_saved_payment, these will be automatically added to the user profile. Returns the user profile id. """ createCustomerProfile = apicontractsv1.createCustomerProfileRequest() createCustomerProfile.merchantAuthentication = self.merchantAuth createCustomerProfile.profile = apicontractsv1.customerProfileType() createCustomerProfile.profile.merchantCustomerId = str(internal_id) createCustomerProfile.profile.email = email controller = createCustomerProfileController(createCustomerProfile) if not self.debug: controller.setenvironment(constants.PRODUCTION) controller.execute() response = controller.getresponse() if response.messages.resultCode == "Ok": customer_profile_id = response.customerProfileId else: if response.messages.message[0]['code'].text == 'E00039': customer_profile_id = filter(str.isdigit, response.messages.message[0]['text'].text) else: raise AuthorizeResponseError( "Failed to create customer profile: %s, debug mode: %s" % ( response.messages.message[0]['text'].text, self.debug)) customer_payment_profile_id = None if payments: createCustomerPaymentProfile = apicontractsv1.createCustomerPaymentProfileRequest() createCustomerPaymentProfile.merchantAuthentication = self.merchantAuth createCustomerPaymentProfile.paymentProfile = payments createCustomerPaymentProfile.customerProfileId = str(customer_profile_id) controller = createCustomerPaymentProfileController(createCustomerPaymentProfile) if not self.debug: controller.setenvironment(constants.PRODUCTION) controller.execute() response = controller.getresponse() if response.messages.resultCode == "Ok": customer_payment_profile_id = response.customerPaymentProfileId else: raise AuthorizeResponseError( "Failed to create customer payment profile: %s" % response.messages.message[0]['text'].text) return customer_profile_id, customer_payment_profile_id
def _make_call(self, service, *args): # Provides standard API call error handling method = getattr(self.client.service, service) try: response = method(self.client_auth, *args) except (WebFault, SSLError) as e: raise AuthorizeConnectionError('Error contacting SOAP API.') if response.resultCode != 'Ok': error = response.messages[0][0] e = AuthorizeResponseError('%s: %s' % (error.code, error.text)) e.full_response = { 'response_code': error.code, 'response_text': error.text, } raise e return response
def _make_call(self, service, *args): # Provides standard API call error handling method = getattr(self.client.service, service) try: response = method(self.client_auth, *args) except WebFault as e: raise AuthorizeConnectionError('Error contacting SOAP API.') if response.resultCode != 'Ok': error = response.messages[0][0] e = AuthorizeResponseError('%s: %s' % (error.code, error.text)) e.full_response = { 'response_code': error.code, 'response_text': error.text, } raise e return response
def _make_call(self, params): for key, val in params.iteritems(): params[key] = val.encode('utf8') params = urllib.urlencode(params) url = '{0}?{1}'.format(self.url, params) try: response = urllib.urlopen(url).read() except IOError as e: raise AuthorizeConnectionError(e) fields = parse_response(response) if fields['response_code'] != '1': e = AuthorizeResponseError('%s full_response=%r' % (fields['response_reason_text'], fields)) e.full_response = fields raise e return fields
def _make_call(self, params): params = convert_params_to_byte_str(params) params = urlencode(params) try: resource = urlopen(self.url, data=b(params)) response = resource.read().decode( get_content_charset(resource) or DEFAULT_CHARSET) except IOError as e: raise AuthorizeConnectionError(e) fields = parse_response(response) if fields['response_code'] != '1': e = AuthorizeResponseError('{0} full_response={1!r}'.format( fields['response_reason_text'], fields)) e.full_response = fields raise e return fields
def _make_call(self, params): params = convert_params_to_byte_str(params) params = urlencode(params) url = '{0}?{1}'.format(self.url, params) try: resource = urlopen(url) response = resource.read().decode( get_content_charset(resource) or DEFAULT_CHARSET) except IOError as e: raise AuthorizeConnectionError(e) fields = parse_response(response) if fields['response_code'] != '1': e = AuthorizeResponseError('%s full_response=%r' % (fields['response_reason_text'], fields)) e.full_response = fields raise e return fields
def _make_call(self, service, *args): # Provides standard API call error handling method = getattr(self.client.service, service) try: response = method(self.client_auth, *args) except (WebFault, SSLError) as e: raise AuthorizeConnectionError(e) if response.resultCode != 'Ok': error = response.messages[0][0] raise AuthorizeResponseError('%s: %s' % (error.code, error.text)) return response
def _make_call(self, call): """Make a call to the Authorize.net server with the XML.""" try: request = urllib2.Request(self.config.environment, E.tostring(call)) request.add_header('Content-Type', 'text/xml') response = urllib2.urlopen(request).read() response = E.fromstring(response) result = parse_response(response) except urllib2.HTTPError: raise AuthorizeConnectionError('Error processing XML request.') # Throw an exception for invalid calls. This makes error handling easier. if result.messages[0].result_code != 'Ok': error = result.messages[0].message e = AuthorizeResponseError('%s: %s' % (error.code, error.text)) e.full_response = result raise e # Exception handling for transaction response errors. try: error = result.transaction_response.errors[0] e = AuthorizeResponseError('Response code %s: %s' % (error.error_code, error.error_text)) e.full_response = result raise e except KeyError: pass return result
def _make_call(self, call): """Make a call to the Authorize.net server with the XML.""" try: request = urllib2.Request(self.config.environment, E.tostring(call)) request.add_header('Content-Type', 'text/xml') response = urllib2.urlopen(request).read() response = E.fromstring(response) response_json = parse_response(response) except urllib2.HTTPError: raise AuthorizeConnectionError('Error processing XML request.') # Exception handling for transaction response errors. try: error = response_json.transaction_response.errors[0] raise AuthorizeResponseError(error.error_code, error.error_text, response_json) except (KeyError, AttributeError): # Attempt to access transaction response errors pass # Throw an exception for invalid calls. This makes error handling easier. if response_json.messages[0].result_code != 'Ok': error = response_json.messages[0].message raise AuthorizeResponseError(error.code, error.text, response_json) return response_json
class AuthorizeAPI(object): def __init__(self, config): """Allow for multiple instances of the Authorize API.""" self.config = config self.customer = CustomerAPI(self) self.credit_card = CreditCardAPI(self) self.bank_account = BankAccountAPI(self) self.address = AddressAPI(self) self.recurring = RecurringAPI(self) self.batch = BatchAPI(self) self.transaction = TransactionAPI(self) @property def client_auth(self): """Generate an XML element with client auth data populated.""" if not hasattr(self, '_client_auth'): self._client_auth = E.Element('merchantAuthentication') E.SubElement(self._client_auth, 'name').text = self.config.login_id E.SubElement(self._client_auth, 'transactionKey').text = self.config.transaction_key return self._client_auth def _base_request(self, method): """Factory method for generating the base XML requests.""" request = E.Element(method) request.set('xmlns', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd') request.append(self.client_auth) return request def _make_call(self, call): """Make a call to the Authorize.net server with the XML.""" try: request = urllib2.Request(self.config.environment, E.tostring(call)) request.add_header('Content-Type', 'text/xml') response = urllib2.urlopen(request).read() response = E.fromstring(response) result = parse_response(response) except HTTPError, e: return AuthorizeConnectionError('Error processing XML request.') # Throw an exception for invalid calls. This makes error handling # easier. if result.messages.result_code != 'Ok': error = result.messages.message e = AuthorizeResponseError('%s: %s' % (error.code, error.text)) e.full_response = result raise e return result
def create_saved_payment(self, credit_card, address=None, profile_id=None): """ Creates a payment profile. """ creditCard = apicontractsv1.creditCardType() creditCard.cardNumber = credit_card.card_number creditCard.expirationDate = '{0.exp_year}-{0.exp_month:0>2}'.format(credit_card) creditCard.cardCode = credit_card.cvv payment = apicontractsv1.paymentType() payment.creditCard = creditCard billTo = apicontractsv1.customerAddressType() if credit_card.first_name: billTo.firstName = credit_card.first_name if credit_card.last_name: billTo.lastName = credit_card.last_name profile = apicontractsv1.customerPaymentProfileType() profile.payment = payment profile.billTo = billTo self._address_to_profile(address, profile) if profile_id: createCustomerPaymentProfile = apicontractsv1.createCustomerPaymentProfileRequest() createCustomerPaymentProfile.merchantAuthentication = self.merchantAuth createCustomerPaymentProfile.paymentProfile = profile createCustomerPaymentProfile.customerProfileId = str(profile_id) controller = createCustomerPaymentProfileController(createCustomerPaymentProfile) if not self.debug: controller.setenvironment(constants.PRODUCTION) controller.execute() response = controller.getresponse() if response.messages.resultCode == "Ok": return response.customerPaymentProfileId else: raise AuthorizeResponseError( "Failed to create customer payment profile %s" % response.messages.message[0]['text'].text) else: return profile