/
payment_provider.py
159 lines (130 loc) · 6.45 KB
/
payment_provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from locale import currency
from .mercadopago_request import MercadoPagoAPI
import logging
import urllib.parse as urlparse
import werkzeug
from odoo import _, api, fields, models
from odoo.addons.payment.models.payment_provider import ValidationError
from odoo.http import request
from ..controllers.main import MercadoPagoController
import pprint
_logger = logging.getLogger(__name__)
class PaymentProvider(models.Model):
_inherit = 'payment.provider'
code = fields.Selection(
selection_add=[('mercadopago', 'MercadoPago')], ondelete={'mercadopago': 'set default'})
mercadopago_publishable_key = fields.Char('MercadoPago Public Key')
mercadopago_access_token = fields.Char('MercadoPago Access Token')
mercadopago_test_publishable_key = fields.Char('MercadoPago test Public Key')
mercadopago_test_access_token = fields.Char('MercadoPago test Access Token')
is_validation = fields.Boolean()
# MercadoPago general item fields
mercadopago_item_id = fields.Char('Item ID', default='001')
mercadopago_item_title = fields.Char('Item Title', default='Website sale')
mercadopago_item_description = fields.Char('Item Description', default='Website sale item')
mercadopago_item_category = fields.Selection(
string='MercadoPago Category', help="The category",
selection=[
('art', "Collectibles & Art"),
('baby', "Toys for Baby, Stroller, Stroller Accessories, Car Safety Seats"),
('coupons', "Coupons"),
('donations', "Donations"),
('computing', "Computers & Tablets"),
('cameras', "Cameras & Photography"),
('video_games', "Video Games & Consoles"),
('television', "LCD, LED, Smart TV, Plasmas, TVs"),
('car_electronics', "Car Audio, Car Alarm Systems & Security, Car DVRs, Car Video Players, Car PC"),
('electronics', "Audio & Surveillance, Video & GPS, Others"),
('automotive', "Parts & Accessories"),
('entertainment', "Music, Movies & Series, Books, Magazines & Comics, Board Games & Toys"),
('fashion', "Men's, Women's, Kids & baby, Handbags & Accessories, Health & Beauty, Shoes, Jewelry & Watches"),
('games', "Online Games & Credits"),
('home', "Home appliances. Home & Garden"),
('musical', "Instruments & Gear"),
('phones', "Cell Phones & Accessories"),
('services', "General services"),
('learnings', "Trainings, Conferences, Workshops"),
('tickets', "Tickets for Concerts, Sports, Arts, Theater, Family, Excursions tickets, Events & more"),
('travels', "Plane tickets, Hotel vouchers, Travel vouchers"),
('virtual_goods', "E-books, Music Files, Software, Digital Images, PDF Files and any item which can be electronically stored in a file, Mobile Recharge, DTH Recharge and any Online Recharge"),
('others', "Other categories"),
],
default='others'
)
mercadopago_capture_method = fields.Selection([
('deferred_capture', 'Deferred capture is posible'),
('refund_payment', 'Always refund payment')
],
string='Capture method',
default='deferred_capture'
)
mercadopago_binary = fields.Boolean('Use binary mode', default=True)
def _get_mercadopago_request(self):
return MercadoPagoAPI(self)
def _get_mercadopago_publishable_key(self):
self.ensure_one()
if self.state == 'test':
return self.mercadopago_test_publishable_key
elif self.state == 'enabled':
return self.mercadopago_publishable_key
def _get_mercadopago_access_token(self):
self.ensure_one()
if self.state == 'test':
return self.mercadopago_test_access_token
elif self.state == 'enabled':
return self.mercadopago_access_token
@api.onchange('code')
def _onchange_code(self):
if self.code == 'mercadopago':
self.inline_form_view_id = self.env.ref('payment_mercadopago.inline_form').id
def action_create_mercadopago_test_user(self):
self.ensure_one()
mercadopago_API = self._get_mercadopago_request()
values = mercadopago_API.create_test_user()
msg = _("Mercadopago test user id: {id}, nickname: {nickname}, password: {password}, status: {site_status}, email: {email} ").format(**values)
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": msg,
"type": "success",
"sticky": True,
},
}
@api.model
def _get_compatible_providers(self, *args, currency_id=None, **kwargs):
""" Override of payment to unlist MercadoPago acquirers when the currency is not ARS. """
providers = super()._get_compatible_providers(*args, currency_id=currency_id, **kwargs)
# TODO: Deberíamos forzar la moneda a ARS ??
# currency = self.env['res.currency'].browse(currency_id).exists()
# if currency and currency.name != 'ARS':
# providers = providers.filtered(lambda a: a.provider != 'mercadopago')
return providers
def _should_build_inline_form(self, is_validation=False):
# if self.code != 'mercadopago':
# return super()._should_build_inline_form(self, is_validation=is_validation)
# TODO: modify for redirect integration
self.is_validation = is_validation
return True
def _get_validation_amount(self):
""" Override of payment to return the amount for MercadoPago validation operations.
:return: The validation amount
:rtype: float
"""
res = super()._get_validation_amount()
if self.code != 'mercadopago':
return res
usd_currency_id = self.env.ref('base.USD')
currency_id = self.journal_id.currency_id or self.journal_id.company_id.currency_id
amount = usd_currency_id._convert(1, currency_id, self.journal_id.company_id, fields.Date.today())
return amount
def _get_validation_currency(self):
""" Override of payment to return the currency for MercadoPago validation operations.
:return: The validation currency
:rtype: recordset of `res.currency`
"""
res = super()._get_validation_currency()
if self.code != 'mercadopago':
return res
# TODO: Deberíamos forzar la moneda a ARS ??
return res