## $Id: browser.py 15759 2019-11-06 16:49:15Z henrik $
##
## Copyright (C) 2017 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 kofacustom.nigeria.etranzact.applicantsbrowser import (
    EtranzactPageApplicant, EtranzactReceiveResponseApplicant,
    EtranzactRequestPaymentStatusPageApplicant)
from kofacustom.nigeria.etranzact.studentsbrowser import (
    EtranzactPageStudent, EtranzactReceiveResponseStudent,
    EtranzactRequestPaymentStatusPageStudent)
from kofacustom.nigeria.etranzact.payoutletbrowser import (
    EtranzactEnterPinPageStudent, EtranzactEnterPinPageApplicant,
    EtranzactQueryHistoryPageStudent, EtranzactQueryHistoryPageApplicant)
from kofacustom.nigeria.etranzact.payoutletwebservice  import NigeriaPaymentDataWebservice

# Temporarily we can use the test portal like in kofacustom.nigeria

from kofacustom.nigeria.etranzact.tests import (
    HOST, HTTPS, SECRET_KEY, TERMINAL_ID, GATEWAY_AMT)

#HOST = 'www.etranzact.net'
#HTTPS = True
#SECRET_KEY =
#TERMINAL_ID = 0570000070
#GATEWAY_AMT =
LOGO_URL = 'https://iuokada.waeup.org/static_custom/iou_logo.png'

class CustomEtranzactPageApplicant(EtranzactPageApplicant):

    host = HOST
    https = HTTPS
    secret_key = SECRET_KEY
    terminal_id = TERMINAL_ID
    logo_url = LOGO_URL
    gateway_amt = GATEWAY_AMT

    def update(self):
        # Already now it becomes an Etranzact payment. We set the net amount
        # and add the gateway amount.
        if not self.context.r_company:
            self.context.net_amt = self.context.amount_auth
            self.context.amount_auth += self.gateway_amt
            self.context.gateway_amt = self.gateway_amt
            self.context.r_company = u'etranzact'
        self.amount = "%.1f" % self.context.amount_auth
        error = self.init_update()
        if error:
            self.flash(error, type='danger')
            self.redirect(self.url(self.context, '@@index'))
            return
        return

class CustomEtranzactReceiveResponseApplicant(EtranzactReceiveResponseApplicant):

    secret_key = SECRET_KEY
    terminal_id = TERMINAL_ID

class CustomEtranzactRequestPaymentStatusPageApplicant(
    EtranzactRequestPaymentStatusPageApplicant):

    host = HOST
    https = HTTPS
    secret_key = SECRET_KEY
    terminal_id = TERMINAL_ID
    logo_url = LOGO_URL

class CustomEtranzactPageStudent(EtranzactPageStudent):

    host = HOST
    https = HTTPS
    secret_key = SECRET_KEY
    terminal_id = TERMINAL_ID
    logo_url = LOGO_URL
    gateway_amt = GATEWAY_AMT

    def update(self):
        # Already now it becomes an Etranzact payment. We set the net amount
        # and add the gateway amount.
        if not self.context.r_company:
            self.context.net_amt = self.context.amount_auth
            self.context.amount_auth += self.gateway_amt
            self.context.gateway_amt = self.gateway_amt
            self.context.r_company = u'etranzact'
        self.amount = "%.1f" % self.context.amount_auth
        error = self.init_update()
        if error:
            self.flash(error, type='danger')
            self.redirect(self.url(self.context, '@@index'))
            return
        return

class CustomEtranzactReceiveResponseStudent(EtranzactReceiveResponseStudent):

    secret_key = SECRET_KEY
    terminal_id = TERMINAL_ID

class CustomEtranzactRequestPaymentStatusPageStudent(
    EtranzactRequestPaymentStatusPageStudent):

    host = HOST
    https = HTTPS
    secret_key = SECRET_KEY
    terminal_id = TERMINAL_ID
    logo_url = LOGO_URL

# Payoutlet customizations

class CustomEtranzactEnterPinPageStudent(EtranzactEnterPinPageStudent):
    """Enter confirmation PIN and submit to `EtranzactQueryHistoryPageStudent`
    """
    gateway_amt = GATEWAY_AMT

class CustomEtranzactEnterPinPageApplicant(EtranzactEnterPinPageApplicant):
    """Enter confirmation PIN and submit to `EtranzactQueryHistoryPageApplicant`
    """
    gateway_amt = GATEWAY_AMT

class CustomEtranzactQueryHistoryPageStudent(EtranzactQueryHistoryPageStudent):
    """ Query history of Etranzact payments
    """
    terminal_id = TERMINAL_ID
    host = HOST
    https = HTTPS

class CustomEtranzactQueryHistoryPageApplicant(EtranzactQueryHistoryPageApplicant):
    """ Query history of Etranzact payments
    """
    terminal_id = TERMINAL_ID
    host = HOST
    https = HTTPS

class CustomPaymentDataWebservice(NigeriaPaymentDataWebservice):
    """A simple webservice to publish payment and payer details on request from
    accepted IP addresses without authentication.
    """
    #ACCEPTED_IP = ('195.219.3.181', '195.219.3.184')
    ACCEPTED_IP = None

    CATEGORY_MAPPING = {
        'SCHOOLFEE': ('schoolfee',),
         }

