From: C.DeRykus on
On Jan 7, 1:46 am, "Graham" <graham.s...(a)stowassocs.co.uk> wrote:
> "C.DeRykus" <dery...(a)gmail.com> wrote in message
>

> > ...
>
>
> I've added '$status = $res->status;' line where suggested, and have also
> added the lines '$response_as_string = $res->as_string;' and '$error_as_html
> = $res->error_as_HTML;'. When I print these, I get the following output:-
> ----------------------------------------------------------------------
> response_as_string = HTTP/1.1 411 Length Required
> ...
> status = 411 Length
> Required-----------------------------------------------So it appears that a
> length is required. It may be a stupid question, but a length of what, and
> how should I provide it within the following code?use LWP::UserAgent;$ua =
> new LWP::UserAgent;$req = new HTTP::Request('POST',
> 'https://www.nochex.com/nochex.dll/apc/apc');$req->content_type("application/x-www-form-urlencoded");$req->content($query);$res
> = $ua->request($req);


A POST will require a valid content-length header IIRC.

> $req->content($query);
> $res = $ua->request($req);

Where is your request $query above defined?

You can check the actual request to ensure that the
request has valid headers and the $query content is
getting set and passed to the server correctly.

print $req->as_string;

As mentioned earlier, 'use LWP::Debug qw/+/' will provide
voluminous details about the transaction too.


--
Charles DeRykus
From: Graham on

"C.DeRykus" <derykus(a)gmail.com> wrote in message
news:88a2c951-be00-4183-834f-2e5f2fbe3d0b(a)r24g2000yqd.googlegroups.com...
On Jan 7, 1:46 am, "Graham" <graham.s...(a)stowassocs.co.uk> wrote:
> "C.DeRykus" <dery...(a)gmail.com> wrote in message
>

> > ...
>
>
> I've added '$status = $res->status;' line where suggested, and have also
> added the lines '$response_as_string = $res->as_string;' and
> '$error_as_html
> = $res->error_as_HTML;'. When I print these, I get the following output:-
> ----------------------------------------------------------------------
> response_as_string = HTTP/1.1 411 Length Required
> ...
> status = 411 Length
> Required-----------------------------------------------So it appears that
> a
> length is required. It may be a stupid question, but a length of what, and
> how should I provide it within the following code?use LWP::UserAgent;$ua =
> new LWP::UserAgent;$req = new HTTP::Request('POST',
> 'https://www.nochex.com/nochex.dll/apc/apc');$req->content_type("application/x-www-form-urlencoded");$req->content($query);$res
> = $ua->request($req);


A POST will require a valid content-length header IIRC.

> $req->content($query);
> $res = $ua->request($req);

Where is your request $query above defined?

$query is defined in the following line near the top of the script:-
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});

You can check the actual request to ensure that the
request has valid headers and the $query content is
getting set and passed to the server correctly.

print $req->as_string;

adding the following lines:-

$request_as_string = $req->as_string;
print "request_as_string = $request_as_string\n";

produced the following output:-

'request_as_string = POST https://www.nochex.com/nochex.dll/apc/apc
User-Agent: libwww-perl/5.79
Content-Type: application/x-www-form-urlencoded'Notably, there is no mention
of any $query content, so I guess there isn't any! However, is that because
none is being provided in the call-back, or am I just not finding it
somehow?As mentioned earlier, 'use LWP::Debug qw/+/' will providevoluminous
details about the transaction too.Unfortunately, the CPAN write up on
LWP::Debug doesn't give me enough information to figure out how to apply
it.--Charles DeRykus


From: Graham on

"Sherm Pendley" <pshendley(a)gmail.com> wrote in message
news:5a663d9b-dc49-4cde-bc1d-558320d5446a(a)m3g2000yqf.googlegroups.com...
On Jan 6, 12:04 pm, "Graham" <graham.s...(a)stowassocs.co.uk> wrote:
> Below is a suggested 'Automatic Payment Confirmation' script copied and
> pasted from the Nochex Developer's forum (Nochex themselves do not offer
> support in Perl). This thread is now closed and I suspect the Nochex forum
> does not get much traffic, hence this post here. I have spotted one error
> (the line '$mail_method = "sendmail";' needs to be added if using
> sendmail).
> However, even with this correction, the script always goes down the first
> of
> the four 'if (res->' options (i.e. (res->is_error)), and thus none of my
> test transactions are being authorised, which makes me suspect that there
> is
> something wrong with the following lines:-
>
> use LWP::UserAgent;
> $ua = new LWP::UserAgent;
> $req = new HTTP::Request
> "POST","https://www.nochex.com/nochex.dll/apc/apc";
> $req->content_type("application/x-www-form-urlencoded");
> $req->content($query);
> $res = $ua->request($req);
>
> Anyone any ideas?
>
> #!/usr/bin/perl
>
> # REPLACE THIS WITH YOUR NOCHEX EMAIL ADDRESS.
> $admin_email = "sa...(a)geodetech.com";
>
> # THIS SHOULD BE EITHER smtp OR sendmail DEPENDING
> # ON THE MAIL METHOD YOU WANT TO USE.
> $mail_method = "smtp";
>
> # IF YOU ARE USING SENDMAIL TO SEND EMAIL
> # SET THE PATH TO SENDMAIL ON YOUR SERVER.
> # IN ALL PROBABILITY THE DEFAULT WILL WORK
> $sendmail_path = "/usr/sbin/sendmail -t";
>
> # IF YOU ARE USING SMTP TO SEND EMAIL
> # SPECIFY THE SMTP SERVER TO USE.
> # IN ALL PROBABILITY THE DEFAULT WILL WORK
> $smtp_server = "localhost";
>
> read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
>
> @pairs = split(/&/, $query);
> $count = 0;
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $variable{$name} = $value;
> $count++;
>
> }
>
> $transaction_id = $variable{'transaction_id'};
> $transaction_date = $variable{'transaction_date'};
> $from_email = $variable{'from_email'};
> $to_email = $variable{'to_email'};
> $order_id = $variable{'order_id'};
> $amount = $variable{'amount'};
> $security_key = $variable{'security_key'};
>
> use LWP::UserAgent;
> $ua = new LWP::UserAgent;
> $req = new HTTP::Request
> "POST","https://www.nochex.com/nochex.dll/apc/apc";
> $req->content_type("application/x-www-form-urlencoded");
> $req->content($query);
> $res = $ua->request($req);
>
> if ($res->is_error) {
> $subject = "Perl APC Script Error - Could not connect to Nochex servers";
> $message = "Your Perl APC script was called but returned an error because
> it \ncould not connect with the NOCHEX servers.\n.";} elsif ($res->content
> eq "AUTHORISED") {
>
> $subject = "APC Result - AUTHORISED";
> $message = "NOCHEX RESPONSE:
> AUTHORISED\n-----------------------------------------------\nOrder
> submitted
> with ID:
> ".$order_id."\n-----------------------------------------------\ntransaction
> id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> email:\t".$from_email."\nto
> email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecu
> rity
> key:\t".$security_key."\n";} elsif ($res->content eq "DECLINED") {
>
> $subject = "APC Result - DECLINED";
> $message = "NOCHEX RESPONSE:
> DECLINED\n-----------------------------------------------\nOrder submitted
> with ID:
> ".$order_id."\n-----------------------------------------------\ntransaction
> id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> email:\t".$from_email."\nto
> email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecu
> rity
> key:\t".$security_key."\n";} else {
>
> $subject = "Invalid APC Result Returned";
> $message = "The NOCHEX APC server returned an unrecognised or invalid
> response. In \nall probability due to en error in your code but could be
> the
> APC \nserver screwing
> up.\n-----------------------------------------------\nOrder submitted with
> ID:
> ".$order_id."\n-----------------------------------------------\ntransaction
> id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> email:\t".$from_email."\nto
> email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecu
> rity
> key:\t".$security_key."\n";
>
> }
>
> print "Content-Type: text/plain\n\n";
>
> if ($mail_method eq "smtp") {
> use Net::SMTP;
> $smtp = Net::SMTP->new($smtp_server);
> $smtp->mail($ENV{USER});
> $smtp->to($admin_email);
> $smtp->data();
> $smtp->datasend("From: APC Script <apc_scr...(a)your.website>\n");
> $smtp->datasend("To: ".$admin_email."\n");
> $smtp->datasend("Subject: ".$admin_email."\n");
> $smtp->datasend("Content-Type: text/plain\n");
> $smtp->datasend("\n");
> $smtp->datasend($message);
> $smtp->dataend();
> $smtp->quit;} elsif ($mail_method eq "sendmail") {
>
> open(SENDMAIL, "|$sendmail_path") or die "Cannot open sendmail: $!";
> print SENDMAIL "From: APC Script <apc_scr...(a)your.website>\n";
> print SENDMAIL "To: ".$admin_email."\n";
> print SENDMAIL "Subject: ".$subject."\n";
> print SENDMAIL "Content-Type: text/plain\n\n";
> print SENDMAIL $message;
> close(SENDMAIL);
>
>
>
> }

Why did you post this here? Seems to me *someone* forgot what groups
are for what! ;)

sherm--

I explained why at the top of the post! This is the number one newsgroup for
miscellaneous perl discussions and queries and, as far as I'm concerned, I'm
bang on topic. Also, it appears Charles DeRykus agrees with me, and he's
being VERY helpful.

Graham


From: C.DeRykus on
On Jan 7, 9:39 am, "Graham" <graham.s...(a)stowassocs.co.uk> wrote:
> ...
>
> A POST will require a valid content-length header IIRC.
>
>   > $req->content($query);
>   > $res = $ua->request($req);
>
> Where is your request $query above defined?
>
> $query is defined in the following line near the top of the script:-
> read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
>
> You can check the actual request to ensure that the
> request has valid headers and the $query content is
> getting set and passed to the server correctly.
>
>     print $req->as_string;
>
> adding the following lines:-
>
> $request_as_string = $req->as_string;
> print "request_as_string = $request_as_string\n";
>
> produced the following output:-
>
> 'request_as_string = POSThttps://www.nochex.com/nochex.dll/apc/apc
> User-Agent: libwww-perl/5.79
> Content-Type: application/x-www-form-urlencoded'Notably, there is no mention
> of any $query content, so I guess there isn't any! However, is that because
> none is being provided in the call-back, or am I just not finding it
> somehow?

There's a major problem then because you're sending no content
in the POST. The code you posted shows:

read (STDIN, $query, $ENV{'CONTENT_LENGTH'});

So the intent is to pick up the correct form pairs from the
user via STDIN to supply the POST content. Adding this check
would be a good idea:

my $len = read( STDIN, $query, $ENV{'CONTENT_LENGTH'})
or die "error: ", defined $len ? "no content" : $!;


As mentioned earlier, 'use LWP::Debug qw/+/' will providevoluminous
> details about the transaction too.Unfortunately, the CPAN write up on
> LWP::Debug doesn't give me enough information to figure out how to apply
> it.

It's applied automatically if added as is.

(This thread is probably better continued in the
libwww-perl mailing list. Check Google for details.)

--
Charles DeRykus
From: Permostat on
On Jan 7, 11:47 am, "Graham" <graham.s...(a)stowassocs.co.uk> wrote:
> "Sherm Pendley" <pshend...(a)gmail.com> wrote in message
>
> news:5a663d9b-dc49-4cde-bc1d-558320d5446a(a)m3g2000yqf.googlegroups.com...
> On Jan 6, 12:04 pm, "Graham" <graham.s...(a)stowassocs.co.uk> wrote:
>
>
>
>
>
> > Below is a suggested 'Automatic Payment Confirmation' script copied and
> > pasted from the Nochex Developer's forum (Nochex themselves do not offer
> > support in Perl). This thread is now closed and I suspect the Nochex forum
> > does not get much traffic, hence this post here. I have spotted one error
> > (the line '$mail_method = "sendmail";' needs to be added if using
> > sendmail).
> > However, even with this correction, the script always goes down the first
> > of
> > the four 'if (res->' options (i.e. (res->is_error)), and thus none of my
> > test transactions are being authorised, which makes me suspect that there
> > is
> > something wrong with the following lines:-
>
> > use LWP::UserAgent;
> > $ua = new LWP::UserAgent;
> > $req = new HTTP::Request
> > "POST","https://www.nochex.com/nochex.dll/apc/apc";
> > $req->content_type("application/x-www-form-urlencoded");
> > $req->content($query);
> > $res = $ua->request($req);
>
> > Anyone any ideas?
>
> > #!/usr/bin/perl
>
> > # REPLACE THIS WITH YOUR NOCHEX EMAIL ADDRESS.
> > $admin_email = "sa...(a)geodetech.com";
>
> > # THIS SHOULD BE EITHER smtp OR sendmail DEPENDING
> > # ON THE MAIL METHOD YOU WANT TO USE.
> > $mail_method = "smtp";
>
> > # IF YOU ARE USING SENDMAIL TO SEND EMAIL
> > # SET THE PATH TO SENDMAIL ON YOUR SERVER.
> > # IN ALL PROBABILITY THE DEFAULT WILL WORK
> > $sendmail_path = "/usr/sbin/sendmail -t";
>
> > # IF YOU ARE USING SMTP TO SEND EMAIL
> > # SPECIFY THE SMTP SERVER TO USE.
> > # IN ALL PROBABILITY THE DEFAULT WILL WORK
> > $smtp_server = "localhost";
>
> > read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
>
> > @pairs = split(/&/, $query);
> > $count = 0;
> > foreach $pair (@pairs) {
> > ($name, $value) = split(/=/, $pair);
> > $value =~ tr/+/ /;
> > $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> > $variable{$name} = $value;
> > $count++;
>
> > }
>
> > $transaction_id = $variable{'transaction_id'};
> > $transaction_date = $variable{'transaction_date'};
> > $from_email = $variable{'from_email'};
> > $to_email = $variable{'to_email'};
> > $order_id = $variable{'order_id'};
> > $amount = $variable{'amount'};
> > $security_key = $variable{'security_key'};
>
> > use LWP::UserAgent;
> > $ua = new LWP::UserAgent;
> > $req = new HTTP::Request
> > "POST","https://www.nochex.com/nochex.dll/apc/apc";
> > $req->content_type("application/x-www-form-urlencoded");
> > $req->content($query);
> > $res = $ua->request($req);
>
> > if ($res->is_error) {
> > $subject = "Perl APC Script Error - Could not connect to Nochex servers";
> > $message = "Your Perl APC script was called but returned an error because
> > it \ncould not connect with the NOCHEX servers.\n.";} elsif ($res->content
> > eq "AUTHORISED") {
>
> > $subject = "APC Result - AUTHORISED";
> > $message = "NOCHEX RESPONSE:
> > AUTHORISED\n-----------------------------------------------\nOrder
> > submitted
> > with ID:
> > ".$order_id."\n-----------------------------------------------\ntransaction
> > id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> > email:\t".$from_email."\nto
> > email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecu
> > rity
> > key:\t".$security_key."\n";} elsif ($res->content eq "DECLINED") {
>
> > $subject = "APC Result - DECLINED";
> > $message = "NOCHEX RESPONSE:
> > DECLINED\n-----------------------------------------------\nOrder submitted
> > with ID:
> > ".$order_id."\n-----------------------------------------------\ntransaction
> > id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> > email:\t".$from_email."\nto
> > email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecu
> > rity
> > key:\t".$security_key."\n";} else {
>
> > $subject = "Invalid APC Result Returned";
> > $message = "The NOCHEX APC server returned an unrecognised or invalid
> > response. In \nall probability due to en error in your code but could be
> > the
> > APC \nserver screwing
> > up.\n-----------------------------------------------\nOrder submitted with
> > ID:
> > ".$order_id."\n-----------------------------------------------\ntransaction
> > id:\t".$transaction_id."\ntransaction date:\t".$transaction_date."\nfrom
> > email:\t".$from_email."\nto
> > email:\t".$to_email."\norder_id:\t".$order_id."\namount:\t".$amount."\nsecu
> > rity
> > key:\t".$security_key."\n";
>
> > }
>
> > print "Content-Type: text/plain\n\n";
>
> > if ($mail_method eq "smtp") {
> > use Net::SMTP;
> > $smtp = Net::SMTP->new($smtp_server);
> > $smtp->mail($ENV{USER});
> > $smtp->to($admin_email);
> > $smtp->data();
> > $smtp->datasend("From: APC Script <apc_scr...(a)your.website>\n");
> > $smtp->datasend("To: ".$admin_email."\n");
> > $smtp->datasend("Subject: ".$admin_email."\n");
> > $smtp->datasend("Content-Type: text/plain\n");
> > $smtp->datasend("\n");
> > $smtp->datasend($message);
> > $smtp->dataend();
> > $smtp->quit;} elsif ($mail_method eq "sendmail") {
>
> > open(SENDMAIL, "|$sendmail_path") or die "Cannot open sendmail: $!";
> > print SENDMAIL "From: APC Script <apc_scr...(a)your.website>\n";
> > print SENDMAIL "To: ".$admin_email."\n";
> > print SENDMAIL "Subject: ".$subject."\n";
> > print SENDMAIL "Content-Type: text/plain\n\n";
> > print SENDMAIL $message;
> > close(SENDMAIL);
>
> > }
>
> Why did you post this here? Seems to me *someone* forgot what groups
> are for what! ;)
>
> sherm--
>
> I explained why at the top of the post! This is the number one newsgroup for
> miscellaneous perl discussions and queries and, as far as I'm concerned, I'm
> bang on topic. Also, it appears Charles DeRykus agrees with me, and he's
> being VERY helpful.
>
> Graham

We don't like socialists in our newsgroup. We'll be seeing not again
commie.

PERMO