Tutorial

Accepting donations on your website

Introduction

In this tutorial we shall be creating a website which collects donations from VirtualBank User's.

Whenever a successful donation is made, the website will collect the Payment Details sent in an IPN and then save them to a MySQL database.

Overview of how it works

Customer visits merchant website to make a donation.

Customer enters the amount they wish to donate on the Merchants website and then clicks the donate button.

Upon clicking the donate button, the customer is directed to the VirtualBank website and they are asked to first log in before confirming that they want to make the donation (payment).

When the customer has successfully completed the payment, they are directed back to the merchants’ website.

An Instant Payment Notification is sent back to the merchant to confirm that the payment/donation has been made.

Website Structure

The website shall have the following pages:

  1. index.html Page

  2. This is the first page users see when they visit our website. This page will have one field which allows Customers to enter the Amount they wish to donate before Clicking a 'Donate' button.

    ipn.php Page

    This is the the page that receives the IPN from the VirtualBank and then saves that information in a MySQL database.

  3. success_url.php Page

  4. After making a donation, a customer is directed to this page. Here we will display a ‘thank you’ message.

Assumptions

  • The website will be hosted at the URL https://www.example.com.
  • Our Merchant Mobile Number is: 263773269075

Implementation

  1. Creating the database

  2. Use the following query to create the database:

    CREATE DATABASE donations_db;
    USE donations_db;
    CREATE TABLE `donations` (
      `transaction_id` varchar(8) NOT NULL COMMENT 'Every transaction that is processed by VirtualBank has a unique Transaction ID. This is where we save it. It''s also our key field.',
      `donor_mobile_number` varchar(15) NOT NULL COMMENT 'Mobile number of donor',
      `amount` double(10,4) NOT NULL COMMENT 'Amount donated by our donor',`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Here we automatically save the exact time upon which we received the IPN'
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
            
  3. Creating index.html

  4. Paste the following code inside index.html:

    <html>
    	<body>
            <form  action="http://www.virtualbank.co.zw/api/payments.php"  name="form1" method="POST">          
                <input  type="hidden" name="recipient_number" value="263773269075" />
                <label>Amount:</label>          
                <input type="text"  name="amount"   value="Enter amount here (without the ‘$’ sign"/> 
                <br />          
                <input type="hidden"  name="ipn_url" value="http://www.example.com/ipn.php"/>            
                <input type="hidden"  name="success_url"   value="http://www.example.com/succes_url.php"/>
                <label>Comment:</label>            
                <textarea name="comment" cols="45" rows="5">Customer can enter a comment here…</textarea> 
                <br />      
                <input  type="submit" name="submitButton" value=”Donate”/>       
            </form>
    	</body>
    </html>
            
  5. Creating ipn.php

  6. Paste the following code inside ipn.php:

    <?php
        $hostname = "localhost";
        $database = "donations_db";
        $username = "username"; //MySQL username goes here
        $password = "password"; //MySQL password goes here
        $connection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 
        $insertSQL = sprintf("INSERT INTO donations_db (transaction_id, buyer, amount) VALUES (%s, %s, %s)",
        $_POST['transaction_id'], 
        $_POST['buyer'], 
        $_POST['amount']);
        
        mysql_select_db($database, $connection);
        $Result1 = mysql_query($insertSQL, $connection) or die(mysql_error());
    ?>
            
  7. Create success_url.php

  8. Just paste the following code inside success.php:

    <html>
    	<head>
    		<title>Many thanks dude</title>
    	</head>
    	<body>
    		<h1>You are seeing this page because you have made a donation to us. You’re the greatest!</h1>
    	</body>
    </html>
            

Conclusion

You should now be able to make a VirtualBank Payment when you go to http://www.example.com. After making the payment, you should be directed to http://www.example.com/success_url.php

Also note that there is nothing that can stop you from having your IPN sent to your RETURN URL

The full source code for the website that was built in this tutorial can be downloaded here