[MERGE] forward port branch saas-6 up to f163f76
This commit is contained in:
@@ -21,18 +21,33 @@ class PaypalController(http.Controller):
|
|||||||
""" Extract the return URL from the data coming from paypal. """
|
""" Extract the return URL from the data coming from paypal. """
|
||||||
return_url = post.pop('return_url', '')
|
return_url = post.pop('return_url', '')
|
||||||
if not return_url:
|
if not return_url:
|
||||||
custom = json.loads(post.pop('custom', False) or '{}')
|
custom = json.loads(post.pop('custom', False) or post.pop('cm', False) or '{}')
|
||||||
return_url = custom.get('return_url', '/')
|
return_url = custom.get('return_url', '/')
|
||||||
return return_url
|
return return_url
|
||||||
|
|
||||||
|
def _parse_pdt_response(self, response):
|
||||||
|
""" Parse a text reponse for a PDT verification .
|
||||||
|
|
||||||
|
:param response str: text response, structured in the following way:
|
||||||
|
STATUS\nkey1=value1\nkey2=value2...\n
|
||||||
|
:rtype tuple(str, dict)
|
||||||
|
:return: tuple containing the STATUS str and the key/value pairs
|
||||||
|
parsed as a dict
|
||||||
|
"""
|
||||||
|
lines = filter(None, response.split('\n'))
|
||||||
|
status = lines.pop(0)
|
||||||
|
pdt_post = dict(line.split('=', 1) for line in lines)
|
||||||
|
return status, pdt_post
|
||||||
|
|
||||||
def paypal_validate_data(self, **post):
|
def paypal_validate_data(self, **post):
|
||||||
""" Paypal IPN: three steps validation to ensure data correctness
|
""" Paypal IPN: three steps validation to ensure data correctness
|
||||||
|
|
||||||
- step 1: return an empty HTTP 200 response -> will be done at the end
|
- step 1: return an empty HTTP 200 response -> will be done at the end
|
||||||
by returning ''
|
by returning ''
|
||||||
- step 2: POST the complete, unaltered message back to Paypal (preceded
|
- step 2: POST the complete, unaltered message back to Paypal (preceded
|
||||||
by cmd=_notify-validate), with same encoding
|
by cmd=_notify-validate or _notify-synch for PDT), with same encoding
|
||||||
- step 3: paypal send either VERIFIED or INVALID (single word)
|
- step 3: paypal send either VERIFIED or INVALID (single word) for IPN
|
||||||
|
or SUCCESS or FAIL (+ data) for PDT
|
||||||
|
|
||||||
Once data is validated, process it. """
|
Once data is validated, process it. """
|
||||||
res = False
|
res = False
|
||||||
@@ -44,18 +59,26 @@ class PaypalController(http.Controller):
|
|||||||
tx_ids = request.registry['payment.transaction'].search(cr, uid, [('reference', '=', reference)], context=context)
|
tx_ids = request.registry['payment.transaction'].search(cr, uid, [('reference', '=', reference)], context=context)
|
||||||
if tx_ids:
|
if tx_ids:
|
||||||
tx = request.registry['payment.transaction'].browse(cr, uid, tx_ids[0], context=context)
|
tx = request.registry['payment.transaction'].browse(cr, uid, tx_ids[0], context=context)
|
||||||
|
pdt_request = bool(new_post.get('amt')) # check for spefific pdt param
|
||||||
|
if pdt_request:
|
||||||
|
# this means we are in PDT instead of DPN like before
|
||||||
|
# fetch the PDT token
|
||||||
|
new_post['at'] = request.registry['ir.config_parameter'].get_param(cr, SUPERUSER_ID, 'payment_paypal.pdt_token')
|
||||||
|
new_post['cmd'] = '_notify-synch' # command is different in PDT than IPN/DPN
|
||||||
paypal_urls = request.registry['payment.acquirer']._get_paypal_urls(cr, uid, tx and tx.acquirer_id and tx.acquirer_id.environment or 'prod', context=context)
|
paypal_urls = request.registry['payment.acquirer']._get_paypal_urls(cr, uid, tx and tx.acquirer_id and tx.acquirer_id.environment or 'prod', context=context)
|
||||||
validate_url = paypal_urls['paypal_form_url']
|
validate_url = paypal_urls['paypal_form_url']
|
||||||
urequest = urllib2.Request(validate_url, werkzeug.url_encode(new_post))
|
urequest = urllib2.Request(validate_url, werkzeug.url_encode(new_post))
|
||||||
uopen = urllib2.urlopen(urequest)
|
uopen = urllib2.urlopen(urequest)
|
||||||
resp = uopen.read()
|
resp = uopen.read()
|
||||||
if resp == 'VERIFIED':
|
if pdt_request:
|
||||||
|
resp, post = self._parse_pdt_response(resp)
|
||||||
|
if resp == 'VERIFIED' or pdt_request and resp == 'SUCCESS':
|
||||||
_logger.info('Paypal: validated data')
|
_logger.info('Paypal: validated data')
|
||||||
res = request.registry['payment.transaction'].form_feedback(cr, SUPERUSER_ID, post, 'paypal', context=context)
|
res = request.registry['payment.transaction'].form_feedback(cr, SUPERUSER_ID, post, 'paypal', context=context)
|
||||||
elif resp == 'INVALID':
|
elif resp == 'INVALID' or pdt_request and resp == 'FAIL':
|
||||||
_logger.warning('Paypal: answered INVALID on data verification')
|
_logger.warning('Paypal: answered INVALID/FAIL on data verification')
|
||||||
else:
|
else:
|
||||||
_logger.warning('Paypal: unrecognized paypal answer, received %s instead of VERIFIED or INVALID' % resp.text)
|
_logger.warning('Paypal: unrecognized paypal answer, received %s instead of VERIFIED/SUCCESS or INVALID/FAIL (validation: %s)' % (resp, 'PDT' if pdt_request else 'IPN/DPN'))
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@http.route('/payment/paypal/ipn/', type='http', auth='none', methods=['POST'], csrf=False)
|
@http.route('/payment/paypal/ipn/', type='http', auth='none', methods=['POST'], csrf=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user