Merge pull request #224 from odoo-dev/master-payment-post-tde

[IMP] website_sale: correctly submit the payment form using a post request, correctly create the tx asynchronously
This commit is contained in:
Thibault Delavallée 2014-05-27 12:09:01 +02:00
commit b5a7156f7d
6 changed files with 32 additions and 22 deletions

View File

@ -20,7 +20,7 @@
<input t-if="tx_values.get('merchantReturnData')" type='hidden' name='merchantReturnData'
t-att-value="tx_values.get('merchantReturnData')"/>
<!-- submit -->
<button type="image" name="submit" width="100px"
<button type="submit" width="100px"
t-att-class="submit_class">
<img t-if="not submit_txt" src="/payment_adyen/static/src/img/adyen_icon.png"/>
<span t-if="submit_txt"><t t-esc="submit_txt"/> <span class="fa fa-long-arrow-right"/></span>

View File

@ -41,7 +41,7 @@
<input type='hidden' name='EXCEPTIONURL' t-att-value='tx_values["EXCEPTIONURL"]'/>
<input type='hidden' name='CANCELURL' t-att-value='tx_values["CANCELURL"]'/>
<!-- submit -->
<button type="image" name="submit" width="100px"
<button type="submit" width="100px"
t-att-class="submit_class">
<img t-if="not submit_txt" src="/payment_ogone/static/src/img/ogone_icon.png"/>
<span t-if="submit_txt"><t t-esc="submit_txt"/> <span class="fa fa-long-arrow-right"/></span>

View File

@ -31,7 +31,7 @@
<input t-if="tx_values.get('cancel_return')" type="hidden" name="cancel_return"
t-att-value="tx_values.get('cancel_return')"/>
<!-- submit -->
<button type="image" name="submit" width="100px"
<button type="submit" width="100px"
t-att-class="submit_class">
<img t-if="not submit_txt" src="/payment_paypal/static/src/img/paypal_icon.png"/>
<span t-if="submit_txt"><t t-esc="submit_txt"/> <span class="fa fa-long-arrow-right"/></span>

View File

@ -11,7 +11,7 @@
<input type='hidden' name='amount' t-att-value='amount or "0.0"'/>
<input type='hidden' name='currency' t-att-value='currency.name'/>
<!-- submit -->
<button name="submit" width="100px"
<button type="submit" width="100px"
t-att-class="submit_class">
<img t-if="not submit_txt" src="/payment_transfer/static/src/img/transfer_icon.png"/>
<span t-if="submit_txt"><t t-esc="submit_txt"/> <span class="fa fa-long-arrow-right"/></span>

View File

@ -490,7 +490,6 @@ class website_sale(http.Controller):
values['acquirers'] = 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']:
render_ctx['tx_url'] = '/shop/payment/transaction/%s' % acquirer.id
acquirer.button = payment_obj.render(
cr, SUPERUSER_ID, acquirer.id,
order.name,
@ -504,20 +503,17 @@ class website_sale(http.Controller):
return request.website.render("website_sale.payment", values)
@http.route(['/shop/payment/transaction/<int:acquirer_id>'], type='http', methods=['POST'], auth="public", website=True)
def payment_transaction(self, acquirer_id, **post):
""" Hook method that creates a payment.transaction and redirect to the
acquirer, using post values to re-create the post action.
@http.route(['/shop/payment/transaction/<int:acquirer_id>'], type='json', auth="public", website=True)
def payment_transaction(self, acquirer_id):
""" Json method that creates a payment.transaction, used to create a
transaction when the user clicks on 'pay now' button. After having
created the transaction, the event continues and the user is redirected
to the acquirer website.
:param int acquirer_id: id of a payment.acquirer record. If not set the
user is redirected to the checkout page
:param dict post: should coutain all post data for the acquirer
"""
# @TDEFIXME: don't know why we received those data, but should not be send to the acquirer
post.pop('submit.x', None)
post.pop('submit.y', None)
cr, uid, context = request.cr, request.uid, request.context
payment_obj = request.registry.get('payment.acquirer')
transaction_obj = request.registry.get('payment.transaction')
sale_order_obj = request.registry['sale.order']
order = request.website.sale_get_order(context=context)
@ -529,7 +525,13 @@ class website_sale(http.Controller):
# find an already existing transaction
tx = request.website.sale_get_transaction()
if not tx:
if tx:
if tx.state == 'draft': # button cliked but no more info -> rewrite on tx or create a new one ?
tx.write({
'acquirer_id': acquirer_id,
})
tx_id = tx.id
else:
tx_id = transaction_obj.create(cr, SUPERUSER_ID, {
'acquirer_id': acquirer_id,
'type': 'form',
@ -541,10 +543,6 @@ class website_sale(http.Controller):
'sale_order_id': order.id,
}, context=context)
request.session['sale_transaction_id'] = tx_id
elif tx and tx.state == 'draft': # button cliked but no more info -> rewrite on tx or create a new one ?
tx.write({
'acquirer_id': acquirer_id,
})
# update quotation
sale_order_obj.write(
@ -555,9 +553,7 @@ class website_sale(http.Controller):
# confirm the quotation
sale_order_obj.action_button_confirm(cr, SUPERUSER_ID, [order.id], context=request.context)
acquirer_form_post_url = payment_obj.get_form_action_url(cr, uid, acquirer_id, context=context)
acquirer_total_url = '%s?%s' % (acquirer_form_post_url, werkzeug.url_encode(post))
return request.redirect(acquirer_total_url)
return tx_id
@http.route('/shop/payment/get_status/<int:sale_order_id>', type='json', auth="public", website=True)
def payment_get_status(self, sale_order_id, **post):

View File

@ -9,4 +9,18 @@ $(document).ready(function () {
})
.find("input[name='acquirer']:checked").click();
// When clicking on payment button: create the tx using json then continue to the acquirer
$('button[type="submit"]').on("click", function (ev) {
ev.preventDefault();
ev.stopPropagation();
var $form = $(ev.currentTarget).parents('form');
var acquirer_id = $(ev.currentTarget).parents('div.oe_sale_acquirer_button').first().data('id');
if (! acquirer_id) {
return false;
}
openerp.jsonRpc('/shop/payment/transaction/' + acquirer_id, 'call', {}).then(function (data) {
$form.submit();
});
});
});