Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to POST a <FORM> from within a script?

Submitting to PayPal, but can't load the new page from PayPal

         

MichaelBluejay

1:53 am on Sep 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's my code:

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

$ua = LWP::UserAgent->new();
my $req = POST 'https://www.paypal.com/cgi-bin/webscr', [
cmd => '_xclick',
business => 'user@domain.com',
item_name => 'product name',
amount => 800,
];
$content = $ua->request($req)->as_string;
print "Content-type: text/html\n\n";
print $content;

-----------------------------------------
This sort of works, except there's a lot of garbage at the top of the page, and the browser's address bar is still set to <http://mydomain/script.cgi> rather than having hustled over to PayPal's domain. There's got to be an easy way to do this but I couldn't find it in Google or in my Perl books.

I know that I could have Perl write out the form and then have the user click, but for this particular application I really need my script to send the form itself.

Thanks for your help!

Storyteller

5:51 pm on Sep 30, 2004 (gmt 0)

10+ Year Member



To get rid of "garbage", use the 'content' method instead of 'as_string'.

MichaelBluejay

7:14 am on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, that does get rid of the garbage, but my browser remains stuck at <mydomain.com> rather than going over to PayPal. As a result all the relative links on PayPal's page to local graphics break.

Surely there has to be a way for me to have Perl submit the form just like would happen if the user clicked a Submit button?

I went to the bookstore today but I couldn't find anything there that was helpful.

upside

7:54 am on Oct 1, 2004 (gmt 0)

10+ Year Member



There is nothing that you can do server side to submit a form on behalf of the user as if they had submitted it themselves. Instead try using Javascript to submit the form. Here is an example:


document.form_name.submit();

MichaelBluejay

6:18 am on Oct 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



LOL... the whole reason I was trying to submit the form with Perl is because I want to make it compatible for those with JavaScript turned off.

But if it can't be done, then that explains why I was having such problems!

Thanks for your help, -MBJ-

cyberws

3:24 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



One way is to create a page on the fly that meta refreshs with in a few seconds.

===CODE===

my $urlstring="http://www.somedomain.tld/cgi-bin/theirscript.cgi?field1=$data1&field2=$data2
print <<HTML;
<html>

<head>
<title>Redirecting...</title>
<META HTTP-EQUIV="Refresh" CONTENT="3;URL=$urlstring">
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
Redirecting your request please wait...
</body>
</html>
HTML

Orsen_Cart

10:37 pm on Oct 12, 2004 (gmt 0)

10+ Year Member



Hi

I'm new to this board and to perl and need some help. I've been trying to get this done for weeks and have been ploughing through some new books but it is proving to be just too difficult for me. What I want to do is to have perl write out an HTML page with a <form> containing some <select> and <input> fields then validate & 'join' the user input into one string, and then submit (upon a button click from the user) the entire form to paypal.

I can do all of the above with HTML & javascript but cannot seen to get very far with perl.

I'm sure such a script must already exist somewhere but I can't find one and my books don't seem to help either. Can someone either tell me how to do this or point me at the appropriate script?

Many thanks

MichaelBluejay

7:47 pm on Oct 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you should have started a new thread for this topic. But since it's already here let's go for it. I've done a lot of what you're trying to do so I can probably help you.

I think your stumbling block is you're trying to do two different things with the same file. You need one file to display the form, whether that's a static HTML file or a .cgi file that generates the form on the fly.

Whichever it is, the form on that page needs to call ANOTHER file to actually mess with the user input and redirect to PayPal. For example, <myform.html> or <myform.cgi> could show the form to the user. That form would start out with <FORM action="process.cgi" method=post>. So when the user clicks the Submit button the <process.cgi> script is executed. That script grabs the user input and assembles the url.

For <process.cgi> here's a routine to get <FORM> input submitted by the POST method:


##### GENERIC ROUTINE TO GET FORM INPUT #####

$dataLength = $ENV{'CONTENT_LENGTH'}; # find out how much data there is, in bytes

$bytes_read = read(STDIN, $formData, $dataLength); # read that data into $formData
@pairs = split('&', $formData);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /; # translates + signs back into spaces
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # translates hex back into ASCII
$form{$name} = $value; # store results in a hash, if I need to reference them by name
}

---------------------
This code assigns the contents of the form field named "price" to the variable $amount:

$amount = $form{'price'};

---------------------
Assuming you have constructed your url and stored it in the variable $url, this code will redirect the user to PayPal:

print "Location:$url\n\n";

Orsen_Cart

10:19 pm on Oct 13, 2004 (gmt 0)

10+ Year Member



Hi & thanks for the response.

Ok, What I need to do (i think) is

1. Have 'index.html' with an <iframe> which loads 'external.html' (external.html is used on a number of webpages, hence the iframe)

2. 'External.html' has a hard coded form with 3 user entered fields. The form is submitted to 'process.cgi'

3. 'process.cgi' reads the 3 paramters passed in from the form and 'joins' them to form the 'item_name' parameter for paypal. This, plus all of the other info required by paypal (business, cmd, amount etc) , then need to be sent to paypal using print "Location:$url\n\n";

My 'validate.cgi' code currently looks like this ...

#!c:/perl/bin/perl.exe -w
use CGI::Carp qw(fatalsToBrowser);
use CGI::Pretty;
my $url = 'https://www.paypal.com/cgi-bin/webscr';
my $first = param ('one');
my $second = param ('two');
my $third = param ('three');
my $item_name = join ",", $first,$second,$third;
my $amount='10.00';
my $buyeremail = param ('three');
my $cmd = '_xclick';
my $business = 'blah@blah.com';
my $no_shipping = '1';
my $currency_code = 'USD';
my $no_note ='1';
print "Location:$url\n\n";

Needless to say, this doesn't work! The stuff I know about which is wrong is ...
1. It takes me to the paypal member login page rather than the payment screen. Presumably this is because the parameters that are defined are not being passed on to paypal. How do I do this?

2. When I do the join (having tested this in isolation) the first parameter in the join list always appears as the character '1' in the joined string, irrespective of which param I actually put first in the list.

3. When I reach the paypal screen, I need this to be on a new page as the iframe it would otherwise appear in is only 200x200 in size and does not scroll.

Could you possibly help with any of these issues, particularly the first one? This is my first ever attempt at writing any cgi script and I've been at it for weeks now!

Many thanks.

MichaelBluejay

1:11 am on Oct 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your first script is always the hardest. It took me hours to get a simple "Hello, World" going. I almost gave up. But I use Perl/CGI every day now. I even wrote a tutorial to help brand-new Perl/CGI scripters.

use CGI::Pretty;

This loads some module I've never heard of. It might be useful, but you definitely don't need it, because I don't need it, and I'm doing the same thing you're trying to do (redirecting users to PayPal).


my $first = param ('one');
my $second = param ('two');
my $third = param ('three');

There are many ways to do the same thing in Perl. You're using a different way than I do to get Form input. I'm not familiar with your way, so I can't help you with it. I suggest you use the code I listed in my earlier post, because I know it works.

my $item_name = join ",", $first,$second,$third;

Again, I've never heard of "join". I don't dispute that it works, but I would combine variables in either of these ways instead:

$item_name = "$first$second$third";

or

$item_name = $first . $second . $third;


my $amount='10.00';
my $buyeremail = param ('three');
my $cmd = '_xclick';
my $business = 'blah@blah.com';
my $no_shipping = '1';
my $currency_code = 'USD';
my $no_note ='1';
print "Location:$url\n\n";

You defined a bunch of variables but where did you add them to $url? Here's how I do it on one of my sites:

$theURL = "https://www.paypal.com/xclick/business=billing%40domain.com\&item_name=$product\&amount=$amount\&currency_code=USD"

Before this I assign the form input to variables, like so:

$product = $form{'description'};

And then I do some validation on those variables.

A good way to troubleshoot the URL is to put this before the print Location command:

print "Content-type:text/html\n\n";
print $url;
exit;

Then you can easily see the URL that's being generated to see if that's what you want.

3. When I reach the paypal screen, I need this to be on a new page as the iframe it would otherwise appear in is only 200x200 in size and does not scroll.

I know very little about iFrames. I don't think you can make Perl bust out of an iFrame. Your best solution will be to have your form on a frameless page. Either that, or instead of using the print Location command, have Perl output a page with Javascript, and have Javascript redirect the page to the new URL in a new window or the parent window.

Orsen_Cart

4:31 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Hi Michael

That's great, thanks

My code looks a lot simpler now....

#!c:/perl/bin/perl.exe -w
use CGI::Carp qw(fatalsToBrowser);
$charge = '10.00';
$int = $form{'interest'};
$place = $form{'location'};
$buyer = $form{'buyeremail'};
$product = $int.$place.$buyer;
my $url = 'https://www.paypal.com/xclick/business=blah@Blah.com\&item_name=$product\&amount=$charge\&currency_code=USD';
print "Location:$url\n\n";

There are a few things still not right. I've tried lots of different syntax, learning what I can from my book (which is generally nothing!)

1. When I execute this, I get the paypal message "The link you have used to enter the PayPal system contains an incorrectly formatted item amount"

2. If I remove the amount from the url, I get the paypal message "We cannot process this transaction because there is a problem with the PayPal email address supplied by the seller". I notice that you have a % in your url. Is that in place of @? The email address is correct as I can lift it straight from the text, copy it and into log-on to paypal with it with no problem.

3. The url it produces is [paypal.com...] . Have you any idea what the ¤ character is and why it is there?

I've presumably got the syntax wrong somewhere. Any ideas?

Many thanks

Orsen_Cart

12:42 pm on Oct 15, 2004 (gmt 0)

10+ Year Member



Hi Michael

Before you 'waste' time looking at my last post, I've managed to do what I was trying to do.

Thank you very much for your help