Example4 - PayPal IPN

Top  Previous  Next



Items covered in this topic

Example Description

Changes made from the example2

The LicOptions utility

Example4 running

Added to avlockunit.pas

Added to regist.pas





* From the avlockunit.pas unit you should set your own values in the following code of the Initializatrion section:


//For PayPal IPN implementation

 paypal_url    := 'https://www.paypal.com/cgi-bin/webscr';

 paypal_email  := 'yourpaypalemail@yourdomain.com';

 currency_code := 'USD';

 notify_url    := 'http://yourdomain.com/olm6/ipn.php';

 licdef_file   := 'mylicdef.ldf';



* Into the script ipn.php replace these values:


//Please replace these values before to try with this script


$to_address   = 'yourreceiveremail@yourdomain.com';

$from_address = 'yoursenderemail@yourdomain.com';

$from_name    = 'yoursendername';

$confirmation_subject = 'Payment Confirmation';




You can place the same email address for both, $to_address and $from_address. These email addresses are to receive reports of payments from the ipn.php script.

Example Description


In this example we will see how to implement the payment through PayPal and automatic registration of the application after made payment through PayPal IPN. See more information in the section PayPal IPN.


We will build on the example 2 on which will add the necessary functionality for this case. This is an example that uses only the advanced OLM. Below we see the forms and units that are part of this example:




Changes made from the Example 2


The main form Form1 and UserData remain unchanged. In the registration form we added controls and code required to make payment via PayPal by selecting the desired setting from a list of options. The avlockunit.pas unit was also modified adding the necessary code for this case. Below we will see in more detail each of these changes.


Changes in Registration Form

Below we see the new registration form prepared for this example:




All changes in this form were made in the "Register Now" section. As can be seen in the image we have added a TPageControl control with two tabs, "With PayPal" and "With a Key" to separate the two registration alternatives we have in this example. In the first "With PayPal" we have all the functionality to register the application at real time doing the payment through Paypal. The second tab "With a Key" allows to register the application using a registration key, option that we already had in the example 2. Below we can see the two tabs deployed:




Let's see in detail now the tab "With PayPal". In the PageControl we show a list with the different variants of our application we offer to our customers, where they should select one to register. Below we can see this list when the application is running:




Here we see that the user have selected "MyApp Standard by 360 days" at a price of $ 18.00. Now you might ask, from where this list is obtained? This should not be defined in the application because we probably will need to modify it later. Do not worry, these data are in the OLM, in a plain text license definition file like you see below:


Below is the content of the licenses definition file mylicdef.ldf


MyApp Standard by 180 days|1000|2|180|00F|~MyApp Standard by 360 days|1800|3|365|00F|~MyApp Standard Forever|3000|5|65535|00F|~MyApp Professional by 180 days|2000|2|180|0FF|~MyApp Professional by 360 days|3000|3|365|0FF|~MyApp Professional Forever|8000|5|65535|0FF|~


Here are 6 groups separated by the character '~' and each group has separate elements with the character '|'. Each group corresponds to a license definition, as we can see below in more orderly manner:


MyApp Standard by 180 days|1000|2|180|00F|~

MyApp Standard by 360 days|1800|3|365|00F|~

MyApp Standard Forever|3000|5|65535|00F|~

MyApp Professional by 180 days|2000|2|180|0FF|~

MyApp Professional by 360 days|3000|3|365|0FF|~

MyApp Professional Forever|8000|5|65535|0FF|~


Each group has elements separated by '|'. For the first group the elements are:


MyApp Standard by 180 days| -> Description

1000|   -> Price in cents

2|      -> Not used. Auxiliar value used by the LicOptions utility

180|    -> Days of the authorized period

00F|    -> Values field (licdata) defining the active modules            


You. Can create this file using any text editor, then upload it to your OLM. Also in the future, and anytime you can replace this file with a new configuration and this will be reflected in its application in PCs of all users.


The LicOptions utility


To help in the task of creating this file we have prepared the LicOptions utility. Below you can see its screen with the data listing we are using:




To edit an existing file open it with the "Load from File" button, and save it with the "Save to File" button. The most useful is given in the definition of active modules set with the optional Values field. Using the binary format as shown in the picture can define the Values field by selecting the appropriate checkboxes for each module. In the Example2 when we explain the StartTrial procedure we also explained the relationship between the Values field and active modules.

The Example4 running


When you run this e example at first time, the trial period is started with the active modules according with Values='EA3' used in the call to AVLock.StartTrialAdv(0,1,1,30,'EA3'); Below we see the Example4 just started.




By clicking the "Registration Form" button we go to the registration form:




Here we see the "With PayPal" tab displayed and selected the option "MyApp Standard by 360 days". Clicking the "Pay Now with PayPal" button we are redirected to the PayPal site to do the payment. In this case we use the "PayPal Sanbox" which emulates the real paypal. We first must login to continue, as shown in the picture below:



And after of review the information click on "Pay Now"




And we have already completed payment.




Now you must restart the application to take the new license. In doing so the first thing we see are the modules that are now active, consistent with the Values field applied '00F'.




With the "Registration Form" button we go to the registration form and see the new state.




Now we will see the new code added to the application to implement this functionality.

Code added to avlockunit.pas


At the beginning of the code the following variables were added:



//For PayPal IPN implementation

 paypal_url, paypal_email, currency_code, notify_url, licdef_file : string;



which at the end of the code are initialized in section initializartion:



//For PayPal IPN implementation

 paypal_url    := 'https://www.paypal.com/cgi-bin/webscr';

 paypal_email  := 'yourpaypalemail@yourdomain.com';

 currency_code := 'USD';

 notify_url    := 'http://yourdomain.com/olm6/ipn.php';

 licdef_file   := 'mylicdef.ldf';



Here you must enter your own data to paypal_email and notify_url and other settings if necessary. To use the Sandbox you should place paypal_url := 'https://www.sandbox.paypal.com/cgi-bin/webscr'; and use your  fictitious email associated with your dummy account that you created on your PayPal account. See more details on PayPal IPN.



Now we will see the additions that were made to the application code to implement this functionality.

Agregados al código en Regist.pas


At the interface section we have declared the following set of global variables:




 item_number, item_name, amount, days, licdata, app, licdef_idx :string;



These variables contain values that define the license that the user selects from the list of definitions and will be assigned in the procedure associated with the OnTimer event handler of TTimer control. below we can see the code:



procedure TRegForm.Timer1Timer(Sender: TObject);

var s,s1:string;




 lmsg.Caption := 'Please select a configuration above';

 n := ListView1.ItemIndex;

 btnpay.Enabled := (n >= 0);

 if btnpay.Enabled then begin

   s:= licarr[n];


   if (m>4) then begin

     licdef_idx := inttostr(n);


     days     := arr[3];

     licdata  := arr[4];

     v := strtoint(strnum(arr[1],0));


     s1 := formatfloat('0.00',v/100);

     lmsg.caption := 'You are about to pay $'+s1+' for '+item_name+'. Click the button below to proceed to checkout.';






Also were added the "Purchase" procedure which is called from the "Pay Now with PayPal" button. Below we see its code:



procedure TRegForm.Purchase;

var s, custom :string;


 custom  := days+'|'+inttostr(AVLock.AppID)+'|'+licdata+'|'+licdef_idx+'|'+licdef_file;


 s := paypal_url+









 ShellExecute(0, 'open', Pchar(s), nil, nil, SW_SHOWMAXIMIZED);





procedure TRegForm.BtnPayClick(Sender: TObject);






We see here that the "Purchase" procedure called from the button "Pay Now with PayPal" calls to the PayPal site with ShellExecute, passing the url and parameters in the s variable.

Some of the parameters passed are those assigned in the Initialization section of avlockunit.pas, which are: paypal_url, paypal_email, notify_url, currency_code and licdef_file and other values that are dependent on the user's selection and were assigned to to the global variables licdef_idx, LicData and amount, etc.


The custom parameter is a parameter of type pass-through that is not used by paypal but is forwarded unchanged to notify_url which is our ipn.php script where PayPal informs us about the payment made. This is where we use the data in the custom parameter to identify the record of the OLM we need to modify and the registration data that we will apply to it.