PayPal, IPN and Delphi

This is the source of an example to implement PayPal IPN with Delphi and Indy.

 (* IPN Notification Example

   The program uses the port numer 82 so the IPN handler URL must be set as following:
   http://yourdomanin:82

   Indy version 10.1.5
*)

unit HttpForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdHTTPServer,
  StdCtrls, StrUtils, IdContext, IdCustomHTTPServer,
  IdCustomTCPServer, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL,
  IdSSLOpenSSL, IdHTTP, IdTCPConnection, IdTCPClient;


type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    Memo1: TMemo;
    http1: TIdHTTP;
    OpenSSL: TIdSSLIOHandlerSocketOpenSSL;
    procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IdHTTPHeaderInfo;

{$R *.dfm}

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  strWrite : TStringList;
  strRead  : String;
begin

  Memo1.Lines.Add('Notification:');
  Memo1.Lines.Add(ARequestInfo.FormParams);

  Memo1.Lines.Add('Answer:');
  strWrite := TStringList.Create;
  try
    strWrite.Add(ARequestInfo.FormParams+'&cmd=_notify-validate');
    Memo1.Lines.Add(ARequestInfo.FormParams+'&cmd=_notify-validate');
    Http1.Request.UserAgent := 'application/x-www-form-urlencoded';
    Http1.ReadTimeout := 0;
    Http1.IOHandler := OpenSSL;
    // If you are in live
    // strRead := Http1.Post('https://www.paypal.com/cgi-bin/webscr', strWrite);
    // If you are in sandbox
    strRead := Http1.Post('https://www.sandbox.paypal.com/cgi-bin/webscr', strWrite);
    Memo1.Lines.Add(strRead);
  except
    on E: Exception do
    begin
      showmessage('Error encountered during POST: ' + E.Message);
    end;
  end;
  strWrite.Free;

end;

end.

 

... and the source of DFM:

  object IdHTTPServer1: TIdHTTPServer
    Active = True
    Bindings = <>
    DefaultPort = 82
    AutoStartSession = True
    OnCommandGet = IdHTTPServer1CommandGet
    Left = 60
    Top = 80
  end
  object http1: TIdHTTP
    IOHandler = OpenSSL
    AllowCookies = True
    ProxyParams.BasicAuthentication = False
    ProxyParams.ProxyPort = 0
    Request.ContentLength = -1
    Request.Accept = 'text/html, */*'
    Request.BasicAuthentication = False
    Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
    HTTPOptions = []
    Left = 175
    Top = 80
  end
  object OpenSSL: TIdSSLIOHandlerSocketOpenSSL
    MaxLineAction = maException
    Port = 0
    DefaultPort = 0
    SSLOptions.Method = sslvSSLv23
    SSLOptions.Mode = sslmClient
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
    Left = 215
    Top = 140
  end
 

 You can download the example from here.