Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests,json
- import requests
- import urlparse
- from datetime import datetime, date
- class NMIException(Exception):
- def __init__(self, error_code, error_text=None):
- self._error_code = error_code
- if error_text:
- self._error_text = error_text
- else:
- self._error_text = NMIClient.RESULT_CODES.get(error_code, u'Unrecognized result code')
- def __unicode__(self):
- return u'%s - %s' % (self._error_code, self._error_text)
- def __str__(self):
- return str(unicode(self))
- class NMIClient(object):
- RESULT_CODES = {
- u'100': u'Transaction was approved',
- u'200': u'Transaction was declined by processor',
- u'201': u'Do not honor',
- u'202': u'Insufficient funds',
- u'203': u'Over limit',
- u'204': u'Transaction not allowed',
- u'220': u'Incorrect payment information',
- u'221': u'No such card issuer',
- u'222': u'No card number on file with issuer',
- u'223': u'Expired card',
- u'224': u'Invalid expiration date',
- u'225': u'Invalid card security code',
- u'240': u'Call issuer for further information',
- u'250': u'Pick up card',
- u'251': u'Lost card',
- u'252': u'Stolen card',
- u'253': u'Fraudulent card',
- u'260': u'Declined with further instructions available',
- u'261': u'Declined-stop all recurring payments',
- u'262': u'Declined-stop this recurring program',
- u'263': u'Declined-update cardholder data available',
- u'264': u'Declined-retry in a few days',
- u'300': u'Transaction was rejected by gateway',
- u'400': u'Transaction error returned by processor',
- u'410': u'Invalid merchant configuration',
- u'411': u'Merchant account is inactive',
- u'420': u'Communication error',
- u'421': u'Communication error with issuer',
- u'430': u'Duplicate transaction at processor',
- u'440': u'Processor format error',
- u'441': u'Invalid transaction information',
- u'460': u'Processor feature not available',
- u'461': u'Unsupported card type'
- }
- def __init__(self, username, password, service_url):
- self._username = username
- self._password = password
- self._service_url = service_url
- def invoke_web_service(self, arguments):
- arguments['username'] = self._username
- arguments['password'] = self._password
- result = requests.post(self._service_url, arguments)
- self.last_response = result.text
- self.last_result_code = ''
- self.last_result_text = ''
- if result.status_code != 200:
- raise NMIException('-1', 'Unexpected HTTP response code (%s)' % (result.status_code))
- result_data = urlparse.parse_qs(result.text)
- if result_data.get('response_code', []):
- result_data['response_code'] = result_data['response_code'][0]
- for result_data_field_name in result_data.keys():
- if type(result_data[result_data_field_name]) == list:
- if len(result_data[result_data_field_name]) == 1:
- result_data[result_data_field_name] = result_data[result_data_field_name][0]
- self.last_result_code = result_data['response_code']
- self.last_result_text = self.RESULT_CODES.get(result_data['response_code'],
- u'Unrecognized result code (%s)' % (
- result_data['response_code']))
- else:
- self.last_result_code = ''
- self.last_result_text = u'Result code missing'
- return result_data
- def add_vault(self, amount,credit_card, exp_month,exp_year,cvv,**kwargs):
- arguments = {
- 'amount': amount,
- 'ccnumber': credit_card,
- 'ccexp': "{}{}".format(exp_month,exp_year),
- 'cvv': cvv,
- 'firstname': kwargs['first_name'],
- 'ipaddress':kwargs['ip_address'],
- 'lastname': kwargs['last_name'],
- 'phone':kwargs['phone'],
- 'customer_vault' : 'add_customer'}
- shipping_details = False
- for k, v in kwargs.iteritems():
- if 'billing_' in k:
- arguments[k.replace('billing_','')] = v
- if 'shipping_' in k:
- arguments[k] = v
- shipping_details = True
- else:
- if '_' in k:
- arguments[k.replace('_', '')] = v
- if shipping_details == True:
- arguments['shipping_first_name'] = arguments['firstname']
- arguments['shipping_last_name'] = arguments['lastname']
- result = self.invoke_web_service(arguments)
- return result
- def charge_vault_id(self, customer_vault_id, amount):
- arguments = {
- 'format': 'json',
- 'amount': amount,
- 'customer_vault_id': customer_vault_id
- }
- resp = self.invoke_web_service(arguments)
- return resp
- def refund(self, transaction_id, amount=None):
- arguments = {
- 'type': 'refund',
- 'transactionid': transaction_id
- }
- if amount:
- arguments['amount'] = amount
- return self.invoke_web_service(arguments)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement