155 lines
7.1 KiB
Python
155 lines
7.1 KiB
Python
from odoo import http, _
|
|
from odoo.addons.website_sale.controllers.main import WebsiteSale
|
|
from odoo.addons.payment.controllers.portal import PaymentProcessing
|
|
from odoo.exceptions import ValidationError
|
|
from odoo.http import request
|
|
import logging
|
|
import phonenumbers
|
|
from phonenumbers.phonenumberutil import NumberParseException
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class WebsiteSaleExtended(WebsiteSale):
|
|
|
|
@http.route()
|
|
def checkout(self, **post):
|
|
response = super(WebsiteSaleExtended, self).checkout(**post)
|
|
response.qcontext.update({'hide_quick_donation': True})
|
|
return response
|
|
|
|
@http.route()
|
|
def payment(self, **post):
|
|
response = super(WebsiteSaleExtended, self).payment(**post)
|
|
response.qcontext.update({'hide_quick_donation': True})
|
|
return response
|
|
|
|
@http.route()
|
|
def cart(self, access_token=None, revive='', **post):
|
|
response = super(WebsiteSaleExtended, self).cart(access_token, revive, **post)
|
|
response.qcontext.update({'hide_quick_donation': True})
|
|
return response
|
|
|
|
@http.route()
|
|
def product(self, product, category='', search='', **kwargs):
|
|
response = super(WebsiteSaleExtended, self).product(product, category, search, **kwargs)
|
|
response.qcontext.update({'hide_quick_donation': True, 'product_details': True})
|
|
return response
|
|
|
|
@http.route()
|
|
def shop(self, page=0, category=None, search='', min_price=0.0, max_price=0.0, ppg=False, **post):
|
|
response = super().shop(page=page, category=category, search=search, min_price=min_price, max_price=max_price, ppg=ppg, **post)
|
|
return response
|
|
|
|
def check_mobile_number_validation(self, phone):
|
|
if phone[0] == '+' and phone[1] != '0':
|
|
if phone[1:4] == '966':
|
|
if len(phone[4:]) >= 8:
|
|
return phone
|
|
else:
|
|
phone = phone[0] + '966' + phone[1:]
|
|
elif phone[0] == '+' and phone[1] == '0':
|
|
phone = phone[0] + '966' + phone[2:]
|
|
elif phone[0] == '0' and phone[1] == '5':
|
|
phone = '+966' + phone[1:]
|
|
elif phone[0:2] == '00': # 00966555555555
|
|
if phone[2:5] == '966':
|
|
phone = '+' + '966' + phone[5:]
|
|
elif phone[0] == '0': # 0966555555555
|
|
if phone[1:4] == '966':
|
|
phone = '+' + '966' + phone[4:]
|
|
else:
|
|
if phone[0:3] == '966':
|
|
phone = '+' + phone
|
|
else:
|
|
phone = '+' + '966' + phone
|
|
return phone
|
|
|
|
@http.route('/customer/data/save', type='json', auth='public', website=True)
|
|
def save_customer_data(self, **kwargs):
|
|
order = request.website.sale_get_order()
|
|
if order:
|
|
try:
|
|
string_phone_number = kwargs.get('extra_mobile', '')
|
|
string_name = kwargs.get('extra_name', '')
|
|
if string_phone_number:
|
|
string_phone_number = kwargs.get('extra_mobile', '')
|
|
string_phone_number = self.check_mobile_number_validation(string_phone_number)
|
|
phone_number = phonenumbers.parse(string_phone_number)
|
|
is_valid = phonenumbers.is_possible_number(phone_number)
|
|
if not is_valid:
|
|
raise ValidationError(_('Mobile Number is invalid!'))
|
|
order.sudo().write({
|
|
'order_mobile_number': string_phone_number.replace('+', ''),
|
|
})
|
|
if string_name:
|
|
order.sudo().write({
|
|
'order_name': string_name
|
|
})
|
|
except NumberParseException:
|
|
raise ValidationError(_('Mobile Number is invalid!'))
|
|
except Exception as e:
|
|
raise ValidationError(_(e))
|
|
return True
|
|
|
|
@http.route('/quickpay/hyperpay/payment/create', type='json', auth='public', website=True)
|
|
def quick_hyperpay_payment_trans_create(self, **kw):
|
|
success = False
|
|
order = request.website.sale_get_order(force_create=True)
|
|
product_id = kw.pop('product_id')
|
|
acquire_id = kw.pop('acquire_id')
|
|
mobile = kw.pop('mobile', '')
|
|
string_phone_number = self.check_mobile_number_validation(mobile)
|
|
phone_number = phonenumbers.parse(string_phone_number)
|
|
is_valid = phonenumbers.is_possible_number(phone_number)
|
|
if not is_valid:
|
|
raise ValidationError(_('Mobile Number is invalid!'))
|
|
if order:
|
|
if product_id:
|
|
product_id = request.env['product.product'].sudo().browse(int(product_id))
|
|
if product_id:
|
|
order.order_line.sudo().unlink()
|
|
order.write({
|
|
'order_line':
|
|
[(0, 0, {
|
|
'product_id': product_id.id,
|
|
'product_uom_qty': 1,
|
|
'price_unit': float(kw.get('amount')),
|
|
'product_uom': product_id.uom_id.id
|
|
})],
|
|
'order_mobile_number': string_phone_number.replace('+', '')
|
|
})
|
|
try:
|
|
if acquire_id:
|
|
payment_acquire_id = request.env['payment.acquirer'].sudo().search(
|
|
[('code', 'in', ['hyperpay', 'applepay']), ('company_id', 'in', (False, order.company_id.id)), ('id', '=', int(acquire_id))], limit=1)
|
|
else:
|
|
payment_acquire_id = request.env['payment.acquirer'].sudo().search(
|
|
[('code', '=', 'hyperpay'), ('company_id', 'in', (False, order.company_id.id))], limit=1)
|
|
|
|
# Create transaction
|
|
vals = {'acquirer_id': payment_acquire_id,
|
|
'return_url': '/shop/payment/validate'}
|
|
|
|
transaction = order._create_payment_transaction(vals)
|
|
|
|
# store the new transaction into the transaction list and if there's an old one, we remove it
|
|
# until the day the ecommerce supports multiple orders at the same time
|
|
last_tx_id = request.session.get('__website_sale_last_tx_id')
|
|
last_tx = request.env['payment.transaction'].browse(last_tx_id).sudo().exists()
|
|
if last_tx:
|
|
PaymentProcessing.remove_payment_transaction(last_tx)
|
|
PaymentProcessing.add_payment_transaction(transaction)
|
|
request.session['__website_sale_last_tx_id'] = transaction.id
|
|
|
|
for sale in transaction.sale_order_ids:
|
|
sale.done_with_quick_donation = True
|
|
final_vals = {
|
|
'success': True,
|
|
'tx_id': transaction.id
|
|
}
|
|
return final_vals
|
|
except Exception as e:
|
|
_logger.info("Error in Quick Donation Payment: %s" % (e))
|
|
return {'success': success}
|