[IMP] payment_acquirer, payment_acquirer_transfer

- payment_acquirer: added base method for form-based validation
calling various model-dependant methods.

[FIX] payment_acquirer: fixed tx values not given to form to render
when having no custom method for a specific acquirer

- payment_acquirer_transfer: added form validation, using the new
acquirer method. Updated controllers. Updated generated form to include
tx details. Fixed method for form_action_url taht was missing.

bzr revid: tde@openerp.com-20131120140119-8zuvm2ypr5arvf5y
This commit is contained in:
Thibault Delavallée 2013-11-20 15:01:19 +01:00
parent 4fb0880259
commit 9bca2bbaac
4 changed files with 83 additions and 16 deletions

View File

@ -110,13 +110,14 @@ class PaymentAcquirer(osv.Model):
acquirer = self.browse(cr, uid, id, context=context)
method_name = '%s_form_generate_values' % (acquirer.name)
tx_values = {}
if tx_id and hasattr(self.pool['payment.transaction'], method_name):
method = getattr(self.pool['payment.transaction'], method_name)
tx_values = method(cr, uid, tx_id, tx_custom_values, context=context)
elif hasattr(self, method_name):
method = getattr(self, method_name)
tx_values = method(cr, uid, id, reference, amount, currency, partner_id, partner_values, tx_custom_values, context=context)
else:
tx_values = tx_custom_values
qweb_context = {
'acquirer': acquirer,
@ -259,6 +260,38 @@ class PaymentTransaction(osv.Model):
}
return {'values': values}
# --------------------------------------------------
# FORM RELATED METHODS
# --------------------------------------------------
def form_feedback(self, cr, uid, data, acquirer_name, context=None):
invalid_parameters, tx = None, None
tx_find_method_name = '_%s_form_get_tx_from_data' % acquirer_name
if hasattr(self, tx_find_method_name):
tx = getattr(self, tx_find_method_name)(cr, uid, data, context=context)
invalid_param_method_name = '_%s_form_get_invalid_parameters' % acquirer_name
if hasattr(self, invalid_param_method_name):
invalid_parameters = getattr(self, invalid_param_method_name)(cr, uid, tx, data, context=context)
if invalid_parameters:
_error_message = '%s: incorrect tx data:\n' % (acquirer_name)
for item in invalid_parameters:
_error_message += '\t%s: received %s instead of %s\n' % (item[0], item[1], item[2])
_logger.error(_error_message)
return False
feedback_method_name = '_%s_form_validate' % acquirer_name
if hasattr(self, feedback_method_name):
return getattr(self, feedback_method_name)(cr, uid, tx, data, context=context)
return True
# --------------------------------------------------
# SERVER2SERVER RELATED METHODS
# --------------------------------------------------
def create_s2s(self, cr, uid, tx_values, cc_values, context=None):
tx_id = self.create(cr, uid, tx_values, context=context)
return tx_id

View File

@ -16,7 +16,7 @@ class OgoneController(http.Controller):
'/payment/transfer/feedback',
], type='http', auth='admin')
def transfer_form_feedback(self, **post):
_logger.info('Transfer: entering form_feedback with post data %s', pprint.pformat(post)) # debug
_logger.info('entering form_feedback with post data %s', pprint.pformat(post)) # debug
cr, uid, context = request.cr, request.uid, request.context
request.registry['payment.transaction'].transfer_form_feedback(cr, uid, post, context)
request.registry['payment.transaction'].form_feedback(cr, uid, post, 'transfer', context)
return request.redirect(post.pop('return_url', '/'))

View File

@ -1,8 +1,11 @@
# -*- coding: utf-'8' "-*-"
from openerp.addons.payment_acquirer.models.payment_acquirer import ValidationError
from openerp.osv import osv
from openerp.tools.float_utils import float_compare
import logging
import pprint
_logger = logging.getLogger(__name__)
@ -10,9 +13,40 @@ _logger = logging.getLogger(__name__)
class TransferPaymentAcquirer(osv.Model):
_inherit = 'payment.acquirer'
def transfer_get_form_action_url(self, cr, uid, id, context=None):
return '/payment/transfer/feedback'
class TransferPaymentTransaction(osv.Model):
_inherit = 'payment.transaction'
def transfer_form_feedback(self, cr, uid, data, context=None):
return True
def _transfer_form_get_tx_from_data(self, cr, uid, data, context=None):
reference, amount, currency_name = data.get('reference'), data.get('amount'), data.get('currency_name')
tx_ids = self.search(
cr, uid, [
('reference', '=', reference),
], context=context)
if not tx_ids or len(tx_ids) > 1:
error_msg = 'received data for reference %s' % (pprint.pformat(reference))
if not tx_ids:
error_msg += '; no order found'
else:
error_msg += '; multiple order found'
_logger.error(error_msg)
raise ValidationError(error_msg)
return self.browse(cr, uid, tx_ids[0], context=context)
def _transfer_form_get_invalid_parameters(self, cr, uid, tx, data, context=None):
invalid_parameters = []
if float_compare(float(data.get('amount', '0.0')), tx.amount, 2) != 0:
invalid_parameters.append(('amount', data.get('amount'), '%.2f' % tx.amount))
if data.get('currency') != tx.currency_id.name:
invalid_parameters.append(('currency', data.get('currency'), tx.currency_id.name))
return invalid_parameters
def _transfer_form_validate(self, cr, uid, tx, data, context=None):
return tx.write({'state': 'pending'})

View File

@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<data noupdate="0">
<!-- templates -->
<template id="transfer_acquirer_button">
<div>
<div t-field="acquirer.message"/>
<form t-if="acquirer" action="/payment/transfer/feedback" method="post" target="_self">
<t t-if="tx_values.get('return_url')">
<input type='hidden' name='return_url' t-att-value='tx_values["return_url"]'/>
</t>
<input type="image" name="submit" id="payment_submit"
src="/payment_acquirer_ogone/static/src/img/ogone_logo_plain.gif"/>
<div t-field="acquirer.message"/>
<form t-if="acquirer" action="/payment/transfer/feedback" method="post" target="_self">
<t t-if="tx_values.get('return_url')">
<input type='hidden' name='return_url' t-att-value='tx_values["return_url"]'/>
</t>
<input type='hidden' name='reference' t-att-value='reference'/>
<input type='hidden' name='amount' t-att-value='amount'/>
<input type='hidden' name='currency' t-att-value='currency.name'/>
<input type="submit" name="submit" id="payment_submit"
class="btn btn-primary" value="Confirmer"/>
</form>
</div>
</template>
</data>