Advertisement
Guest User

Untitled

a guest
Dec 31st, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. import requests,json
  2.  
  3. import requests
  4. import urlparse
  5. from datetime import datetime, date
  6.  
  7.  
  8. class NMIException(Exception):
  9.     def __init__(self, error_code, error_text=None):
  10.         self._error_code = error_code
  11.  
  12.         if error_text:
  13.             self._error_text = error_text
  14.         else:
  15.             self._error_text = NMIClient.RESULT_CODES.get(error_code, u'Unrecognized result code')
  16.  
  17.     def __unicode__(self):
  18.         return u'%s - %s' % (self._error_code, self._error_text)
  19.  
  20.     def __str__(self):
  21.         return str(unicode(self))
  22.  
  23.  
  24. class NMIClient(object):
  25.     RESULT_CODES = {
  26.         u'100': u'Transaction was approved',
  27.         u'200': u'Transaction was declined by processor',
  28.         u'201': u'Do not honor',
  29.         u'202': u'Insufficient funds',
  30.         u'203': u'Over limit',
  31.         u'204': u'Transaction not allowed',
  32.         u'220': u'Incorrect payment information',
  33.         u'221': u'No such card issuer',
  34.         u'222': u'No card number on file with issuer',
  35.         u'223': u'Expired card',
  36.         u'224': u'Invalid expiration date',
  37.         u'225': u'Invalid card security code',
  38.         u'240': u'Call issuer for further information',
  39.         u'250': u'Pick up card',
  40.         u'251': u'Lost card',
  41.         u'252': u'Stolen card',
  42.         u'253': u'Fraudulent card',
  43.         u'260': u'Declined with further instructions available',
  44.         u'261': u'Declined-stop all recurring payments',
  45.         u'262': u'Declined-stop this recurring program',
  46.         u'263': u'Declined-update cardholder data available',
  47.         u'264': u'Declined-retry in a few days',
  48.         u'300': u'Transaction was rejected by gateway',
  49.         u'400': u'Transaction error returned by processor',
  50.         u'410': u'Invalid merchant configuration',
  51.         u'411': u'Merchant account is inactive',
  52.         u'420': u'Communication error',
  53.         u'421': u'Communication error with issuer',
  54.         u'430': u'Duplicate transaction at processor',
  55.         u'440': u'Processor format error',
  56.         u'441': u'Invalid transaction information',
  57.         u'460': u'Processor feature not available',
  58.         u'461': u'Unsupported card type'
  59.     }
  60.  
  61.     def __init__(self, username, password, service_url):
  62.         self._username = username
  63.         self._password = password
  64.         self._service_url = service_url
  65.  
  66.     def invoke_web_service(self, arguments):
  67.         arguments['username'] = self._username
  68.         arguments['password'] = self._password
  69.  
  70.         result = requests.post(self._service_url, arguments)
  71.  
  72.         self.last_response = result.text
  73.         self.last_result_code = ''
  74.         self.last_result_text = ''
  75.  
  76.         if result.status_code != 200:
  77.             raise NMIException('-1', 'Unexpected HTTP response code (%s)' % (result.status_code))
  78.  
  79.         result_data = urlparse.parse_qs(result.text)
  80.  
  81.         if result_data.get('response_code', []):
  82.             result_data['response_code'] = result_data['response_code'][0]
  83.  
  84.             for result_data_field_name in result_data.keys():
  85.                 if type(result_data[result_data_field_name]) == list:
  86.                     if len(result_data[result_data_field_name]) == 1:
  87.                         result_data[result_data_field_name] = result_data[result_data_field_name][0]
  88.  
  89.             self.last_result_code = result_data['response_code']
  90.             self.last_result_text = self.RESULT_CODES.get(result_data['response_code'],
  91.                                                           u'Unrecognized result code (%s)' % (
  92.                                                           result_data['response_code']))
  93.         else:
  94.             self.last_result_code = ''
  95.             self.last_result_text = u'Result code missing'
  96.  
  97.  
  98.         return result_data
  99.  
  100.     def add_vault(self, amount,credit_card, exp_month,exp_year,cvv,**kwargs):
  101.         arguments = {
  102.             'amount': amount,
  103.             'ccnumber': credit_card,
  104.             'ccexp': "{}{}".format(exp_month,exp_year),
  105.             'cvv': cvv,
  106.             'firstname': kwargs['first_name'],
  107.             'ipaddress':kwargs['ip_address'],
  108.             'lastname': kwargs['last_name'],
  109.             'phone':kwargs['phone'],
  110.             'customer_vault' : 'add_customer'}
  111.         shipping_details = False
  112.         for k, v in kwargs.iteritems():
  113.             if 'billing_'  in k:
  114.                 arguments[k.replace('billing_','')] = v
  115.             if 'shipping_' in k:
  116.                 arguments[k] = v
  117.                 shipping_details = True
  118.             else:
  119.                 if '_' in k:
  120.                     arguments[k.replace('_', '')] = v
  121.  
  122.         if shipping_details == True:
  123.             arguments['shipping_first_name'] = arguments['firstname']
  124.             arguments['shipping_last_name'] = arguments['lastname']
  125.         result = self.invoke_web_service(arguments)
  126.         return result
  127.  
  128.  
  129.  
  130.     def charge_vault_id(self, customer_vault_id, amount):
  131.         arguments = {
  132.             'format': 'json',
  133.             'amount': amount,
  134.             'customer_vault_id': customer_vault_id
  135.         }
  136.         resp = self.invoke_web_service(arguments)
  137.         return resp
  138.  
  139.  
  140.     def refund(self, transaction_id, amount=None):
  141.         arguments = {
  142.             'type': 'refund',
  143.             'transactionid': transaction_id
  144.         }
  145.  
  146.         if amount:
  147.             arguments['amount'] = amount
  148.  
  149.         return self.invoke_web_service(arguments)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement