From 07faa777109b631b33365a4d05cc248bee98a4cf Mon Sep 17 00:00:00 2001 From: Tom Hendrikx Date: Mon, 7 Nov 2022 17:52:28 +0100 Subject: [PATCH] Update all example code to match the new API --- README.md | 4 ++-- examples/09-create-customer-payment.py | 2 +- examples/10-customer-payment-history.py | 2 +- examples/11-refund-payment.py | 2 +- examples/15-list-orders.py | 6 +++++- examples/16-cancel-order-line.py | 7 +++---- examples/18-ship-order-completely.py | 2 +- examples/19-ship-order-partially.py | 2 +- examples/20-get-shipment.py | 5 +++-- examples/21-list-order-shipments.py | 2 +- examples/22-refund-order-completely.py | 2 +- examples/23-update-shipment-tracking.py | 19 +++++++++++-------- examples/app.py | 2 +- examples/oauth/02-onboarding.py | 2 +- examples/oauth/05-profiles.py | 8 ++++---- examples/oauth/06-settlements.py | 7 +++---- examples/oauth/{app.py => oauth_app.py} | 7 ++++--- 17 files changed, 44 insertions(+), 37 deletions(-) rename examples/oauth/{app.py => oauth_app.py} (92%) diff --git a/README.md b/README.md index 8388c273..55884507 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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. diff --git a/examples/09-create-customer-payment.py b/examples/09-create-customer-payment.py index e9479417..276a9fb0 100644 --- a/examples/09-create-customer-payment.py +++ b/examples/09-create-customer-payment.py @@ -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", diff --git a/examples/10-customer-payment-history.py b/examples/10-customer-payment-history.py index 2fb7a5db..645db58f 100644 --- a/examples/10-customer-payment-history.py +++ b/examples/10-customer-payment-history.py @@ -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'

Showing the last {len(payments)} payments for customer "{customer.id}"

' diff --git a/examples/11-refund-payment.py b/examples/11-refund-payment.py index 95120ea7..6c283f2b 100644 --- a/examples/11-refund-payment.py +++ b/examples/11-refund-payment.py @@ -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'

{refund.amount["currency"]} {refund.amount["value"]} of payment {payment_id} refunded

' else: body += f"

Payment {payment_id} can not be refunded

" diff --git a/examples/15-list-orders.py b/examples/15-list-orders.py index e8fad9b3..aa7f55e5 100644 --- a/examples/15-list-orders.py +++ b/examples/15-list-orders.py @@ -48,7 +48,11 @@ def main(): body += "" body += f"{order.id}" body += f'{order.billing_address["givenName"]} {order.billing_address["familyName"]}' - body += f'{order.shipping_address["givenName"]} {order.shipping_address["familyName"]}' + body += str(order.shipping_address) + if order.shipping_address: + body += f'{order.shipping_address["givenName"]} {order.shipping_address["familyName"]}' + else: + body += "No shipping address" body += f'{order.amount["currency"]} {order.amount["value"]}' body += f'Pay order' body += f'Cancel order' diff --git a/examples/16-cancel-order-line.py b/examples/16-cancel-order-line.py index 345aca2c..2e97859c 100644 --- a/examples/16-cancel-order-line.py +++ b/examples/16-cancel-order-line.py @@ -26,11 +26,10 @@ def main(): # body = "

Attempting to retrieve the first page of orders, and grabbing the first.

" 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:" diff --git a/examples/18-ship-order-completely.py b/examples/18-ship-order-completely.py index ff63ffb3..309dcf36 100644 --- a/examples/18-ship-order-completely.py +++ b/examples/18-ship-order-completely.py @@ -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: {line.status}" diff --git a/examples/19-ship-order-partially.py b/examples/19-ship-order-partially.py index 38791758..4f70395b 100644 --- a/examples/19-ship-order-partially.py +++ b/examples/19-ship-order-partially.py @@ -25,7 +25,7 @@ def main(): body = "

Attempting to retrieve the first page of orders and grabbing the first.

" order = next(mollie_client.orders.list()) - line = next(order.lines) + line = next(order.lines.list()) data = { "lines": [ { diff --git a/examples/20-get-shipment.py b/examples/20-get-shipment.py index 5d1b2caf..264ea0e8 100644 --- a/examples/20-get-shipment.py +++ b/examples/20-get-shipment.py @@ -25,11 +25,12 @@ def main(): body = "

Attempting to retrieve the first page of orders and grabbing the first.

" order = next(mollie_client.orders.list()) - if not len(order.shipments): + shipments = order.shipments.list() + if not len(shipments): body += "

You have no shipments. You can create one from the examples.

" 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: diff --git a/examples/21-list-order-shipments.py b/examples/21-list-order-shipments.py index 0ffaf97d..bc7c8c72 100644 --- a/examples/21-list-order-shipments.py +++ b/examples/21-list-order-shipments.py @@ -24,7 +24,7 @@ def main(): # body = "

Attempting to retrieve the first page of orders, and grabbing the first.

" order = next(mollie_client.orders.list()) - shipments = order.shipments + shipments = order.shipments.list() body += f"Shipments for order with ID {order.id}:" body += """ diff --git a/examples/22-refund-order-completely.py b/examples/22-refund-order-completely.py index 78a9a693..ea5a1e5d 100644 --- a/examples/22-refund-order-completely.py +++ b/examples/22-refund-order-completely.py @@ -25,7 +25,7 @@ def main(): body = "

Attempting to retrieve the first page of orders, and grabbing the first.

" 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}:" diff --git a/examples/23-update-shipment-tracking.py b/examples/23-update-shipment-tracking.py index e5375577..5b88053c 100644 --- a/examples/23-update-shipment-tracking.py +++ b/examples/23-update-shipment-tracking.py @@ -25,19 +25,22 @@ def main(): body = "

Attempting to retrieve the first page of orders, and grabbing the first.

" order = next(mollie_client.orders.list()) - if not len(order.shipments): + shipments = order.shipments.list() + if not len(shipments): body += "

You have no shipments. You can create one from the examples.

" return body body = "

Attempting to retrieve the first page of shipments if your order, and grabbing the first.

" - 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:" diff --git a/examples/app.py b/examples/app.py index 975b8695..af12e444 100644 --- a/examples/app.py +++ b/examples/app.py @@ -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}/" diff --git a/examples/oauth/02-onboarding.py b/examples/oauth/02-onboarding.py index 14622731..ef2fe62f 100644 --- a/examples/oauth/02-onboarding.py +++ b/examples/oauth/02-onboarding.py @@ -14,7 +14,7 @@ def main(client): body += "

Submit onboarding data

" data = {"profile": {"categoryCode": "6012"}} - onboarding = client.onboarding.create(resource_id="me", data=data) + onboarding = client.onboarding.create(data) body += str(onboarding) return body diff --git a/examples/oauth/05-profiles.py b/examples/oauth/05-profiles.py index 005dea1c..e7f22ae0 100644 --- a/examples/oauth/05-profiles.py +++ b/examples/oauth/05-profiles.py @@ -23,8 +23,8 @@ def main(client): # https://docs.mollie.com/reference/v2/profiles-api/get-profile body += "

Get profile

" - 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 @@ -42,13 +42,13 @@ def main(client): body += "

Enable payment method

" 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 += "

Disable payment method

" - 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 diff --git a/examples/oauth/06-settlements.py b/examples/oauth/06-settlements.py index 924387ef..b83eea60 100644 --- a/examples/oauth/06-settlements.py +++ b/examples/oauth/06-settlements.py @@ -8,10 +8,10 @@ def main(client): # https://docs.mollie.com/reference/v2/settlements-api/list-settlements body += "

List settlements

" - 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 @@ -52,7 +52,6 @@ def main(client): # https://docs.mollie.com/reference/v2/settlements-api/list-settlement-captures body += "

List settlement captures

" - settlement = client.settlements.get(settlement_id) response = settlement.captures.list() body += str(response) diff --git a/examples/oauth/app.py b/examples/oauth/oauth_app.py similarity index 92% rename from examples/oauth/app.py rename to examples/oauth/oauth_app.py index 506f9fec..33507da0 100644 --- a/examples/oauth/app.py +++ b/examples/oauth/oauth_app.py @@ -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 \ @@ -91,6 +91,7 @@ def index(): if not authorized: body = "

Your applications config panel

" + body += "You need to setup OAuth first:
" body += f'{authorization_url}' return body return redirect(url_for("examples_view")) @@ -103,7 +104,7 @@ def callback(*args, **kwargs): url = request.url.replace("http", "https") # This fakes httpS. DONT DO THIS! client.setup_oauth_authorization_response(url) body = "

Oauth client is setup

" - body += 'Examples

' + body += 'Examples

' return body @@ -111,7 +112,7 @@ def callback(*args, **kwargs): def examples_view(): body = "

Examples

" return body