## Script (Python) "pay_online"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: request_interswitch2.py 5264 2010-07-14 13:21:21Z henrik $
"""
pay online
"""
from urllib import urlencode
import logging
logger = logging.getLogger('Skins.request_interswitch2')
import DateTime

if context.portal_membership.isAnonymousUser():
    return None

try:
    from Products.zdb import set_trace
except:
    def set_trace():
        pass
request = context.REQUEST
wftool = context.portal_workflow
students = context.portal_url.getPortalObject().campus.students


student_id = context.getStudentId()
student = getattr(students,student_id)

student_record = context.students_catalog.getRecordByKey(student_id)
info = {}
info['student'] = student_record
info['payment_possible'] = False
fee_dict = context.getSchoolFeeForRequest(student_record)
fulltime = student_record.mode.endswith('_ft')

if not fee_dict:
    return context.interswitch_form(info=info)

info['payment_possible'] = True
info['type_description'] = fee_dict['description']
info['student_id'] = student_id
info['reg_no'] = student_record.jamb_reg_no
info['matric_no'] = student_record.matric_no
info['student_name'] = student_record.name
info['type'] = 'request'
info['status'] = 'started'
info['session_id'] = fee_dict['next_session_id']
info['session_str'] = fee_dict['next_session_str']
info['item'] = student_record.course
info['category'] = 'schoolfee2'
info['student_email'] = student_record.email
now = DateTime.DateTime()
info['date'] = now
info['pay_ship_to'] = "Ambrose Alli University Ekpoma"
timestamp = "%d" % int(now.timeTime()*1000)
info['order_id'] = "%s%s" % (student_id[1:],timestamp)
p_id = "p%s" % timestamp

if student_id is None:
    return context.REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url())

#query_url = "http://testwebpay.interswitchng.com/CollegepayService/TransactionQueryURL.aspx"
query_url = "http://webpay.interswitchng.com/CollegepayService/TransactionQueryURL.aspx"

info['callback_url'] = "%s/payments/%s/interswitch_request_cb2" % (student.absolute_url(),p_id)

request_url = query_url
request_url += '?redirectURL=%s' % info['callback_url']
request_url += '&productid=53'
request_url += '&merchantref=ignore'
request_url += '&custNumber=%s' % student_id
request_url += '&session=%s' % info['session_str']
request_url += '&semester=Semester 2' 

info['request_url'] = request_url

if "payments" not in student.objectIds():
    student.invokeFactory('PaymentsFolder','payments')
    payments = getattr(student,'payments')
    d = {}
    d['Title'] = 'Payments'
    payments.getContent().edit(mapping=d)
    wftool.doActionFor(payments,'open')
else:
    payments = getattr(student,'payments')

payments.invokeFactory('Payment', p_id)
payment = getattr(payments,p_id)
wftool.doActionFor(payment,'open')
d = {}
d.update(info)
payment.getContent().edit(mapping=d)

logger.info('%(student_id)s initiated second school fee payment request with order_id %(order_id)s and request url %(request_url)s' % info)

return context.interswitch_request_form(info=info)