Skip to content

Instantly share code, notes, and snippets.

@sengupta
Last active December 17, 2022 03:34
Show Gist options
  • Save sengupta/8052859 to your computer and use it in GitHub Desktop.
Save sengupta/8052859 to your computer and use it in GitHub Desktop.
Sample Webhook Receiver to test Instamojo's (or any web service's) webhook feature.

Sample Webhook Receiver

You may use any of the following files (PHP or Python) to test the webhook notification system.

To use this, copy this file to the server where you're testing your webhook receiver. Change the output file name from /tmp/webhook_data.txt to any other file name that your webserver can write to and that you can read.

Now, copy the link to the script and paste it under the Webhook URL field of your Instamojo offer's edit page. Now, make a purchase of this offer. After the purchase is done, open the output text file. You should see the webhook data in this file.

You may modify the file according to your needs.

Copyright (c) 2014, Instamojo, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the Instamojo, Inc. nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<?php
$filename = "/tmp/webhook_data.txt";
foreach($_POST as $key => $value)
{
file_put_contents($filename, "$key: $value\n", FILE_APPEND);
}
file_put_contents($filename, "----------\n", FILE_APPEND);
?>
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import urlparse
PORT = 8000
FILENAME = '/tmp/webhook_data.txt'
class MojoHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['content-length'])
querystring = self.rfile.read(content_length)
data = urlparse.parse_qs(querystring)
with open(FILENAME, 'a') as file_pointer:
for key, value in data.iteritems():
file_pointer.write(': '.join([key, value[0]])+'\n')
file_pointer.write("----------\n")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
httpd = HTTPServer(('', PORT), MojoHandler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment