## Script (Python) "pay_online"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: pay_online.py 1226 2007-01-07 16:48:21Z joachim $
"""
pay online
"""
import logging
logger = logging.getLogger('EPayment.')
import DateTime
request = context.REQUEST
students = context.portal_url.getPortalObject().campus.students

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

student = getattr(students,student_id)
sbrain = context.students_catalog(id=student_id)[0]
res = context.portal_catalog(portal_type="Certificate",
                                     id = sbrain.course)
if not res:
    logger.info('"%s","certificate not found", "%s"' % (student_id,course))
    sfc = "ART"
else:    
    sfc = res[0].getPath().split('/')[-4]
info = {}
info['amount'] = "75000"
info['type_code'] = sfc
info['type_description'] = "Schoolfee %s for 2006/2007 Session" % sfc
info['pay_bill_to'] = sbrain.name
info['pay_ship_to'] = "University of Benin"
info['student_id'] = student_id
info['student_name'] = sbrain.name
info['student_email'] = sbrain.email
now = DateTime.DateTime()
info['pay_date'] = now.strftime("%d%m%y%H%M%S")
order_id = info['order_id'] = "%d" % int(now.timeTime()*1000)
info['order_id'] = "%s%s" % (student_id[1:],order_id)
if "payments" not in student.objectIds():
    student.invokeFactory('PaymentsFolder','payments')
    payments = getattr(student,'payments')
    d = {}
    d['Title'] = 'Online Payments'
    payments.getContent().edit(mapping=d)
else:
    payments = getattr(student,'payments')
p_id = "p%s" % order_id
payments.invokeFactory('Payment', p_id)
#from Products.zdb import set_trace;set_trace()
payment = getattr(payments,p_id)
d = {}
d.update(info)
d['Title'] = "%(type_description)s %(pay_date) %(amount)s N" % info
payment.getContent().edit(mapping=d)
info['callback_url'] = "%s/payments/%s/epayment_cb" % (student.absolute_url(),p_id)
logger.info('"%(student_id)s","%(type_description)s", "%(amount)s N"' % info)
return context.wema_form(info=info)
