| 1 |
from odoo import fields, models, api, _ |
| 2 |
from odoo.exceptions import ValidationError |
| 3 |
from odoo.addons.payment_mollie import const |
| 4 |
|
| 5 |
from odoo.tools import hash_sign |
| 6 |
|
| 7 |
|
| 8 |
class PosPaymentMethod(models.Model): |
| 9 |
_inherit = "pos.payment.method" |
| 10 |
|
| 11 |
def _get_payment_terminal_selection(self): |
| 12 |
return super()._get_payment_terminal_selection() + [("mollie", "Mollie")] |
| 13 |
|
| 14 |
mollie_terminal_id = fields.Char("Mollie Terminal ID", copy=False) |
| 15 |
mollie_payment_provider_id = fields.Many2one("payment.provider", domain=[("code", "=", "mollie")]) |
| 16 |
|
| 17 |
@api.constrains('mollie_payment_provider_id') |
| 18 |
def _check_mollie_payment_provider_id(self): |
| 19 |
for payment_method in self: |
| 20 |
if not payment_method.mollie_payment_provider_id: |
| 21 |
continue |
| 22 |
if not payment_method.mollie_payment_provider_id.mollie_api_key: |
| 23 |
raise ValidationError(_( |
| 24 |
'Please set the Mollie API Key field on the %s payment provider.', |
| 25 |
payment_method.mollie_payment_provider_id.name |
| 26 |
)) |
| 27 |
|
| 28 |
def mollie_create_payment(self, amount: float, payment_uuid: str, pos_session_id: int): |
| 29 |
self.ensure_one() |
| 30 |
|
| 31 |
user_lang = self.env.context.get("lang") |
| 32 |
currency = self.journal_id.currency_id or self.company_id.currency_id |
| 33 |
payload = { |
| 34 |
"payment_uuid": payment_uuid, |
| 35 |
"payment_method_id": self.id, |
| 36 |
"pos_session_id": pos_session_id, |
| 37 |
} |
| 38 |
signed_payload = hash_sign(self.sudo().env, "pos_mollie", payload, expiration_hours=27) # Mollie webhooks can retry for up to 26 hours |
| 39 |
payment_request = { |
| 40 |
"amount": { |
| 41 |
"currency": currency.name, |
| 42 |
"value": f"{amount:.{currency.decimal_places}f}" |
| 43 |
}, |
| 44 |
"locale": user_lang if user_lang in const.SUPPORTED_LOCALES else "en_US", |
| 45 |
"description": f"pos_session_id={pos_session_id},payment_uuid={payment_uuid}", |
| 46 |
"redirectUrl": f"{self.get_base_url()}", # Not used for POS payments but required by Mollie API |
| 47 |
"webhookUrl": f"{self.get_base_url()}/pos_mollie/webhook?payload={signed_payload}", |
| 48 |
"method": "pointofsale", |
| 49 |
"terminalId": self.mollie_terminal_id |
| 50 |
} |
| 51 |
return self.mollie_payment_provider_id._send_api_request("POST", "/payments", json=payment_request) |
| 52 |
|
| 53 |
def mollie_create_refund(self, original_payment_id: str, amount: float, payment_uuid: str, pos_session_id: int): |
| 54 |
self.ensure_one() |
| 55 |
|
| 56 |
currency = self.journal_id.currency_id or self.company_id.currency_id |
| 57 |
payment_request = { |
| 58 |
"amount": { |
| 59 |
"currency": currency.name, |
| 60 |
"value": f"{amount:.{currency.decimal_places}f}" |
| 61 |
}, |
| 62 |
"description": f"pos_session_id={pos_session_id},payment_uuid={payment_uuid}", |
| 63 |
} |
| 64 |
return self.mollie_payment_provider_id._send_api_request("POST", f"/payments/{original_payment_id}/refunds", json=payment_request) |
| 65 |
|
| 66 |
def mollie_cancel_payment(self, payment_id: str): |
| 67 |
self.ensure_one() |
| 68 |
return self.mollie_payment_provider_id._send_api_request("DELETE", f"/payments/{payment_id}") |
| 69 |
|
| 70 |
def _mollie_get_payment(self, payment_id: str): |
| 71 |
self.ensure_one() |
| 72 |
return self.mollie_payment_provider_id._send_api_request("GET", f"/payments/{payment_id}") |