diff --git a/odex25_donation/applepay_fast_checkout/__init__.py b/odex25_donation/applepay_fast_checkout/__init__.py new file mode 100644 index 000000000..a03bfd097 --- /dev/null +++ b/odex25_donation/applepay_fast_checkout/__init__.py @@ -0,0 +1 @@ +from . import controllers \ No newline at end of file diff --git a/odex25_donation/applepay_fast_checkout/__manifest__.py b/odex25_donation/applepay_fast_checkout/__manifest__.py new file mode 100644 index 000000000..3b0e21fa8 --- /dev/null +++ b/odex25_donation/applepay_fast_checkout/__manifest__.py @@ -0,0 +1,19 @@ +{ + 'name': 'HyperPay ApplePay CopyAndPay Fast-Checkout', + 'version': '14.0.1.0', + 'description': 'Technical module to add apple pay in any place in the document', + 'summary': 'Technical module to add apple pay in any place in the document', + 'author': 'Expert Co. Ltd.', + 'website': 'https://www.exp-sa.com', + 'license': 'LGPL-3', + 'category': 'Payment', + 'depends': [ + 'payment_applepay' + ], + 'data': [ + 'views/applepay_iframe.xml', + 'views/templates.xml' + ], + 'auto_install': True, + 'application': False, +} \ No newline at end of file diff --git a/odex25_donation/applepay_fast_checkout/controllers/__init__.py b/odex25_donation/applepay_fast_checkout/controllers/__init__.py new file mode 100644 index 000000000..deec4a8b8 --- /dev/null +++ b/odex25_donation/applepay_fast_checkout/controllers/__init__.py @@ -0,0 +1 @@ +from . import main \ No newline at end of file diff --git a/odex25_donation/applepay_fast_checkout/controllers/main.py b/odex25_donation/applepay_fast_checkout/controllers/main.py new file mode 100644 index 000000000..bad68f626 --- /dev/null +++ b/odex25_donation/applepay_fast_checkout/controllers/main.py @@ -0,0 +1,33 @@ +import json + +from odoo.http import route, request, Controller + + +class ApplePayFastCheckout(Controller): + + @route('/applepay', type='http', auth='public', website=True, csrf=False) + def apple_pay_iframe(self, **kwargs): + acquirer_id = request.env['payment.acquirer'].sudo().search([('provider', '=', 'applepay')], limit=1) + + if acquirer_id.state == 'test': + url = "https://eu-test.oppwa.com/v1/paymentWidgets.js" + else: + url = "https://oppwa.com/v1/paymentWidgets.js" + + response = request.render("applepay_fast_checkout.apple_pay_iframe", {'hyperpay_src': url}) + 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 + + @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')) + processed_data = self._process_checkout_data(data) + checkout_id = self._get_checkout_id(processed_data) + return {'checkout_id': checkout_id} + + def _process_checkout_data(self, data): + return data + + def _get_checkout_id(self, vals): + return '' diff --git a/odex25_donation/applepay_fast_checkout/static/src/js/applepay_iframe.js b/odex25_donation/applepay_fast_checkout/static/src/js/applepay_iframe.js new file mode 100644 index 000000000..f3daef228 --- /dev/null +++ b/odex25_donation/applepay_fast_checkout/static/src/js/applepay_iframe.js @@ -0,0 +1,72 @@ +var wpwlOptions = { + applePay: { + version: 3, + displayName: "ENSAN", + total: { label: "ENSAN", amount: "10" }, + checkAvailability: "applePayCapabilities", + currencyCode: "SAR", + supportedNetworks: ["mada", "masterCard", "visa"], + merchantCapabilities: ["supports3DS", "supportsCredit", "supportsDebit"], + merchantIdentifier: "8ac9a4ca811e7d6f018132e7a3654ddf", + supportedCountries: ["SA"], + buttonSource: "css", + buttonStyle: "white", + buttonType: "pay", + countryCode: "SA", + submitOnPaymentAuthorized: ["customer"], + requiredShippingContactFields: ["phone"], + onCancel: function () { + if (window.wpwl && window.wpwl.checkout && window.wpwl.checkout.id) { + window.wpwl.checkout.id = ""; + } + }, + }, + locale: "ar-AB", + onReady: function () { + window.parent.document.addEventListener("applePayAmountUpdate", function (e) { + try { + window.wpwlOptions.applePay.total.amount = e.detail.amount; + console.log("Apple Pay Amount updated to:", window.wpwlOptions.applePay.total.amount); + } catch (error) { + console.error(error); + } + }); + + // let isCheckoutPage = $("#oe_structure_website_sale_payment_1", window.parent.document).length; + // if (isCheckoutPage) { + // let applepayButton = $("apple-pay-button"); + // let totalAmount = $("tr#order_total", window.parent.document).find("span.oe_currency_value").text().replaceAll(",", ""); + // window.wpwlOptions.applePay.total.amount = totalAmount; + // console.log("Apple Pay Amount updated to:", window.wpwlOptions.applePay.total.amount); + // applepayButton.css({ + // "--apple-pay-button-height": "60px", + // "--apple-pay-button-border-radius": "4px", + // "--apple-pay-button-padding": "15px 5px", + // "--apple-pay-button-box-sizing": "border-box", + // }); + // } + }, + createCheckout: function () { + const iframeElement = window.frameElement; // The + + + + diff --git a/odex25_donation/ensan_website_sale/__manifest__.py b/odex25_donation/ensan_website_sale/__manifest__.py index 98bf81495..a152119ad 100644 --- a/odex25_donation/ensan_website_sale/__manifest__.py +++ b/odex25_donation/ensan_website_sale/__manifest__.py @@ -11,6 +11,7 @@ 'website_sale', 'payment_hyperpay', 'payment_applepay', + 'applepay_fast_checkout', 'ensan_sale_management' ], 'data': ['views/templates.xml', 'views/assets.xml'], diff --git a/odex25_donation/ensan_website_sale/controllers/__init__.py b/odex25_donation/ensan_website_sale/controllers/__init__.py index deec4a8b8..dacc61b4c 100644 --- a/odex25_donation/ensan_website_sale/controllers/__init__.py +++ b/odex25_donation/ensan_website_sale/controllers/__init__.py @@ -1 +1,2 @@ -from . import main \ No newline at end of file +from . import main +from . import applepay_fast_checkout \ No newline at end of file diff --git a/odex25_donation/ensan_website_sale/controllers/applepay_fast_checkout.py b/odex25_donation/ensan_website_sale/controllers/applepay_fast_checkout.py new file mode 100644 index 000000000..50498106b --- /dev/null +++ b/odex25_donation/ensan_website_sale/controllers/applepay_fast_checkout.py @@ -0,0 +1,74 @@ +import logging + +from odoo.http import request + +from odoo.addons.applepay_fast_checkout.controllers.main import ApplePayFastCheckout +from odoo.addons.payment.controllers.portal import PaymentProcessing + +class WebsiteSaleFastCheckout(ApplePayFastCheckout): + def _process_checkout_data(self, data): + product_ids = data.get('product_ids', []) + gifts = data.get('gifts', []) + use_current_order = data.get('use_current_order', False) + current_order = request.website.sale_get_order(force_create=True) + + if use_current_order: + order = current_order + else: + order = current_order.copy() + 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 + + tx.sale_order_ids.sudo().write({'done_with_quick_donation': data.get('done_with_quick_donation', False)}) + + return {'partner_id': tx.partner_id.id, + 'reference': tx.reference, + 'payment_acquire_id': payment_acquire_id, + 'order_id': order, + 'amount': tx.amount} + + def _get_checkout_id(self, vals): + payment_acquire_id = vals.get('payment_acquire_id') + checkout_id = payment_acquire_id._get_authenticate_apple_pay(vals) + return checkout_id diff --git a/odex25_donation/ensan_website_sale/views/templates.xml b/odex25_donation/ensan_website_sale/views/templates.xml index 762f6866c..53e06c97c 100644 --- a/odex25_donation/ensan_website_sale/views/templates.xml +++ b/odex25_donation/ensan_website_sale/views/templates.xml @@ -202,10 +202,10 @@