Add PayDunya payment provider integration
- Implemented PayDunya payment provider with necessary models, controllers, and views. - Added configuration files for Docker and Odoo setup. - Included .gitignore for Python and Odoo specific files.
This commit is contained in:
1
addons/payment_paydunya/controllers/__init__.py
Normal file
1
addons/payment_paydunya/controllers/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import main
|
||||
80
addons/payment_paydunya/controllers/main.py
Normal file
80
addons/payment_paydunya/controllers/main.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import logging
|
||||
import requests
|
||||
from odoo import http
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PaydunyaController(http.Controller):
|
||||
@http.route('/payment/paydunya/return', type='http', auth='public', methods=['GET'], csrf=False)
|
||||
def paydunya_return(self, **kwargs):
|
||||
"""Handle return from PayDunya after payment attempt."""
|
||||
token = kwargs.get('token') or http.request.params.get('token')
|
||||
if not token:
|
||||
return http.request.redirect('/')
|
||||
|
||||
provider = http.request.env['payment.provider'].sudo().search([('code', '=', 'paydunya')], limit=1)
|
||||
if not provider:
|
||||
_logger.warning('PayDunya return called but no provider found')
|
||||
return http.request.redirect('/')
|
||||
|
||||
# Build check URL using the confirm endpoint
|
||||
base = provider._get_paydunya_api_base()
|
||||
confirm_url = base + '/checkout-invoice/confirm/{}'.format(token)
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'PAYDUNYA-MASTER-KEY': provider.paydunya_master_key or '',
|
||||
'PAYDUNYA-PRIVATE-KEY': provider.paydunya_private_key or '',
|
||||
'PAYDUNYA-TOKEN': provider.paydunya_token or ''
|
||||
}
|
||||
|
||||
try:
|
||||
# GET request to verify status
|
||||
resp = requests.get(confirm_url, headers=headers, timeout=15)
|
||||
data = resp.json()
|
||||
_logger.info('PayDunya confirm response: %s', data)
|
||||
except Exception as e:
|
||||
_logger.exception('Error verifying PayDunya invoice: %s', e)
|
||||
data = {'token': token, 'status': 'failed'}
|
||||
|
||||
# Trigger transaction handling with the confirmed data
|
||||
try:
|
||||
tx_model = http.request.env['payment.transaction'].sudo()
|
||||
handled = tx_model._handle_notification_data(data)
|
||||
if handled:
|
||||
return http.request.redirect('/payment/process')
|
||||
except Exception:
|
||||
_logger.exception('Error handling PayDunya notification data')
|
||||
|
||||
return http.request.redirect('/')
|
||||
|
||||
@http.route('/payment/paydunya/cancel', type='http', auth='public', methods=['GET'], csrf=False)
|
||||
def paydunya_cancel(self, **kwargs):
|
||||
"""Handle cancellation from PayDunya."""
|
||||
token = kwargs.get('token') or http.request.params.get('token')
|
||||
_logger.info('PayDunya payment cancelled: token=%s', token)
|
||||
return http.request.redirect('/shop/cart')
|
||||
|
||||
@http.route('/payment/paydunya/callback', type='http', auth='public', methods=['POST'], csrf=False)
|
||||
def paydunya_callback(self, **kwargs):
|
||||
"""Handle IPN callback from PayDunya.
|
||||
|
||||
PayDunya sends data as application/x-www-form-urlencoded with 'data' key containing JSON.
|
||||
"""
|
||||
try:
|
||||
# The 'data' parameter contains the JSON-encoded transaction info
|
||||
import json
|
||||
data_str = http.request.params.get('data')
|
||||
if data_str:
|
||||
data = json.loads(data_str)
|
||||
_logger.info('PayDunya IPN callback received: %s', data)
|
||||
|
||||
tx_model = http.request.env['payment.transaction'].sudo()
|
||||
tx_model._handle_notification_data(data)
|
||||
except Exception:
|
||||
_logger.exception('Error handling PayDunya callback')
|
||||
|
||||
# Always return 200 OK to PayDunya
|
||||
return 'OK'
|
||||
|
||||
Reference in New Issue
Block a user