Skip to content

Instantly share code, notes, and snippets.

@cruor99
Created May 8, 2016 16:19
Show Gist options
  • Select an option

  • Save cruor99/a2aca37e9a2b80b3ccc8492199fdf4a9 to your computer and use it in GitHub Desktop.

Select an option

Save cruor99/a2aca37e9a2b80b3ccc8492199fdf4a9 to your computer and use it in GitHub Desktop.
import requests
import json
from xml.dom import minidom
class PresendAPI():
# TESTURL requires its own certificates, located at certs/testcerts/
TESTURL = "https://pilot.presend.com:4442/cc/servlet/CC_XML?ACTION="
# LIVEURL requires its own certificates, located at certs/
LIVEURL = "https://live.presend.com:4442/cc/servlet/CC_XML?ACTION="
def __init__(self):
config = json.load(open("config.json"))
self.cacert = "certs/cacert.pem"
if config["deployment"] == "test":
self.presendurl = self.TESTURL
self.cert = ("certs/testcerts/cert.pem", "certs/testcerts/server.key")
if config["deployment"] == "live":
self.presendurl = self.LIVEURL
self.cert = ("certs/nordby.pem", "certs/nordbyserver.key")
def discover(self, cardnum):
xmlrequest = """\
<REQUEST>
<DISCOVER VER="1.0">
<CARDID>{}</CARDID>
</DISCOVER>
</REQUEST>
""".format(cardnum)
dom = minidom.parseString(xmlrequest)
print dom.toprettyxml()
r = requests.post(self.presendurl+"40004",
data=dom.toxml(),
headers={"Content-Type": "application/xml"},
cert=self.cert, verify=self.cacert
)
return r.content
def activate(self, cardnum, chargeamount):
xmlrequest = """\
<REQUEST>
<ACTIVATE VER="1.0">
<CARDID>{}</CARDID>
<AMOUNT>{}</AMOUNT>
<CURRENCYCODE>SEK</CURRENCYCODE>
</ACTIVATE>
</REQUEST>
""".format(cardnum, chargeamount)
dom = minidom.parseString(xmlrequest)
r = requests.post(self.presendurl+"40005",
data=dom.toxml(),
headers={"Content-Type": "application/xml"},
cert=self.cert, verify=self.cacert)
return r.content
if __name__ == "__main__":
presend = PresendAPI()
print presend.discover("0503400877850135")
print presend.activate("0500368553103520", "10000")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment