## $Id: browser.py 16954 2022-05-18 05:47:33Z 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
##
import httplib
import hashlib
import grok
from waeup.kofa.interfaces import CLEARED, RETURNING
from kofacustom.nigeria.interswitch.browser import (
    InterswitchPaymentRequestWebservicePageApplicant,
    InterswitchPaymentRequestWebservicePageStudent,
    InterswitchPaymentVerifyWebservicePageApplicant,
    InterswitchPaymentVerifyWebservicePageStudent,
    InterswitchPageStudent, InterswitchPageApplicant,
    )
from kofacustom.dspg.students.interfaces import ICustomStudentOnlinePayment
from kofacustom.dspg.applicants.interfaces import ICustomApplicantOnlinePayment
from kofacustom.dspg.interfaces import MessageFactory as _
from kofacustom.dspg.students.utils import (
    MICROSOFT_FEE, DEVELOP_FEE, TECH_FEE)

PRODUCT_ID = '7269' # must be provided by Interswitch
SITE_NAME = 'dspg.waeup.org'
PROVIDER_ACCT = '2028964403'
PROVIDER_BANK_ID = '8'
PROVIDER_ITEM_NAME = 'WAEAC'
INSTITUTION_NAME = 'Delta State Polytechnic Ogwashi-Uku'
CURRENCY = '566'
GATEWAY_AMT = 250.0
MAC = 'D43ED93B7A307B152C7111D04E1F384428057CD1E75428D3A4C5631C28ACB9C8C822A88CA76DEDC5F9A520CF4BFDB2824FF8B207AAFC667DF3CBB13685B66627'

POST_ACTION = 'https://webpay.interswitchng.com/paydirect/pay'
#POST_ACTION = 'https://sandbox.interswitchng.com/webpay/pay'
HOST = 'webpay.interswitchng.com'
#HOST = 'sandbox.interswitchng.com'
URL = '/paydirect/api/v1/gettransaction.json'
#URL = '/webpay/api/v1/gettransaction.json'

httplib.HTTPSConnection.debuglevel = 0
HTTPS = True

SPECIAL_PAYMENT_PARAMS = {

    'carryover1': ('101', 500.0, '1015220292', '117'),
    'carryover4': ('101', 500.0, '1015220292', '117'),
    'carryover5': ('101', 500.0, '1015220292', '117'),
    'carryover6': ('101', 500.0, '1015220292', '117'),
    'carryover7': ('101', 500.0, '1015220292', '117'),
    'carryover8': ('101', 500.0, '1015220292', '117'),
    'carryover9': ('101', 500.0, '1015220292', '117'),
    'carryover10': ('101', 500.0, '1015220292', '117'),
    'carryover11': ('101', 500.0, '1015220292', '117'),
    'carryover12': ('101', 500.0, '1015220292', '117'),
    'balance': ('101', 500.0, '1015220292', '117'),

    'certificate': ('111', 500.0, '1012808851', '117'),
    'state_result': ('112', 500.0, '1012808851', '117'),
    'transcript_local': ('113', 500.0, '1012808851', '117'),
    'transcript_foreign': ('114', 500.0, '1012808851', '117'),
    'ver_result': ('115', 500.0, '1012808851', '117'),
    'change_course': ('116', 500.0, '1012808851', '117'),
    'change_inst': ('117', 500.0, '1012808851', '117'),
    'jamb_reject': ('118', 500.0, '1012808851', '117'),
    'cert_of_cert': ('119', 500.0, '1012808851', '117'),
    'ref_let': ('120', 500.0, '1012808851', '117'),
    'proc_cert': ('121', 500.0, '1012808851', '117'),
    'loss_idcard': ('122', 9.0, '1012808851', '117'),
    'loss_examcard': ('123', 4.5, '1012808851', '117'),
    'loss_result': ('124', 500.0, '1012808851', '117'),
    'loss_receipt': ('125', 500.0, '1012808851', '117'),
    'loss_clearance': ('126', 500.0, '1012808851', '117'),
    'conv_brochure': ('127', 500.0, '1012808851', '117'),
    'hnd_certificate': ('128', 500.0, '1012808851', '117'),
    'hnd_state_result': ('129', 500.0, '1012808851', '117'),
    'hnd_transcript_local': ('130', 500.0, '1012808851', '117'),
    'hnd_transcript_foreign': ('131', 500.0, '1012808851', '117'),
    'staff_loss_idcard': ('132', 9.0, '1012808851', '117'),
    'hnd_conv_brochure': ('133', 500.0, '1012808851', '117'),
    'pgd_state_result': ('134', 500.0, '1012808851', '117'),
    'pgd_conv_brochure': ('135', 500.0, '1012808851', '117'),
    'pgd_certificate': ('136', 500.0, '1012808851', '117'),
    'log_book': ('137', 4.5, '1012808851', '117'),
    'pgd_transcript_foreign': ('138', 500.0, '1012808851', '117'),
    'pgd_transcript_local': ('139', 500.0, '1012808851', '117'),
    'jamb_regularization': ('140', 500.0, '1012808851', '117'),
    'utme_registration': ('142', 9.0, '1012808851', '117'),
    'utme_cbt': ('143', 9.0, '1012808851', '117'),
    'nysc_id_card': ('144', 9.0, '1012808851', '117'),
    'ijmb_result': ('147', 500.0, '1012808851', '117'),
    'conv': ('151', 500.0, '1012808851', '117'),
    }


# Not yet readily configured. Bank account numbers are
# contradictory. pay_item_ids are not yet assigned.

class CustomInterswitchPageStudent(InterswitchPageStudent):
    """ View which sends a POST request to the Interswitch
    CollegePAY payment gateway.
    """
    grok.context(ICustomStudentOnlinePayment)
    action = POST_ACTION
    site_name = SITE_NAME
    currency = CURRENCY
    product_id = PRODUCT_ID
    mac = MAC
    pay_item_id = '000'

    def update(self):
        error = self.init_update()
        if error:
            self.flash(error, type='danger')
            self.redirect(self.url(self.context, '@@index'))
            return
        self.context.r_company = u'interswitch'
        student = self.student
        xmldict = self.xmldict
        # 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
        # Institution data
        xmldict['institution_acct'] = '00000000'
        xmldict['institution_bank_id'] = '00'
        xmldict['institution_amt'] = '0.0'
        provider_amt = 0.0
        tech_fee = 0.0
        microsoft_fee = 0.0
        develop_fee = 0.0
        if self.context.p_category.startswith('schoolfee'):
            self.pay_item_id = '102'
            if self.context.p_item != 'Balance':
                provider_amt = 2800.0
                tech_fee = TECH_FEE(student)
                develop_fee = DEVELOP_FEE(student)
                microsoft_fee = MICROSOFT_FEE(student)
            if student.faccode == 'SPAT':
                xmldict['institution_acct'] = '4011034703'
                xmldict['institution_bank_id'] = '51'
            elif student.current_mode in ('nd_ft', 'hnd_ft'):
                xmldict['institution_acct'] = '2004145840'
                xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'clearance':
            self.pay_item_id = '103'
            if self.context.p_item != 'Balance':
                provider_amt = 500.0
            if student.faccode == 'SPAT':
                xmldict['institution_acct'] = '1015220292'
                xmldict['institution_bank_id'] = '117'
            elif student.current_mode in ('nd_ft', 'hnd_ft'):
                xmldict['institution_acct'] = '0072223654'
                xmldict['institution_bank_id'] = '121'
        elif self.context.p_category == 'gown':
            self.pay_item_id = '102' # We use school fee item id.
            xmldict['institution_acct'] = '2006612782'
            xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'sports':
            self.pay_item_id = '110'
            xmldict['institution_acct'] = '6060300688'
            xmldict['institution_bank_id'] = '51'
        elif self.context.p_category == 'siwes':
            self.pay_item_id = '111'
            xmldict['institution_acct'] = '0080527351'
            xmldict['institution_bank_id'] = '11'
        elif self.context.p_category == 'eed_1':
            self.pay_item_id = '112'
            xmldict['institution_acct'] = '0039782431'
            xmldict['institution_bank_id'] = '11'
        elif self.context.p_category == 'eed_2':
            self.pay_item_id = '116'
            xmldict['institution_acct'] = '0039782431'
            xmldict['institution_bank_id'] = '11'
        elif self.context.p_category == 'eed_extra':
            self.pay_item_id = '157'
            xmldict['institution_acct'] = '0039782431'
            xmldict['institution_bank_id'] = '11'
        elif self.context.p_category == 'olevel_results_1':
            self.pay_item_id = '113'
            xmldict['institution_acct'] = '2015327204'
            xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'olevel_results_2':
            self.pay_item_id = '115'
            xmldict['institution_acct'] = '2015327204'
            xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'gns_1':
            self.pay_item_id = '154'
            xmldict['institution_acct'] = '2017447652'
            xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'gns_2':
            self.pay_item_id = '155'
            xmldict['institution_acct'] = '2017447652'
            xmldict['institution_bank_id'] = '8'
        #elif self.context.p_category == 'hostel_maintenance':
        #    if self.context.p_item != 'Balance':
        #        provider_amt = 500.0
        #    self.pay_item_id = '117'
        #    xmldict['institution_acct'] = '1012808851'
        #    xmldict['institution_bank_id'] = '117'
        elif self.context.p_category == 'lsfp_penalty':
            self.pay_item_id = '102'
            xmldict['institution_acct'] = '1012808851'
            xmldict['institution_bank_id'] = '117'
        elif self.context.p_category == 'eng_1':
            self.pay_item_id = '118'
            xmldict['institution_acct'] = '2024473077'
            xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'eng_2':
            self.pay_item_id = '119'
            xmldict['institution_acct'] = '2024473077'
            xmldict['institution_bank_id'] = '8'
        elif self.context.p_category == 'bfn':
            self.pay_item_id = '125'
            xmldict['institution_acct'] = '0048039049'
            xmldict['institution_bank_id'] = '11'
        elif self.context.p_category == 'facilities':
            self.pay_item_id = '156'
            xmldict['institution_acct'] = '1012808851'
            xmldict['institution_bank_id'] = '117'
        elif self.context.p_category in SPECIAL_PAYMENT_PARAMS.keys():
            self.pay_item_id = SPECIAL_PAYMENT_PARAMS[self.context.p_category][0]
            provider_amt = SPECIAL_PAYMENT_PARAMS[self.context.p_category][1]
            xmldict['institution_acct'] = SPECIAL_PAYMENT_PARAMS[self.context.p_category][2]
            xmldict['institution_bank_id'] = SPECIAL_PAYMENT_PARAMS[self.context.p_category][3]
        xmldict['provider_amt'] = 100 * provider_amt
        xmldict['tech_fee'] = 100 * tech_fee
        xmldict['develop_fee'] = 100 * develop_fee
        xmldict['microsoft_fee'] = 100 * microsoft_fee
        xmldict['institution_item_name'] = self.context.category
        xmldict['institution_name'] = INSTITUTION_NAME
        xmldict['institution_amt'] = institution_amt = 100 * (
            self.context.amount_auth - provider_amt - tech_fee -
            develop_fee - microsoft_fee - GATEWAY_AMT)

        if provider_amt == 0:
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
</item_details>
</payment_item_detail>""" % xmldict
        elif tech_fee == 0:   # implies develop_fee == 0
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
</item_details>
</payment_item_detail>""" % xmldict
        elif microsoft_fee == 0:
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
<item_detail item_id="3" item_name="Technology Fee" item_amt="%(tech_fee)d" bank_id="10" acct_num="0032256360" />
<item_detail item_id="4" item_name="Developmental Levy" item_amt="%(develop_fee)d" bank_id="117" acct_num="1012963301" />
</item_details>
</payment_item_detail>""" % xmldict
        else:
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
<item_detail item_id="3" item_name="Technology Fee" item_amt="%(tech_fee)d" bank_id="10" acct_num="0032256360" />
<item_detail item_id="4" item_name="Developmental Levy" item_amt="%(develop_fee)d" bank_id="117" acct_num="1012963301" />
<item_detail item_id="5" item_name="Microsoft Academy Fee" item_amt="%(microsoft_fee)d" bank_id="51" acct_num="4011256518" />
</item_details>
</payment_item_detail>""" % xmldict

        # isouaba: All certificates in SPAT are part time thus any payments
        # coming from those certificates (Application, Acceptance, school fees
        # etc) 40% should be split to keystone account.
        # abraham: Please, kindly exclude the 40% going to Keystone bank for school of part-
        # time (SPAT) O'level Results Verification payment.
        if student.faccode == 'SPAT' and not self.context.p_category.startswith('olevel_results'):
            xmldict['institution_amt_2'] = 0.4 * institution_amt
            xmldict['institution_amt_1'] = 0.6 * institution_amt
            if provider_amt == 0:
                xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s 1" item_amt="%(institution_amt_1)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(institution_item_name)s 2" item_amt="%(institution_amt_2)d" bank_id="8" acct_num="2034761924" />
</item_details>
</payment_item_detail>""" % xmldict
            elif tech_fee == 0: # implies develop_fee == 0
                xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s 1" item_amt="%(institution_amt_1)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(institution_item_name)s 2" item_amt="%(institution_amt_2)d" bank_id="8" acct_num="2034761924" />
<item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
</item_details>
</payment_item_detail>""" % xmldict
            elif microsoft_fee == 0:
                xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s 1" item_amt="%(institution_amt_1)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(institution_item_name)s 2" item_amt="%(institution_amt_2)d" bank_id="8" acct_num="2034761924" />
<item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
<item_detail item_id="4" item_name="Technology Fee" item_amt="%(tech_fee)d" bank_id="10" acct_num="0032256360" />
<item_detail item_id="5" item_name="Developmental Levy" item_amt="%(develop_fee)d" bank_id="117" acct_num="1012963301" />
</item_details>
</payment_item_detail>""" % xmldict
            else:
                xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s 1" item_amt="%(institution_amt_1)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(institution_item_name)s 2" item_amt="%(institution_amt_2)d" bank_id="8" acct_num="2034761924" />
<item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
<item_detail item_id="4" item_name="Technology Fee" item_amt="%(tech_fee)d" bank_id="10" acct_num="0032256360" />
<item_detail item_id="5" item_name="Developmental Levy" item_amt="%(develop_fee)d" bank_id="117" acct_num="1012963301" />
<item_detail item_id="6" item_name="Microsoft Academy Fee" item_amt="%(microsoft_fee)d" bank_id="51" acct_num="4011256518" />
</item_details>
</payment_item_detail>""" % xmldict


        # Some payments are treated completely separately. The following lines
        # override the configuration above.

        if self.context.p_category == 'dep_sug':
            self.pay_item_id = '102' # We use school fee item id
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 2000
            xmldict['amt2'] = 100 * 1000
            xmldict['amt3'] = 100 * 900
            xmldict['amt4'] = 100 * 1000

            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="SUG" item_amt="%(amt1)d" bank_id="11" acct_num="0038079930" />
<item_detail item_id="2" item_name="Students Welfare" item_amt="%(amt2)d" bank_id="11" acct_num="0037892949" />
<item_detail item_id="3" item_name="Anti-Cult Book" item_amt="%(amt3)d" bank_id="11" acct_num="0037892949" />
<item_detail item_id="4" item_name="NADESSTU" item_amt="%(amt4)d" bank_id="11" acct_num="0036375968" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'sbs_1':
            self.pay_item_id = '120'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmldict['amt3'] = 100 * 1000

            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="SBS Dues I" item_amt="%(amt1)d" bank_id="11" acct_num="0010772626" />
<item_detail item_id="2" item_name="Journal fee" item_amt="%(amt2)d" bank_id="11" acct_num="0010772626" />
<item_detail item_id="3" item_name="ABSOBS" item_amt="%(amt3)d" bank_id="8" acct_num="2008185712" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'sbs_2':
            self.pay_item_id = '123'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="SBS Dues II" item_amt="%(amt1)d" bank_id="11" acct_num="0010772626" />
<item_detail item_id="2" item_name="ABSOBS" item_amt="%(amt2)d" bank_id="8" acct_num="2008185712" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'mkt':
            self.pay_item_id = '127'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="MKT Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0048297823" />
<item_detail item_id="2" item_name="NAMS" item_amt="%(amt2)d" bank_id="8" acct_num="2032309812" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'mcn':
            self.pay_item_id = '129'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmldict['amt3'] = 100 * 500
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="MCN Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0010789213" />
<item_detail item_id="2" item_name="AMCOS" item_amt="%(amt2)d" bank_id="8" acct_num="3020253234" />
<item_detail item_id="3" item_name="Lab. Maintenance Levy" item_amt="%(amt3)d" bank_id="11" acct_num="0040467901" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'otm':
            self.pay_item_id = '131'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="OTM Dues" item_amt="%(amt1)d" bank_id="8" acct_num="3068446702" />
<item_detail item_id="2" item_name="NAOTM" item_amt="%(amt2)d" bank_id="11" acct_num="0036652027" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'acct':
            self.pay_item_id = '134'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 3000
            xmldict['amt2'] = 100 * 1000
            xmldict['amt3'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Profession Accreditation" item_amt="%(amt1)d" bank_id="11" acct_num="0040285084" />
<item_detail item_id="2" item_name="ACCT Dues" item_amt="%(amt2)d" bank_id="11" acct_num="0036184955" />
<item_detail item_id="3" item_name="NAPAS" item_amt="%(amt3)d" bank_id="8" acct_num="2008498661" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'bam':
            self.pay_item_id = '136'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="BAM Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0010788058" />
<item_detail item_id="2" item_name="NABAS" item_amt="%(amt2)d" bank_id="8" acct_num="2006936848" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'pom':
            self.pay_item_id = '138'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="POM Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0096851444" />
<item_detail item_id="2" item_name="NAMS" item_amt="%(amt2)d" bank_id="8" acct_num="2033423003" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'hrm':
            self.pay_item_id = '140'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="HRM Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0096858546" />
<item_detail item_id="2" item_name="NAMS" item_amt="%(amt2)d" bank_id="8" acct_num="2033423003" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'ndgrad':
            self.pay_item_id = '152'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 3000
            xmldict['amt2'] = 100 * 5000
            xmldict['amt3'] = 100 * 1000
            xmldict['amt4'] = 100 * 3500
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Collection of Results" item_amt="%(amt1)d" bank_id="117" acct_num="1012808851" />
<item_detail item_id="2" item_name="Collection of Certificate" item_amt="%(amt2)d" bank_id="117" acct_num="1012808851" />
<item_detail item_id="3" item_name="Alumni" item_amt="%(amt3)d" bank_id="8" acct_num="2011810346" />
<item_detail item_id="4" item_name="Convocation" item_amt="%(amt4)d" bank_id="8" acct_num="2006612782" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'hndgrad':
            self.pay_item_id = '153'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 5000
            xmldict['amt2'] = 100 * 7500
            xmldict['amt3'] = 100 * 1000
            xmldict['amt4'] = 100 * 3500
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Collection of Results" item_amt="%(amt1)d" bank_id="117" acct_num="1012808851" />
<item_detail item_id="2" item_name="Collection of Certificate" item_amt="%(amt2)d" bank_id="117" acct_num="1012808851" />
<item_detail item_id="3" item_name="Alumni" item_amt="%(amt3)d" bank_id="8" acct_num="2011810346" />
<item_detail item_id="4" item_name="Convocation" item_amt="%(amt4)d" bank_id="8" acct_num="2006612782" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'sas_1':
            self.pay_item_id = '158'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 500
            xmldict['amt3'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="SAS Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0038956822" />
<item_detail item_id="2" item_name="Handbook Fee" item_amt="%(amt2)d" bank_id="11" acct_num="0010772626" />
<item_detail item_id="3" item_name="Association Due" item_amt="%(amt3)d" bank_id="8" acct_num="3069091989" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'sas_2':
            self.pay_item_id = '159'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="SAS Dues" item_amt="%(amt1)d" bank_id="11" acct_num="0038956822" />
<item_detail item_id="2" item_name="Association Due" item_amt="%(amt2)d" bank_id="8" acct_num="3069091989" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'com':
            self.pay_item_id = '160'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Departmental Due" item_amt="%(amt1)d" bank_id="11" acct_num="0010787594" />
<item_detail item_id="2" item_name="NACOS " item_amt="%(amt2)d" bank_id="8" acct_num="3047146881" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'sta':
            self.pay_item_id = '161'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Departmental Due" item_amt="%(amt1)d" bank_id="11" acct_num="0048794281" />
<item_detail item_id="2" item_name="NASS " item_amt="%(amt2)d" bank_id="8" acct_num="203000173" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'slt_1':
            self.pay_item_id = '162'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmldict['amt3'] = 100 * 3000
            xmldict['amt4'] = 100 * 500
            xmldict['amt5'] = 100 * 3000
            xmldict['amt6'] = 100 * 2000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Departmental Due" item_amt="%(amt1)d" bank_id="8" acct_num="2033492885" />
<item_detail item_id="2" item_name="Association Due" item_amt="%(amt2)d" bank_id="11" acct_num="0115800071" />
<item_detail item_id="3" item_name="Professional Due" item_amt="%(amt3)d" bank_id="8" acct_num="2033492885" />
<item_detail item_id="4" item_name="Prospectus" item_amt="%(amt4)d" bank_id="8" acct_num="2033492885" />
<item_detail item_id="5" item_name="Practical Manual" item_amt="%(amt4)d" bank_id="8" acct_num="2033492885" />
<item_detail item_id="6" item_name="Lab. Coat" item_amt="%(amt4)d" bank_id="8" acct_num="2033492885" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'slt_2':
            self.pay_item_id = '163'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmldict['amt3'] = 100 * 3000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Departmental Due" item_amt="%(amt1)d" bank_id="8" acct_num="2033492885" />
<item_detail item_id="2" item_name="Association Due" item_amt="%(amt2)d" bank_id="11" acct_num="0115800071" />
<item_detail item_id="3" item_name="Practical Manual" item_amt="%(amt3)d" bank_id="8" acct_num="2033492885" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'lis':
            self.pay_item_id = '164'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Departmental Due" item_amt="%(amt1)d" bank_id="" acct_num="0068264131" />
<item_detail item_id="2" item_name="Association Due" item_amt="%(amt2)d" bank_id="" acct_num="" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'htm':
            self.pay_item_id = '165'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="Departmental Due" item_amt="%(amt1)d" bank_id="" acct_num="0068264131" />
<item_detail item_id="2" item_name="Association Due" item_amt="%(amt2)d" bank_id="" acct_num="" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'pa':
            self.pay_item_id = '166'
            # amount_auth is being ignored. However, the sum of all split payments
            # must coincide with the amount_auth configured in students/utils.py
            xmldict['amt1'] = 100 * 1000
            xmldict['amt2'] = 100 * 1000
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="PAD" item_amt="%(amt1)d" bank_id="11" acct_num="0160125880" />
<item_detail item_id="2" item_name="NAPAS" item_amt="%(amt2)d" bank_id="11" acct_num="0164863395" />
</item_details>
</payment_item_detail>""" % xmldict

        if self.context.p_category == 'hostel_maintenance':
            provider_amt = 500.0
            hostel_due = 2000
            self.pay_item_id = '117'
            xmldict['institution_acct'] = '1012808851'
            xmldict['institution_bank_id'] = '117'
            xmldict['provider_amt'] = 100 * provider_amt
            xmldict['hostel_due'] = 100 * hostel_due
            xmldict['institution_amt'] = institution_amt = 100 * (
                self.context.amount_auth - provider_amt - hostel_due - GATEWAY_AMT)
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s" department="%(department)s" faculty="%(faculty)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
<item_detail item_id="3" item_name="Hostel Due" item_amt="%(hostel_due)d" bank_id="11" acct_num="0170897506" />
</item_details>
</payment_item_detail>""" % xmldict


        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
        self.context.provider_amt = provider_amt
        self.context.gateway_amt = GATEWAY_AMT
        self.context.net_amt = self.context.amount_auth - GATEWAY_AMT
        self.amount_auth = int(100 * self.context.amount_auth)
        hashargs = (
            self.context.p_id +
            PRODUCT_ID +
            self.pay_item_id +
            str(int(self.amount_auth)) +
            self.site_redirect_url +
            self.mac)
        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
        return

class CustomInterswitchPageApplicant(InterswitchPageApplicant):
    """ View which sends a POST request to the Interswitch
    CollegePAY payment gateway.
    """
    grok.context(ICustomApplicantOnlinePayment)
    action = POST_ACTION
    site_name = SITE_NAME
    currency = CURRENCY
    pay_item_id = '101' # must be provided by Interswitch
    product_id = PRODUCT_ID
    mac = MAC

    def update(self):
        error = self.init_update()
        if error:
            self.flash(error, type='danger')
            self.redirect(self.url(self.context, '@@index'))
            return
        self.context.r_company = u'interswitch'
        xmldict = {}
        provider_amt = 500.0
        gateway_amt = GATEWAY_AMT
        xmldict['institution_acct'] = '00000000'
        xmldict['institution_bank_id'] = '00'
        if self.applicant.__parent__.application_category in ('ndft', 'hndft'):
            xmldict['institution_acct'] = '0072223654'
            xmldict['institution_bank_id'] = '121'
        elif self.applicant.__parent__.application_category in (
            'ndpt', 'hndpt', 'ndwe', 'hndwe'):
            xmldict['institution_acct'] = '1015220292'
            xmldict['institution_bank_id'] = '117'
        elif self.applicant.__parent__.prefix in ('conv', 'hndfincl', 'ndfincl'):
            xmldict['institution_acct'] = '1012808851'
            xmldict['institution_bank_id'] = '117'
            self.pay_item_id = '151'
        elif self.applicant.applicant_id.startswith('special'):
            if self.context.p_category in SPECIAL_PAYMENT_PARAMS.keys():
                self.pay_item_id = SPECIAL_PAYMENT_PARAMS[self.context.p_category][0]
                provider_amt = SPECIAL_PAYMENT_PARAMS[self.context.p_category][1]
                xmldict['institution_acct'] = SPECIAL_PAYMENT_PARAMS[self.context.p_category][2]
                xmldict['institution_bank_id'] = SPECIAL_PAYMENT_PARAMS[self.context.p_category][3]
        if self.pay_item_id in ('122', '123', '132', '137', '141',
                                '142', '143', '144', '146', '147',):
                gateway_amt = round(0.015 * self.context.amount_auth, 2)
        xmldict['detail_ref'] = self.context.p_id
        xmldict['provider_amt'] = 100 * provider_amt
        xmldict['provider_acct'] = PROVIDER_ACCT
        xmldict['provider_bank_id'] = PROVIDER_BANK_ID
        xmldict['provider_item_name'] = PROVIDER_ITEM_NAME
        xmldict['institution_amt'] = institution_amt = 100 * (
            self.context.amount_auth - provider_amt - gateway_amt)
        xmldict['institution_item_name'] = self.context.category
        xmldict['institution_name'] = INSTITUTION_NAME
        xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
<item_detail item_id="1" item_name="%(institution_item_name)s" item_amt="%(institution_amt)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
</item_details>
</payment_item_detail>""" % xmldict

        # isouaba: All certificates in SPAT are part time thus any payments
        # coming from those certificates (Application, Acceptance, school fees
        # etc) 40% should be split to keystone account.
        if self.applicant.__parent__.application_category in (
            'ndpt', 'hndpt', 'ndwe', 'hndwe'):
            xmldict['institution_amt_2'] = 0.4 * institution_amt
            xmldict['institution_amt_1'] = 0.6 * institution_amt
            xmltext = """<payment_item_detail>
<item_details detail_ref="%(detail_ref)s" college="%(institution_name)s">
<item_detail item_id="1" item_name="%(institution_item_name)s 1" item_amt="%(institution_amt_1)d" bank_id="%(institution_bank_id)s" acct_num="%(institution_acct)s" />
<item_detail item_id="2" item_name="%(institution_item_name)s 2" item_amt="%(institution_amt_2)d" bank_id="8" acct_num="2034761924" />
<item_detail item_id="3" item_name="%(provider_item_name)s" item_amt="%(provider_amt)d" bank_id="%(provider_bank_id)s" acct_num="%(provider_acct)s" />
</item_details>
</payment_item_detail>""" % xmldict

        self.xml_data = """<input type="hidden" name="xml_data" value='%s'  />""" % xmltext
        self.context.provider_amt = provider_amt
        self.context.gateway_amt = GATEWAY_AMT
        self.context.net_amt = self.context.amount_auth - GATEWAY_AMT
        self.amount_auth = int(100 * self.context.amount_auth)
        hashargs = (
            self.context.p_id +
            PRODUCT_ID +
            self.pay_item_id +
            str(int(self.amount_auth)) +
            self.site_redirect_url +
            self.mac)
        self.hashvalue = hashlib.sha512(hashargs).hexdigest()
        return

class CustomInterswitchPaymentRequestWebservicePageStudent(
    InterswitchPaymentRequestWebservicePageStudent):
    """Request webservice view for the CollegePAY gateway
    """
    grok.context(ICustomStudentOnlinePayment)
    product_id = PRODUCT_ID
    gateway_host = HOST
    gateway_url = URL
    mac = MAC

class CustomInterswitchPaymentVerifyWebservicePageStudent(
    InterswitchPaymentVerifyWebservicePageStudent):
    """Payment verify view for the CollegePAY gateway
    """
    grok.context(ICustomStudentOnlinePayment)
    product_id = PRODUCT_ID
    gateway_host = HOST
    gateway_url = URL
    mac = MAC

class CustomInterswitchPaymentRequestWebservicePageApplicant(
    InterswitchPaymentRequestWebservicePageApplicant):
    """Request webservice view for the CollegePAY gateway
    """
    grok.context(ICustomApplicantOnlinePayment)
    product_id = PRODUCT_ID
    gateway_host = HOST
    gateway_url = URL
    mac = MAC

class CustomInterswitchPaymentVerifyWebservicePageApplicant(
    InterswitchPaymentVerifyWebservicePageApplicant):
    """Payment verify view for the CollegePAY gateway
    """
    grok.context(ICustomApplicantOnlinePayment)
    product_id = PRODUCT_ID
    gateway_host = HOST
    gateway_url = URL
    mac = MAC