File: payment_provider.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (69 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download
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
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import hashlib
import logging

from odoo import fields, models

from odoo.addons.payment_aps import const


_logger = logging.getLogger(__name__)


class PaymentProvider(models.Model):
    _inherit = 'payment.provider'

    code = fields.Selection(
        selection_add=[('aps', "Amazon Payment Services")], ondelete={'aps': 'set default'}
    )
    aps_merchant_identifier = fields.Char(
        string="APS Merchant Identifier",
        help="The code of the merchant account to use with this provider.",
        required_if_provider='aps',
    )
    aps_access_code = fields.Char(
        string="APS Access Code",
        help="The access code associated with the merchant account.",
        required_if_provider='aps',
        groups='base.group_system',
    )
    aps_sha_request = fields.Char(
        string="APS SHA Request Phrase",
        required_if_provider='aps',
        groups='base.group_system',
    )
    aps_sha_response = fields.Char(
        string="APS SHA Response Phrase",
        required_if_provider='aps',
        groups='base.group_system',
    )

    #=== BUSINESS METHODS ===#

    def _aps_get_api_url(self):
        if self.state == 'enabled':
            return 'https://checkout.payfort.com/FortAPI/paymentPage'
        else:  # 'test'
            return 'https://sbcheckout.payfort.com/FortAPI/paymentPage'

    def _aps_calculate_signature(self, data, incoming=True):
        """ Compute the signature for the provided data according to the APS documentation.

        :param dict data: The data to sign.
        :param bool incoming: Whether the signature must be generated for an incoming (APS to Odoo)
                              or outgoing (Odoo to APS) communication.
        :return: The calculated signature.
        :rtype: str
        """
        sign_data = ''.join([f'{k}={v}' for k, v in sorted(data.items()) if k != 'signature'])
        key = self.aps_sha_response if incoming else self.aps_sha_request
        signing_string = ''.join([key, sign_data, key])
        return hashlib.sha256(signing_string.encode()).hexdigest()

    def _get_default_payment_method_codes(self):
        """ Override of `payment` to return the default payment method codes. """
        default_codes = super()._get_default_payment_method_codes()
        if self.code != 'aps':
            return default_codes
        return const.DEFAULT_PAYMENT_METHOD_CODES