Skip to content

Commit

Permalink
- Added option to set LIVE or TEST API mode
Browse files Browse the repository at this point in the history
- Added examples for create order, update order, retreive order and retrive gateway(s)
- Changed code in gateways.py
  • Loading branch information
sliemen committed Jun 18, 2019
1 parent 4539134 commit 6692375
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,6 @@ multisafepay/__pycache__/__init__.cpython-37.pyc
.idea/modules.xml
.idea/misc.xml
.idea/encodings.xml
*.pyc
*.xml
*.iml
19 changes: 15 additions & 4 deletions multisafepay/client.py
Expand Up @@ -6,8 +6,9 @@


class Client:
def __init__(self, api_key=None):
self.api_url = 'https://testapi.multisafepay.com/v1/json'
def __init__(self, modus=None, api_key=None):
self.modus = modus
self.api_url = None
self.api_key = api_key
self.order = Orders(self)
self.paymentmethod = PaymentMethod
Expand All @@ -16,6 +17,15 @@ def __init__(self, api_key=None):
def set_api_key(self, api_key):
self.api_key = self.validate_api_key(api_key)

def set_modus(self, modus):
self.modus = modus
if self.modus is 'TEST':
self.api_url = 'https://testapi.multisafepay.com/v1/json'
elif self.modus is 'LIVE':
self.api_url = 'https://api.multisafepay.com/v1/json'
else:
raise ValueError('Invalid API mode, needs to be LIVE or TEST')

@staticmethod
def validate_api_key(api_key):
api_key = api_key.strip()
Expand All @@ -24,10 +34,11 @@ def validate_api_key(api_key):
"characters long".format(api_key=api_key))
return api_key

def execute_http_call(self, http_method, endpoint, data=None,**kwargs):
def execute_http_call(self, http_method, endpoint, data=None, **kwargs):
print(self.api_url)
response = requests.request(http_method,
url='{0}/{1}'.format(self.api_url, endpoint),
headers={'api_key':'{api_key}'.format(
api_key=self.api_key)}, json=data,**kwargs)
api_key=self.api_key)}, json=data, **kwargs)
json_data = json.loads(response.text)
return json_data
9 changes: 5 additions & 4 deletions app.py → multisafepay/examples/create_order.py
@@ -1,13 +1,15 @@
from multisafepay.client import Client

msp_client = Client()
#fill in TEST API key here
msp_client.set_api_key('')
# Here you can set the mode to TEST or LIVE based on the API you want to use
msp_client.set_modus('TEST')
msp_client.set_api_key('REPLACE WITH API KEY')

# The following code will create a iDEAL order
print(msp_client.order.create({
"type": "redirect",
"order_id": "my-order-id-1",
"gateway": msp_client.paymentmethod.EPS,
"gateway": msp_client.paymentmethod.IDEAL,
"currency": "EUR",
"amount": "1000",
"description": "Test Order Description",
Expand All @@ -21,4 +23,3 @@
}
}))

print(msp_client.gateways.gateways(country='NL',currency='100'))
8 changes: 8 additions & 0 deletions multisafepay/examples/get_order.py
@@ -0,0 +1,8 @@
from multisafepay.client import Client

msp_client = Client()
# Here you can set the mode to TEST or LIVE based on the API you want to use
msp_client.set_modus('TEST')
msp_client.set_api_key('REPLACE WITH API KEY')

print(msp_client.order.get(51))
13 changes: 13 additions & 0 deletions multisafepay/examples/retrieve_gateways.py
@@ -0,0 +1,13 @@
from multisafepay.client import Client

msp_client = Client()
# Here you can set the mode to TEST or LIVE based on the API you want to use
msp_client.set_modus('TEST')
msp_client.set_api_key('REPLACE WITH API KEY')

# To retrieve all the gateways you can use the following
print(msp_client.gateways.allgateways())
# To use filters use the following code
print(msp_client.gateways.allgateways('?country=BE&currency=EUR&amount=10000'))
# To retrieve information about one gateway use the following
print(msp_client.gateways.gateway('IDEAL'))
16 changes: 16 additions & 0 deletions multisafepay/examples/update_order.py
@@ -0,0 +1,16 @@
from multisafepay.client import Client

msp_client = Client()
# Here you can set the mode to TEST or LIVE based on the API you want to use
msp_client.set_modus('TEST')
msp_client.set_api_key('REPLACE WITH API KEY')

# to update a existing order use the following example
print(msp_client.order.update(49, {
"status":"shipped",
"tracktrace_code":"3SMSP0123456789",
"carrier":"MSP Logistics",
"ship_date":"01-01-1911",
"reason":"Fulfilled by warehouse",
"invoice_id":"AB12345"
}))
22 changes: 8 additions & 14 deletions multisafepay/resources/gateways.py
Expand Up @@ -5,18 +5,12 @@ def __init__(self, client):
self.method = 'GET'

def gateway(self, gatewayid):
self.endpoint = '{0}/{1}'.format(self.endpoint,gatewayid)
return self.msp_client.execute_http_call(self.method, self.endpoint)
endpoint = '{0}/{1}'.format(self.endpoint,gatewayid)
return self.msp_client.execute_http_call(self.method, endpoint)

def gateways(self, country=None, currency=None, amount=None, include=None):
if country or currency or amount or include:
self.endpoint = '{0}?'.format(self.endpoint)
if country:
self.endpoint = '{0}country={1}'.format(self.endpoint, country)
if currency:
self.endpoint = '{0}&currency={1}'.format(self.endpoint, currency)
if amount:
self.endpoint = '{0}&amount={1}'.format(self.endpoint, amount)
if include:
self.endpoint = '{0}&include={1}'.format(self.endpoint, include)
return self.msp_client.execute_http_call(self.method, self.endpoint)
def allgateways(self, *args):
if not args:
endpoint = self.endpoint
else:
endpoint = '{0}{1}'.format(self.endpoint, *args)
return self.msp_client.execute_http_call(self.method, endpoint)

0 comments on commit 6692375

Please sign in to comment.