Skip to content

Commit

Permalink
Add examples for customers and their payments and update link to API …
Browse files Browse the repository at this point in the history
…keys from old ‘beheer’ to new dashboard
  • Loading branch information
Thijs-Riezebeek committed Jan 5, 2017
1 parent 3662d1b commit 55c74ac
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/1-new-payment.py
Expand Up @@ -21,7 +21,7 @@ def main():
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.nl/beheer/account/profielen/
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')
Expand Down
70 changes: 70 additions & 0 deletions examples/10-customer-payment-history.py
@@ -0,0 +1,70 @@
# coding=utf-8
#
# Example 10 - Retrieving the payment history for a customer
#
from __future__ import print_function

import sys, os, time, flask

#
# Add Mollie library to module path so we can import it.
# This is not necessary if you use pip or easy_install.
#
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../'))

import Mollie

def main():
try:
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')

body = ''

customer_id = flask.request.args.get('customer_id')

# If no customer ID was provided in the URL, we grab the first customer
if customer_id is None:
customers = mollie.customers.all()

body += '<p>No customer ID specified. Attempting to retrieve all customers and grabbing the first.</p>'

if int(customers['totalCount']) == 0:
body += '<p>You have no customers. You can create one from the examples.</p>'
return body

for customer in customers:
customer_id = customer['id']
break

customer = mollie.customers.get(customer_id)

amount_of_payments_to_retrieve = 20

#
# Retrieve the latest payments for the customer
#
# See: https://www.mollie.com/nl/docs/reference/customers/list-payments
#
payments = mollie.customer_payments.withParentId(customer_id).all(offset=0, count=amount_of_payments_to_retrieve)

body += '<p>Customer "%s" has %s payments</p>' % (customer['id'], payments['totalCount'])

if int(payments['totalCount']) > amount_of_payments_to_retrieve:
body += '<p><b>Note: Only showing first %s payments</b></p>' % amount_of_payments_to_retrieve

for payment in payments:
body += "<p>Payment %s (%s) EUR</p>" % (payment['id'], payment['amount'])

return body

except Mollie.API.Error as e:
return 'API call failed: ' + e.message

if __name__ == '__main__':
print(main())
2 changes: 1 addition & 1 deletion examples/2-webhook-verification.py
Expand Up @@ -20,7 +20,7 @@ def main():
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.lib.nl/beheer/account/profielen/
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')
Expand Down
2 changes: 1 addition & 1 deletion examples/4-ideal-payment.py
Expand Up @@ -21,7 +21,7 @@ def main ():
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.lib.nl/beheer/account/profielen/
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')
Expand Down
2 changes: 1 addition & 1 deletion examples/5-payments-history.py
Expand Up @@ -20,7 +20,7 @@ def main():
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.lib.nl/beheer/account/profielen/
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')
Expand Down
2 changes: 1 addition & 1 deletion examples/6-list-activated-methods.py
Expand Up @@ -20,7 +20,7 @@ def main():
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.nl/beheer/account/profielen/
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')
Expand Down
44 changes: 44 additions & 0 deletions examples/7-new-customer.py
@@ -0,0 +1,44 @@
# coding=utf-8
#
# Example 7 - How to create a new customer with the Mollie API.
#
from __future__ import print_function

import sys, os, flask

#
# Add Mollie library to module path so we can import it.
# This is not necessary if you use pip or easy_install.
#
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../'))

import Mollie


def main():
try:
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')

#
# See: https://www.mollie.com/nl/docs/reference/customers/create
#
customer = mollie.customers.create({
'name': 'Mr. First Customer',
'email': 'first.customer@example.com',
'locale': 'nl_NL'
})

return "Created new customer '%s' (%s)" % (customer['name'], customer['email'])

except Mollie.API.Error as e:
return 'API call failed: ' + e.message


if __name__ == '__main__':
print(main())
78 changes: 78 additions & 0 deletions examples/8-list-customers.py
@@ -0,0 +1,78 @@
# coding=utf-8
#
# Example 8 - Retrieving all of your customers with offset and count
#
from __future__ import print_function

import sys, os

#
# Add Mollie library to module path so we can import it.
# This is not necessary if you use pip or easy_install.
#
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../'))

import Mollie


def main():
try:
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')

amount_of_customers_to_retrieve = 20

#
# Get the latest 20 customers
#
# See: https://www.mollie.com/nl/docs/reference/customers/list
#
customers = mollie.customers.all(offset=0, count=amount_of_customers_to_retrieve)

body = '<p>Your API key has %u customers.</p>' % int(customers['totalCount'])

if int(customers['totalCount']) == 0:
return body

if int(customers['totalCount']) > amount_of_customers_to_retrieve:
body += '<p><b>Note: Only the first %s are shown here.</b></p>' % amount_of_customers_to_retrieve

body += """
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Payment creation</th>
<th>Payment History</th>
</tr>
</thead>
<tbody>
"""

for customer in customers:
body += '<tr>'
body += '<td>%s</td>' % customer['id']
body += '<td>%s</td>' % customer['name']
body += '<td>%s</td>' % customer['email']
body += '<td><a href="/9-create-customer-payment?customer_id=%s">Create payment for customer</a></td>' % \
customer['id']
body += '<td><a href="/10-customer-payment-history?customer_id=%s">Show payment history</a>' % \
customer['id']
body += '</tr>'

body += "</tbody></table>"

return body

except Mollie.API.Error as e:
return 'API call failed: ' + e.message

if __name__ == '__main__':
print(main())
76 changes: 76 additions & 0 deletions examples/9-create-customer-payment.py
@@ -0,0 +1,76 @@
# coding=utf-8
#
# Example 9 - Creating a payment for a customer
#
from __future__ import print_function

import sys, os, time, flask
from app import database_write

#
# Add Mollie library to module path so we can import it.
# This is not necessary if you use pip or easy_install.
#
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../'))

import Mollie


def main():
try:
#
# Initialize the Mollie API library with your API key.
#
# See: https://www.mollie.com/dashboard/settings/profiles
#
mollie = Mollie.API.Client()
mollie.setApiKey('test_bt7vvByF6jTcBR4dLuW66eNnHYNIJp')

body = ''

customer_id = flask.request.args.get('customer_id')

# If no customer ID was provided in the URL, we grab the first customer
if customer_id is None:
customers = mollie.customers.all()

body += '<p>No customer ID specified. Attempting to retrieve all customers and grabbing the first.</p>'

if int(customers['totalCount']) == 0:
body += '<p>You have no customers. You can create one from the examples.</p>'
return body

for customer in customers:
customer_id = customer['id']
break

customer = mollie.customers.get(customer_id)

#
# Generate a unique order number for this example. It is important to include this unique attribute
# in the redirectUrl (below) so a proper return page can be shown to the customer.
#
order_nr = int(time.time())

#
# See: https://www.mollie.com/nl/docs/reference/customers/create-payment
#
payment = mollie.customer_payments.withParentId(customer_id).create({
'amount': (time.time() % 15) * 3, # Create some variety in the payment amounts
'description': 'My first API payment',
'webhookUrl': flask.request.url_root + '2-webhook-verification?order_nr=' + str(order_nr),
'redirectUrl': flask.request.url_root + '3-return-page?order_nr=' + str(order_nr),
'metadata': {
'order_nr': order_nr
}
})

database_write(order_nr, payment['status'])

return '<p>Created payment of %s EUR for %s (%s)<p>' % (payment['amount'], customer['name'], customer['id'])

except Mollie.API.Error as e:
return 'API call failed: ' + e.message

if __name__ == '__main__':
print(main())
6 changes: 5 additions & 1 deletion examples/app.py
Expand Up @@ -8,7 +8,11 @@
'3-return-page',
'4-ideal-payment',
'5-payments-history',
'6-list-activated-methods'
'6-list-activated-methods',
'7-new-customer',
'8-list-customers',
'9-create-customer-payment',
'10-customer-payment-history'
]


Expand Down

0 comments on commit 55c74ac

Please sign in to comment.