131 lines
5.6 KiB
Python
131 lines
5.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
#################################################################################
|
|
#
|
|
# Copyright (c) 2015-Present Webkul Software Pvt. Ltd. (<https://webkul.com/>)
|
|
#
|
|
#################################################################################
|
|
|
|
import requests
|
|
from odoo import http
|
|
import random
|
|
|
|
from odoo.http import request
|
|
from odoo.addons.payment_hyperpay.data.payment_icon import payment_icon
|
|
|
|
import logging
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
# test_domain = "https://test.oppwa.com"
|
|
# live_domain = "https://oppwa.com"
|
|
test_domain = "https://eu-test.oppwa.com"
|
|
live_domain = "https://eu-prod.oppwa.com"
|
|
|
|
def _get_checkout_id(**params):
|
|
auth = params.pop('auth', '')
|
|
env = params.pop('env')
|
|
if env == 'enabled':
|
|
domain = live_domain
|
|
else:
|
|
domain = test_domain
|
|
url_string = domain + "/v1/checkouts"
|
|
|
|
headers = {
|
|
"Authorization": auth,
|
|
}
|
|
try:
|
|
r = requests.post(url=url_string, data=params, headers=headers)
|
|
r = r.json()
|
|
if 'parameterErrors' in r.get('result'):
|
|
raise Exception
|
|
return r
|
|
except Exception as e:
|
|
return {}
|
|
|
|
class HyperPayController(http.Controller):
|
|
|
|
@http.route('/payment/hyperpay/checkout/create', type='json', auth='public', csrf=False, website=True)
|
|
def create_hyperpay_checkout(self, **post):
|
|
_logger.info('--post---%r', post)
|
|
tx = request.env['payment.transaction'].sudo().search([('id', '=', int(post.get('txId', 0)))])
|
|
final_response = {}
|
|
if tx:
|
|
base_url = request.httprequest.host_url
|
|
acq = tx.acquirer_id
|
|
partner_id = tx.partner_id
|
|
order_id = tx.sale_order_ids[0] if tx.sale_order_ids else None
|
|
partner_name = partner_id.name or 'Guest'
|
|
partner_mobile = partner_id.mobile or ''
|
|
if order_id:
|
|
partner_mobile = order_id.order_mobile_number
|
|
partner_name = order_id.order_name
|
|
kwargs = {
|
|
"auth": "Bearer " + acq.hyperpay_authorization,
|
|
"entityId": acq.hyperpay_merchant_id,
|
|
"amount": '%.2f' % tx.amount,
|
|
"currency": tx.currency_id and tx.sudo().currency_id.name or '',
|
|
"paymentType": "DB",
|
|
"env": acq.state,
|
|
"customParameters[SHOPPER_tx_id]": tx.id,
|
|
"merchantTransactionId": tx.reference,
|
|
"billing.street1": partner_id.street or 'Riyadh',
|
|
"billing.street2": partner_id.street2 or '',
|
|
"billing.city": partner_id.city or 'Riyadh',
|
|
"billing.state": partner_id.state_id.name or 'Riyadh',
|
|
"billing.postcode": partner_id.zip or '',
|
|
"billing.country": partner_id.country_id.code or 'SA',
|
|
"customer.givenName": partner_name,
|
|
"customer.surname": '',
|
|
"customer.email": '{seq}@ensan.sa'.format(seq=random.randint(0, 10000)),
|
|
"customer.mobile": partner_mobile,
|
|
"customer.phone": partner_id and partner_id.phone or partner_id.mobile or '',
|
|
}
|
|
resp = _get_checkout_id(**kwargs)
|
|
tx.hyperpay_checkout_id = resp.get('id')
|
|
domain = live_domain if acq.state == 'enabled' else test_domain
|
|
|
|
payment_icons = acq.payment_icon_ids.mapped('name')
|
|
data_brands = "VISA"
|
|
if (len(payment_icon) > 1):
|
|
brands = [payment_icon[i.upper()] for i in payment_icons if i.upper() in payment_icon.keys()]
|
|
data_brands = brands and " ".join(brands) or data_brands
|
|
|
|
final_response = {
|
|
"checkoutId": resp.get('id', ''),
|
|
'domain': domain,
|
|
'base_url': base_url,
|
|
'data_brands': data_brands,
|
|
'acq': acq.id,
|
|
'website_id': request.session.get('force_website_id') or request.website.id
|
|
}
|
|
|
|
return final_response
|
|
|
|
@http.route('/payment/hyperpay/result', type='http', auth='public', csrf=False, website=True, save_session=True)
|
|
def hyperpay_shopper_result(self, **post):
|
|
acq = request.env['payment.acquirer'].sudo().search([('id', '=', post.get('acq'))])
|
|
if acq.state == 'enabled':
|
|
url = live_domain
|
|
else:
|
|
url = test_domain
|
|
url += '/' + post.get('resourcePath') + "?entityId=%s" % (acq.hyperpay_merchant_id)
|
|
headers = {
|
|
"Authorization": "Bearer " + acq.hyperpay_authorization,
|
|
}
|
|
resp = requests.get(url=url, headers=headers).json()
|
|
|
|
_logger.info("-----/payment/hyperpay/result-----response------------%r----", resp)
|
|
|
|
payment = request.env['payment.transaction'].sudo()
|
|
tx_id = payment.search([('hyperpay_checkout_id', '=', post.get('id', ''))])
|
|
tx = resp.get('customParameters', {}).get('SHOPPER_tx_id') or tx_id and tx_id.id or ''
|
|
resp.update({'tx_id': tx})
|
|
|
|
res = request.env['payment.transaction'].sudo().form_feedback(resp, "hyperpay")
|
|
ids = tx_id.sale_order_ids.sudo().ids
|
|
request.session['sale_last_order_id'] = ids[0] if len(ids) else None
|
|
if post.get('website'):
|
|
request.session['force_website_id'] = int(post['website'])
|
|
_logger.info('/payment/hyperpay/result--force_website_id = %s' % request.session.get('force_website_id'))
|
|
_logger.info('/payment/hyperpay/result--force_website_id = %s' % request.website.id)
|
|
return request.redirect('/payment/process')
|