_return_url = '/payment/paypal/dpn/'
_cancel_url = '/payment/paypal/cancel/'
+ def _get_return_url(self, **post):
+ """ Extract the return URL from the data coming from paypal. """
+ return_url = post.pop('return_url', '')
+ if not return_url:
+ custom = json.loads(post.pop('custom', '{}'))
+ return_url = custom.get('return_url', '/')
+ return return_url
+
def paypal_validate_data(self, **post):
""" Paypal IPN: three steps validation to ensure data correctness
urequest = urllib2.Request("https://www.sandbox.paypal.com/cgi-bin/webscr", urllib.urlencode(new_post))
uopen = urllib2.urlopen(urequest)
resp = uopen.read()
- if resp.text == 'VERIFIED':
+ if resp == 'VERIFIED':
_logger.info('Paypal: validated data')
cr, uid, context = request.cr, request.uid, request.context
res = request.registry['payment.transaction'].form_feedback(cr, uid, post, 'paypal', context=context)
- elif resp.text == 'INVALID':
+ elif resp == 'INVALID':
_logger.warning('Paypal: answered INVALID on data verification')
else:
_logger.warning('Paypal: unrecognized paypal answer, received %s instead of VERIFIED or INVALID' % resp.text)
@website.route([
'/payment/paypal/ipn/',
- ], type='http', auth='public')
+ ], type='http', auth='public', methods=['POST'])
def paypal_ipn(self, **post):
""" Paypal IPN. """
_logger.info('Beginning Paypal IPN form_feedback with post data %s', pprint.pformat(post)) # debug
@website.route([
'/payment/paypal/dpn',
- ], type='http', auth="public")
+ ], type='http', auth="public", methods=['POST'])
def paypal_dpn(self, **post):
""" Paypal DPN """
_logger.info('Beginning Paypal DPN form_feedback with post data %s', pprint.pformat(post)) # debug
- return_url = post.pop('return_url', '')
- if not return_url:
- custom = json.loads(post.pop('custom', '{}'))
- return_url = custom.pop('return_url', '/')
+ return_url = self._get_return_url(**post)
self.paypal_validate_data(**post)
return request.redirect(return_url)
'/payment/paypal/cancel',
], type='http', auth="public")
def paypal_cancel(self, **post):
- """ TODO
- """
+ """ When the user cancels its Paypal payment: GET on this route """
cr, uid, context = request.cr, request.uid, request.context
- print 'Entering paypal_cancel with post', post
-
- return_url = post.pop('return_url', '/')
- print 'return_url', return_url
+ _logger.info('Beginning Paypal cancel with post data %s', pprint.pformat(post)) # debug
+ return_url = self._get_return_url(**post)
return request.redirect(return_url)