Skip to content

Instantly share code, notes, and snippets.

@alexandervlpl
Created August 10, 2018 13:57
Show Gist options
  • Save alexandervlpl/4c6ec764403f8bcc39d873d1e1e75a32 to your computer and use it in GitHub Desktop.
Save alexandervlpl/4c6ec764403f8bcc39d873d1e1e75a32 to your computer and use it in GitHub Desktop.
Example: Python Mollie API v2 request (with multicurrency support)
import urllib2
import json
MOLLIE_API_KEY = "123"
# Construct a request:
payment_data = {
'amount': { 'currency': 'USD', 'value': '2.50'},
'description': 'something',
'redirectUrl': 'https://mysite.com/redirect',
'webhookUrl': 'https://mysite.com/webhook',
'metadata': {
'my_key': 'some value'
}
}
# Set opener, headers and method:
opener = urllib2.build_opener(urllib2.HTTPSHandler)
request = urllib2.Request("https://api.mollie.com/v2/payments")
request.add_header('Authorization', 'Bearer ' + MOLLIE_API_KEY)
request.add_header('Content-Type', 'application/json')
request.get_method = lambda: 'POST'
request.add_data(json.dumps(payment_data))
# Send request:
r = opener.open(request)
# Check response code and parse the JSON response:
assert r.code == 200
response = json.loads(r.read())
r.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment