## $Id: browser.py 9288 2012-10-04 10:04:56Z henrik $
##
## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
from datetime import datetime
import httplib
import urllib
from xml.dom.minidom import parseString
import grok
from zope.component import getUtility
from waeup.kofa.browser.layout import KofaPage, UtilityView
from waeup.kofa.accesscodes import create_accesscode
from waeup.kofa.interfaces import RETURNING, CLEARED, IKofaUtils
from waeup.kofa.utils.helpers import to_timezone
from waeup.kofa.students.viewlets import ApprovePaymentActionButton as APABStudent
from waeup.kofa.applicants.viewlets import ApprovePaymentActionButton as APABApplicant
from waeup.kofa.payments.interfaces import payment_categories
from waeup.futminna.students.interfaces import ICustomStudentOnlinePayment
from waeup.futminna.applicants.interfaces import ICustomApplicantOnlinePayment
from waeup.futminna.interfaces import MessageFactory as _
PRODUCT_ID = '117'
SITE_NAME = 'futminna-kofa.waeup.org'
PROVIDER_ACCT = '0061001000021095'
PROVIDER_BANK_ID = '89'
PROVIDER_ITEM_NAME = 'BT Education'
INSTITUTION_NAME = 'FUTMinna'
CURRENCY = '566'
#QUERY_URL = 'https://webpay.interswitchng.com/paydirect/services/TransactionQueryURL.aspx'
#QUERY_URL = 'https://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryURL.aspx'
POST_ACTION = 'https://webpay.interswitchng.com/paydirect/webpay/pay.aspx'
#POST_ACTION = 'https://testwebpay.interswitchng.com/test_paydirect/webpay/pay.aspx'
HOST = 'webpay.interswitchng.com'
#HOST = 'testwebpay.interswitchng.com'
URL = '/paydirect/services/TransactionQueryWs.asmx'
#URL = '/test_paydirect/services/TransactionQueryWs.asmx'
httplib.HTTPConnection.debuglevel = 0
def SOAP_post(soap_action,xml):
"""Handles making the SOAP request.
Further reading:
http://testwebpay.interswitchng.com/test_paydirect/services/TransactionQueryWs.asmx?op=getTransactionData
"""
h = httplib.HTTPConnection(HOST)
headers={
'Host':HOST,
'Content-Type':'text/xml; charset=utf-8',
'Content-Length':len(xml),
'SOAPAction':'"%s"' % soap_action,
}
h.request('POST', URL, body=xml,headers=headers)
r = h.getresponse()
d = r.read()
if r.status!=200:
raise ValueError('Error connecting: %s, %s' % (r.status, r.reason))
return d
def get_SOAP_response(product_id, transref):
xml="""\
%s
%s
""" % (product_id, transref)
result_xml=SOAP_post("http://tempuri.org/getTransactionData",xml)
doc=parseString(result_xml)
response=doc.getElementsByTagName('getTransactionDataResult')[0].firstChild.data
return response
def query_interswitch(payment):
sr = get_SOAP_response(PRODUCT_ID, payment.p_id)
wlist = sr.split(':')
if len(wlist) != 7:
msg = _('Invalid callback: ${a}', mapping = {'a': sr})
log = 'invalid callback for payment %s: %s' % (payment.p_id, sr)
return False, msg, log
payment.r_code = wlist[0]
payment.r_desc = wlist[1]
payment.r_amount_approved = float(wlist[2]) / 100
payment.r_card_num = wlist[3]
payment.r_pay_reference = wlist[5]
payment.r_company = u'interswitch'
if payment.r_code != '00':
msg = _('Unsuccessful callback: ${a}', mapping = {'a': sr})
log = 'unsuccessful callback for payment %s: %s' % (payment.p_id, sr)
payment.p_state = 'failed'
return False, msg, log
if payment.r_amount_approved != payment.amount_auth:
msg = _('Callback amount does not match.')
log = 'wrong callback for payment %s: %s' % (payment.p_id, sr)
payment.p_state = 'failed'
return False, msg, log
if wlist[4] != payment.p_id:
msg = _('Callback transaction id does not match.')
log = 'wrong callback for payment %s: %s' % (payment.p_id, sr)
payment.p_state = 'failed'
return False, msg, log
payment.p_state = 'paid'
payment.payment_date = datetime.utcnow()
msg = _('Successful callback received')
log = 'valid callback for payment %s: %s' % (payment.p_id, sr)
return True, msg, log
class InterswitchActionButtonStudent(APABStudent):
grok.order(1)
grok.context(ICustomStudentOnlinePayment)
grok.require('waeup.payStudent')
icon = 'actionicon_pay.png'
text = _('CollegePAY')
target = 'goto_interswitch'
@property
def target_url(self):
if self.context.p_state != 'unpaid':
return ''
return self.view.url(self.view.context, self.target)
class InterswitchRequestWebserviceActionButtonStudent(APABStudent):
grok.order(2)
grok.context(ICustomStudentOnlinePayment)
grok.require('waeup.payStudent')
icon = 'actionicon_call.png'
text = _('Requery CollegePAY')
target = 'request_webservice'
class InterswitchPageStudent(KofaPage):
""" View which sends a POST request to the Interswitch
CollegePAY payment gateway.
"""
grok.context(ICustomStudentOnlinePayment)
grok.name('goto_interswitch')
grok.template('student_goto_interswitch')
grok.require('waeup.payStudent')
label = _('Submit data to CollegePAY (Interswitch Payment Gateway)')
submit_button = _('Submit')
action = POST_ACTION
site_name = SITE_NAME
currency = CURRENCY
pay_item_id = ''
product_id = PRODUCT_ID
def update(self):
#if self.context.p_state != 'unpaid':
if self.context.p_state == 'paid':
self.flash(_("Payment ticket can't be re-send to CollegePAY."))
self.redirect(self.url(self.context, '@@index'))
return
student = self.student = self.context.student
certificate = getattr(student['studycourse'],'certificate',None)
self.amount_auth = 100 * self.context.amount_auth
xmldict = {}
if certificate is not None:
xmldict['department'] = certificate.__parent__.__parent__.code
xmldict['faculty'] = certificate.__parent__.__parent__.__parent__.code
else:
xmldict['department'] = None
xmldict['faculty'] = None
self.category = payment_categories.getTermByToken(
self.context.p_category).title
tz = getUtility(IKofaUtils).tzinfo
self.local_date_time = to_timezone(
self.context.creation_date, tz).strftime("%Y-%m-%d %H:%M:%S %Z")
self.site_redirect_url = self.url(self.context, 'request_webservice')
# Provider data
xmldict['detail_ref'] = self.context.p_id
xmldict['provider_acct'] = PROVIDER_ACCT
xmldict['provider_bank_id'] = PROVIDER_BANK_ID
xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
provider_amt = 1500
xmldict['provider_amt'] = 100 * provider_amt
# Institution data
studycourse = student['studycourse']
xmldict['institution_acct'] = ''
xmldict['institution_bank_id'] = ''
xmldict['institution_acct'] = '000000000000'
xmldict['institution_bank_id'] = '00'
xmldict['institution_amt'] = 100 * (self.context.amount_auth - provider_amt - 300)
if self.context.student.current_mode in ('pg_ft'):
self.pay_item_id = "11703"
elif self.context.student.state == CLEARED and \
self.context.student.current_level == 100:
self.pay_item_id = "11700"
elif self.context.student.state == CLEARED and \
self.context.student.current_level == 200:
self.pay_item_id = "11701"
elif self.context.student.state == CLEARED and \
self.context.student.current_level == 300:
self.pay_item_id = "11702"
elif self.context.student.state == RETURNING and \
self.context.student.current_level in (100,110):
self.pay_item_id = "11701"
elif self.context.student.state == RETURNING and \
self.context.student.current_level in (200,210):
self.pay_item_id = "11702"
elif self.context.student.state == RETURNING and \
self.context.student.current_level in (300,310):
self.pay_item_id = "11703"
elif self.context.student.state == RETURNING and \
self.context.student.current_level in (400,410,500,510,600):
self.pay_item_id = "11704"
if self.context.student.current_mode == 'jm_ft':
xmldict['institution_acct'] = "000000000000"
xmldict['institution_bank_id'] = '0'
elif self.context.student.current_mode == 'pg_ft':
xmldict['institution_acct'] = "2522040000134"
xmldict['institution_bank_id'] = '8'
elif self.context.student.state == CLEARED and \
self.context.student.current_level == 100:
xmldict['institution_acct'] = "0030001000017110"
xmldict['institution_bank_id'] = '89'
elif self.context.student.state == CLEARED and \
self.context.student.current_level == 200:
xmldict['institution_acct'] = "0591201005146"
xmldict['institution_bank_id'] = '101'
elif self.context.student.state == CLEARED and \
self.context.student.current_level == 300:
xmldict['institution_acct'] = "6013803345"
xmldict['institution_bank_id'] = '117'
elif self.context.student.state == RETURNING and \
self.context.student.current_level in (100,110):
xmldict['institution_acct'] = "0591201005146"
xmldict['institution_bank_id'] = '101'
elif self.context.student.current_level in (200,210):
xmldict['institution_acct'] = "6013803345"
xmldict['institution_bank_id'] = '117'
elif self.context.student.current_level in (300,310):
xmldict['institution_acct'] = "2522040000134"
xmldict['institution_bank_id'] = '8'
#Change of acct from UBA to GTBank
elif self.context.student.current_level in (400,410,500,510,600):
xmldict['institution_acct'] = "3713516838111"
xmldict['institution_bank_id'] = '10'
xmldict['institution_item_name'] = self.context.p_category
xmldict['institution_name'] = INSTITUTION_NAME
# Interswitch amount is not part of the xml data
xmltext = """
""" % xmldict
self.xml_data = """""" % xmltext
return
class InterswitchPaymentRequestWebservicePageStudent(UtilityView, grok.View):
""" Request webservice view for the CollegePAY gateway
"""
grok.context(ICustomStudentOnlinePayment)
grok.name('request_webservice')
grok.require('waeup.payStudent')
def update(self):
ob_class = self.__implemented__.__name__
if self.context.p_state == 'paid':
self.flash(_('This ticket has already been paid.'))
return
student = self.context.student
import pdb; pdb.set_trace()
success, msg, log = query_interswitch(self.context)
student.writeLogMessage(self, log)
if not success:
self.flash(msg)
return
success, msg, log = self.context.doAfterStudentPayment()
if log is not None:
student.writeLogMessage(self, log)
self.flash(msg)
return
def render(self):
self.redirect(self.url(self.context, '@@index'))
return