2013-10-10 14:24:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import openerp
|
|
|
|
from openerp.osv import osv, fields
|
|
|
|
from openerp.tools import float_repr
|
2013-10-17 11:00:46 +00:00
|
|
|
import urlparse
|
|
|
|
import requests
|
|
|
|
import logging
|
2013-10-10 14:24:15 +00:00
|
|
|
|
2013-10-17 11:00:46 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2013-10-10 14:24:15 +00:00
|
|
|
|
2013-10-16 15:08:37 +00:00
|
|
|
class type(osv.osv):
|
2013-10-10 14:24:15 +00:00
|
|
|
_name = 'payment.acquirer.type'
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Name', required=True),
|
|
|
|
}
|
2013-10-17 11:00:46 +00:00
|
|
|
def validate_payement(self, cr, uid, id, object, reference, currency, amount, context=None):
|
|
|
|
"""
|
|
|
|
return (payment, retry_time)
|
|
|
|
payment: "validated" or "refused" or "pending"
|
|
|
|
retry_time = False (don't retry validation) or int (seconds for retry validation)
|
|
|
|
"""
|
|
|
|
if isinstance(id, list):
|
|
|
|
id = id[0]
|
|
|
|
pay_type = self.browse(cr, uid, id, context=context)
|
|
|
|
method = getattr(self, '_validate_payement_%s' % pay_type.name)
|
|
|
|
return method(object, reference, currency, amount, context=context)
|
|
|
|
|
|
|
|
def _validate_payement_virement(self, object, reference, currency, amount, context=None):
|
|
|
|
return ("pending", False)
|
|
|
|
|
|
|
|
|
|
|
|
class type_paypal(osv.osv):
|
|
|
|
_inherit = "payment.acquirer.type"
|
|
|
|
|
|
|
|
def _validate_payement_paypal(self, object, reference, currency, amount, context=None):
|
|
|
|
parameters = {}
|
|
|
|
parameters.update(
|
|
|
|
cmd='_notify-validate',
|
|
|
|
business=object.company_id.paypal_account,
|
|
|
|
item_name="%s %s" % (object.company_id.name, reference),
|
|
|
|
item_number=reference,
|
|
|
|
amount=amount,
|
|
|
|
currency_code=currency.name
|
|
|
|
)
|
|
|
|
paypal_url = "https://www.paypal.com/cgi-bin/webscr"
|
|
|
|
paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr"
|
|
|
|
response = urlparse.parse_qsl(requests.post(paypal_url, data=parameters))
|
|
|
|
|
|
|
|
# transaction's unique id
|
|
|
|
# response["txn_id"]
|
|
|
|
|
|
|
|
if response["payment_status"] == "Voided":
|
|
|
|
raise "Paypal authorization has been voided."
|
|
|
|
elif response["payment_status"] in ("Completed", "Processed") and response["item_number"] == reference and response["mc_gross"] == amount:
|
|
|
|
return ("validated", False)
|
|
|
|
elif response["payment_status"] == "Expired":
|
|
|
|
_logger.warn("Paypal Validate Payement status: Expired")
|
|
|
|
return ("pending", 5)
|
|
|
|
elif response["payment_status"] == "Pending":
|
|
|
|
_logger.warn("Paypal Validate Payement status: Pending, reason: %s" % response["pending_reason"])
|
|
|
|
return ("pending", 5)
|
|
|
|
|
|
|
|
# Canceled_Reversal, Denied, Failed, Refunded, Reversed
|
|
|
|
return ("refused", False)
|
2013-10-10 14:24:15 +00:00
|
|
|
|
|
|
|
|
2013-10-16 15:08:37 +00:00
|
|
|
class acquirer(osv.osv):
|
2013-10-10 14:24:15 +00:00
|
|
|
_name = 'payment.acquirer'
|
|
|
|
_description = 'Online Payment Acquirer'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Name', required=True),
|
|
|
|
'type_id': fields.many2one('payment.acquirer.type', required=True),
|
2013-10-16 15:08:37 +00:00
|
|
|
'form_template_id': fields.many2one('ir.ui.view', required=True),
|
2013-10-10 14:24:15 +00:00
|
|
|
'visible': fields.boolean('Visible', help="Make this payment acquirer available (Customer invoices, etc.)"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
|
|
|
'visible': True,
|
|
|
|
}
|
|
|
|
|
2013-10-17 11:00:46 +00:00
|
|
|
def render(self, cr, uid, id, object, reference, currency, amount, cancel_url=None, return_url=None, context=None):
|
2013-10-10 14:24:15 +00:00
|
|
|
""" Renders the form template of the given acquirer as a qWeb template """
|
|
|
|
user = self.pool.get("res.users")
|
2013-10-17 11:00:46 +00:00
|
|
|
precision = self.pool.get("decimal.precision").precision_get(cr, openerp.SUPERUSER_ID, 'Account')
|
2013-10-10 14:24:15 +00:00
|
|
|
|
2013-10-16 15:08:37 +00:00
|
|
|
if not context:
|
|
|
|
context = {}
|
|
|
|
|
2013-10-10 14:24:15 +00:00
|
|
|
qweb_context = {}
|
|
|
|
qweb_context.update(
|
|
|
|
object=object,
|
|
|
|
reference=reference,
|
|
|
|
currency=currency,
|
|
|
|
amount=amount,
|
|
|
|
amount_str=float_repr(amount, precision),
|
|
|
|
user_id=user.browse(cr, uid, uid),
|
2013-10-17 11:00:46 +00:00
|
|
|
context=context,
|
|
|
|
cancel_url=cancel_url,
|
|
|
|
return_url=return_url
|
2013-10-10 14:24:15 +00:00
|
|
|
)
|
|
|
|
|
2013-10-17 11:00:46 +00:00
|
|
|
return self.browse(cr, uid, id, context=context) \
|
|
|
|
.form_template_id.render(qweb_context, engine='ir.qweb', context=context) \
|
|
|
|
.strip()
|
2013-10-10 14:24:15 +00:00
|
|
|
|
2013-10-17 11:00:46 +00:00
|
|
|
def validate_payement(self, cr, uid, id, object, reference, currency, amount, context=None):
|
|
|
|
"""
|
2013-10-10 15:54:47 +00:00
|
|
|
return (payment, retry_time)
|
2013-10-17 11:00:46 +00:00
|
|
|
payment: "validated" or "refused" or "pending"
|
|
|
|
retry_time = False (don't retry validation) or int (seconds for retry validation)
|
|
|
|
"""
|
|
|
|
if isinstance(id, list):
|
|
|
|
id = id[0]
|
|
|
|
type_id = self.browse(cr, uid, id, context=context).type_id
|
|
|
|
return type_id.validate_payement(object, reference, currency, amount, context=context)
|