Index: /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/helpers.py
===================================================================
--- /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/helpers.py	(revision 16486)
+++ /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/helpers.py	(revision 16487)
@@ -26,4 +26,5 @@
 from xml.dom.minidom import parseString
 from zope.event import notify
+from waeup.kofa.payments.interfaces import IPayer
 from kofacustom.nigeria.interfaces import MessageFactory as _
 
@@ -236,4 +237,53 @@
 # PAYDirect helper functions
 
+def create_paydirect_booking(merchant_id, payment, host, url, https):
+    p_id = payment.p_id
+    description = payment.p_category
+    amount = int(payment.amount_auth)
+    date_booked = payment.creation_date.strftime("%Y-%m-%d")
+    date_expired = "2099-12-31"
+    firstname = IPayer(payment).display_fullname.split()[0]
+    lastname = IPayer(payment).display_fullname.split()[-1]
+    id = IPayer(payment).id
+    email = IPayer(payment).email
+
+    xml="""\
+<?xml version="1.0" encoding="utf-8"?>
+<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/">
+  <soap:Body>
+    <CreateBooking xmlns="http://interswitchng.com/bookonhold">
+      <ReservationRequest>
+        <MerchantId>%s</MerchantId>
+        <Bookings>
+          <Booking>
+            <ReferenceNumber>%s%s</ReferenceNumber>
+            <Description>%s</Description>
+            <Amount>%s</Amount>
+            <DateBooked>%s</DateBooked>
+            <DateExpired>%s</DateExpired>
+            <FirstName>%s</FirstName>
+            <LastName>%s</LastName>
+            <Email>%s</Email>
+            <ItemCode>%s</ItemCode>
+          </Booking>
+        </Bookings>
+      </ReservationRequest>
+    </CreateBooking>
+  </soap:Body>
+</soap:Envelope>""" % (
+    merchant_id, merchant_id, p_id[1:],
+    description, amount,
+    date_booked, date_expired,
+    firstname, lastname,
+    email, p_id)
+    response=SOAP_post(
+        "http://interswitchng.com/bookonhold/CreateBooking",
+        xml, host, url, https)
+    if response.status!=200:
+        error = 'Connection error (%s, %s)' % (response.status, response.reason)
+        return error
+    result_xml = response.read()
+    return result_xml
+
 def get_SOAP_response_paydirect(merchant_id, p_id, host, url, https):
     xml="""\
Index: /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/paydirectbrowser.py
===================================================================
--- /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/paydirectbrowser.py	(revision 16486)
+++ /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/paydirectbrowser.py	(revision 16487)
@@ -21,4 +21,5 @@
 from zope.component import getUtility
 from zope.security import checkPermission
+from xml.dom.minidom import parseString
 from waeup.kofa.interfaces import IKofaUtils
 from waeup.kofa.utils.helpers import to_timezone
@@ -29,10 +30,13 @@
 from waeup.kofa.students.browser import StudentBasePDFFormPage
 from waeup.kofa.applicants.browser import OnlinePaymentDisplayFormPage as OPDPApplicant
+from waeup.kofa.applicants.browser import ApplicantBaseDisplayFormPage
 from kofacustom.nigeria.interswitch.helpers import (
-    query_interswitch, write_payments_log, fetch_booking_details)
+    query_interswitch, write_payments_log,
+    fetch_booking_details, create_paydirect_booking)
 from kofacustom.nigeria.payments.interfaces import INigeriaOnlinePayment
 from kofacustom.nigeria.students.interfaces import INigeriaStudentOnlinePayment
 from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
-from kofacustom.nigeria.interswitch.tests import PAYDIRECT_URL, PAYDIRECT_HOST, MERCHANT_ID
+from kofacustom.nigeria.interswitch.tests import (
+    PAYDIRECT_URL, PAYDIRECT_HOST, MERCHANT_ID)
 from kofacustom.nigeria.interfaces import MessageFactory as _
 
@@ -106,9 +110,18 @@
         if self.context.r_company and self.context.r_company != 'interswitch':
             return _("Payment ticket has been used for another payment gateway.")
-        tz = getUtility(IKofaUtils).tzinfo
-        time_delta = datetime.utcnow() - self.context.creation_date
-        if time_delta.days > 7:
-            return _("This payment ticket is too old. Please create a new ticket.")
         self.ref_number = self.merchant_id + self.context.p_id[1:]
+        # Create a PAYDirect Booking
+        if not self.context.r_company:
+            result_xml = create_paydirect_booking(
+                self.merchant_id, self.context, self.gateway_host,
+                self.gateway_url, True)
+            if result_xml.startswith('Connection error'):
+                return result_xml
+            doc=parseString(result_xml)
+            if not doc.getElementsByTagName('ResponseCode'):
+                return _('Invalid callback from Interswitch')
+            rc = doc.getElementsByTagName('ResponseCode')[0].firstChild.data
+            if rc != '100':
+                return 'Error response code from Interswitch: %s' % rc
         return
 
@@ -198,7 +211,7 @@
         return
 
-class PaymentRefNumberSlipActionButton(ManageActionButton):
-    grok.order(1)  # This button should always be the last one.
-    grok.context(INigeriaOnlinePayment)
+class StudentPaymentRefNumberSlipActionButton(ManageActionButton):
+    grok.order(1)
+    grok.context(INigeriaStudentOnlinePayment)
     grok.view(PAYDirectPageStudent)
     grok.require('waeup.viewStudent')
@@ -213,8 +226,13 @@
         return self.view.url(self.view.context, self.target)
 
-class RefNumberSlipStudent(UtilityView, grok.View):
+class ApplicantPaymentRefNumberSlipActionButton(StudentPaymentRefNumberSlipActionButton):
+    grok.context(INigeriaApplicantOnlinePayment)
+    grok.view(PAYDirectPageApplicant)
+    grok.require('waeup.viewApplication')
+
+class StudentRefNumberSlip(UtilityView, grok.View):
     """Deliver a PDF slip of the context.
     """
-    grok.context(INigeriaOnlinePayment)
+    grok.context(INigeriaStudentOnlinePayment)
     grok.name('refnumberslip.pdf')
     grok.require('waeup.viewStudent')
@@ -256,2 +274,25 @@
             self.context.student, studentview, note=self.note,
             omit_fields=self.omit_fields)
+
+class ApplicantRefNumberSlip(StudentRefNumberSlip):
+    """Deliver a PDF slip of the context.
+    """
+    grok.context(INigeriaApplicantOnlinePayment)
+    grok.require('waeup.viewApplication')
+
+    @property
+    def note(self):
+        return """<br /><br />
+Go to your bank and make your PAYDirect payment with the reference number <strong>%s</strong>.
+""" % self.refnumber
+    def render(self):
+        if self.context.p_state == 'paid':
+            self.flash('Payment has already been made.')
+            self.redirect(self.url(self.context))
+            return
+        applicantview = ApplicantBaseDisplayFormPage(self.context.__parent__,
+            self.request)
+        students_utils = getUtility(IStudentsUtils)
+        return students_utils.renderPDF(self, 'refnumberslip.pdf',
+            self.context.__parent__, applicantview, note=self.note,
+            omit_fields=self.omit_fields)
Index: /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py
===================================================================
--- /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py	(revision 16486)
+++ /main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/interswitch/tests.py	(revision 16487)
@@ -17,4 +17,5 @@
 ##
 import os
+import random
 from datetime import datetime, timedelta, date
 from zope.component import createObject, getUtility
@@ -28,5 +29,7 @@
 from waeup.kofa.browser.tests.test_pdf import samples_dir
 from kofacustom.nigeria.interswitch.helpers import (
-    query_interswitch, get_SOAP_response_paydirect, SOAP_post)
+    query_interswitch, get_SOAP_response_paydirect, SOAP_post,
+    create_paydirect_booking
+    )
 from kofacustom.nigeria.testing import FunctionalLayer
 
@@ -227,49 +230,18 @@
 # PAYDirect tests
 
-    def create_booking(self, p_id):
-        xml="""\
-<?xml version="1.0" encoding="utf-8"?>
-<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/">
-  <soap:Body>
-    <CreateBooking xmlns="http://interswitchng.com/bookonhold">
-      <ReservationRequest>
-        <MerchantId>%s</MerchantId>
-        <Bookings>
-          <Booking>
-            <ReferenceNumber>%s%s</ReferenceNumber>
-            <ResponseCode>01</ResponseCode>
-            <ResponseDescription>02</ResponseDescription>
-            <Description>My Description</Description>
-            <Amount>1000</Amount>
-            <DateBooked>2021-05-05</DateBooked>
-            <DateExpired>2025-05-05</DateExpired>
-            <FirstName>Dummy</FirstName>
-            <LastName>Student</LastName>
-            <Status>string</Status>
-            <PaymentStatus>string</PaymentStatus>
-            <PaymentReference>string</PaymentReference>
-            <ChannelName>04</ChannelName>
-            <TerminalId>05</TerminalId>
-            <Location>string</Location>
-            <PaymentDate>2020-05-05</PaymentDate>
-            <PaymentMethod>string</PaymentMethod>
-            <Email>aa@aa.aa</Email>
-            <ItemCode>01</ItemCode>
-          </Booking>
-        </Bookings>
-      </ReservationRequest>
-    </CreateBooking>
-  </soap:Body>
-</soap:Envelope>""" % (MERCHANT_ID, MERCHANT_ID, p_id[1:])
-
-        SOAP_post("http://interswitchng.com/bookonhold/CreateBooking",xml,
-                  PAYDIRECT_HOST, PAYDIRECT_URL, True)
-
     @external_test_2
-    def test_get_SOAP_response_paydirect(self):
-        p_id = 'p6021570467807'
-        self.create_booking(p_id)
+    def test_SOAP_paydirect(self):
+        payment = createObject('waeup.StudentOnlinePayment')
+        payment.p_category = u'schoolfee'
+        payment.p_session = self.student.current_session
+        payment.p_item = u'My Certificate'
+        payment.p_id = u'p1234'
+        payment.p_id = 'p' + str(random.randint(10000000000, 90000000000))
+        payment.amount_auth = 1000.0
+        self.student['payments'][payment.p_id] = payment
+        result_xml = create_paydirect_booking(
+            MERCHANT_ID, payment, PAYDIRECT_HOST, PAYDIRECT_URL, True)
         result_xml = get_SOAP_response_paydirect(
-            MERCHANT_ID, p_id, PAYDIRECT_HOST, PAYDIRECT_URL, True)
+            MERCHANT_ID, payment.p_id, PAYDIRECT_HOST, PAYDIRECT_URL, True)
         doc=parseString(result_xml)
         status=doc.getElementsByTagName('PaymentStatus')[0].firstChild.data
@@ -315,11 +287,8 @@
         self.assertTrue(ref_number in self.browser.contents)
         self.browser.getControl("Requery").click()
-        self.assertTrue('Your payment %s was not found.' % value in self.browser.contents)
-        self.create_booking(value)
-        self.browser.getLink("Pay via Interswitch PAYDirect").click()
-        self.browser.getControl("Requery").click()
         self.assertTrue('pending' in self.browser.contents)
         # Reference number has still not been saved because the element was empty
         self.assertEqual(self.student['payments'][value].r_pay_reference, None)
+
         # Here we have to stop testing because we cannot create completed bookings
 
