Page 1 of 1

Incorporating PayPal PDT for use with IWCGJQPayPalCart

PostPosted: 20 Mar 2014 19:47
by Peter_B
IWCGJQPayPalCart is very good for simple PayPal transactions and will suffice the return of a limited amount of information eg. transaction ID. If you require more extensive information, perhaps for your records or to display to the user, then this is where PDT (Payment Data Transfer) comes in. PDT requires another HTTP POST to the PayPal server incorporating the already returned transaction ID and your personal PayPal PDT ID in order to verify the transaction is actually completed. This is difficult using jQuery but Indy comes to the rescue.

Initially you must provide for an IWForm to be created for the content returned from the PayPal server, which must correlate with your PayPal auto return URL defined in your paypal merchant account. This can be done in your ServerController.OnConfig event eg...
Code: Select all
  {instantiate content handler for PayPal auto-return URL as setup in PayPal merchant account}
  with THandlers.Add(EmptyStr, 'transaction.html', TContentForm.Create(TformPDT_TX)) do
    CanStartSession := False;


... the TformPDT_TX.OnCreate event will need code similar to the following in order to manage the PDT information. An instance of TIdHTTP and TIdSSLIOHandlerSocketOpenSSL (to handle the secure protocol) will be required ...
Code: Select all
procedure TformPDT_TX.IWAppFormCreate(Sender: TObject);
var
  S, R: TStringList;
  M: TStream;
begin
  if (WebApplication.Request.QueryFields.IndexOfName('tx') > -1) then begin
    R := TStringList.Create;
    try
      S := TStringList.Create;
      M := TMemoryStream.Create;
      try
        S.Add('cmd=_notify-synch');
        S.Add('tx=' + WebApplication.Request.QueryFields.Values['tx']);
        S.Add('at=' + 'Your PayPal PDT ID');

        IdHTTP.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
        IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
        IdHTTP.Post('https://www.sandbox.paypal.com/cgi-bin/webscr', S, M);   // sandbox for testing only, remove for live transactions

        M.Position := 0;
        S.LoadFromStream(M);
        R.AddStrings(S);
      finally
        FreeAndNil(S);
        FreeAndNil(M);
      end;

      if (idHTTP.ResponseCode = 200) and (R[0] = 'SUCCESS') then begin
        // display as required
        lablTXNID.Caption := R.Values['txn_id'];
        IWCGJQLabel2.Caption := Format('Item: %s', [AnsiReplaceStr(R.Values['item_name1'], '+', ' ')]);
        // and whatever else you want to display
      end else
        WebApplication.ShowMessage('PayPal Payment Data Transfer failed, please check your PayPal Account to verify transaction has been completed.');

    finally
      FreeAndNil(R);
    end;
  end else
    WebApplication.ShowMessage('Unable to obtain transaction information from PayPal.');
end;


... basically that's it. Does anyone want to write a listener to handle PayPal's IPN :lol:

cheers

Pete B.

Re: Incorporating PayPal PDT for use with IWCGJQPayPalCart

PostPosted: 20 Mar 2014 20:11
by Jorge Sousa
Hi Peter

Thanks for sharing this, we're very glad you could make it!

Re: Incorporating PayPal PDT for use with IWCGJQPayPalCart

PostPosted: 01 Feb 2015 17:58
by eosventas
@Peter_B

I just wanto to say THANK YOU, your code was very usefull