Skip to content

Instantly share code, notes, and snippets.

@cbsmith
Last active December 14, 2015 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbsmith/5069769 to your computer and use it in GitHub Desktop.
Save cbsmith/5069769 to your computer and use it in GitHub Desktop.
My rather lame approach to setting parameter storage class. It also demonstrates how to write an IPN handler with Flask, as that was my use case where form argument order mattered (most annoying). End result is uglier than just setting the storage class inside your function, but does make it easier to see when that special case is being used.
#demonstration of hack to declaratively set param_storage_class declaratively using a decorator
#also demonstrates how to write an IPN handler with Flask
from flask import Flask, make_response
from itertools import chain
app = Flask(__name__)
#Normally this parameter would come from a config
IPN_URLSTRING = 'https://www.sandbox.paypal.com/cgi-bin/webscr'
IPN_VERIFY_EXTRA_PARAMS = (('cmd', '_notify-validate'),)
def ordered_storage(f):
import werkzeug.datastructures
import flask
def decorator(*args, **kwargs):
flask.request.parameter_storage_class = werkzeug.datastructures.ImmutableOrderedMultiDict
return f(*args, **kwargs)
return decorator
@app.route('/webhooks/paypal', methods=['POST'])
@ordered_storage
def paypal_webhook():
#probably should have a sanity check here on the size of the form data to guard against DoS attacks
verify_args = chain(request.form.iteritems(), IPN_VERIFY_EXTRA_PARAMS)
verify_string = '&'.join(('%s=%s' % (param, value) for param, value in verify_args))
with closing(urlopen(IPN_URLSTRING, data=verify_string)) as paypal_verify_request:
response_string = paypal_verify_request.read()
if response_string != 'VERIFIED':
raise ValueError('Did not receive expected IPN confirmation from PayPal')
return make_response('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment