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.

111 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. }
  39. if len(capital_release_request) > 0:
  40. values['partner_id'] = capital_release_request.partner_id.id
  41. values['release_capital_request'] = capital_release_request.id
  42. tx = request.env['payment.transaction'].sudo().create(values)
  43. request.session['website_payment_tx_id'] = tx.id
  44. return tx.id
  45. @http.route(['/website_payment/confirm'],
  46. type='http',
  47. auth='public', website=True)
  48. def confirm(self, **kw):
  49. tx_id = request.session.pop('website_payment_tx_id', False)
  50. if tx_id:
  51. tx = request.env['payment.transaction'].sudo().browse(tx_id)
  52. status = (tx.state == 'done' and 'success') or 'danger'
  53. 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.'
  54. return request.website.render('website_payment.confirm', {'tx': tx, 'status': status, 'message': message})
  55. else:
  56. return request.redirect('/my/home')
  57. @http.route(['/website_payment/pay'],
  58. type='http',
  59. auth='public', website=True)
  60. def pay(self, reference='', amount=False, currency_id=None,
  61. acquirer_id=None, **kw):
  62. env = request.env
  63. user = env.user.sudo()
  64. currency_id = currency_id and int(currency_id) or user.company_id.currency_id.id
  65. currency = env['res.currency'].browse(currency_id)
  66. # Try default one then fallback on first
  67. acquirer_id = acquirer_id and int(acquirer_id) or \
  68. env['ir.values'].get_default('payment.transaction', 'acquirer_id', company_id=user.company_id.id) or \
  69. env['payment.acquirer'].search([('website_published', '=', True), ('company_id', '=', user.company_id.id)])[0].id
  70. acquirer = env['payment.acquirer'].with_context(submit_class='btn btn-primary pull-right',
  71. submit_txt=_('Pay Now')).browse(acquirer_id)
  72. # auto-increment reference with a number suffix if the reference already exists
  73. reference = request.env['payment.transaction'].get_next_reference(reference)
  74. partner_id = user.partner_id.id if user.partner_id.id != request.website.partner_id.id else False
  75. capital_release_request = request.env['account.invoice'].sudo().search(
  76. [('release_capital_request', '=', True),
  77. ('number', '=', reference)]
  78. )
  79. if len(capital_release_request) > 0:
  80. partner_id = capital_release_request.partner_id.id
  81. payment_form = acquirer.sudo().render(reference, float(amount), currency.id, values={'return_url': '/website_payment/confirm', 'partner_id': partner_id})[0]
  82. values = {
  83. 'reference': reference,
  84. 'acquirer': acquirer,
  85. 'currency': currency,
  86. 'amount': float(amount),
  87. 'payment_form': payment_form,
  88. }
  89. return request.website.render('website_payment.pay', values)