Skip to content

Commit

Permalink
Merge pull request #270 from mollie/new-api-update-examples
Browse files Browse the repository at this point in the history
Update all example code to match the new API
  • Loading branch information
geertjanvdenbosch authored Nov 8, 2022
2 parents 61a2f03 + 07faa77 commit ee6b603
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ ING Home'Pay and bank transfer payments. Other types of payments cannot be refun
payment = mollie_client.payments.get(payment.id)

# Refund € 2 of this payment
refund = mollie_client.refunds.on(payment).create({
refund = payment.refunds.create({
'amount': {
'currency': 'EUR',
'value': '2.00'
Expand All @@ -207,7 +207,7 @@ Run the oauth2 examples:
export MOLLIE_CLIENT_ID=your_client_id
export MOLLIE_CLIENT_SECRET=your_client_secret
export MOLLIE_PUBLIC_URL=https://some.ngrok.url.io
python examples/oauth/app.py
python examples/oauth/oauth_app.py
```

The Authorize endpoint is the endpoint on the Mollie web site where the merchant logs in, and grants authorization to your client application. E.g. when the merchant clicks on the Connect with Mollie button, you should redirect the merchant to the Authorize endpoint.
Expand Down
2 changes: 1 addition & 1 deletion examples/09-create-customer-payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def main():
#
# See: https://www.mollie.com/nl/docs/reference/customers/create-payment
#
payment = mollie_client.customer_payments.with_parent_id(customer.id).create(
payment = customer.payments.create(
{
"amount": {"currency": "EUR", "value": "100.00"},
"description": "My first API payment",
Expand Down
2 changes: 1 addition & 1 deletion examples/10-customer-payment-history.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def main():
params = {
"limit": amount_of_payments_to_retrieve,
}
payments = mollie_client.customer_payments.with_parent_id(customer.id).list(**params)
payments = customer.payments.list(**params)

body += f'<p>Showing the last {len(payments)} payments for customer "{customer.id}"</p>'

Expand Down
2 changes: 1 addition & 1 deletion examples/11-refund-payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main():
and float(payment.amount_remaining["value"]) >= 2.0
):
data = {"amount": {"value": "2.00", "currency": "EUR"}}
refund = mollie_client.payment_refunds.with_parent_id(payment_id).create(data)
refund = payment.refunds.create(data)
body += f'<p>{refund.amount["currency"]} {refund.amount["value"]} of payment {payment_id} refunded</p>'
else:
body += f"<p>Payment {payment_id} can not be refunded</p>"
Expand Down
6 changes: 5 additions & 1 deletion examples/15-list-orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def main():
body += "<tr>"
body += f"<td>{order.id}</td>"
body += f'<td>{order.billing_address["givenName"]} {order.billing_address["familyName"]}</td>'
body += f'<td>{order.shipping_address["givenName"]} {order.shipping_address["familyName"]}</td>'
body += str(order.shipping_address)
if order.shipping_address:
body += f'<td>{order.shipping_address["givenName"]} {order.shipping_address["familyName"]}</td>'
else:
body += "<td>No shipping address</td>"
body += f'<td>{order.amount["currency"]} {order.amount["value"]}</td>'
body += f'<td><a href="{order.checkout_url}" target="_blank">Pay order</a></td>'
body += f'<td><a href="/14-cancel-order?order_id={order.id}">Cancel order</a></td>'
Expand Down
7 changes: 3 additions & 4 deletions examples/16-cancel-order-line.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ def main():
#
body = "<p>Attempting to retrieve the first page of orders, and grabbing the first.</p>"
order = next(mollie_client.orders.list())
line = next(order.lines)
first_line = next(order.lines.list())

if line and line.is_cancelable:
data = {"lines": [{"id": line.id, "quantity": 1}]}
order.cancel_lines(data)
if first_line and first_line.is_cancelable:
order.lines.delete(first_line.id)

order = mollie_client.orders.get(order.id)
body += f"Your order {order.id} was updated:"
Expand Down
2 changes: 1 addition & 1 deletion examples/18-ship-order-completely.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def main():

order = mollie_client.orders.get(order_id) if order_id else next(mollie_client.orders.list())

shipment = order.create_shipment()
shipment = order.shipments.create()
body += f"A shipment with ID {shipment.id} has been created for your order with ID {order.id}"
for line in shipment.lines:
body += f"{line.name} Status: <b>{line.status}</b>"
Expand Down
2 changes: 1 addition & 1 deletion examples/19-ship-order-partially.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def main():

body = "<p>Attempting to retrieve the first page of orders and grabbing the first.</p>"
order = next(mollie_client.orders.list())
line = next(order.lines)
line = next(order.lines.list())
data = {
"lines": [
{
Expand Down
5 changes: 3 additions & 2 deletions examples/20-get-shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ def main():
body = "<p>Attempting to retrieve the first page of orders and grabbing the first.</p>"

order = next(mollie_client.orders.list())
if not len(order.shipments):
shipments = order.shipments.list()
if not len(shipments):
body += "<p>You have no shipments. You can create one from the examples.</p>"
return body

shipment = next(order.shipments)
shipment = next(shipments)
body += f"Shipment with ID {shipment.id} for order with ID {order.id}"

for line in shipment.lines:
Expand Down
2 changes: 1 addition & 1 deletion examples/21-list-order-shipments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main():
#
body = "<p>Attempting to retrieve the first page of orders, and grabbing the first.</p>"
order = next(mollie_client.orders.list())
shipments = order.shipments
shipments = order.shipments.list()

body += f"Shipments for order with ID {order.id}:"
body += """
Expand Down
2 changes: 1 addition & 1 deletion examples/22-refund-order-completely.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def main():
body = "<p>Attempting to retrieve the first page of orders, and grabbing the first.</p>"

order = next(mollie_client.orders.list())
refund = order.create_refund()
refund = order.refunds.create()

body += f"Refund {refund.id} was created for order {order.id}:"

Expand Down
19 changes: 11 additions & 8 deletions examples/23-update-shipment-tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ def main():
body = "<p>Attempting to retrieve the first page of orders, and grabbing the first.</p>"
order = next(mollie_client.orders.list())

if not len(order.shipments):
shipments = order.shipments.list()
if not len(shipments):
body += "<p>You have no shipments. You can create one from the examples.</p>"
return body

body = "<p>Attempting to retrieve the first page of shipments if your order, and grabbing the first.</p>"
shipment = next(order.shipments)

tracking = {
"carrier": "PostNL",
"code": "3SKABA000000000",
"url": "http://postnl.nl/tracktrace/?B=3SKABA000000000&P=1016EE&D=NL&T=C",
shipment = next(shipments)

data = {
"tracking": {
"carrier": "PostNL",
"code": "3SKABA000000000",
"url": "http://postnl.nl/tracktrace/?B=3SKABA000000000&P=1016EE&D=NL&T=C",
}
}
shipment = order.update_shipment(shipment.id, tracking)
shipment = order.shipments.update(shipment.id, data)

body += f"Shipment {shipment.id} for order {order.id}:"
body += "Tracking information updated:"
Expand Down
2 changes: 1 addition & 1 deletion examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_public_url():
try:
url = os.environ["MOLLIE_PUBLIC_URL"]
except KeyError:
url = flask.flask.request.url_root
url = flask.request.url_root

if not url.endswith("/"):
return f"{url}/"
Expand Down
2 changes: 1 addition & 1 deletion examples/oauth/02-onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main(client):

body += "<h1>Submit onboarding data</h1>"
data = {"profile": {"categoryCode": "6012"}}
onboarding = client.onboarding.create(resource_id="me", data=data)
onboarding = client.onboarding.create(data)
body += str(onboarding)

return body
Expand Down
8 changes: 4 additions & 4 deletions examples/oauth/05-profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def main(client):
# https://docs.mollie.com/reference/v2/profiles-api/get-profile

body += "<h1>Get profile</h1>"
response = client.profiles.get(profile_id)
body += str(response)
profile = client.profiles.get(profile_id)
body += str(profile)

# https://docs.mollie.com/reference/v2/profiles-api/update-profile

Expand All @@ -42,13 +42,13 @@ def main(client):

body += "<h1>Enable payment method</h1>"
profile = client.profiles.get(profile_id)
response = client.profile_methods.on(profile, "bancontact").create()
response = profile.methods.enable("bancontact")
body += str(response)

# https://docs.mollie.com/reference/v2/profiles-api/disable-method

body += "<h1>Disable payment method</h1>"
response = client.profile_methods.on(profile, "bancontact").delete()
response = profile.methods.disable("bancontact")
body += str(response)

# https://docs.mollie.com/reference/v2/profiles-api/list-profiles
Expand Down
7 changes: 3 additions & 4 deletions examples/oauth/06-settlements.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def main(client):
# https://docs.mollie.com/reference/v2/settlements-api/list-settlements

body += "<h1>List settlements</h1>"
response = client.settlements.list()
body += str(response)
settlements = client.settlements.list()
body += str(settlements)

settlement_id = next(response).id
settlement_id = next(settlements).id

# https://docs.mollie.com/reference/v2/settlements-api/get-settlement

Expand Down Expand Up @@ -52,7 +52,6 @@ def main(client):
# https://docs.mollie.com/reference/v2/settlements-api/list-settlement-captures

body += "<h1>List settlement captures</h1>"
settlement = client.settlements.get(settlement_id)
response = settlement.captures.list()
body += str(response)

Expand Down
7 changes: 4 additions & 3 deletions examples/oauth/app.py → examples/oauth/oauth_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def set_token(token):
@app.route("/")
def index():
"""
FLASK_APP=examples/oauth/app.py \
FLASK_APP=examples/oauth/oauth_app.py \
MOLLIE_CLIENT_ID=your_client_id \
MOLLIE_CLIENT_SECRET=your_client_secret \
MOLLIE_PUBLIC_URL=https://your_domain.tld \
Expand Down Expand Up @@ -91,6 +91,7 @@ def index():

if not authorized:
body = "<h1>Your applications config panel</h1>"
body += "You need to setup OAuth first:<br>"
body += f'<a href="{authorization_url}">{authorization_url}</a>'
return body
return redirect(url_for("examples_view"))
Expand All @@ -103,15 +104,15 @@ def callback(*args, **kwargs):
url = request.url.replace("http", "https") # This fakes httpS. DONT DO THIS!
client.setup_oauth_authorization_response(url)
body = "<h1>Oauth client is setup</h1>"
body += '<a href="/examples">Examples</a></p>'
body += '<a href="/examples/oauth">Examples</a></p>'
return body


@app.route("/examples")
def examples_view():
body = "<h1>Examples</h1><ul>"
for example in examples:
body += f'<li><a href="/{example}">{example}</a></li>'
body += f'<li><a href="{example}">{example}</a></li>'
body += "</ul>"
return body

Expand Down

0 comments on commit ee6b603

Please sign in to comment.