-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] add wizards check payment in MP
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
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 @@ | ||
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,83 @@ | ||
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" | ||
|
||
acquirer_id = fields.Many2one('payment.acquirer', domain="[('provider', '=', '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 action_search_payments(self): | ||
MP = MercadoPagoAPI(self.acquirer_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}) | ||
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']: | ||
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': tx_ids.filtered(lambda t: t.reference == payment['external_reference']).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') | ||
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.acquirer_id.provider != 'mercadopago': | ||
continue | ||
MP = MercadoPagoAPI(rec.check_id.acquirer_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 += ['---------------------------'] | ||
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,61 @@ | ||
<?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="acquirer_id"/></h1> | ||
<group col="4"> | ||
<field name="confirmed"/> | ||
<field name="no_match"/> | ||
<field name="date_form"/> | ||
<field name="date_to"/> | ||
</group> | ||
<group> | ||
<span> | ||
<field name="line_ids"> | ||
<tree> | ||
<field name="transaction_id"/> | ||
<field name="mp_external_reference"/> | ||
<field name="mp_amount"/> | ||
<field name="mp_id"/> | ||
<field name="mp_state"/> | ||
<button string="Info" type="object" name="get_tx_info_from_mercadopago" /> | ||
</tree> | ||
</field> | ||
</span> | ||
</group> | ||
</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">{}</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_acquirer"/> | ||
<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_acquirer"/> | ||
<field name="binding_type">action</field> | ||
</record> | ||
|
||
</odoo> |