109 lines
4.8 KiB
Python
109 lines
4.8 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
|
|
import io
|
|
import base64
|
|
|
|
from odoo import http, _
|
|
from odoo.http import request, send_file
|
|
from odoo.exceptions import UserError
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ApplepayController(http.Controller):
|
|
|
|
@http.route(['/.well-known/apple-developer-merchantid-domain-association'], type='http', auth="public", website=True, sitemap=False)
|
|
def applepay_merchant_file(self, **post):
|
|
attachement_id = request.env['ir.attachment'].sudo().search([
|
|
('res_model', '=', 'payment.acquirer'),
|
|
('res_field', '=', 'apple_pay_server_auth_file'),
|
|
('type', '=', 'binary'),
|
|
('datas', '!=', False)])
|
|
if not attachement_id:
|
|
return request.not_found()
|
|
return send_file(
|
|
io.BytesIO(base64.b64decode(attachement_id.datas)),
|
|
filename='apple-developer-merchantid-domain-association',
|
|
as_attachment=True
|
|
)
|
|
|
|
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', website=True, 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)
|
|
response.update({'tx_id': tx_id.id})
|
|
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')})
|
|
|