## Script (Python) "pay_online"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
# $Id: pay_interswitch.py 1892 2007-06-13 15:57:09Z joachim $
"""
pay online
"""
from urllib import urlencode
import logging
logger = logging.getLogger('Skins.pay_interswitch')
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)

sbrain = context.students_catalog(id=student_id)[0]
amount,description = context.getSchoolFee(sbrain.faculty)
info = {}
info['site_id'] = '61'
info['currency_id'] = '566'
info['amount'] = amount
info['type_code'] = sbrain.faculty
info['type_description'] = description
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['date'] = now
order_id = info['order_id'] = "%d" % int(now.timeTime()*1000)
info['order_id'] = "%s%s" % (student_id[1:],order_id)
p_id = "p%s" % order_id

#from Products.zdb import set_trace;set_trace()
if student_id is None:
    return context.REQUEST.RESPONSE.redirect("%s/srp_anonymous_view" % context.portal_url())

if True or context.portal_url().find('uniben-demo.waeup.org') >-1 or\
   context.portal_url().find('uniben.waeup.org') >-1:
    info['action'] = "https://webpay.interswitchng.com/test_paydirect/webpay/pay.aspx"
else:
    #pass
    #return None
    info['action'] = "%s/payments/%s/simulate_callback" % (student.absolute_url(),p_id)


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')
if request.has_key('epayment'):
    return context.interswitch_form(info=info)


payments.invokeFactory('Payment', p_id)
payment = getattr(payments,p_id)
wftool.doActionFor(payment,'open')
d = {}
d.update(info)
#from Products.zdb import set_trace;set_trace()
payment.getContent().edit(mapping=d)
#wftool.doActionFor(payment,'close')
#wftool.doActionFor(student,'pay_school_fee')
info['callback_url'] = "%s/payments/%s/interswitch_cb?echo=%s" % (student.absolute_url(),
                                                                  p_id,
                                                                  p_id)

logger.info('%(student_id)s initiated online school fee payment with order_id %(order_id)s and callback url %(callback_url)s' % info)
payment_fields = (('product_id','site_id'),
                  ('site_redirect_url','callback_url'),
                  ('Currency','currency_id'),
                  ('cust_id','student_id'),
                  ('cust_id_desc','type_description'),
                  ('cust_name','student_name'),
                  ('txn_ref','order_id'),
                  ('Amount','amount'),
                  )
args = {}
for arg,field in payment_fields:
    args[arg] = info[field]
xmldict = {}
xmldict['detail_ref'] = args['txn_ref'] 
xmldict['department'] = sbrain.department 
xmldict['faculty'] = sbrain.faculty
xmldict['uniben_amt'] = "%s00" % amount
xmldict['wema_amt'] = "10000"
xmldict['provider_amt'] = "35000" 
xmltext = """
<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="UNIBEN" department="%(department)s" 
faculty="%(faculty)s">
<item_detail item_id="1" item_name="UNIBEN" item_amt="%(uniben_amt)s" bank_id="16" acct_num="384950902390" /> 
<item_detail item_id="2" item_name="WEMA" item_amt="%(wema_amt)s" bank_id="16" acct_num="48398023902" />
<item_detail item_id="3" item_name="Portal Provider" item_amt="%(provider_amt)s" bank_id="47" acct_num="0980938209483" />
</item_details>
</payment_item_detail>
""" % xmldict
total = int(xmldict['uniben_amt'])+int(xmldict['wema_amt'])+int(xmldict['provider_amt'])
args['Amount'] = "%d" % (total + 15000)
args['site_name'] = "uniben.waeup.org"
args['cust_name_desc'] = "Student Name"
args['pay_item_id'] = "6100"
args['pay_item_name'] = "UNIBEN Tuition Payments"
args['payment_params'] = 'college_split'
args['xml_data'] = xmltext % info
info['args'] = args
return context.goto_interswitch_form(info=info)
url = info['action'] + "?" + urlencode(args)
request.RESPONSE.redirect(url)
