cancel
Showing results for 
Search instead for 
Did you mean: 

Sending XML using Delphi, cant solve encoding problem.

Ok, so after much debugging I finally had everything working, and then it just stopped.  I sware I had the sample from here: http://community.developer.authorize.net/t5/News-and-Announcements/New-XML-API-for-Transaction-Proce... working and responding with "Invalid account", then I changed it to my account and got "invaild CC experation" through MY program.

 

Then, to save time, I used a 3rd party program (XML Transmitter) to continue tests in forming my XML.  Once it got it trimmed down, I put the XML (which works fine using XML Transmitter) in to my program, but now, even if I put it back to how it was before, all my program ever returns is:

...

<code>E00003</code>

<text>'%' is an unexpected token. The expected token is '?gt;'. Line 1, position 20.</text>

...

 

 

I have no % anywhere in my XML, however I assume this is an encoding issue as I know Delphi uses %num fro caracters sometimes (%20 is Space), and gt; is a >.  Here is exactly what I am sending:

 

XML2Send.Add('<?xml version="1.0" encoding="utf-8"?>' +

               '<createTransactionRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">' +

                 '<merchantAuthentication>' +

                   '<name>myName123</name>' +

                   '<transactionKey>MyKey1234567890t</transactionKey>' +

                 '</merchantAuthentication>' +

                 '<transactionRequest>' +

                   '<transactionType>authCaptureTransaction</transactionType>' +

                   '<amount>365.51</amount>' +

                   '<payment>' +

                     '<creditCard>' +

                       '<cardNumber>5424000000000015</cardNumber>' +

                       '<expirationDate>1112</expirationDate>' +

                     '</creditCard>' +

                   '</payment>' +

                 '</transactionRequest>' +

               '</createTransactionRequest>');

 

 

I am so mad it was working fine and then just stopped (?!?!?).  Does anyone know how to get it working again?

 

FYI: XML2Send is a TStringList, but it only works if the entire XML is in the 1st string (so it is a list of one string).  I am using Indy TIdHttp to send with the following command:

XMLResponse := AuthNetHTTP.Post('https://api.authorize.net/xml/v1/request.api', XML2Send);

 

 

 

 

 

rpmccormick
Contributor
1 ACCEPTED SOLUTION

Accepted Solutions

Alright, well, even though I could sware it worked a few times with a TStringList, in the end the answer was just to change it to a TStringStream which is not encoded.

 

Here is the working code for anybody looking for the easiest way to use AIM over XML in Delphi using Indy:

 

procedure TMainForm.ProcessButtClick(Sender: TObject);
var
  AuthNetHTTP: TIdHttp;
  AuthNetSSL: TIdSSLIOHandlerSocketOpenSSL;
  XMLRequest: TStringStream;
  XMLResponse: String;
 begin
  XMLRequest := TStringStream.Create(
               '<?xml version="1.0" encoding="utf-8"?>' +
               '<createTransactionRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">' +
                 '<merchantAuthentication>' +
                   '<name>YourName.</name>' +
                   '<transactionKey>YourTransKey....</transactionKey>' +
                 '</merchantAuthentication>' +
                 '<transactionRequest>' +
                   '<transactionType>authCaptureTransaction</transactionType>' +
                   '<amount>365.51</amount>' +
                   '<payment>' +
                     '<creditCard>' +
                       '<cardNumber>5424000000000015</cardNumber>' +
                       '<expirationDate>1118</expirationDate>' +
                     '</creditCard>' +
                   '</payment>' +
                 '</transactionRequest>' +
               '</createTransactionRequest>');

  //Prep Web Request
  AuthNetHTTP := TidHTTP.Create(nil);
  AuthNetSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  AuthNetSSL.SSLOptions.Method := sslvSSLv3;
  AuthNetHTTP.IOHandler := AuthNetSSL;
  AuthNetHTTP.Request.ContentType := 'text/xml';
  AuthNetHTTP.ConnectTimeout := 10000;
  AuthNetHTTP.ReadTimeout := 10000;
  try
    XMLResponse := AuthNetHTTP.Post('https://api.authorize.net/xml/v1/request.api', XMLRequest);
    AuthNetHTTP.Disconnect;
  except on e: exception do
    ShowMessage('Server Error: ' + e.message);
   end;

  Memo1.Text := XMLResponse;
  ShowMessage(XMLResponse);

  AuthNetHTTP.Free;
  AuthNetSSL.Free;
  XMLRequest.Free;
 end;

 

View solution in original post

3 REPLIES 3

Here is the complete function code as it stands now (I had thought assigning the XML to a sting first, and then assigning that sting to the TStringList might fix it, as I was doing that before when it worked, but still no love).  I just can't believe it worked before, and no it has some dumb encoding issue that I cannot solve for the life of me.

 

procedure TMainForm.WelcomeProcessButtClick(Sender: TObject);
var
  AuthNetHTTP: TIdHttp;
  AuthNetSSL: TIdSSLIOHandlerSocketOpenSSL;
  XMLRequest: TStringList;
  XML2Send, XMLResponse: String;
 begin
  SQLStatusBar.Panels[1].Text := 'Loading Database';
  SQLStatusBar.Panels[2].Text := 'Processing Payments...';
  MainForm.Update;

  //Auth.net
  XML2Send := '<?xml version="1.0" encoding="utf-8"?>' +
               '<createTransactionRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">' +
                 '<merchantAuthentication>' +
                   '<name>MyNameW8g</name>' +
                   '<transactionKey>MyTransKeynL479t</transactionKey>' +
                 '</merchantAuthentication>' +
                 '<transactionRequest>' +
                   '<transactionType>authCaptureTransaction</transactionType>' +
                   '<amount>365.51</amount>' +
                   '<payment>' +
                     '<creditCard>' +
                       '<cardNumber>5424000000000015</cardNumber>' +
                       '<expirationDate>1112</expirationDate>' +
                     '</creditCard>' +
                   '</payment>' +
                   {'<order>' +
                     '<invoiceNumber>DemoHotel-0611</invoiceNumber>' +
                     '<description>June 2011 payment from DemoHotel</description>' +
                   '</order>' +}
                 '</transactionRequest>' +
               '</createTransactionRequest>';
  XMLRequest := TStringList.Create;
  XMLRequest.Append(XML2Send);

  //Prep Web Request
  AuthNetHTTP := TidHTTP.Create(nil);
  AuthNetSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  AuthNetSSL.SSLOptions.Method := sslvSSLv3;
  AuthNetHTTP.IOHandler := AuthNetSSL;
  AuthNetHTTP.Request.ContentType := 'text/xml';
  AuthNetHTTP.ConnectTimeout := 10000;
  AuthNetHTTP.ReadTimeout := 10000;
  //AuthNetHTTP.Request.ContentEncoding := 'utf-8';
  try
    XMLResponse := AuthNetHTTP.Post('https://api.authorize.net/xml/v1/request.api', XMLRequest);
    AuthNetHTTP.Disconnect;
  except on e: exception do
    ShowMessage('Server Error: ' + e.message);
   end;

  ShowMessage(XMLResponse);

  AuthNetHTTP.Free;
  AuthNetSSL.Free;
  XMLRequest.Free;

  Step2.Visible := True;
  SQLStatusBar.Panels[1].Text := 'Payments Processed!';
  SQLStatusBar.Panels[2].Text := 'Idle.';
  MainForm.Update;
 end;

 Please help.

rpmccormick
Contributor

Here is the complete response (starting with a few random chars???):

 

<?xml version="1.0" encoding="utf-8"?>
<ErrorResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <messages>
    <resultCode>Error</resultCode>
    <message>
      <code>E00003</code>
      <text>'%' is an unexpected token. The expected token is '?&gt;'. Line 1, position 20.</text>
    </message>
  </messages>
</ErrorResponse>

 

Alright, well, even though I could sware it worked a few times with a TStringList, in the end the answer was just to change it to a TStringStream which is not encoded.

 

Here is the working code for anybody looking for the easiest way to use AIM over XML in Delphi using Indy:

 

procedure TMainForm.ProcessButtClick(Sender: TObject);
var
  AuthNetHTTP: TIdHttp;
  AuthNetSSL: TIdSSLIOHandlerSocketOpenSSL;
  XMLRequest: TStringStream;
  XMLResponse: String;
 begin
  XMLRequest := TStringStream.Create(
               '<?xml version="1.0" encoding="utf-8"?>' +
               '<createTransactionRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">' +
                 '<merchantAuthentication>' +
                   '<name>YourName.</name>' +
                   '<transactionKey>YourTransKey....</transactionKey>' +
                 '</merchantAuthentication>' +
                 '<transactionRequest>' +
                   '<transactionType>authCaptureTransaction</transactionType>' +
                   '<amount>365.51</amount>' +
                   '<payment>' +
                     '<creditCard>' +
                       '<cardNumber>5424000000000015</cardNumber>' +
                       '<expirationDate>1118</expirationDate>' +
                     '</creditCard>' +
                   '</payment>' +
                 '</transactionRequest>' +
               '</createTransactionRequest>');

  //Prep Web Request
  AuthNetHTTP := TidHTTP.Create(nil);
  AuthNetSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  AuthNetSSL.SSLOptions.Method := sslvSSLv3;
  AuthNetHTTP.IOHandler := AuthNetSSL;
  AuthNetHTTP.Request.ContentType := 'text/xml';
  AuthNetHTTP.ConnectTimeout := 10000;
  AuthNetHTTP.ReadTimeout := 10000;
  try
    XMLResponse := AuthNetHTTP.Post('https://api.authorize.net/xml/v1/request.api', XMLRequest);
    AuthNetHTTP.Disconnect;
  except on e: exception do
    ShowMessage('Server Error: ' + e.message);
   end;

  Memo1.Text := XMLResponse;
  ShowMessage(XMLResponse);

  AuthNetHTTP.Free;
  AuthNetSSL.Free;
  XMLRequest.Free;
 end;