Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FW] payment_mercadopago: add check payment wizard to payment provider
- Loading branch information
1 parent
be3aee4
commit 7a889d8
Showing
6 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | ||
payment_mercadopago.mercadopago_check_payment,mercadopago_check_payment,payment_mercadopago.model_payment_mercadopago_check_payment,base.group_system,1,1,1,1 | ||
payment_mercadopago.mercadopago_check_payment_line,mercadopago_check_payment_line,payment_mercadopago.model_payment_mercadopago_check_payment_line,base.group_system,1,1,1,1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import check_payments | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from odoo import fields, models | ||
import logging | ||
from odoo.addons.payment_mercadopago.models.mercadopago_request import MercadoPagoAPI | ||
from odoo.exceptions import UserError | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
class PaymentMercadopagoCheckPayment(models.TransientModel): | ||
_name = "payment.mercadopago.check_payment" | ||
_description = "Mercadopago check payments" | ||
|
||
provider_id = fields.Many2one('payment.provider', domain="[('code', '=', 'mercadopago')]", required=True) | ||
date_form = fields.Date(required=True, default = lambda self: fields.Date.today()) | ||
date_to = fields.Date(required=True, default = lambda self: fields.Date.today()) | ||
line_ids = fields.One2many('payment.mercadopago.check_payment.line', 'check_id', string='lines') | ||
confirmed = fields.Boolean(default=True) | ||
no_match = fields.Boolean() | ||
|
||
def check_status(self, mp_status, odoo_state): | ||
states = { | ||
'pending': 'pending', | ||
'approved': 'done', | ||
'authorized': 'authorized', | ||
'in_process': 'pending', | ||
'in_mediation': 'pending', | ||
'rejected': 'cancel', | ||
'cancelled': 'cancel', | ||
'refunded': 'done', | ||
'charged_back': 'done' | ||
} | ||
custom_state = states.get(mp_status) | ||
return odoo_state == custom_state | ||
|
||
def action_search_payments(self): | ||
MP = MercadoPagoAPI(self.provider_id) | ||
lines = [] | ||
self.line_ids = False | ||
payments = MP.mp.payment().search(filters={'begin_date':str(self.date_form) + "T00:00:00.000Z", 'end_date':str(self.date_to) + "T23:59:59.000Z", 'limit':1}) | ||
if 'error' in payments['response']: | ||
raise UserError(payments['response']['message']) | ||
offset = 0 | ||
total = payments['response']['paging']['total'] | ||
while offset <= total: | ||
filters={'begin_date':str(self.date_form) + "T00:00:00.000Z", 'end_date':str(self.date_to) + "T23:59:59.000Z", 'limit':100, 'offset':offset} | ||
_logger.info("Search in MP %s" % filters) | ||
payments = MP.mp.payment().search(filters=filters) | ||
offset += 100 | ||
external_reference = [payment['external_reference'] for payment in payments['response']['results']] | ||
tx_ids = self.env['payment.transaction'].search([('reference', 'in', external_reference)]) | ||
if 'results' in payments['response']: | ||
for payment in payments['response']['results']: | ||
if not self.confirmed or payment.get('status') in ['refunded','approved','authorized','charged_back']: | ||
transaction_id = tx_ids.filtered(lambda t: t.reference == payment['external_reference']) | ||
if transaction_id and self.no_match and self.check_status(payment.get('status'), transaction_id.state): | ||
continue | ||
lines.append((0, 0, { | ||
'mp_id': payment.get('id'), | ||
'mp_amount': payment.get('transaction_amount'), | ||
'mp_state': payment.get('status'), | ||
'mp_external_reference':payment.get('external_reference'), | ||
'mp_partner':payment.get('external_reference'), | ||
'transaction_id': transaction_id.id | ||
})) | ||
self.line_ids = lines | ||
return { | ||
'type': 'ir.actions.act_window', | ||
'res_model': 'payment.mercadopago.check_payment', | ||
'res_id': self.id, | ||
'target': 'new', | ||
'views': [(self.env.ref('payment_mercadopago.check_payments_view_form').id, 'form')], | ||
} | ||
|
||
|
||
class PaymentMercadopagoCheckPaymentLine(models.TransientModel): | ||
_name = "payment.mercadopago.check_payment.line" | ||
_description = "Mercadopago check payments" | ||
|
||
check_id = fields.Many2one('payment.mercadopago.check_payment') | ||
transaction_id = fields.Many2one('payment.transaction') | ||
transaction_state = fields.Selection('State', related='transaction_id.state') | ||
mp_amount = fields.Float() | ||
mp_id = fields.Char() | ||
mp_state = fields.Char() | ||
mp_external_reference = fields.Char() | ||
mp_partner = fields.Char() | ||
|
||
def get_tx_info_from_mercadopago(self): | ||
txt = [] | ||
for rec in self: | ||
if rec.check_id.provider_id.code != 'mercadopago': | ||
continue | ||
MP = MercadoPagoAPI(rec.check_id.provider_id) | ||
|
||
payments = MP.mp.payment().search(filters = {'external_reference': rec.mp_external_reference}) | ||
for payment in payments['response']['results']: | ||
txt += ['---------------------------'] | ||
txt += ["STATUS: %s" % payment['status']] | ||
txt += ["AMOUNT: %s" % payment['transaction_amount']] | ||
txt += ["description: %s" % payment['description']] | ||
txt += ['---------------------------'] | ||
txt += ['%s: %s' % (x, payment[x]) for x in payment] | ||
txt += ['---------------------------'] | ||
try: | ||
rec._mercadopago_s2s_validate_tree(payment) | ||
except: | ||
_logger.error('cant validate_tree') | ||
|
||
raise UserError("%s" % ' \n'.join(txt)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<odoo> | ||
|
||
<record id="check_payments_view_form" model="ir.ui.view"> | ||
<field name="name">check_payments.view.form</field> | ||
<field name="model">payment.mercadopago.check_payment</field> | ||
<field name="arch" type="xml"> | ||
<form string=""> | ||
<sheet> | ||
<h1><field name="provider_id"/></h1> | ||
<group> | ||
<group col="4"> | ||
<field name="confirmed"/> | ||
<field name="date_form"/> | ||
</group> | ||
<group col="4"> | ||
<field name="no_match"/> | ||
<field name="date_to"/> | ||
</group> | ||
</group> | ||
<field name="line_ids"> | ||
<tree create='false' editable='false'> | ||
<field name="transaction_id"/> | ||
<field name="mp_external_reference"/> | ||
<field name="mp_amount"/> | ||
<field name="mp_id"/> | ||
<field name="mp_state"/> | ||
<field name="transaction_state"/> | ||
<button string="Info" type="object" name="get_tx_info_from_mercadopago" /> | ||
</tree> | ||
</field> | ||
</sheet> | ||
<footer> | ||
<button string="Search" type="object" name="action_search_payments" class="oe_highlight"/> | ||
</footer> | ||
</form> | ||
</field> | ||
</record> | ||
<record id="check_payments_action" model="ir.actions.act_window"> | ||
<field name="name">Check payments</field> | ||
<field name="res_model">payment.mercadopago.check_payment</field> | ||
<field name="view_mode">form</field> | ||
<field name="context">{'default_provider_id':active_id}</field> | ||
<field name="target">new</field> | ||
</record> | ||
<record id="action_info_check_payments_mercadopago" model="ir.actions.server"> | ||
<field name="name">Check payments</field> | ||
<field name="model_id" ref="payment.model_payment_provider"/> | ||
<field name="state">code</field> | ||
<field name="code"> | ||
action = { | ||
'type': 'ir.actions.act_window', | ||
'res_model': 'payment.mercadopago.check_payment', | ||
'target': 'new', | ||
'views': [(env.ref('payment_mercadopago.check_payments_view_form').id, 'form')], | ||
} | ||
</field> | ||
<field name="binding_model_id" ref="payment.model_payment_provider"/> | ||
<field name="binding_type">action</field> | ||
</record> | ||
|
||
</odoo> |