Python SDK Sample: Order Webhook

See: Creating a Simple Webhook

import cherrypy
import simplejson as json
# import pprint


class Webhook(object):

    @cherrypy.expose
    def index(self):
        content_length = cherrypy.request.headers['Content-Length']
        raw_body = cherrypy.request.body.read(int(content_length))
        wrapper = json.loads(raw_body)

        events = wrapper['events']

        # at this point, consume the order_updates as you see fit.
        # the json is an array of dicts, with the key being the type of transaction.
        # the value is a dict comprising the order properties.
        for event in events:
            cherrypy.log("examining record")

            order = event.get('order_create', None)
            if order is not None:
                cherrypy.log('order_create' + ": " + order['order_id'])

            order = event.get('order_delete', None)
            if order is not None:
                cherrypy.log('order_delete' + ": " + order['order_id'])

            order = event.get('order_payment_process', None)
            if order is not None:
                cherrypy.log('order_payment_process' + ": " + order['order_id'])

            order = event.get('order_refund', None)
            if order is not None:
                cherrypy.log('order_refund' + ": " + order['order_id'])

            order = event.get('order_reject', None)
            if order is not None:
                cherrypy.log('order_reject' + ": " + order['order_id'])

            order = event.get('order_ship', None)
            if order is not None:
                cherrypy.log('order_ship' + ": " + order['order_id'])

            order = event.get('order_stage_change', None)
            if order is not None:
                cherrypy.log('order_stage_change' + ": " + order['order_id'])

            order = event.get('order_update', None)
            if order is not None:
                cherrypy.log('order_update' + ": " + order['order_id'])

        # echo out the json if you need to see the structure.
        # pp = pprint.PrettyPrinter(indent=4)
        # return pp.pprint(wrapper)
        return "Success"


if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True
        }
    }
    cherrypy.quickstart(Webhook(), '/webhook', conf)