173 lines
8.2 KiB
Python
173 lines
8.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# This module and its content is copyright of Technaureus Info Solutions Pvt. Ltd.
|
|
# - © Technaureus Info Solutions Pvt. Ltd 2020. All rights reserved.
|
|
|
|
import json
|
|
import logging
|
|
import pprint
|
|
|
|
import requests
|
|
from odoo import http, _
|
|
from odoo.addons.payment.controllers.portal import PaymentProcessing
|
|
from odoo.http import request
|
|
from odoo.exceptions import UserError
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ApplepayController(http.Controller):
|
|
|
|
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.replace('+', '')
|
|
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.replace('+', '')
|
|
|
|
@http.route('/payment/applepay/return', type='http', auth='public', csrf=False)
|
|
def applepay_return(self, **post):
|
|
""" applepay."""
|
|
acquirer = request.env['payment.acquirer'].sudo().search([('provider', '=', 'applepay')], limit=1)
|
|
_logger.info("Post Return = %s" % (post))
|
|
if post.get('resourcePath'):
|
|
if acquirer.state == 'test':
|
|
url = "https://eu-test.oppwa.com"
|
|
else:
|
|
url = "https://oppwa.com"
|
|
url += post.get('resourcePath')
|
|
url += '?entityId=' + acquirer.applepay_entity_id
|
|
authorization_bearer = "Bearer " + acquirer.applepay_authorization_bearer
|
|
_logger.info("Url = %s", url)
|
|
_logger.info("Headers = %s", authorization_bearer)
|
|
try:
|
|
headers = {'Authorization': authorization_bearer}
|
|
response = requests.get(url, headers=headers)
|
|
response = json.loads(response.text)
|
|
except Exception as e:
|
|
raise UserError(_(e))
|
|
_logger.info('applepay: entering form_feedback with post data %s', pprint.pformat(post))
|
|
_logger.info("Response = %s", response)
|
|
phone = response.get('customer', {}).get('phone', '')
|
|
phone = self.check_mobile_number_validation(phone)
|
|
payment = request.env['payment.transaction'].sudo()
|
|
tx_id = payment.search([('applepay_checkout_id', '=', post.get('id', ''))])
|
|
tx_id.sale_order_ids.sudo().write({'order_mobile_number': phone})
|
|
_logger.info("Tx Sudo = %s", tx_id)
|
|
tx = response.get('customParameters', {}).get('SHOPPER_tx_id') or tx_id or ''
|
|
response.update({'tx_id': tx})
|
|
ids = tx_id.sale_order_ids.sudo().ids
|
|
request.session['sale_last_order_id'] = ids[0] if len(ids) else None
|
|
request.env['payment.transaction'].sudo().form_feedback(response, "applepay")
|
|
return request.redirect('/payment/process')
|
|
|
|
@http.route('/shop/applepay/payment/', type='http', auth="none", methods=['POST'], csrf=False)
|
|
def _payment_applepay_card(self, **kw):
|
|
acquirer = request.env['payment.acquirer'].sudo().search([('provider', '=', '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')})
|
|
|
|
|
|
@http.route('/applepay', type='http', auth='public', website=True, csrf=False)
|
|
def apple_pay_iframe(self, **kwargs):
|
|
response = request.render("payment_applepay.apple_pay_iframe")
|
|
response.headers['Content-Security-Policy'] = "script-src blob: 'self' 'unsafe-inline' 'unsafe-eval' https://*; worker-src blob: 'self' 'unsafe-inline' 'unsafe-eval' https://*;connect-src 'self' https://* wss://*;frame-src 'self' blob: https://*;"
|
|
return response
|
|
|
|
@http.route('/applepay/checkout', type='json', auth='public', website=True)
|
|
def apple_pay_create_checkout(self, **post):
|
|
data = json.loads(request.httprequest.data.decode('utf-8'))
|
|
checkout_id = self._apple_pay_create_checkout(data)
|
|
return {'checkout_id': checkout_id}
|
|
|
|
def _apple_pay_create_checkout(self, data):
|
|
try:
|
|
order = request.website.sale_get_order(force_create=True)
|
|
product_ids = data.get('product_ids', [])
|
|
gifts = data.get('gifts', [])
|
|
use_current_order = data.get('use_current_order', False)
|
|
|
|
if not order:
|
|
return ''
|
|
|
|
if not use_current_order:
|
|
|
|
order_mobile_number = data.get('mobile', '')
|
|
order_name = data.get('name', '')
|
|
|
|
extra_donators_ids = [(0, 0, {
|
|
'sale_id': order.id,
|
|
'product_id': gift.get('product_id'),
|
|
'donated_amount': gift.get('amount'),
|
|
'donator_name': gift.get('receiver_name'),
|
|
'donator_mobile_number': gift.get('receiver_mobile')
|
|
}) for gift in gifts if gift.get('product_id')]
|
|
|
|
order_line = [(0, 0, {
|
|
'product_id': product.get('id'),
|
|
'extra_donators_ids': extra_donators_ids,
|
|
'product_uom_qty': product.get('quantity', 1),
|
|
'price_unit': product.get('price_unit', 0) + sum(gift.get('amount', 0) for gift in gifts if gift.get('product_id') == product.get('id'))}) for product in product_ids if product.get('id')]
|
|
|
|
if order_line:
|
|
order.order_line.sudo().unlink()
|
|
order.write({
|
|
'order_line': order_line,
|
|
'order_mobile_number': order_mobile_number,
|
|
'order_name': order_name
|
|
})
|
|
|
|
payment_acquire_id = request.env['payment.acquirer'].sudo().search([('provider', '=', 'applepay'),
|
|
('company_id', 'in', (False, order.company_id.id))], limit=1)
|
|
|
|
# Create transaction
|
|
vals = {'acquirer_id': payment_acquire_id.id,
|
|
'return_url': '/payment/applepay/return'}
|
|
|
|
tx = 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(tx)
|
|
request.session['__website_sale_last_tx_id'] = tx.id
|
|
|
|
vals = {'partner_id': tx.partner_id.id,
|
|
'reference': tx.reference,
|
|
'amount': tx.amount}
|
|
|
|
checkout_id = payment_acquire_id._get_authenticate_apple_pay(vals)
|
|
tx.sale_order_ids.sudo().write({'done_with_quick_donation': data.get('done_with_quick_donation', False)})
|
|
|
|
return checkout_id
|
|
except Exception as e:
|
|
_logger.error("Error in Apple pay Payment: %s" % (e))
|
|
return ''
|