RTC Forums
November 24, 2024, 06:11:57 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Received Content.  (Read 6625 times)
SteveG
RTC License++
*****
Posts: 17


« on: September 23, 2019, 07:02:09 AM »

Hi,

For many years I have processed orders from PayPal using a payment processing system that I wrote in PHP with a MySQL database. 

I'm now rewriting the system as a web server using TRtcHTTPServer.  There are a number of reasons I'm doing this, including adding other payment processing services.

I have written a TRTCDataProvider event to extract the PayPal data and begin processing it, eg:

   with Sender as TRtcDataServer do
   begin
      if Request.Complete then
      begin
         Request.Params.AddText(Read);

         // Transaction Information
         TransactionID := URL_Decode(Request.Params['txn_id']);
          .
          .
          .
          .


One of the requirements of using PayPal is that when I receive a pending order from them, I have to send all of the data back in the exact format I received it.

An extract from the data received and sent back is one long string that looks like this:

&mc_gross=234.00&protection_eligibility=Ineligible&address_status=confirmed&item_number1=


In PHP that was quite easy:

      if (! empty($_POST) && count($_POST) > 0)
      {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_USERAGENT, '');
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_POST, TRUE);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $ARequestData);
         curl_exec($ch);
         curl_close($ch);
      }


Is there a way with RTC to send back the received contents exactly as it is received, or do I need to rebuild the text to send back?

Thanks.

= Steve


Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: September 23, 2019, 07:41:34 AM »

If you want to sent the received content body back in the response content body, here's your example code modified to do that. I've marked all the important bits in bold red, to make them clearly visible:

  var myContent:RtcString;
  begin
  with Sender as TRtcDataServer do
    begin
      if Request.Complete then
      begin
         myContent:=Read; // store the received content body (this is exactly as received)

         Request.Params.AddText(myContent); // use the "myContent" variable to access the content body

         // Transaction Information
         TransactionID := URL_Decode(Request.Params['txn_id']);

         Write(myContent); // send back the received content body
     end;
  end;

Logged
SteveG
RTC License++
*****
Posts: 17


« Reply #2 on: September 23, 2019, 07:52:13 AM »

If you want to sent the received content body back in the response content body, here's your example code modified to do that. I've marked all the important bits in bold red, to make them clearly visible:

  var myContent:RtcString;
  begin
  with Sender as TRtcDataServer do
    begin
      if Request.Complete then
      begin
         myContent:=Read; // store the received content body (this is exactly as received)

         Request.Params.AddText(myContent); // use the "myContent" variable to access the content body

         // Transaction Information
         TransactionID := URL_Decode(Request.Params['txn_id']);

         Write(myContent); // send back the received content body
     end;
  end;


Wow, that was a fast response!  So simple when you know how. :-)

Thanks very much Danijel!

= Steve
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: September 23, 2019, 07:58:35 AM »

Well, I'm not familiar with PHP and its "curl" function set, but ... after reading some PHP docs, it seems like curl is used to send HTTP requests to another Server. If that is the case, then you'll need TRtcHttpClient and TRtcDataRequest components to send a request to the PayPal Server, since you can't send a request to another Server from your Server as part of a response to the Client.
Logged
SteveG
RTC License++
*****
Posts: 17


« Reply #4 on: September 23, 2019, 09:58:30 AM »

Well, I'm not familiar with PHP and its "curl" function set, but ... after reading some PHP docs, it seems like curl is used to send HTTP requests to another Server. If that is the case, then you'll need TRtcHttpClient and TRtcDataRequest components to send a request to the PayPal Server, since you can't send a request to another Server from your Server as part of a response to the Client.

Yeah, good point.  I'll do that.

One thing I've noticed is that the format of the data is different.

For example, this is the format of the data as it is in the current system:



The content in the RTC server looks like this:



Will this make a difference?

= Steve




Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #5 on: September 23, 2019, 10:13:34 AM »

From what I can see, you are receiving data as a MULTIPART-FORM POST request, with each parameter in a separate variable  (that is what you get from a Web Browser when a user fills in a Form with INPUT fields and clicks the SUBMIT button, or a POST request is sent with hidden fields). If the PayPal Server also expects the content in a MULTIPART-FORM POST format (as you've received it, with each variable separated), then you can send it back exactly as you've received it from the Web Browser and the PayPal Server should be happy with it. But ... if the PayPal Server expects the content in a raw request content body without using the MULTIPART-FORM POST format, then you need go through all the variables you've received and put together a string as in your other example, which seems to be URL-Encoded.
Logged
SteveG
RTC License++
*****
Posts: 17


« Reply #6 on: September 23, 2019, 10:27:51 AM »

Thanks Danijel.

= Steve
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.028 seconds with 17 queries.