# VBForums CodeBank > CodeBank - Visual Basic .NET >  how to make a Payment system in VB!

## mmuziek

the code is simple no passwords are needed no saving/loading just 1 small code and 3 web scripts

everything i use here what u must enter or replace i underline it

in this case i use paypal and have 1 addon in my application (or 1 pro edition)

start making a project (windows forms)

go to the settings section in your project page

add the setting (do not change this just enter as it is here)
addon  String user  false

thats it start by saving your project

ok now we are gonna make 3 php scripts and i give u 1 (config.php,paypal.php,validatekey.php) and u get (paypal.class.php) from me

ok lets start make a new text file call it config.php (d*ont forget to remove the .txt*)

enter this code and replace values with your own



```

<?php//setup SQL connection$db = "database"; // database$host = "localhost"; // most of the time localhost$username = "root"; // your username$pass = "root"; // your password?>


```

if u use a free host as ur hosting provider if u have SQL and what the details are if you dont know yet

ok now the Biggest script the payment script call it paypal.php for ordening

its a BIG code so read it fully!



```

<?php$game_name = "bakkery"; // product name change this$costs = "3.30"; // price in dollars change this$pay_pal = "bla@bla.com"; // paypal email change this// UNDER THIS LINE DO NOT EDIT IF U DONT KNOW WHAT UR DOING!require_once('paypal.class.php');  $p = new paypal_class;             $p->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';   //$p->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';                $this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];if (empty($_GET['action'])) $_GET['action'] = 'process';  switch ($_GET['action']) {       case 'process':                        $p->add_field('business', $pay_pal);      $p->add_field('return', $this_script.'?action=success');      $p->add_field('cancel_return', $this_script.'?action=cancel');      $p->add_field('notify_url', $this_script.'?action=ipn');      $p->add_field('item_name', $game_name);      $p->add_field('amount', $costs);            $p->submit_paypal_post();       //$p->dump_fields();            break;         case 'success':                      echo "<html><head><title>Success</title></head><body><h3>Thank you for your order.</h3>";      foreach ($_POST as $key => $value) { echo "$key: $value<br>"; }      echo "</body></html>";                        break;         case 'cancel':                    echo "<html><head><title>Canceled</title></head><body><h3>The order was canceled.</h3>";      echo "</body></html>";            break;         case 'ipn':                      if ($p->validate_ipn()) {          include "config.php";         mysql_connect("$host", "$username", "$pass")or die("cannot connect");         mysql_select_db("$db")or die("cannot select DB");                    $subject = 'Instant Payment Notification - Recieved Payment'; // we are emailing this to use with all the data         $to = '$pay_pal';         $body =  "An instant payment notification was successfully recieved MCG will now transact it to your paypal accound\n";         $body .= "from ".$p->ipn_data['payer_email']." on ".date('m/d/Y');         $body .= " at ".date('g:i A')."\n\nDetails:\n";                  foreach ($p->ipn_data as $key => $value) { $body .= "\n$key: $value"; }         mail($to, $subject, $body);                  $test = rand(1990345, 14235622346); //we now randomly generate a key so there are never easy keys and all are different         $test2 = rand(1990345, 14235622346);         $test3 = rand(1990345, 14235622346);         $test4 = rand(1990345, 14235622346);         $test5 = rand(1990345, 14235622346);         $mega = "$test-$test2-$test3-$test4-$test5"; // we combine then to XXXXXXX-XXXXXXXXXXX-XXXXXXX-XXXXXXXXXXX         $ip = $p->ipn_data['payer_email'];         $sql2="INSERT INTO `database`.`tablename` (`key`, `email`, `uses`, `addon`) VALUES ('$mega', '$ip', '5', '^^the addon or product name^^');"; // we are inserting the data to sql database for checking replace database and table name with your own         $result2=mysql_query($sql2);         echo "your registration info <br/>"; // this is the text user gets to see after payment         echo "your Registration key : $mega <br/>";         echo "your email : $ip <br/>";         echo "you bought : $game_name <br/>";         echo "you get an email with your information within 1 hour if not please contact us with your paypal details to $pay_pal";         $subject2 = 'Instant Payment Notification - payment complete!'; // this is sended to the customer with its details         $to2 = $p->ipn_data['payer_email'];          $body2 = " we have your payment succesfully <br/>";         $body2 .= "Registration key : $mega -- your email : $ip -- you bought : ^^the addon or product name^^ <br/>";          $body2 .= "thank you for your support buying this software addon good luck <br/>";          $body2 .= "for support contact us at $pay_pal";          mail($to2, $subject2, $body2);      }      break; }     // thats it?>


```

 replace the values where i put a ^^ on every end or where i put change this after

wow half way there! easy or not? i did al the word for u XD

now lets make the last script validatekey.php



```

<?phpinclude "config.php";ob_start();// PLEASE DO NOT EDIT THE CODE JUST THE TABLE NAMES WHEN ASKED// Connect to server and select databse.mysql_connect("$host", "$username", "$pass")or die("cannot connect");mysql_select_db("$db")or die("cannot select DB");// Define the values given by VB$myusername=$_REQUEST['mykey'];$mypassword=$_REQUEST['email'];$addon=$_REQUEST['addon'];// To protect MySQL injection$myusername = stripslashes($myusername);$mypassword = stripslashes($mypassword);$myusername = mysql_real_escape_string($myusername);$mypassword = mysql_real_escape_string($mypassword);// check if the key exists$sql="SELECT mykey,email,uses,addon FROM `!!TABLE NAME!!` WHERE mykey='$myusername' AND email='$mypassword' AND uses='5'OR uses='4' OR uses='3' OR uses='2' OR uses='1' AND addon='$addon'"; // change the table nameif (!$result=mysql_query($sql)){trigger_error(mysql_error().'<br />In query: '.$sql);}elseif(mysql_num_rows($result) == 0) {echo 'keyerror'; // if not give a key error}else{$sql2="UPDATE !!tablename!! set uses=uses-1 WHERE mykey='$myusername' AND email='$mypassword'"; // if true lower the uses count and !!change the table name!!if (!$result=mysql_query($sql2)){trigger_error(mysql_error().'<br />In query: '.$sql2);}else{    echo"keyok"; // and if that succeeded give the ok signal    }} //check that u removed the !! marks around the table name otherwise u get errorsob_end_flush();?>


```

so thats ready! now i give u the last peace of code
download

put all the files together put them on 1 folder in ur site/server and go back to VB

make a button and give it the text buy now then enter this code
replace the adress to paypal.php with your site aka www.yourdomain.com/vb/paypal.php

ok now when the user clicks the button they are pointed to the payment page

ok now the heavy duty 
make another form place 2 textboxes and a combobox and a button 
call the button register
give the combobox the same value as u gave the addon name in the paypal script ( when there are more addons make multiple paypal scripts each with different addon names)

textbox1 = key
textbox2 = email used when the used payd
combobox1 = addon selection

ok now the code open the click button 1

and enter this code


VB.Net Code:
Dim wc As New Net.WebClientDim bt() As Byte 'the returned bytes Dim html As String 'the returned HTML text   Dim email As String = TextBox1.Text      Dim mykey As String = TextBox2.Text     Dim addon As String = ComboBox1.Text    Dim fields As New Specialized.NameValueCollectionfields.Add("mykey", mykey) 'email         fields.Add("email", email) 'email          fields.Add("addon", addon) 'email          bt = wc.UploadValues("http://yourdomain.com/vb/validatekey.php", fields) 'replace the adress          html = System.Text.Encoding.ASCII.GetString(bt)          Dim returnValue As String = html          If returnValue = "keyok" And ComboBox1.Text = "extras" Then             My.Settings.addon = "true"             My.Settings.Save()             MsgBox("registration success!")             Application.Restart()         End If      If returnValue = "keyerror" Then             MsgBox("you have used your code more then 5 times or your key is invalid")         End If

ok now to see the difference go back to form1 make a label give it the text i'm not registered

open the forms code then upper dropdownbox form1.events on the right load

teh u get form1 load box now make this code


VB.Net Code:
if my.settings.addon = "true" thenlabel1.text = "i'm registered"end if

thats it now try the payment system and see that your application has been upgraded! 

rate me if u think this tutorial was or is helpfull  :Thumb:

----------


## mmuziek

please reply give feedback

----------


## formlesstree4

This should be in the codebank, but you have a PHP class as the code you're showing us with a link to a paypal.class.rar file. I haven't dl'd it to see if it's in VB.net, or if it's compiled/source code. Either way, this should be in the codebank regardless. I shall inform a moderator if one hasn't been informed already.

EDIT: Even the downloaded file is a PHP class. This code has hardly any relation to VB.net since you did not actually code a physical application that puts this to use. This actually belongs in our PHP Codebank.

----------


## mmuziek

listen up this topic is not ment for discussion about what code it is

this is intergrating Paypal and registration system in VB 8 or 10 no matter how its made

its the First time i share something and i only get Complains about nothing

----------


## Grimfort

It is good that you share your code. The example you give does not have VB.Net code, but suggests how to create the code. 
I suggest you show an example of the VB.Net code you would use to complete your example, then it would go into the codebank on something like 'Calling php Payment system from VB.Net'

----------


## si_the_geek

I have moved this thread from the 'VB.Net' forum to the 'CodeBank VB.Net' forum - and removed several posts which discussed the move, and the language involved.

It is debatable which CodeBank it should be in (VB.Net or PHP), but I think the VB.Net one will do for now.



> listen up this topic is not ment for discussion about 
> what code it is
> 
> this is intergrating Paypal and registration system in VB 8 or 10 no matter how its made
> 
> its the First time i share something and i only get Complains about nothing


In future I suggest you try to understand why people are telling you something like that, rather than simply denying it as you have done in this thread.

People thought this was nothing to do with VB because your post did not show any VB code whatsoever (you accidentally hid it by making a mistake in the code tags).  Based on the comments you were getting, you should have at least looked at your post again.

I have fixed the tags, but I have not corrected the lack of line breaks in the code (which was also caused by your mistake), so I suggest that you go up and edit the post to fix that.

----------


## mmuziek

ok but how do u guys think is my tutorial?

----------


## mmuziek

thanks for the support i will never post a tutorial here anymore as its definitely not appreciated

----------


## Grimfort

Unless someone has a need for this code, and it is quite specific, I doubt anyone would have used it in a project. Adding things to the forums are not about self-gratification, if you managed to help someone, then great, maybe you learn a few things yourself in return.

----------


## formlesstree4

> thanks for the support i will never post a tutorial here anymore as its definitely not appreciated


I've posted a few things here myself and gotten little feedback. It's not that it's not wanted/supported, it's as Grimfort said: There might not be a demand for such a tutorial.

No need to get all huffy when something you wrote/created doesn't get used by the masses here, I certainly know that all my stuff isn't used by the masses so no reason for you to get all up in arms about it.

----------


## mmuziek

sorry. i'm a autist and doesnt know when to calculate it like that.
sorry.

when my domains nameservers works u can watch all my written tutorials on www.mtut.co.cc

----------


## refreblast

Very Good! Worked flawlessy for me.  :Smilie:

----------


## mmuziek

no problem m8

----------


## v7web

Love it mmuziek, I have been looking for something similar.

many thamks

----------


## Grimfort

There you go ! 2 members have used your code, gj.

----------


## mmuziek

wel good luck for both of them.
its very usefull so i hope its working for them.

----------


## JJMPSP

I keep on getting the error, "cannot connect" in a browser, and nothing happens in the application at all.

I think everything in my php file is correct, do I need to do anything to the database ?

Thanks for this script, I would love to get it working though  :Frown:

----------


## mmuziek

youmust have a SQL DB
with a table with : mykey email uses addon

point the script to that DB and table by replacing the right information

do not do this if you dont know how SQL DB's work

----------


## Merko

> youmust have a SQL DB
> with a table with : mykey email uses addon
> 
> point the script to that DB and table by replacing the right information
> 
> do not do this if you dont know how SQL DB's work


Great code.

I'm currently making a "App Store"  (kinda wannabe apple appstore), but got stuck where i wanted people to buy points, so they could buy apps.

Maybe i can modify this code ?  :Smilie:

----------


## PiratePaul

how would you set up the SQL database for this?

----------


## mmuziek

sir i shal quote what i alraidy said



> youmust have a SQL DB
> with a table with : mykey email uses addon
> 
> point the script to that DB and table by replacing the right information
> 
> do not do this if you dont know how SQL DB's work

----------


## PiratePaul

is there away to do this with using excel spreadsheet/tables.

----------


## mmuziek

> is there away to do this with using excel spreadsheet/tables.


well if u dont even know how to make the tables.
then how are u supose to do that.
its not easy to make that such system. atleast not for me.
i have some engines that create excell data sheet from a SQL db. but thats way to big for such small script as this,

----------


## PiratePaul

> well if u dont even know how to make the tables.
> then how are u supose to do that.
> its not easy to make that such system. atleast not for me.
> i have some engines that create excell data sheet from a SQL db. but thats way to big for such small script as this,


I understand Excel I don't understand SQL DB

----------


## mmuziek

> I understand Excel I don't understand SQL DB


well i do understand both but that does not solve the fact that u need to have the experience and know how to code it so in php that it outputs/reads from excel.

wich is almost an impossible task for a normal dev. u need  Pro devs for that.
so i cannon help u further on ur problem.
its Mysql. wich is almost the same. Database/sheet > table/table > data/values

----------


## Miklogak

*+Rep Given..*

OK I think the Paypal.class.php is missing plus the database fields too. I will add them both below for you guys. And I wasnt lucky to make it work. Maybe someone else can fix it. I found out that the Paypal.php is not reacting at all, and its not writing anything into my Database. 

Below I have added the Missing MYSQL TABLES, and the Paypal.class.php:


Mysql Tables:


```

-- phpMyAdmin SQL Dump-- version 2.11.4-- http://www.phpmyadmin.net---- Host: localhost-- Generation Time: Jan 10, 2014 at 01:29 PM-- Server version: 5.1.57-- PHP Version: 5.2.17SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";---- Database: `DATABASE-NAME`---- ------------------------------------------------------------ Table structure for table `payment`--CREATE TABLE `payment` (  `mykey` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,  `email` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,  `uses` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,  `addon` varchar(255) COLLATE latin1_general_ci DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;---- Dumping data for table `payment`-- 


```

Just add the content above in text editor save it as "table.sql" or anything ending with .sql, and simply upload it to your database. Make sure you add the DATABASE name inside where it says "DATABASE-NAME"  

Now about paypal.class.php:
Muzziek sent me a the php.class.php which wasnt the correct one, but I have found another class that I blieve is the correct one. I have attached the paypal.class.php below in the attachments

Thanks anyways for your hard work and the tutorial. Your work is really appereciated.

----------

