262 lines
12 KiB
Python
262 lines
12 KiB
Python
from odoo import http, _
|
|
from odoo.addons.website_sale.controllers.main import PaymentPortal
|
|
from odoo.exceptions import ValidationError
|
|
from odoo.http import request
|
|
import requests, logging
|
|
import phonenumbers
|
|
from phonenumbers.phonenumberutil import NumberParseException
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class PaymentPortalExtraFields(PaymentPortal):
|
|
|
|
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()
|
|
def shop_payment_transaction(self, *args, **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 = '+' + str(string_phone_number)
|
|
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,
|
|
})
|
|
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 super().shop_payment_transaction(*args, **kwargs)
|
|
|
|
@http.route(
|
|
'/quickpay/hyperpay/payment', type='json', auth='public', website=True
|
|
)
|
|
def quick_hyperpay_payment(self, **kw):
|
|
success = False
|
|
order = request.website.sale_get_order(force_create=True)
|
|
test_domain = "https://test.oppwa.com/v1/payments"
|
|
live_domain = "https://oppwa.com/v1/payments"
|
|
product_id = kw.pop('product_id')
|
|
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
|
|
})]
|
|
})
|
|
try:
|
|
payment_acquire_id = request.env['payment.provider'].search([('code', '=','hyperpay'), ('company_id', 'in', (False, order.company_id.id))], limit=1)
|
|
temp_vals = {
|
|
'access_token': order._portal_ensure_token(),
|
|
'order_id': order.id,
|
|
'payment_option_id': payment_acquire_id.id,
|
|
'flow': 'direct',
|
|
'tokenization_requested': False,
|
|
'landing_route': '/',
|
|
'currency_id': order.currency_id.id,
|
|
}
|
|
vals = self.shop_payment_transaction(**(temp_vals))
|
|
payment_sudo = request.env['payment.transaction'].sudo()
|
|
tx = payment_sudo.search([('reference', '=', vals.get('reference', False))], limit=1)
|
|
if tx:
|
|
acq = tx.provider_id
|
|
payment_vals = {
|
|
"entityId": acq.hyperpay_merchant_id,
|
|
"amount": '%.2f' % tx.amount,
|
|
"currency": tx.currency_id and tx.sudo().currency_id.name or '',
|
|
"paymentType": "DB",
|
|
"customParameters[SHOPPER_tx_id]": tx.id,
|
|
"merchantTransactionId": tx.id,
|
|
'paymentBrand': kw.get('paymentBrand'),
|
|
'card.number': int(kw.get('card.number')),
|
|
'card.holder': kw.get('card.holder'),
|
|
'card.expiryMonth': '%02d' % int(kw.get('card.expiryMonth')),
|
|
'card.expiryYear': kw.get('card.expiryYear'),
|
|
'card.cvv': '%03d' % int(kw.get('card.cvv')),
|
|
}
|
|
try:
|
|
headers = {
|
|
"Authorization": "Bearer " + acq.hyperpay_authorization,
|
|
}
|
|
if acq.state == 'enabled':
|
|
p_url = live_domain
|
|
else:
|
|
p_url = test_domain
|
|
hyper_response = requests.post(url=p_url, data=payment_vals, headers=headers)
|
|
hyper_response = hyper_response.json()
|
|
tx.hyperpay_checkout_id = hyper_response.get('id')
|
|
hyper_response.update({
|
|
'tx_id': hyper_response.get('customParameters', {}).get('SHOPPER_tx_id') or tx and tx.id or ''
|
|
})
|
|
tx._handle_notification_data('hyperpay', hyper_response)
|
|
confirmed_orders = tx._check_amount_and_confirm_order()
|
|
confirmed_orders._send_order_confirmation_mail()
|
|
if tx and tx.state == 'done':
|
|
success = True
|
|
except Exception as e:
|
|
_logger.info("Error in Quick Donation Payment: %s" % (e))
|
|
except Exception as e:
|
|
_logger.info("Error in Quick Donation Payment: %s" % (e))
|
|
return {
|
|
'success': success,
|
|
}
|
|
|
|
@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', request.env.ref('payment_applepay.payment_acquirer_applepay').id)
|
|
mobile = kw.pop('mobile', '')
|
|
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': mobile
|
|
})
|
|
try:
|
|
if acquire_id:
|
|
payment_acquire_id = request.env['payment.provider'].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.provider'].sudo().search(
|
|
[('code', '=', 'hyperpay'), ('company_id', 'in', (False, order.company_id.id))], limit=1)
|
|
temp_vals = {
|
|
'access_token': order._portal_ensure_token(),
|
|
'order_id': order.id,
|
|
'payment_option_id': payment_acquire_id.id,
|
|
'flow': 'direct',
|
|
'tokenization_requested': False,
|
|
'landing_route': '/',
|
|
'currency_id': order.currency_id.id,
|
|
}
|
|
vals = self.shop_payment_transaction(**(temp_vals))
|
|
payment_sudo = request.env['payment.transaction'].sudo()
|
|
tx = payment_sudo.search([('reference', '=', vals.get('reference', False))], limit=1)
|
|
for sale in tx.sale_order_ids:
|
|
sale.done_with_quick_donation = True
|
|
final_vals = {
|
|
'success': True,
|
|
'tx_id': tx.id
|
|
}
|
|
if payment_acquire_id and payment_acquire_id.code == 'applepay' and tx:
|
|
if kw.get('is_donation_detail_page'):
|
|
redirect_url = '/quick/applepay/payment/json?tx=%s' % (tx.id)
|
|
else:
|
|
redirect_url = '/quick/applepay/payment?tx=%s' % (tx.id)
|
|
final_vals.update({
|
|
'redirect_url': redirect_url
|
|
})
|
|
return final_vals
|
|
except Exception as e:
|
|
_logger.info("Error in Quick Donation Payment: %s" % (e))
|
|
return {
|
|
'success': success,
|
|
}
|
|
|
|
@http.route('/quick/applepay/payment', type='http', auth='public', website=True, csrf=False)
|
|
def apple_pay_payment_method(self, **kwargs):
|
|
tx = kwargs.get('tx')
|
|
payment_sudo = request.env['payment.transaction'].sudo()
|
|
tx = payment_sudo.search([('id', '=', int(tx))], limit=1)
|
|
if tx:
|
|
kw = tx._get_processing_values()
|
|
kw = tx._get_specific_rendering_values(kw)
|
|
|
|
acquirer = request.env['payment.provider'].sudo().search([('code', '=', 'applepay')], limit=1)
|
|
|
|
kw['currency'] = 'SAR'
|
|
_logger.info("Post Values From Apple Payment = %s" % (kw))
|
|
|
|
if acquirer.state == 'test':
|
|
return request.render("payment_applepay.payment_applepay_card",
|
|
{'check_out_id': kw.get('check_out_id'), 'return_url': kw.get('applepay_return')})
|
|
else:
|
|
return request.render("payment_applepay.payment_applepay_card_live",
|
|
{'check_out_id': kw.get('check_out_id'), 'return_url': kw.get('applepay_return')})
|
|
else:
|
|
return request.redirect('/')
|
|
|
|
@http.route('/quick/applepay/payment/json', type='json', auth='public', website=True)
|
|
def apple_pay_payment_method_json(self, **kwargs):
|
|
tx = kwargs.get('tx')
|
|
payment_sudo = request.env['payment.transaction'].sudo()
|
|
tx = payment_sudo.search([('id', '=', int(tx))], limit=1)
|
|
if tx:
|
|
kw = tx._get_processing_values()
|
|
kw = tx._get_specific_rendering_values(kw)
|
|
|
|
acquirer = request.env['payment.provider'].sudo().search([('code', '=', 'applepay')], limit=1)
|
|
|
|
kw['currency'] = 'SAR'
|
|
_logger.info("Post Values From Apple Payment = %s" % (kw))
|
|
|
|
if acquirer.state == 'test':
|
|
content = request.env['ir.ui.view'].sudo()._render_template(
|
|
'payment_applepay.payment_applepay_card',
|
|
{'check_out_id': kw.get('check_out_id'), 'return_url': kw.get('applepay_return')}
|
|
)
|
|
return {'content': content}
|
|
else:
|
|
content = request.env['ir.ui.view'].sudo()._render_template(
|
|
'payment_applepay.payment_applepay_card_live',
|
|
{'check_out_id': kw.get('check_out_id'), 'return_url': kw.get('applepay_return')}
|
|
)
|
|
return {'content': content}
|
|
else:
|
|
return request.redirect('/')
|