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.

112 lines
5.3 KiB

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