Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for customers and their payments and update link to API …
…keys from old ‘beheer’ to new dashboard
- Loading branch information
1 parent
3662d1b
commit 55c74ac
Showing
10 changed files
with
278 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters