fix(payment_paydunya): fiabilise retour PayDunya et post-traitement paiement
This commit is contained in:
@@ -11,40 +11,61 @@ class PaydunyaController(http.Controller):
|
||||
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('/')
|
||||
tx_id = kwargs.get('tx_id') or http.request.params.get('tx_id')
|
||||
sale_order_id = kwargs.get('sale_order_id') or http.request.params.get('sale_order_id')
|
||||
|
||||
tx_model = http.request.env['payment.transaction'].sudo()
|
||||
tx = False
|
||||
if tx_id:
|
||||
try:
|
||||
tx = tx_model.search([('id', '=', int(tx_id))], limit=1)
|
||||
except (TypeError, ValueError):
|
||||
tx = False
|
||||
|
||||
provider = http.request.env['payment.provider'].sudo().search([('code', '=', 'paydunya')], limit=1)
|
||||
if not provider:
|
||||
if not provider and token:
|
||||
_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)
|
||||
data = {}
|
||||
if token and provider:
|
||||
# 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 ''
|
||||
}
|
||||
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'}
|
||||
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', 'sale_order_id': sale_order_id, 'tx_id': tx_id}
|
||||
elif tx:
|
||||
# Fallback when PayDunya did not return token: rely on tx current state.
|
||||
data = {'tx_id': tx.id, 'sale_order_id': sale_order_id}
|
||||
else:
|
||||
return http.request.redirect('/')
|
||||
|
||||
# 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:
|
||||
tx = tx or tx_model._get_tx_from_notification(data)
|
||||
if tx and tx.state == 'done':
|
||||
order = tx._paydunya_get_related_sale_order(sale_order_id=sale_order_id)
|
||||
if order:
|
||||
http.request.session['sale_last_order_id'] = order.id
|
||||
http.request.session['sale_transaction_id'] = tx.id
|
||||
return http.request.redirect('/shop/confirmation')
|
||||
if handled:
|
||||
return http.request.redirect('/shop/cart')
|
||||
except Exception:
|
||||
_logger.exception('Error handling PayDunya notification data')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user