2013-11-07 18:04:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-11-27 16:02:18 +00:00
|
|
|
try:
|
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
import json
|
2013-11-07 18:04:44 +00:00
|
|
|
import logging
|
2013-11-22 11:48:31 +00:00
|
|
|
import pprint
|
2013-12-03 14:25:01 +00:00
|
|
|
import urllib2
|
2014-01-24 14:59:23 +00:00
|
|
|
import werkzeug
|
2013-12-03 14:25:01 +00:00
|
|
|
|
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2013-11-07 18:04:44 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class PaypalController(http.Controller):
|
|
|
|
_notify_url = '/payment/paypal/ipn/'
|
|
|
|
_return_url = '/payment/paypal/dpn/'
|
|
|
|
_cancel_url = '/payment/paypal/cancel/'
|
|
|
|
|
2013-12-04 16:07:38 +00:00
|
|
|
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
|
|
|
|
|
2013-11-22 11:48:31 +00:00
|
|
|
def paypal_validate_data(self, **post):
|
|
|
|
""" Paypal IPN: three steps validation to ensure data correctness
|
2013-11-07 18:04:44 +00:00
|
|
|
|
2013-11-22 11:48:31 +00:00
|
|
|
- step 1: return an empty HTTP 200 response -> will be done at the end
|
|
|
|
by returning ''
|
|
|
|
- step 2: POST the complete, unaltered message back to Paypal (preceded
|
|
|
|
by cmd=_notify-validate), with same encoding
|
|
|
|
- step 3: paypal send either VERIFIED or INVALID (single word)
|
|
|
|
|
|
|
|
Once data is validated, process it. """
|
|
|
|
res = False
|
2013-12-03 14:25:01 +00:00
|
|
|
new_post = dict(post, cmd='_notify-validate')
|
2014-01-29 12:13:16 +00:00
|
|
|
urequest = urllib2.Request("https://www.sandbox.paypal.com/cgi-bin/webscr", werkzeug.url_encode(new_post))
|
2013-12-03 14:25:01 +00:00
|
|
|
uopen = urllib2.urlopen(urequest)
|
|
|
|
resp = uopen.read()
|
2013-12-04 16:07:38 +00:00
|
|
|
if resp == 'VERIFIED':
|
2013-11-22 11:48:31 +00:00
|
|
|
_logger.info('Paypal: validated data')
|
2013-11-07 18:04:44 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-11-22 11:48:31 +00:00
|
|
|
res = request.registry['payment.transaction'].form_feedback(cr, uid, post, 'paypal', context=context)
|
2013-12-04 16:07:38 +00:00
|
|
|
elif resp == 'INVALID':
|
2013-11-22 11:48:31 +00:00
|
|
|
_logger.warning('Paypal: answered INVALID on data verification')
|
2013-11-07 18:04:44 +00:00
|
|
|
else:
|
2013-11-22 11:48:31 +00:00
|
|
|
_logger.warning('Paypal: unrecognized paypal answer, received %s instead of VERIFIED or INVALID' % resp.text)
|
|
|
|
return res
|
2013-11-07 18:04:44 +00:00
|
|
|
|
2014-01-23 15:48:33 +00:00
|
|
|
@http.route('/payment/paypal/ipn/', type='http', auth='admin', methods=['POST'])
|
2013-11-22 11:48:31 +00:00
|
|
|
def paypal_ipn(self, **post):
|
|
|
|
""" Paypal IPN. """
|
|
|
|
_logger.info('Beginning Paypal IPN form_feedback with post data %s', pprint.pformat(post)) # debug
|
|
|
|
self.paypal_validate_data(**post)
|
2013-11-07 18:04:44 +00:00
|
|
|
return ''
|
|
|
|
|
2014-01-23 15:48:33 +00:00
|
|
|
@http.route('/payment/paypal/dpn', type='http', auth="admin", methods=['POST'])
|
2013-11-15 13:31:33 +00:00
|
|
|
def paypal_dpn(self, **post):
|
2013-11-22 11:48:31 +00:00
|
|
|
""" Paypal DPN """
|
|
|
|
_logger.info('Beginning Paypal DPN form_feedback with post data %s', pprint.pformat(post)) # debug
|
2013-12-04 16:07:38 +00:00
|
|
|
return_url = self._get_return_url(**post)
|
2013-11-22 11:48:31 +00:00
|
|
|
self.paypal_validate_data(**post)
|
2014-01-24 14:59:23 +00:00
|
|
|
return werkzeug.utils.redirect(return_url)
|
2013-11-15 13:31:33 +00:00
|
|
|
|
2014-01-23 15:48:33 +00:00
|
|
|
@http.route('/payment/paypal/cancel', type='http', auth="admin")
|
2013-11-15 13:31:33 +00:00
|
|
|
def paypal_cancel(self, **post):
|
2013-12-04 16:07:38 +00:00
|
|
|
""" When the user cancels its Paypal payment: GET on this route """
|
2013-11-07 18:04:44 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-12-04 16:07:38 +00:00
|
|
|
_logger.info('Beginning Paypal cancel with post data %s', pprint.pformat(post)) # debug
|
|
|
|
return_url = self._get_return_url(**post)
|
2014-01-24 14:59:23 +00:00
|
|
|
return werkzeug.utils.redirect(return_url)
|