[IMP] payment, website_sale, website_payment: unique reference on payment no longer prevent paying
Payment transactions referenceis have a unique constraint which was problematic when a payment was cancelled from the acquirer's page. To stick the to DRY principle, I factorized a method that checks for existing references and happens a numerical suffix if necessary. This was already implemented in website_payment but was moved to payment and used in website_sale.
This commit is contained in:
@@ -419,6 +419,14 @@ class PaymentTransaction(osv.Model):
|
||||
}}
|
||||
return {}
|
||||
|
||||
def get_next_reference(self, cr, uid, reference, context=None):
|
||||
ref_suffix = 1
|
||||
init_ref = reference
|
||||
while self.pool['payment.transaction'].search_count(cr, uid, [('reference', '=', reference)], context=context):
|
||||
reference = init_ref + '-' + str(ref_suffix)
|
||||
ref_suffix += 1
|
||||
return reference
|
||||
|
||||
# --------------------------------------------------
|
||||
# FORM RELATED METHODS
|
||||
# --------------------------------------------------
|
||||
|
||||
@@ -41,11 +41,7 @@ class website_payment(http.Controller):
|
||||
acquirer = env['payment.acquirer'].with_context(submit_class='btn btn-primary pull-right',
|
||||
submit_txt=_('Pay Now')).browse(acquirer_id)
|
||||
# auto-increment reference with a number suffix if the reference already exists
|
||||
ref_suffix = 1
|
||||
init_ref = reference
|
||||
while request.env['payment.transaction'].sudo().search_count([('reference', '=', reference)]):
|
||||
reference = init_ref + '-' + str(ref_suffix)
|
||||
ref_suffix += 1
|
||||
reference = request.env['payment.transaction'].get_next_reference(reference)
|
||||
|
||||
partner_id = user.partner_id.id if user.partner_id.id != request.website.partner_id.id else False
|
||||
|
||||
|
||||
@@ -727,18 +727,16 @@ class website_sale(http.Controller):
|
||||
values['errors'] = sale_order_obj._get_errors(cr, uid, order, context=context)
|
||||
values.update(sale_order_obj._get_website_data(cr, uid, order, context))
|
||||
|
||||
# fetch all registered payment means
|
||||
# if tx:
|
||||
# acquirer_ids = [tx.acquirer_id.id]
|
||||
# else:
|
||||
if not values['errors']:
|
||||
# find an already existing transaction
|
||||
tx = request.website.sale_get_transaction()
|
||||
acquirer_ids = payment_obj.search(cr, SUPERUSER_ID, [('website_published', '=', True), ('company_id', '=', order.company_id.id)], context=context)
|
||||
values['acquirers'] = list(payment_obj.browse(cr, uid, acquirer_ids, context=context))
|
||||
render_ctx = dict(context, submit_class='btn btn-primary', submit_txt=_('Pay Now'))
|
||||
for acquirer in values['acquirers']:
|
||||
acquirer.button = payment_obj.render(
|
||||
cr, SUPERUSER_ID, acquirer.id,
|
||||
order.name,
|
||||
tx and tx.reference or request.env['payment.transaction'].get_next_reference(order.name),
|
||||
order.amount_total,
|
||||
order.pricelist_id.currency_id.id,
|
||||
values={
|
||||
@@ -785,7 +783,7 @@ class website_sale(http.Controller):
|
||||
'currency_id': order.pricelist_id.currency_id.id,
|
||||
'partner_id': order.partner_id.id,
|
||||
'partner_country_id': order.partner_id.country_id.id,
|
||||
'reference': order.name,
|
||||
'reference': request.env['payment.transaction'].get_next_reference(order.name),
|
||||
'sale_order_id': order.id,
|
||||
}, context=context)
|
||||
request.session['sale_transaction_id'] = tx_id
|
||||
|
||||
Reference in New Issue
Block a user