Ignore:
Timestamp:
20 May 2021, 10:54:17 (3 years ago)
Author:
Henrik Bettermann
Message:

A paydirect booking must be created before students can go to their bank.

Location:
main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/helpers.py

    r16484 r16487  
    2626from xml.dom.minidom import parseString
    2727from zope.event import notify
     28from waeup.kofa.payments.interfaces import IPayer
    2829from kofacustom.nigeria.interfaces import MessageFactory as _
    2930
     
    236237# PAYDirect helper functions
    237238
     239def create_paydirect_booking(merchant_id, payment, host, url, https):
     240    p_id = payment.p_id
     241    description = payment.p_category
     242    amount = int(payment.amount_auth)
     243    date_booked = payment.creation_date.strftime("%Y-%m-%d")
     244    date_expired = "2099-12-31"
     245    firstname = IPayer(payment).display_fullname.split()[0]
     246    lastname = IPayer(payment).display_fullname.split()[-1]
     247    id = IPayer(payment).id
     248    email = IPayer(payment).email
     249
     250    xml="""\
     251<?xml version="1.0" encoding="utf-8"?>
     252<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
     253  <soap:Body>
     254    <CreateBooking xmlns="http://interswitchng.com/bookonhold">
     255      <ReservationRequest>
     256        <MerchantId>%s</MerchantId>
     257        <Bookings>
     258          <Booking>
     259            <ReferenceNumber>%s%s</ReferenceNumber>
     260            <Description>%s</Description>
     261            <Amount>%s</Amount>
     262            <DateBooked>%s</DateBooked>
     263            <DateExpired>%s</DateExpired>
     264            <FirstName>%s</FirstName>
     265            <LastName>%s</LastName>
     266            <Email>%s</Email>
     267            <ItemCode>%s</ItemCode>
     268          </Booking>
     269        </Bookings>
     270      </ReservationRequest>
     271    </CreateBooking>
     272  </soap:Body>
     273</soap:Envelope>""" % (
     274    merchant_id, merchant_id, p_id[1:],
     275    description, amount,
     276    date_booked, date_expired,
     277    firstname, lastname,
     278    email, p_id)
     279    response=SOAP_post(
     280        "http://interswitchng.com/bookonhold/CreateBooking",
     281        xml, host, url, https)
     282    if response.status!=200:
     283        error = 'Connection error (%s, %s)' % (response.status, response.reason)
     284        return error
     285    result_xml = response.read()
     286    return result_xml
     287
    238288def get_SOAP_response_paydirect(merchant_id, p_id, host, url, https):
    239289    xml="""\
  • main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/paydirectbrowser.py

    r16484 r16487  
    2121from zope.component import getUtility
    2222from zope.security import checkPermission
     23from xml.dom.minidom import parseString
    2324from waeup.kofa.interfaces import IKofaUtils
    2425from waeup.kofa.utils.helpers import to_timezone
     
    2930from waeup.kofa.students.browser import StudentBasePDFFormPage
    3031from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant
     32from waeup.kofa.applicants.browser import ApplicantBaseDisplayFormPage
    3133from kofacustom.nigeria.interswitch.helpers import (
    32     query_interswitch, write_payments_log, fetch_booking_details)
     34    query_interswitch, write_payments_log,
     35    fetch_booking_details, create_paydirect_booking)
    3336from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
    3437from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment
    3538from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
    36 from kofacustom.nigeria.interswitch.tests import PAYDIRECT_URL, PAYDIRECT_HOST, MERCHANT_ID
     39from kofacustom.nigeria.interswitch.tests import (
     40    PAYDIRECT_URL, PAYDIRECT_HOST, MERCHANT_ID)
    3741from kofacustom.nigeria.interfaces import MessageFactory as _
    3842
     
    106110        if self.context.r_company and self.context.r_company != 'interswitch':
    107111            return _("Payment ticket has been used for another payment gateway.")
    108         tz = getUtility(IKofaUtils).tzinfo
    109         time_delta = datetime.utcnow() - self.context.creation_date
    110         if time_delta.days > 7:
    111             return _("This payment ticket is too old. Please create a new ticket.")
    112112        self.ref_number = self.merchant_id + self.context.p_id[1:]
     113        # Create a PAYDirect Booking
     114        if not self.context.r_company:
     115            result_xml = create_paydirect_booking(
     116                self.merchant_id, self.context, self.gateway_host,
     117                self.gateway_url, True)
     118            if result_xml.startswith('Connection error'):
     119                return result_xml
     120            doc=parseString(result_xml)
     121            if not doc.getElementsByTagName('ResponseCode'):
     122                return _('Invalid callback from Interswitch')
     123            rc = doc.getElementsByTagName('ResponseCode')[0].firstChild.data
     124            if rc != '100':
     125                return 'Error response code from Interswitch: %s' % rc
    113126        return
    114127
     
    198211        return
    199212
    200 class PaymentRefNumberSlipActionButton(ManageActionButton):
    201     grok.order(1)  # This button should always be the last one.
    202     grok.context(INigeriaOnlinePayment)
     213class StudentPaymentRefNumberSlipActionButton(ManageActionButton):
     214    grok.order(1)
     215    grok.context(INigeriaStudentOnlinePayment)
    203216    grok.view(PAYDirectPageStudent)
    204217    grok.require('waeup.viewStudent')
     
    213226        return self.view.url(self.view.context, self.target)
    214227
    215 class RefNumberSlipStudent(UtilityView, grok.View):
     228class ApplicantPaymentRefNumberSlipActionButton(StudentPaymentRefNumberSlipActionButton):
     229    grok.context(INigeriaApplicantOnlinePayment)
     230    grok.view(PAYDirectPageApplicant)
     231    grok.require('waeup.viewApplication')
     232
     233class StudentRefNumberSlip(UtilityView, grok.View):
    216234    """Deliver a PDF slip of the context.
    217235    """
    218     grok.context(INigeriaOnlinePayment)
     236    grok.context(INigeriaStudentOnlinePayment)
    219237    grok.name('refnumberslip.pdf')
    220238    grok.require('waeup.viewStudent')
     
    256274            self.context.student, studentview, note=self.note,
    257275            omit_fields=self.omit_fields)
     276
     277class ApplicantRefNumberSlip(StudentRefNumberSlip):
     278    """Deliver a PDF slip of the context.
     279    """
     280    grok.context(INigeriaApplicantOnlinePayment)
     281    grok.require('waeup.viewApplication')
     282
     283    @property
     284    def note(self):
     285        return """<br /><br />
     286Go to your bank and make your PAYDirect payment with the reference number <strong>%s</strong>.
     287""" % self.refnumber
     288    def render(self):
     289        if self.context.p_state == 'paid':
     290            self.flash('Payment has already been made.')
     291            self.redirect(self.url(self.context))
     292            return
     293        applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__,
     294            self.request)
     295        students_utils = getUtility(IStudentsUtils)
     296        return students_utils.renderPDF(self, 'refnumberslip.pdf',
     297            self.context.__parent__, applicantview, note=self.note,
     298            omit_fields=self.omit_fields)
  • main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py

    r16484 r16487  
    1717##
    1818import os
     19import random
    1920from datetime import datetime, timedelta, date
    2021from zope.component import createObject, getUtility
     
    2829from waeup.kofa.browser.tests.test_pdf import samples_dir
    2930from kofacustom.nigeria.interswitch.helpers import (
    30     query_interswitch, get_SOAP_response_paydirect, SOAP_post)
     31    query_interswitch, get_SOAP_response_paydirect, SOAP_post,
     32    create_paydirect_booking
     33    )
    3134from kofacustom.nigeria.testing import FunctionalLayer
    3235
     
    227230# PAYDirect tests
    228231
    229     def create_booking(self, p_id):
    230         xml="""\
    231 <?xml version="1.0" encoding="utf-8"?>
    232 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    233   <soap:Body>
    234     <CreateBooking xmlns="http://interswitchng.com/bookonhold">
    235       <ReservationRequest>
    236         <MerchantId>%s</MerchantId>
    237         <Bookings>
    238           <Booking>
    239             <ReferenceNumber>%s%s</ReferenceNumber>
    240             <ResponseCode>01</ResponseCode>
    241             <ResponseDescription>02</ResponseDescription>
    242             <Description>My Description</Description>
    243             <Amount>1000</Amount>
    244             <DateBooked>2021-05-05</DateBooked>
    245             <DateExpired>2025-05-05</DateExpired>
    246             <FirstName>Dummy</FirstName>
    247             <LastName>Student</LastName>
    248             <Status>string</Status>
    249             <PaymentStatus>string</PaymentStatus>
    250             <PaymentReference>string</PaymentReference>
    251             <ChannelName>04</ChannelName>
    252             <TerminalId>05</TerminalId>
    253             <Location>string</Location>
    254             <PaymentDate>2020-05-05</PaymentDate>
    255             <PaymentMethod>string</PaymentMethod>
    256             <Email>aa@aa.aa</Email>
    257             <ItemCode>01</ItemCode>
    258           </Booking>
    259         </Bookings>
    260       </ReservationRequest>
    261     </CreateBooking>
    262   </soap:Body>
    263 </soap:Envelope>""" % (MERCHANT_ID, MERCHANT_ID, p_id[1:])
    264 
    265         SOAP_post("http://interswitchng.com/bookonhold/CreateBooking",xml,
    266                   PAYDIRECT_HOST, PAYDIRECT_URL, True)
    267 
    268232    @external_test_2
    269     def test_get_SOAP_response_paydirect(self):
    270         p_id = 'p6021570467807'
    271         self.create_booking(p_id)
     233    def test_SOAP_paydirect(self):
     234        payment = createObject('waeup.StudentOnlinePayment')
     235        payment.p_category = u'schoolfee'
     236        payment.p_session = self.student.current_session
     237        payment.p_item = u'My Certificate'
     238        payment.p_id = u'p1234'
     239        payment.p_id = 'p' + str(random.randint(10000000000, 90000000000))
     240        payment.amount_auth = 1000.0
     241        self.student['payments'][payment.p_id] = payment
     242        result_xml = create_paydirect_booking(
     243            MERCHANT_ID, payment, PAYDIRECT_HOST, PAYDIRECT_URL, True)
    272244        result_xml = get_SOAP_response_paydirect(
    273             MERCHANT_ID, p_id, PAYDIRECT_HOST, PAYDIRECT_URL, True)
     245            MERCHANT_ID, payment.p_id, PAYDIRECT_HOST, PAYDIRECT_URL, True)
    274246        doc=parseString(result_xml)
    275247        status=doc.getElementsByTagName('PaymentStatus')[0].firstChild.data
     
    315287        self.assertTrue(ref_number in self.browser.contents)
    316288        self.browser.getControl("Requery").click()
    317         self.assertTrue('Your payment %s was not found.' % value in self.browser.contents)
    318         self.create_booking(value)
    319         self.browser.getLink("Pay via Interswitch PAYDirect").click()
    320         self.browser.getControl("Requery").click()
    321289        self.assertTrue('pending' in self.browser.contents)
    322290        # Reference number has still not been saved because the element was empty
    323291        self.assertEqual(self.student['payments'][value].r_pay_reference, None)
     292
    324293        # Here we have to stop testing because we cannot create completed bookings
    325294
Note: See TracChangeset for help on using the changeset viewer.