# coding=utf-8
#
# Example 13 - Handle an order status change using the Mollie API.
#
from __future__ import print_function

import os

import flask

from app import database_write
from mollie.api.client import Client
from mollie.api.error import Error


def main():
    try:
        #
        # Initialize the Mollie API library with your API key.
        #
        # See: https://www.mollie.com/dashboard/settings/profiles
        #
        api_key = os.environ.get('MOLLIE_API_KEY', 'test_test')
        mollie_client = Client()
        mollie_client.set_api_key(api_key)
        #
        # After your webhook has been called with the order ID in its body, you'd like
        # to handle the order's status change. This is how you can do that.
        #
        # See: https://docs.mollie.com/reference/v2/orders-api/get-order
        #
        if 'id' not in flask.request.form:
            flask.abort(404, 'Unknown order id')

        order_id = flask.request.form['id']
        order = mollie_client.orders.get(order_id)
        my_webshop_id = order.metadata['my_webshop_id']
        #
        # Update the order in the database.
        #
        data = {'order_id': order.id, 'status': order.status}
        database_write(my_webshop_id, data)

        if order.is_paid() or order.is_authorized():
            #
            # At this point you'd probably want to start the process of delivering the product to the customer.
            #
            return 'Paid'
        if order.is_canceled():
            #
            # Here you'd probably want to create a refund.
            #
            return 'Canceled'
        if order.is_completed():
            #
            # At this point you'd could inform the customer that all deliveries to the customer have started.
            #
            return 'Completed'

    except Error as err:
        return 'API call failed: {error}'.format(error=err)


if __name__ == '__main__':
    print(main())

@@ -1532,12 +1532,21 @@
   # 
-Here
+At this point
  you
@@ -1569,22 +1569,59 @@
  to 
-create a refun
+inform the customer that the order has been cancele
 d.%0A 
@@ -1741,18 +1741,16 @@
 oint you
-'d
  could i

improve example 13 comments