Skip to content

Instantly share code, notes, and snippets.

@domasx2
Last active September 15, 2016 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domasx2/d700cc0564698783bd7a9bbc1153e67d to your computer and use it in GitHub Desktop.
Save domasx2/d700cc0564698783bd7a9bbc1153e67d to your computer and use it in GitHub Desktop.
icepay client in python
import json
import hashlib
import datetime
import requests
class IcepayClient:
BASE_API_URL = 'https://connect.icepay.com/webservice/api/v1/'
def __init__(self, merchant_id, secret_code):
self.merchant_id = merchant_id
self.secret_code = secret_code
def calc_checksum(self, url, method, body):
assert isinstance(body, str), 'body must be a string'
parts = [
url,
method,
self.merchant_id,
self.secret_code,
body
]
sig = ''.join([str(x) for x in parts])
m = hashlib.sha256()
m.update(sig.encode('utf8'))
return m.hexdigest()
def format_timestamp(self, time=None):
#returns timestamp formatted as per icepay specs. must be utc.
#returns now() if specific time not provided
time = time or datetime.datetime.utcnow()
return time.isoformat()[:19]
def call_api(self, method, endpoint, data={}):
assert method in ['POST', 'GET'], 'invalid method: %s, expected POST or GET' % method
if 'Timestamp' not in data:
data['Timestamp'] = self.format_timestamp()
full_url = self.BASE_API_URL + endpoint
data = json.dumps(data, separators=(',', ':'))
headers = {
"MerchantID": self.merchant_id,
"Checksum": self.calc_checksum(full_url, method, data)
}
if method == 'POST':
r = requests.post(full_url, headers=headers, data=data)
else:
r = requests.get(full_url, headers=headers, data=data)
if r.status_code == requests.codes.ok:
return r.json()
else:
print('body %s' % r.text)
print(r.headers)
r.raise_for_status()
def GetMyPaymentMethods(self):
return self.call_api('POST', 'payment/getmypaymentmethods')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment