Last active
December 14, 2015 10:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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