## 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 1653 2007-03-28 02:35:25Z uli $
"""
pay online
"""
from urllib import urlencode
import logging
logger = logging.getLogger('Skins.pay_online')
import DateTime

if context.portal_membership.isAnonymousUser():
    return None

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)
##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['site_id'] = '300100100'
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 context.portal_url().find('uniben-demo.waeup.org') >-1 or\
   context.portal_url().find('uniben.waeup.org') >-1:
    info['action'] = "http://www.wemaonlinepayments.biz/payment.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.wema_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/epayment_cb" % (student.absolute_url(),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 = (('x_SiteID','site_id'),
                  ('x_Redirect_url','callback_url'),
                  ('x_CurrencyID','currency_id'),
                  ('x_PaymentCode','type_code'),
                  ('x_PayDesc','type_description'),
                  ('x_Billto','pay_bill_to'),
                  ('x_Shipto','pay_ship_to'),
                  ('x_PayerID','student_id'),
                  ('x_PayerNames','student_name'),
                  ('x_PayerEmail','student_email'),
                  ('x_OrderID','order_id'),
                  ('x_amt','amount'),
                  )
args = {}
for arg,field in payment_fields:
    args[arg] = info[field]
info['args'] = args
return context.goto_wema_form(info=info)
url = info['action'] + "?" + urlencode(args)
request.RESPONSE.redirect(url)
