You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
5.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # import datetime
  3. # import re
  4. import werkzeug
  5. import werkzeug.urls
  6. from openerp import http, SUPERUSER_ID
  7. from openerp.http import request
  8. from openerp.tools.translate import _
  9. from openerp.addons.easy_my_coop.controllers.main import WebsiteSubscription
  10. from openerp.addons.website_payment.controllers.main import website_payment
  11. class SubscriptionOnlinePayment(WebsiteSubscription):
  12. def fill_values(self, values, is_company, load_from_user=False):
  13. values = super(SubscriptionOnlinePayment,self).fill_values(values, is_company,load_from_user)
  14. fields_desc = request.env['subscription.request'].sudo().fields_get(['payment_type'])
  15. values['payment_types'] = fields_desc['payment_type']['selection']
  16. return values
  17. def get_subscription_response(self, values, kwargs):
  18. subscription = values.get('subscription_id', False)
  19. if kwargs.get('payment_type') == 'online':
  20. invoice = subscription.validate_subscription_request()[0]
  21. acquirer = request.env['payment.acquirer'].search([('website_published', '=', True)])[0]
  22. return website_payment().pay(reference=invoice.number, amount=invoice.residual, currency_id=invoice.currency_id.id, acquirer_id=acquirer.id)
  23. else:
  24. values = self.preRenderThanks(values, kwargs)
  25. return request.website.render(kwargs.get("view_callback", "easy_my_coop.cooperator_thanks"), values)
  26. return True
  27. class SubscriptionWebsitePayment(website_payment):
  28. @http.route(['/website_payment/transaction'], type='json', auth="public", website=True)
  29. def transaction(self, reference, amount, currency_id, acquirer_id):
  30. partner_id = request.env.user.partner_id.id if request.env.user.partner_id != request.website.partner_id else False
  31. capital_release_request = request.env['account.invoice'].sudo().search([('release_capital_request','=',True),('number','=',reference)])
  32. values = {
  33. 'acquirer_id': int(acquirer_id),
  34. 'reference': reference,
  35. 'amount': float(amount),
  36. 'currency_id': int(currency_id),
  37. 'partner_id': partner_id,
  38. 'release_capital_request' : capital_release_request.id,
  39. }
  40. if len(capital_release_request) > 0:
  41. values['partner_id'] = capital_release_request.partner_id.id
  42. values['release_capital_request'] = capital_release_request.id
  43. tx = request.env['payment.transaction'].sudo().create(values)
  44. request.session['website_payment_tx_id'] = tx.id
  45. return tx.id
  46. @http.route(['/website_payment/confirm'], type='http', auth='public', website=True)
  47. def confirm(self, **kw):
  48. tx_id = request.session.pop('website_payment_tx_id', False)
  49. if tx_id:
  50. tx = request.env['payment.transaction'].sudo().browse(tx_id)
  51. status = (tx.state == 'done' and 'success') or 'danger'
  52. message = (tx.state == 'done' and 'Your payment was successful! It may take some time to be validated on our end.') or 'OOps! There was a problem with your payment.'
  53. return request.website.render('website_payment.confirm', {'tx': tx, 'status': status, 'message': message})
  54. else:
  55. return request.redirect('/my/home')
  56. @http.route(['/website_payment/pay'], type='http', auth='public', website=True)
  57. def pay(self, reference='', amount=False, currency_id=None, acquirer_id=None, **kw):
  58. env = request.env
  59. user = env.user.sudo()
  60. currency_id = currency_id and int(currency_id) or user.company_id.currency_id.id
  61. currency = env['res.currency'].browse(currency_id)
  62. # Try default one then fallback on first
  63. acquirer_id = acquirer_id and int(acquirer_id) or \
  64. env['ir.values'].get_default('payment.transaction', 'acquirer_id', company_id=user.company_id.id) or \
  65. env['payment.acquirer'].search([('website_published', '=', True), ('company_id', '=', user.company_id.id)])[0].id
  66. acquirer = env['payment.acquirer'].with_context(submit_class='btn btn-primary pull-right',
  67. submit_txt=_('Pay Now')).browse(acquirer_id)
  68. # auto-increment reference with a number suffix if the reference already exists
  69. reference = request.env['payment.transaction'].get_next_reference(reference)
  70. partner_id = user.partner_id.id if user.partner_id.id != request.website.partner_id.id else False
  71. capital_release_request = request.env['account.invoice'].sudo().search([('release_capital_request','=',True),('number','=',reference)])
  72. if len(capital_release_request) > 0:
  73. partner_id = capital_release_request.partner_id.id
  74. payment_form = acquirer.sudo().render(reference, float(amount), currency.id, values={'return_url': '/website_payment/confirm', 'partner_id': partner_id})[0]
  75. values = {
  76. 'reference': reference,
  77. 'acquirer': acquirer,
  78. 'currency': currency,
  79. 'amount': float(amount),
  80. 'payment_form': payment_form,
  81. }
  82. return request.website.render('website_payment.pay', values)