I'm having some issues trying to write a module for a payment gateway. The External API we need to use requires 3 things through a POST request:
1. ClientID: Token given by the API Issuer
2. Amount: Total amount to pay
3. TransactionID: Field provided by Odoo with the ID for the entire transaction
When I code it outside of Odoo everything works fine, I'm new to Odoo syntax so I don't know how to move all that to my module despite of mimicking 3 similar modules like "stripe", "payumoney" and "paytm".
External Code:
import requests
idcliente = '97bd8cf4-e894-4cb5-b8cc-3fc31f79c81a'
valor = 42.75
id_transaccioncomercio = 20354
base_url = 'https://test.serfinsacheckout.com:8080/'
query_tring = 'Pay/GateWay?token=' + idcliente + '&idTransaccion=2565'
params = 'api/PayApi/TokeyTran'
payload = {
'TokeyComercio': idcliente,
'Monto': valor,
'IdTransaccionCliente': id_transaccioncomercio
}
r = requests.post(base_url + params, data = payload)
print(r.json())
r = requests.get(base_url + query_tring)
# print("Status Code: {} [OK]".format(r.status_code))
print(r.content)
My Code in Odoo (Controller):
import logging
import pprint
import werkzeug
from odoo import http
from odoo.http import request
_logger = logging.getLogger(__name__)
class SerfinsaController(http.Controller):
_return_url = '/payment/serfinsa/return'
def _get_return_url(self, **post):
post = dict(post)
return_url = '/payment/serfinsa/validate'
return return_url
def serfinsa_validate_data(self, **post):
res = False
reference = post['idcliente']
if reference:
_logger.info('Serfinsa: validated data')
res = request.env['payment.transaction'].sudo().form_feedback(post, 'serfinsa_payment')
return res
@http.route('/payment/serfinsa/return', type='http',
auth='none', methods=['GET','POST'])
def serfinsa_return(self, **post):
# Receives POST data from Serfinsa after making the payment
_logger.info('Serfinsa: entering form_feedback with post data %s',
pprint.pformat(post))
return_url = self._get_return_url(**post)
self.serfinsa_validate_data(**post)
return werkzeug.utils.redirect(return_url)
My Code in Models > Payment:
import logging
from odoo import api, fields, models, _
from odoo.addons.payment.models.payment_acquirer import ValidationError
_logger = logging.getLogger(__name__)
class PaymentAcquirerSerfinsa(models.Model):
_inherit = 'payment.acquirer'
provider = fields.Selection(selection_add=[('serfinsa_payment', 'Serfinsa')])
idcliente = fields.Char(string='Serfinsa Token', required_if_provider='serfinsa_payment')
def _get_serfinsa_urls(self, environment):
if environment == 'prod':
return {'base_url': 'https://test.serfinsacheckout.com:8080/'}
else:
return {'base_url': 'https://test.serfinsacheckout.com:8080/'}
@api.multi
def serfinsa_form_generate_values(self, values):
self.ensure_one()
base_url = self.get_base_url()
serfinsa_values = dict(values,
idcliente=self.idcliente,
valor=values['amount'],
id_transaccioncomercio=values['reference'],
#txnid=values['reference'],
#firstname=values.get('partner_name'),
#email=values.get('partner_email'),
#phone=values.get('partner_phone'),
service_provider='serfinsa',
)
serfinsa_values['udf1'] = serfinsa_values.pop('return_url', '/')
return serfinsa_values
Any Help is really appreciated...Thanks in advance.