[FIX] [IMP] paypal
[odoo/odoo.git] / addons / payment_acquirer_paypal / controllers / main.py
index 2fdb193..d5ec939 100644 (file)
@@ -21,6 +21,14 @@ class PaypalController(http.Controller):
     _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
 
@@ -36,11 +44,11 @@ class PaypalController(http.Controller):
         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)
@@ -48,7 +56,7 @@ class PaypalController(http.Controller):
 
     @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
@@ -57,14 +65,11 @@ class PaypalController(http.Controller):
 
     @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)
 
@@ -72,11 +77,8 @@ class PaypalController(http.Controller):
         '/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)