You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

227 lines
11 KiB

# -*- coding: utf-8 -*-
#from urllib.parse import urlparse, urljoin
#import werkzeug
from odoo import http
from odoo.http import request
from odoo import fields, http, tools, _
import base64
import requests
import json
import logging
_logger = logging.getLogger(__name__)
import payplug
payplug.set_api_version("2019-08-06")
import pprint
from odoo.addons.website_sale.controllers.main import WebsiteSale
from odoo.tools.json import scriptsafe as json_scriptsafe
from odoo.exceptions import AccessError, UserError, ValidationError
class WebsiteSaleOney(WebsiteSale):
# CREATE SIMULATION ONEY IN PRODUCT FORM
def _prepare_product_values(self, product, category, search, **kwargs):
res=super(WebsiteSaleOney,self)._prepare_product_values(product, category, search, **kwargs)
# IF PAYMENT ACQUIRER IS ONEY UPDATE VALUES
payment_provider=request.env['payment.provider'].sudo().search([('code','=','payplug')], limit=1)
# ONEY IS ACTIVE
if payment_provider.state != 'disabled' and payment_provider.payment_type_oney == True:
# IF THE PROVIDER IS ALLOWED FROM THE WEBSITE COMPANY
company_ids=request.env.context.get('allowed_company_ids')
if payment_provider.company_id.id in company_ids:
authorized_by_company = True
else:
authorized_by_company = False
# TO VERIFY THAT THE AMOUNT IS IN THE ONEY AUTHORIZED RANGE
amount = res.get('product').list_price
if amount < 100 or amount > 3000 :
authorized_by_oney = False
else:
authorized_by_oney = True
result_simulation=request.env['payment.provider'].sudo()._oney_payment_simulation(amount, 'FR')
# UPDATE DICT WITH SIMULATION ONEY
res.update(result_simulation)
res.update({
'InformationMessage': payment_provider.oney_information_message,
'FooterMessage': payment_provider.oney_footer_message,
'amount_total_oney': amount,
'authorized_by_oney': authorized_by_oney,
'authorized_by_company': authorized_by_company,
'acquirer': payment_provider,
'currency_symbol': request.website.currency_id.symbol,
})
else:
res.update({
'InformationMessage': False,
'FooterMessage': False,
'amount_total_oney': 0,
'authorized_by_oney': None,
'authorized_by_company': None,
'acquirer': payment_provider,
'currency_symbol': request.website.currency_id.symbol,
})
return res
#UPDATE CART
@http.route(['/shop/cart'], type='http', auth="public", website=True, sitemap=False)
def cart(self, access_token=None, revive='', **post):
res=super(WebsiteSaleOney,self).cart(access_token, revive, **post)
# IF PAYMENT ACQUIRER IS ONEY UPDATE VALUES
payment_provider=request.env['payment.provider'].sudo().search([('code','=','payplug')], limit=1)
# ONEY IS ACTIVE
if payment_provider.state != 'disabled' and payment_provider.payment_type_oney == True:
amount=res.__dict__['qcontext']['website_sale_order'].amount_total
# IF THE PROVIDER IS ALLOWED FROM THE WEBSITE COMPANY
company_ids=request.env.context.get('allowed_company_ids')
if payment_provider.company_id.id in company_ids:
authorized_by_company = True
else:
authorized_by_company = False
if amount < 100 or amount > 3000 :
authorized_by_oney = False
else:
authorized_by_oney = True
currency=res.__dict__['qcontext']['website_sale_order'].currency_id.symbol
res.__dict__['qcontext'].update({
'InformationMessage': payment_provider.oney_information_message,
'FooterMessage': payment_provider.oney_footer_message,
'currency_symbol': request.website.currency_id.symbol,
'amount_total_oney': amount,
'authorized_by_oney': authorized_by_oney,
'authorized_by_company': authorized_by_company,
})
res.__dict__['qcontext'].update({'acquirer': payment_provider}) # IF ONEY IS DISABLED
return res
# UPDATE ONEY SIMULATION
@http.route(['/shop/product/oney_simulation'], type='json', auth="public", website=True, csrf=False)
def oney_simulation(self, **kw):
values={}
payment_provider=request.env['payment.provider'].sudo().search([('code','=','payplug')], limit=1)
# ONEY IS ACTIVE
if payment_provider.state != 'disabled' and payment_provider.payment_type_oney == True:
result_simulation=request.env['payment.provider'].sudo()._oney_payment_simulation(float(kw.get('product_total_price')), 'FR')
values = {
'amount_total_oney': "%.2f" %(float(kw.get('product_total_price'))),
'payment_3x': result_simulation['payment_3x'],
'payment_4x': result_simulation['payment_4x'],
}
return values
# CREATE SIMULATION ONEY IN CART PAYMENT
def _get_shop_payment_values(self, order, **kwargs):
res = super(WebsiteSaleOney, self)._get_shop_payment_values(order, **kwargs)
# Return de specific values for Payment Oney
payment_provider = request.env['payment.provider'].sudo().search([('code','=','payplug')], limit=1)
if payment_provider.state != 'disabled':
country=order.partner_id.country_id
if payment_provider.payment_type_oney == True:
result_simulation=request.env['payment.provider'].sudo()._oney_payment_simulation(order.amount_total, 'FR')
res.update(result_simulation)
# ONEY PAYMENT CONTROL BETWEEN €100 AND €3000
if order.amount_total >= 100.00 and order.amount_total <= 3000.00:
authorized_by_oney=True
else:
authorized_by_oney=False
# AUTHORIZED COUNTRY FOR ONEY
authorized_country_oney=True
if country not in payment_provider.available_oney_country_ids:
authorized_country_oney=False
total_qty_card=0.0
limit_quantity=False
for line in res['website_sale_order'].order_line:
total_qty_card+=line.product_uom_qty
if total_qty_card > 10.0:
limit_quantity=True
authorized_by_oney=False
res.update({
'amount_oney': order.amount_total,
'authorized_by_oney': authorized_by_oney,
'limit_quantity': limit_quantity,
'InformationMessage': payment_provider.oney_information_message,
'type_oney': 'oney_x3_with_fees',
'currency_symbol': order.currency_id.symbol,
'authorized_country_oney': authorized_country_oney,
})
# AUTHORIZED COUNTRY FOR PAYPLUG
if payment_provider.payment_type_payplug == True:
authorized_country_payplug=True
if country not in payment_provider.available_payplug_country_ids:
authorized_country_payplug=False
res.update({'authorized_country_payplug': authorized_country_payplug})
# AUTHORIZED COUNTRY FOR AMEX
if payment_provider.payment_type_amex == True:
authorized_country_amex=True
if country not in payment_provider.available_amex_country_ids:
authorized_country_amex=False
res.update({'authorized_country_amex': authorized_country_amex})
# AUTHORIZED COUNTRY FOR BANCONTACT
if payment_provider.payment_type_bancontact == True:
authorized_country_bancontact=True
if country not in payment_provider.available_bancontact_country_ids:
authorized_country_bancontact=False
res.update({'authorized_country_bancontact': authorized_country_bancontact})
return res
# UPDATE SESSION WITH PROVIDER PAYPLUG 'payplug','oney','amex','bancontact' AND 'oney_x3_with_fees','oney_x4_with_fees
@http.route(['/provider_payplug'], type='json', auth="public", website=True)
def provider_payplug(self, **kw):
payment_provider = kw.get('provider_payplug')
type_oney = kw.get('type_oney')
if payment_provider is None:
request.session['provider_payplug']='payplug'
else:
request.session['provider_payplug']=payment_provider
if type_oney is None:
request.session['type_oney']='oney_x3_with_fees'
else:
request.session['type_oney']=type_oney
class PayPlugController(http.Controller):
_return_url = '/payment/payplug/return'
_webhook_url = '/payment/payplug/webhook'
@http.route(
_return_url, type='http', auth='public', methods=['GET'], csrf=False, save_session=False
)
def payplug_return_from_checkout(self, **post):
payment=request.env['payment.transaction'].sudo().browse(int(post.get('transaction')))
# SECRET KEY TEST OR LIVE
secret_payplug_key=request.env['payment.provider'].sudo()._key_acquirer_state(payment.provider_id)
# API PAYPLUG
payplug.set_secret_key(secret_payplug_key)
payment_payplug=payplug.Payment.retrieve(str(payment.provider_reference))
tx_sudo = request.env['payment.transaction'].sudo()._get_tx_from_notification_data('payplug', payment_payplug)
tx_sudo._handle_notification_data('payplug', payment_payplug)
return request.redirect('/payment/status')
@http.route(
_webhook_url, type='http', auth='public', methods=['GET'], csrf=False
)
def payplug_webhook(self, **post):
payment=request.env['payment.transaction'].sudo().browse(int(post.get('transaction')))
# DOWNLOAD SECRET KEY PAYPLUG TEST OR LIVE
secret_payplug_key=request.env['payment.provider'].sudo()._key_acquirer_state(payment.provider_id)
# API PAYPLUG
payplug.set_secret_key(secret_payplug_key)
payment_payplug=payplug.Payment.retrieve(str(payment.provider_reference))
# TRANSACTION CANCEL OR ERROR
CodeError=payment_payplug.failure.__dict__.get('_attributes')
if CodeError['code']=='canceled':
ErrorMessage='PayPlug Error\n'
ErrorMessage+='Transaction canceled by customer'
payment.sudo().write({
'state_message': ErrorMessage,
'state': 'cancel',
})
else:
ErrorMessage='PayPlug Error\n'
ErrorMessage+='Code Error : '+CodeError['code']+'\n'
ErrorMessage+='Message : '+CodeError['message']
payment.sudo().write({
'state_message': ErrorMessage,
'state': 'error',
})
return request.redirect('/shop')