122 lines
4.7 KiB
Python
122 lines
4.7 KiB
Python
import json
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo import http
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.http import request, Response
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.addons.portal.controllers.portal import CustomerPortal
|
|
# pyrefly: ignore [missing-import]
|
|
from odoo.addons.education_portal.services.payment_service import PaymentService
|
|
|
|
|
|
class StudentPaymentController(CustomerPortal):
|
|
|
|
def _get_student(self):
|
|
return request.env.user.student_id
|
|
|
|
@http.route(['/my/education/payment/create'], type='json', auth="user", website=True, csrf=True)
|
|
def payment_create(self, **post):
|
|
student = self._get_student()
|
|
if not student:
|
|
return {'error': 'Student record not found.'}
|
|
|
|
fee_ids = post.get('fee_ids') or []
|
|
custom_amount = post.get('amount')
|
|
try:
|
|
order_data = PaymentService.create_order(
|
|
env=request.env,
|
|
student=student,
|
|
fee_ids=fee_ids,
|
|
custom_amount=custom_amount
|
|
)
|
|
return order_data
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
@http.route(['/my/education/payment/verify'], type='json', auth="user", website=True, csrf=True)
|
|
def payment_verify(self, **post):
|
|
transaction_id = post.get('transaction_id')
|
|
razorpay_order_id = post.get('razorpay_order_id')
|
|
razorpay_payment_id = post.get('razorpay_payment_id')
|
|
razorpay_signature = post.get('razorpay_signature')
|
|
|
|
if not transaction_id:
|
|
return {'error': 'Missing transaction ID.'}
|
|
|
|
try:
|
|
res = PaymentService.verify_and_reconcile(
|
|
env=request.env,
|
|
transaction_id=transaction_id,
|
|
razorpay_order_id=razorpay_order_id,
|
|
razorpay_payment_id=razorpay_payment_id,
|
|
razorpay_signature=razorpay_signature
|
|
)
|
|
return res
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
@http.route(['/my/education/payment/webhook'], type='http', auth="public", methods=['POST'], csrf=False)
|
|
def payment_webhook(self, **kw):
|
|
payload_bytes = request.httprequest.data
|
|
sig_header = request.httprequest.headers.get('X-Razorpay-Signature', '')
|
|
|
|
result = PaymentService.handle_webhook(request.env, payload_bytes, sig_header)
|
|
return Response(json.dumps(result), status=200, mimetype='application/json')
|
|
|
|
@http.route(['/my/education/payment/success/<int:tx_id>'], type='http', auth="user", website=True)
|
|
def payment_success(self, tx_id, **kw):
|
|
student = self._get_student()
|
|
if not student:
|
|
return request.redirect('/my/education/dashboard')
|
|
|
|
tx = request.env['education.payment.transaction'].sudo().browse(tx_id)
|
|
if not tx.exists() or tx.student_id.id != student.id:
|
|
return request.redirect('/my/education/finance')
|
|
|
|
values = {
|
|
'student': student,
|
|
'transaction': tx,
|
|
'page_name': 'payment_success',
|
|
}
|
|
return request.render("education_portal.portal_payment_success", values)
|
|
|
|
@http.route(['/my/education/payment/failed/<int:tx_id>'], type='http', auth="user", website=True)
|
|
def payment_failed(self, tx_id, **kw):
|
|
student = self._get_student()
|
|
if not student:
|
|
return request.redirect('/my/education/dashboard')
|
|
|
|
tx = request.env['education.payment.transaction'].sudo().browse(tx_id)
|
|
if not tx.exists() or tx.student_id.id != student.id:
|
|
return request.redirect('/my/education/finance')
|
|
|
|
values = {
|
|
'student': student,
|
|
'transaction': tx,
|
|
'page_name': 'payment_failed',
|
|
}
|
|
return request.render("education_portal.portal_payment_failed", values)
|
|
|
|
@http.route(['/my/education/payment/history'], type='http', auth="user", website=True)
|
|
def payment_history(self, **kw):
|
|
student = self._get_student()
|
|
if not student:
|
|
return request.redirect('/my/education/dashboard')
|
|
|
|
domain = [('student_id', '=', student.id)]
|
|
status_filter = kw.get('status')
|
|
if status_filter:
|
|
domain.append(('status', '=', status_filter))
|
|
|
|
transactions = request.env['education.payment.transaction'].sudo().search(domain, order='create_date desc')
|
|
payments = request.env['education.student.payment'].sudo().search([('student_id', '=', student.id)], order='payment_date desc')
|
|
|
|
values = {
|
|
'student': student,
|
|
'transactions': transactions,
|
|
'payments': payments,
|
|
'status_filter': status_filter,
|
|
'page_name': 'payment_history',
|
|
}
|
|
return request.render("education_portal.portal_payment_history", values)
|